|
@@ -1,86 +1,86 @@
|
|
|
-import store from '@/store'
|
|
|
-import config from './config'
|
|
|
-import utils from './utils'
|
|
|
-
|
|
|
-const request = (method, url, data, opt) => {
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- opt.showLoading && uni.showLoading({
|
|
|
- title: opt.loadingTitle,
|
|
|
- mask: method === 'POST' ? true : false
|
|
|
- })
|
|
|
-
|
|
|
- uni.request({
|
|
|
- method,
|
|
|
- url: utils.isHttp(url) ? url : config.apiBaseUrl + url,
|
|
|
- header: {
|
|
|
- 'content-type': 'application/x-www-form-urlencoded',
|
|
|
- Authorization: store.state.token,
|
|
|
- ...opt.header
|
|
|
- },
|
|
|
- data
|
|
|
- }).then(res => {
|
|
|
- opt.showLoading && uni.hideLoading()
|
|
|
-
|
|
|
- const data = res.data
|
|
|
- if (data.code === 201) {
|
|
|
- store.commit('setUserInfo', null)
|
|
|
- store.commit('token', '')
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- if (data.code === 200 || data.success) {
|
|
|
- resolve(data)
|
|
|
- } else {
|
|
|
- opt.showErrorMsg && uni.showToast({
|
|
|
- title: data.msg,
|
|
|
- icon: 'none'
|
|
|
- })
|
|
|
- reject(data)
|
|
|
- }
|
|
|
- }).catch(err => {
|
|
|
- opt.showLoading && uni.hideLoading()
|
|
|
- reject(err)
|
|
|
- })
|
|
|
- })
|
|
|
-}
|
|
|
-
|
|
|
-const http = {}
|
|
|
-const methods = ['GET', 'POST', 'PUT', 'DELETE']
|
|
|
-
|
|
|
-methods.forEach(method => {
|
|
|
- http[method.toLowerCase()] = (url, data, options) => {
|
|
|
- const loadingTitle = method === 'GET' ? '加载中...' : '提交中...'
|
|
|
- const opt = Object.assign(
|
|
|
- { loadingTitle },
|
|
|
- config.httpDefaultOption,
|
|
|
- options
|
|
|
- )
|
|
|
-
|
|
|
- if (opt.isAuth) {
|
|
|
- if (!store.state.token) {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if (typeof data === 'object') {
|
|
|
- for (let key in data) {
|
|
|
- if (data[key] === 'undefined') {
|
|
|
- data[key] = ''
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return request(method, url, data, opt)
|
|
|
- }
|
|
|
-})
|
|
|
-
|
|
|
+import store from '@/store'
|
|
|
+import config from './config'
|
|
|
+import utils from './utils'
|
|
|
+
|
|
|
+const request = (method, url, data, opt) => {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ opt.showLoading && uni.showLoading({
|
|
|
+ title: opt.loadingTitle,
|
|
|
+ mask: method === 'POST' ? true : false
|
|
|
+ })
|
|
|
+
|
|
|
+ uni.request({
|
|
|
+ method,
|
|
|
+ url: utils.isHttp(url) ? url : config.apiBaseUrl + url,
|
|
|
+ header: {
|
|
|
+ Authorization: store.state.token,
|
|
|
+ ...opt.header
|
|
|
+ },
|
|
|
+ data
|
|
|
+ }).then(res => {
|
|
|
+ opt.showLoading && uni.hideLoading()
|
|
|
+
|
|
|
+ const data = res.data
|
|
|
+ if (data.code === 201) {
|
|
|
+ store.commit('setUserInfo', null)
|
|
|
+ store.commit('token', '')
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (data.code === 200 || data.success) {
|
|
|
+ resolve(data)
|
|
|
+ } else {
|
|
|
+ opt.showErrorMsg && uni.showToast({
|
|
|
+ title: data.msg,
|
|
|
+ icon: 'none'
|
|
|
+ })
|
|
|
+ reject(data)
|
|
|
+ }
|
|
|
+ }).catch(err => {
|
|
|
+ opt.showLoading && uni.hideLoading()
|
|
|
+ reject(err)
|
|
|
+ })
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+const http = {}
|
|
|
+const methods = ['GET', 'POST', 'PUT', 'DELETE']
|
|
|
+
|
|
|
+methods.forEach(method => {
|
|
|
+ http[method.toLowerCase()] = (url, data, options) => {
|
|
|
+ const loadingTitle = method === 'GET' ? '加载中...' : '提交中...'
|
|
|
+ const opt = Object.assign({
|
|
|
+ loadingTitle
|
|
|
+ },
|
|
|
+ config.httpDefaultOption,
|
|
|
+ options
|
|
|
+ )
|
|
|
+
|
|
|
+ if (opt.isAuth) {
|
|
|
+ if (!store.state.token) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (typeof data === 'object') {
|
|
|
+ for (let key in data) {
|
|
|
+ if (data[key] === 'undefined') {
|
|
|
+ data[key] = ''
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return request(method, url, data, opt)
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
export default http
|