import appInfo from "./appInfo"; let BASE_URL = 'https://dou.wenxingshuju.com' // 请求地址 - 生产环境 // let BASE_URL = 'https://dou.wenxingshuju.com' // 请求地址 - 测试环境 // #ifdef H5 BASE_URL = '' //线上模式 // BASE_URL = '/api' // 开发模式 // #endif const LOGIN_ERR_CODES = [ // 登录状态失效错误码 '2008', '2005', '4001', '4002', ] const handleToLogin = () => { uni.clearStorageSync() uni.reLaunch({ url: '/pages/subPackages/login/loginPhone' }); } const req = (url, method, data, selfHeader, isShowLoading = true) => { const dataes = { app_id: appInfo.appId || '', open_id: uni.getStorageSync('openid') || '', } const header = { // 'source': uni.getStorageSync('channel_source') || '', // 'version': uni.getStorageSync('channel_version') || '', // 'token': uni.getStorageSync('token') || '', } return new Promise((resolve, reject) => { // S 联调日志 try { console.log('$$req url => ', url) console.log('$$req method => ', method) console.log('$$req params => ', data ? Object.assign(dataes, data) : dataes) } catch (error) { console.log(error) } // E 联调日志 if (isShowLoading) { // 默认显示loading uni.showLoading({ title: '加载中...', mask: true }) } uni.request({ url:`${BASE_URL}${url}`, data: data ? Object.assign(dataes, data) : dataes, method: method, header: selfHeader ? Object.assign(header, selfHeader) : header, success: function (res) { uni.hideLoading() // if (LOGIN_ERR_CODES.includes(String(res.data.errno))) { // handleToLogin() // } resolve(res) }, fail: function (err) { uni.hideLoading() reject(err) } }) }) } const get = (url, data, header = {}, isShowLoading = true) => { return req(url, 'GET', data, { ...header }, isShowLoading) } const post = (url, data, header = {}, isShowLoading = true) => { return req(url, 'POST', data, { 'Content-Type': 'application/x-www-form-urlencoded', ...header, }, isShowLoading) } const postJSON = (url, data, header = {}, isShowLoading = true) => { return req(url, 'POST', data, { 'Content-Type': 'application/json', ...header, }, isShowLoading) } // 获取随机参数 h5使用 const getUuid = () => { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { const r = (Math.random() * 16) | 0, v = c == 'x' ? r : (r & 0x3) | 0x8 return v.toString(16) }) } export { req, BASE_URL, get, post, postJSON, getUuid, }