init
This commit is contained in:
56
utils/http.js
Normal file
56
utils/http.js
Normal file
@@ -0,0 +1,56 @@
|
||||
import clientConfig from "../config/index"
|
||||
import tool from "./tool"
|
||||
|
||||
|
||||
// 一下错误代码需要重新登录
|
||||
const reloadCodes = [401, 1011007, 1011008]
|
||||
|
||||
const moduleRequest = (module) => {
|
||||
return{
|
||||
get(endpoint,data,options = {}){
|
||||
return fetch({ method:'get', url: clientConfig.BASE_URL+module+endpoint, data, ...options} )
|
||||
},
|
||||
post(endpoint,data,options = {}){
|
||||
return fetch({ method:'post', url: clientConfig.BASE_URL+module+endpoint, data, ...options } )
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fetch = (config={}) => {
|
||||
wx.showLoading({title: ''})
|
||||
return new Promise((resolve,reject) => {
|
||||
wx.request({
|
||||
...config,
|
||||
timeout: clientConfig.TIME_OUT,
|
||||
header: {
|
||||
Token: tool.data.get('TOKEN') || null
|
||||
},
|
||||
success: (res)=>{
|
||||
if (res.data.code!==200) {
|
||||
wx.showToast({ title: res.data.msg, icon: 'none' })
|
||||
reject(res)
|
||||
if (reloadCodes.includes(res.data.code)) {
|
||||
tool.data.clear()
|
||||
wx.showToast({ title: '认证失败,请重新进入小程序~' })
|
||||
}
|
||||
}else{
|
||||
if (res.data.msg!==null) {
|
||||
// wx.showToast({ title: res.data.msg, icon: 'none' })
|
||||
}
|
||||
resolve(res.data)
|
||||
}
|
||||
|
||||
},
|
||||
fail: (res)=>{
|
||||
wx.showToast({ title: '服务器过载或维护', icon: 'none' })
|
||||
reject(res)
|
||||
},
|
||||
complete: ()=> {
|
||||
wx.hideLoading()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
export default moduleRequest
|
||||
28
utils/tool.js
Normal file
28
utils/tool.js
Normal file
@@ -0,0 +1,28 @@
|
||||
const tool = {}
|
||||
|
||||
/**
|
||||
* localStorage
|
||||
*/
|
||||
tool.data = {
|
||||
set(table, settings) {
|
||||
const _set = JSON.stringify(settings)
|
||||
return wx.setStorageSync(table, _set)
|
||||
},
|
||||
get(table) {
|
||||
let data = wx.getStorageSync(table)
|
||||
try {
|
||||
data = JSON.parse(data)
|
||||
} catch (err) {
|
||||
return null
|
||||
}
|
||||
return data
|
||||
},
|
||||
remove() {
|
||||
return wx.removeStorageSync(table)
|
||||
},
|
||||
clear() {
|
||||
return wx.clearStorageSync()
|
||||
}
|
||||
}
|
||||
|
||||
export default tool
|
||||
19
utils/util.js
Normal file
19
utils/util.js
Normal file
@@ -0,0 +1,19 @@
|
||||
const formatTime = date => {
|
||||
const year = date.getFullYear()
|
||||
const month = date.getMonth() + 1
|
||||
const day = date.getDate()
|
||||
const hour = date.getHours()
|
||||
const minute = date.getMinutes()
|
||||
const second = date.getSeconds()
|
||||
|
||||
return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
|
||||
}
|
||||
|
||||
const formatNumber = n => {
|
||||
n = n.toString()
|
||||
return n[1] ? n : `0${n}`
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
formatTime
|
||||
}
|
||||
Reference in New Issue
Block a user