// 引入配置文件 import toast from "../uni_modules/uview-plus/components/u-toast/toast.js"; import config from "./config.js"; import User from "./user.js"; export default { config: { baseUrl: config.webUrl, header: { 'Content-Type': 'application/json;charset=UTF-8', }, data: {}, method: "GET", dataType: "json", }, request(options = {}) { options.header = options.header || this.config.header; options.method = options.method || this.config.method; options.dataType = options.dataType || this.config.dataType; options.url = this.config.baseUrl + options.url; options.header.Authorization = uni.getStorageSync('token') // TODO:token增加等操作 // console.log(options); if (options.token) { // 验证用户是否登录 if (!this.checkToken(options.checkToken)) return; // 验证用户操作权限(验证是否绑定手机号码) // if (!this.checkAuth(options.checkAuth)) return; options.header.Authorization = User.token; } else { this.checkToken() } return new Promise((success, resolve) => { uni.request(options) //请求成功 .then(res => { // console.log(res,'resresresresresresres'); if (res.data.code == 200) { console.log('请求成功'); success(res) } else if (res.data.code == 2001) { console.log('请求成功'); success(res) } else if (res.data.code == 403) { this.checkLoginExpired(); } else if (res.data.code == 500) { uni.showToast({ title: res.data.msg, icon: "none" }); resolve(res) } this.logApiRequest(options, res) }) //请求失败 .catch(err => { console.log('请求失败'); }) }) }, get(url, data, options = {}) { options.url = url; options.data = data; options.method = 'GET'; return this.request(options); }, post(url, data, options = {}) { options.url = url; options.data = data; options.method = 'POST'; return this.request(options); }, // 上传图片 upload(url, options = {}) { options.url = this.config.baseUrl + url; options.header = options.header || this.config.header; options.fileType = options.fileType || "image"; options.formData = options.formData || {}; options.filePath = options.filePath; options.name = options.name; // TODO:token增加等操作 if (options.token) { // 验证是否登录 if (!this.checkToken(options.checkToken)) return; // 验证权限 // if (!this.checkAuth(options.checkAuth)) return; options.header.Authorization = User.token; } return uni.uploadFile(options); }, // 错误处理 errorCheck(err, res, errfun = false, resfun = false) { if (err) { typeof errfun === 'function' && errfun(); uni.showToast({ title: '加载失败', icon: "none" }); return false; } if (res.data.errorCode) { typeof errfun === 'function' && resfun(); uni.showToast({ title: res.data.msg, icon: "none" }); return false; } return true; }, // 验证用户是否登录 checkToken(checkToken) { if (checkToken && !User.token) { uni.showToast({ title: '请先登录', icon: "none" }) uni.navigateTo({ url: '/pages/login/login' }); return false; } return true; }, checkLoginExpired(res) { uni.clearStorageSync(); uni.hideLoading() uni.showModal({ title: '提示', content: '请先登录', success(res) { console.log(res) if (res.confirm) { uni.redirectTo({ url: '/pages/login/login' }) } } }) return true; }, //封装日志打印函数 logApiRequest(data, response) { console.log(`========== 接口请求start日志 ==========`); console.log(data); console.log(response); console.log(`========== 接口请求end日志 ==========`); } // 验证用户权限 // checkAuth(checkAuth){ // if (checkAuth && !User.userinfo.phone) { // uni.showToast({ title: '请先绑定手机号码', icon:"none" }) // uni.navigateTo({ // url: '/pages/user-bind-phone/user-bind-phone' // }); // return false; // } // return true; // } }