企获客基础班本接口方法等描述
This commit is contained in:
62
apis/err.js
Normal file
62
apis/err.js
Normal file
@@ -0,0 +1,62 @@
|
||||
|
||||
/**
|
||||
* 处理错误信息
|
||||
* @property {Object} errInfo
|
||||
*/
|
||||
|
||||
const errInfo = (obj) =>{
|
||||
if(obj.status_code == 401){
|
||||
wx.showModal({
|
||||
title : "登录提示",
|
||||
content : "长时间未操作,登录已过期,请重新登录",
|
||||
confirmColor: "#2d6bf6",
|
||||
confirmText : "立即登录",
|
||||
cancelText : "返回首页",
|
||||
cancelColor : "#aeafb1",
|
||||
success : res=>{
|
||||
// 清理客户端登录缓存
|
||||
wx.removeStorageSync("token")
|
||||
if(res.confirm){
|
||||
wx.navigateTo({
|
||||
url: "/pages/login/login",
|
||||
})
|
||||
//转跳到登录页面
|
||||
}else if(res.cancel){
|
||||
//考虑是否增加一个跳转首页的操作
|
||||
wx.redirectTo({
|
||||
url: "/pages/index/index",
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}else if(obj.status_code == 422){
|
||||
wx.showToast({
|
||||
title: obj.message,
|
||||
icon : "none"
|
||||
})
|
||||
}else if(obj.status_code == 400 || obj.status_code == 0){
|
||||
wx.showToast({
|
||||
title: obj.message,
|
||||
icon : "none"
|
||||
})
|
||||
}else if(obj.status_code == 404){
|
||||
wx.showToast({
|
||||
title: "接口地址不存在,请联系系统管理员",
|
||||
icon : "none"
|
||||
})
|
||||
}else if(obj.status_code == 500){
|
||||
wx.showToast({
|
||||
title: "服务端:" + obj.message,
|
||||
icon : "none"
|
||||
})
|
||||
}else {
|
||||
wx.showToast({
|
||||
title: "code:" + obj.status_code + ", msg:" + obj.message,
|
||||
icon : "none"
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
errInfo
|
||||
}
|
||||
13
apis/index.js
Normal file
13
apis/index.js
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
/*
|
||||
* 接口路由
|
||||
*/
|
||||
|
||||
import file from "./interfaces/file"
|
||||
import auth from "./interfaces/auth"
|
||||
|
||||
|
||||
export default{
|
||||
file,
|
||||
auth
|
||||
}
|
||||
13
apis/interfaces/auth.js
Normal file
13
apis/interfaces/auth.js
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
/*
|
||||
* 授权
|
||||
*/
|
||||
import {req} from "../request"
|
||||
|
||||
const authPhone = data => req({url: "auth/mini", method: "POST", data: data}) //登录
|
||||
const authInfo = data => req({url: "auth/mini/info", method: "POST", data: data}) //完善用户信息
|
||||
|
||||
export default({
|
||||
authPhone,
|
||||
authInfo
|
||||
})
|
||||
12
apis/interfaces/file.js
Normal file
12
apis/interfaces/file.js
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
/*
|
||||
* 公用接口
|
||||
*/
|
||||
|
||||
import {upload} from "../request"
|
||||
|
||||
const uploadImg = (imgPaht, data) => upload({url: "storage", key: "image", path: imgPaht, data: data}) //上传图片
|
||||
|
||||
export default({
|
||||
uploadImg
|
||||
})
|
||||
111
apis/request.js
Normal file
111
apis/request.js
Normal file
@@ -0,0 +1,111 @@
|
||||
|
||||
import {errInfo} from './err'
|
||||
import {updToken} from './updateToken'
|
||||
|
||||
// 请求方式配置
|
||||
const api = "https://mi-org.cnskl.com/api/"
|
||||
const header = {
|
||||
"Accept": "application/json"
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求
|
||||
* @property {Object} req
|
||||
*/
|
||||
|
||||
const req = (obj) => {
|
||||
// header
|
||||
header.Authorization = wx.getStorageSync("token") || ""
|
||||
// 处理请求信息
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.showLoading({
|
||||
title: "加载中..",
|
||||
mask: true
|
||||
})
|
||||
wx.request({
|
||||
url : api + obj.url,
|
||||
header : header,
|
||||
method : obj.method || 'GET',
|
||||
data : obj.data || {},
|
||||
success : res => {
|
||||
wx.hideLoading();
|
||||
// 更新token
|
||||
if (res.header.Authorization) updToken(res.header.Authorization)
|
||||
// 处理信息
|
||||
if (res.data.status_code == 200) {
|
||||
resolve(res.data.data)
|
||||
} else {
|
||||
if (res.data.status_code == 401 || res.data.status_code == 400) {
|
||||
reject({
|
||||
login : false,
|
||||
codeBeen: false
|
||||
})
|
||||
}
|
||||
errInfo(res.data)
|
||||
}
|
||||
},
|
||||
fail: err => {
|
||||
wx.showToast({
|
||||
title : err.errMsg,
|
||||
icon : "none"
|
||||
})
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传
|
||||
* @property {Object} upload
|
||||
*/
|
||||
|
||||
const upload = (obj) => {
|
||||
// header
|
||||
header.Authorization = wx.getStorageSync("token") || ""
|
||||
// 处理上传信息
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.showLoading({
|
||||
title: "上传中..",
|
||||
mask: true
|
||||
})
|
||||
|
||||
wx.uploadFile({
|
||||
url : api + obj.url,
|
||||
header : header,
|
||||
name : obj.key || "",
|
||||
filePath: obj.path || "",
|
||||
formData: obj.data || {},
|
||||
success : res=>{
|
||||
wx.hideLoading();
|
||||
// 处理返回值
|
||||
let jsonData = JSON.parse(res.data)
|
||||
// 更新token
|
||||
if (res.header.Authorization) updToken(res.header.Authorization)
|
||||
// 处理信息
|
||||
if (jsonData.status_code == 200) {
|
||||
resolve(jsonData.data)
|
||||
} else {
|
||||
if (jsonData.status_code == 401) {
|
||||
reject({
|
||||
login: false
|
||||
})
|
||||
}
|
||||
errInfo(jsonData)
|
||||
}
|
||||
},
|
||||
fail : err=>{
|
||||
wx.showToast({
|
||||
title : err.errMsg,
|
||||
icon : "none"
|
||||
})
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
req,
|
||||
upload
|
||||
}
|
||||
17
apis/updateToken.js
Normal file
17
apis/updateToken.js
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
/**
|
||||
* 更新token
|
||||
* @property {String} updToken
|
||||
*/
|
||||
|
||||
const updToken = (token) =>{
|
||||
// 更新全局存储器
|
||||
getApp().globalData.token = token
|
||||
// 更新客户端登录缓存
|
||||
wx.setStorageSync('token', token)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
updToken
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user