1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import config from '@/config'
- import {
- getToken,
- getServerUrl
- } from '@/utils/auth'
- import errorCode from '@/utils/errorCode'
- import {
- toast,
- showConfirm,
- tansParams
- } from '@/utils/common'
- let timeout = 10000
- const baseUrl = config.baseUrl
- const request = config => {
- // 是否需要设置 token
- const isToken = (config.headers || {}).isToken === false
- config.header = config.header || {}
- // if (getToken() && !isToken) {
- // // config.header['Authorization'] = 'Bearer ' + getToken()
- // config.header['UserKey'] = getToken()
- // config.header['ServerUrl'] = getServerUrl()
- // } else {
- // uni.reLaunch({
- // url: '/pages/index/login'
- // })
- // }
- // get请求映射params参数
- if (config.params) {
- let url = config.url + '?' + tansParams(config.params)
- url = url.slice(0, -1)
- config.url = url
- }
- const xml2js = require('xml2js');
- return new Promise((resolve, reject) => {
- uni.request({
- method: config.method || 'get',
- timeout: config.timeout || timeout,
- url: config.baseUrl || baseUrl + config.url,
- data: config.data,
- header: config.header,
- dataType: 'json',
- success: res => {
- if(res.statusCode == 200){
- xml2js.parseString(res.data, (err, result) => {
- if (err) {
- reject(err);
- }
- // 解析后的结果保存在result中
- const jsonResult = JSON.parse(result.string._);
- resolve(jsonResult)
- // if(jsonResult.flag){
- // resolve(jsonResult.result)
- // }else{
- // reject(jsonResult.message)
- // }
- });
- }else{
- reject("获取数据失败!")
- }
-
- },
- fail: err => {
- reject(err)
- },
- complete: () => {
- //uni.hideLoading()
- }
- })
- })
- }
- export default request
|