import environments from '@/environments'
|
import message from './message'
|
import storage from './storage'
|
import store from '../store'
|
import utils from './util.js'
|
// import Vue from 'vue'
|
// import {
|
// Base64
|
// } from 'js-base64'
|
let pre = 'api'
|
let version = 'v1.0'
|
const http = {
|
isShowMessage: false,
|
requestTemp(method, url, options) {
|
options = options || {}
|
options.responseType = options.responseType || 'text'
|
// 如果没有定义ignore则非post或者put请求默认ignore为true
|
if (utils.isTrueEmpty(options.ignore)) {
|
if (method.toUpperCase() !== 'POST' && method.toUpperCase() !== 'PUT') {
|
options.ignore = true
|
}
|
}
|
if (!options.ignore) {
|
message.showLoading()
|
}
|
let paramsArray = []
|
if (options.params) {
|
paramsArray = Object.keys(options.params).map((key) => `${key}=${options.params[key]}`)
|
}
|
if (paramsArray.length > 0) {
|
url += `?${paramsArray.join('&')}`
|
}
|
const token = storage.getItem('token')
|
return new Promise((resolve, reject) => {
|
let header = {
|
'Accept-Language': 'zh-CN',
|
'clientType': 'app',
|
|
// 'Access-Control-Allow-Origin': '*'
|
}
|
if (!!token)
|
header.Authorization = `Bearer ${token}`
|
// if (options.basicToken) {
|
// header.Authorization = `Basic ${Base64.encode(environments.clientId + ':' + environments.secret)}`
|
// } else if (token) {
|
// header.Authorization = `Bearer ${token}`
|
// }
|
uni.request({
|
method: method.toUpperCase(),
|
url: environments.httpBaseUri + url,
|
responseType: options.responseType ? options.responseType : 'text',
|
dataType: options.dataType ? options.dataType : 'json',
|
data: options.data || null,
|
header,
|
timeout: 30000,
|
success: async (res) => {
|
if (!options.ignore) {
|
message.hideLoading()
|
}
|
if (res.statusCode === 200) {
|
if (options.responseType !== 'text') {
|
resolve({
|
data: res.data,
|
code: 0,
|
msg: ''
|
})
|
} else {
|
console.log('res', res.data)
|
if (res.data.code == ('0')) {
|
resolve({
|
data: res.data.data,
|
code: 0,
|
msg: ''
|
})
|
} else if (res.data.code === '3000' || res.data.code === '30000' || res.data && res.data.code &&
|
res.data.code.startsWith('3000')) {
|
let validmsg = ''
|
if (Array.isArray(res.data.data)) {
|
if (res.data.data.length > 0 && res.data.data[0]
|
.msg) {
|
validmsg = res.data.data[0].msg
|
// message.showToast(res.data.data[0].msg)
|
}
|
} else if (res.data && res.data.msg) {
|
validmsg = res.data.msg
|
// message.showToast(res.data.msg)
|
} else if (res.data.data && typeof res.data.data === 'string') {
|
validmsg = res.data.msg
|
message.showToast(res.data.data)
|
}
|
if (validmsg) {
|
//兼容安卓手机提醒过快问题
|
setTimeout(() => {
|
message.showToast(validmsg)
|
}, 200)
|
}
|
|
|
|
reject({
|
data: null,
|
code: parseInt(res.data.code),
|
msg: res.data.data || res.data.msg
|
})
|
} else if (res.data.code === '4000' || res.data && res.data.code &&
|
res.data.code.startsWith('4000')) {
|
message.showToast('系统异常')
|
} else if (res.data.code === '401' || res.data && res.data.code &&
|
res.data.code.startsWith('401')) {
|
console.log('resp', res)
|
// storage.removeItem('token')
|
// await store.dispatch('logout')
|
store.commit('updateLogin', false)
|
|
await message.showToast('登录信息失效')
|
|
// #ifdef PUB_CUSTOMER
|
uni.reLaunch({
|
url: '/pages/user/supplier-user'
|
})
|
// #endif
|
// #ifndef PUB_CUSTOMER
|
uni.reLaunch({
|
url: '/pages/login/supplier-login'
|
})
|
// #endif
|
reject({
|
data: null,
|
code: 401,
|
msg: 'Unauthorized'
|
})
|
|
} else {
|
if (res.data.data && typeof res.data.data === 'string') {
|
message.showToast(res.data.data)
|
}
|
reject({
|
data: null,
|
code: parseInt(res.data.code),
|
msg: res.data.data
|
})
|
}
|
}
|
} else if (res.statusCode === 400) {
|
message.showToast('请求有误')
|
reject({
|
data: null,
|
code: 400,
|
msg: 'Bad Request'
|
})
|
} else if (res.statusCode === 401) {
|
// store.dispatch('/clearUserInfo')
|
// commit('updat')
|
// store.commit('updateLogin', false)
|
console.log('401', res)
|
// storage.removeItem('token')
|
// await store.dispatch('logout')
|
store.commit('updateLogin', false)
|
await message.showToast('登录信息失效')
|
|
|
// #ifdef PUB_CUSTOMER
|
uni.reLaunch({
|
url: '/pages/user/supplier-user'
|
})
|
// #endif
|
// #ifndef PUB_CUSTOMER
|
uni.reLaunch({
|
url: '/pages/login/supplier-login'
|
})
|
// #endif
|
reject({
|
data: null,
|
code: 401,
|
msg: 'Unauthorized'
|
})
|
|
} else if (res.statusCode === 403) {
|
message.showToast('请求被拒绝')
|
reject({
|
data: null,
|
code: 403,
|
msg: 'Forbidden'
|
})
|
} else if (res.statusCode === 429) {
|
message.showToast('系统操作太频繁,请稍后再试!')
|
reject({
|
data: null,
|
code: 429,
|
msg: 'Forbidden'
|
})
|
} else if (res.statusCode === 404) {
|
message.showToast('api不存在')
|
reject({
|
data: null,
|
code: 404,
|
msg: 'Not Found'
|
})
|
} else if (res.statusCode === 500) {
|
message.showToast('服务更新升级中,请稍等一分钟左右')
|
reject({
|
data: null,
|
code: 500,
|
msg: '服务更新升级中,请稍等一分钟左右'
|
})
|
} else if (res.statusCode === 502) {
|
message.showToast('服务更新升级中,请稍等一分钟左右')
|
reject({
|
data: null,
|
code: 502,
|
msg: '服务更新升级中,请稍等一分钟左右'
|
})
|
} else {
|
message.showToast('其他错误')
|
reject({
|
data: null,
|
code: 999,
|
msg: 'Other Error'
|
})
|
}
|
},
|
fail: (err) => {
|
if (!options.ignore) {
|
message.hideLoading()
|
}
|
message.showToast('网络异常')
|
reject({
|
data: null,
|
code: 9999,
|
msg: err
|
})
|
}
|
})
|
})
|
},
|
|
uploadTemp(filePath, dto) {
|
message.showLoading()
|
const token = storage.getItem('token')
|
let header = {
|
Authorization: token ? `Bearer ${token}` : null,
|
}
|
var url = environments.httpBaseUri + '/api/upload/oss/file'
|
if (environments.type == 'prod') {
|
//需要拼接
|
var year = utils.toYear(new Date())
|
url +=
|
`/local?filePath=${dto.serviceContent + '//' + dto.area+'//'+dto.housingEstate+'//'+dto.buildingNo+'//'+dto.roomNo}` +
|
`//${year}`
|
} else {
|
|
}
|
|
return new Promise((resolve, reject) => {
|
console.log('upload-filePath', filePath)
|
uni.uploadFile({
|
url: url,
|
header,
|
filePath: filePath,
|
name: 'file',
|
success: (res) => {
|
console.log('res debug', res)
|
message.hideLoading()
|
if (res.statusCode === 200) {
|
const obj = JSON.parse(res.data)
|
console.log('res debug2', obj)
|
if (obj.code.startsWith('200') || obj.code === '0') {
|
resolve({
|
data: obj.data,
|
code: 0,
|
msg: ''
|
})
|
} else {
|
reject()
|
}
|
} else {
|
reject()
|
}
|
},
|
fail: (e) => {
|
message.hideLoading()
|
console.log('resfail', e)
|
|
reject()
|
}
|
})
|
})
|
},
|
|
async requestCode(method, url, options) {
|
try {
|
return await http.requestTemp(method, `${pre}/${version}` + url, options)
|
} catch (e) {
|
return e
|
}
|
},
|
async request(method, url, options) {
|
try {
|
// console.log('request', url, options)
|
return await http.requestTemp(method, url, options)
|
} catch (e) {
|
return e
|
}
|
},
|
//需要按照规则,拼接
|
async upload(filePath, dto = {}) {
|
try {
|
return await http.uploadTemp(filePath, dto)
|
} catch (e) {
|
console.log('error', e)
|
return e
|
}
|
},
|
async commonDetail(code, params) {
|
return await http.request('get', `${pre}/${version}/${code}`, {
|
params
|
})
|
},
|
async commonPage(code, params) {
|
return await http.request('get', `${pre}/${version}/${code}/page`, {
|
params
|
})
|
},
|
async commonDelete(code, params) {
|
return await http.request('delete', `${pre}/${version}/${code}`, {
|
params
|
})
|
},
|
async commonDisabled(code, params) {
|
return await http.request('delete', `${pre}/${version}/${code}/disabled`, {
|
params
|
})
|
},
|
async commonAdd(code, data) {
|
return await http.request('post', `${pre}/${version}/${code}`, {
|
data
|
})
|
},
|
async commonUpdate(code, data) {
|
return await http.request('put', `${pre}/${version}/${code}`, {
|
data
|
})
|
},
|
|
}
|
|
|
export default http
|