import Vue from "vue"; import token from "@/store/modules/token" import store from '@/store' // 基础 // 根据体验版本和线上版本区分接口地址 const baseUrl = /* __wxConfig.envVersion !== "release" ? "" : */"https://id.ssjlai.com/shaping"; const apiArray = { // 登录 Login: { url: baseUrl + "/api/User/Login", config: {}, }, upload: { url: baseUrl + '/api/File/upload', config: {}, }, }; let api = {}; Object.keys(apiArray).forEach((item) => { api[item] = async function (config = {}, deep = false) { return new Promise(async (res, rej) => { let temp = apiArray[item]; let apiconfig = temp.config; let header = config.header ? config.header : apiconfig.header ? apiconfig.header : { "content-type": "application/json;charset:utf-8", }; header.AuthToken = /* store.getter.token */token // 鉴权-登录检测 if (!(temp.auth === false)) { // 如果没有用户信息则跳转至登录页 // Replace // if (!store.state.agentId) { // setTimeout(() => { // uni.showToast({ // title: '请先登录', // icon: 'none', // duration: 4000, // }) // }, 500) // uni.reLaunch({ // url: '/pages/login/login', // }) // return // } // 统一注入token } let url = config.url ? config.url : temp.url; if (config.query) { url += Vue.prototype.$u.queryParams(config.query); } uni.request({ url: url, data: config.data ? config.data : {}, method: config.method ? config.method : apiconfig.method ? apiconfig.method : "POST", timeout: config.timeout ? config.timeout : apiconfig.timeout ? apiconfig.timeout : 60000, header: header, dataType: config.dataType ? config.dataType : apiconfig.dataType ? apiconfig.dataType : "json", responseType: config.responseType ? config.responseType : apiconfig.responseType ? apiconfig.responseType : "text", sslVerify: config.sslVerify === false ? false : apiconfig.sslVerify === false ? false : true, withCredentials: config.withCredentials === false ? false : apiconfig.withCredentials === true ? true : false, firstIpv4: config.firstIpv4 === false ? false : apiconfig.firstIpv4 === false ? false : true, async success(data) { // console.log(config.url?config.url:temp.url); if (data.statusCode === 200 || data.code === 200) { console.log("请求数据", { ...config, }); console.log("响应数据", { ...data, }); let re = data.data; console.log("re", re); if ( re.code === 200 || re.stateCode === 0 || re.statusCode === 1 || re.code === 106 || apiconfig.err === false ) { res(re); } else { setTimeout(() => { uni.showToast({ icon: "none", title: re.msg || re.message, duration: 3000, }); }, 500); res(false); } } else { let str = `${data.statusCode}:${data.data.msg}`; setTimeout(() => { uni.showToast({ icon: "none", title: str, duration: 3000, }); }, 500); res(false); } }, fail(err) { console.log(err); let str = "网络错误:" + (err.statusCode ? err.statusCode : err.errMsg); if (err.errMsg == "request:fail timeout") { str = "网络异常,请检查网络"; } setTimeout(() => { uni.showToast({ icon: "none", title: str, duration: 3000, }); }, 500); res(false); }, }); }); }; }); export default { api, baseUrl, };