import App from './App' var baseUrl ='https://css.zhenyangtang.com.cn/'; function joinApiUrl(base, path) { const b = String(base || '').replace(/\/+$/, '') const p = String(path || '').replace(/^\/+/, '') return p ? `${b}/${p}` : b } // #ifndef VUE3 import Vue from 'vue' import './uni.promisify.adaptor' Vue.config.productionTip = false Vue.prototype.$url =baseUrl Vue.prototype.apiUrl =function apiurl(promise, Loading = true){ let token=uni.getStorageSync('token') promise.header={ token:token, 'content-type':'application/x-www-form-urlencoded', } let header = {} if (promise.header) { header = promise.header } let data = {} if (promise.data) { data = promise.data } let url = joinApiUrl(Vue.prototype.$url, promise.url) return new Promise(function(resolve, reject) { uni.request({ url: url, data: data, timeout: promise.timeout != null ? promise.timeout : 15000, method: promise.method ? promise.method : 'POST', header: header, sslVerify: false, success: (res) => { const data=res.data //-1检测登陆失效删除登陆状态 resolve(data); }, fail(e) { reject(e) }, complete(e) { //console.log('complete', e) } }); }); } App.mpType = 'app' const app = new Vue({ ...App }) app.$mount() // #endif // #ifdef VUE3 import { createSSRApp } from 'vue' // 定义全局基础URL // 封装Vue3版本的apiUrl方法 function apiUrl(promise, Loading = true) { // 获取token const token = uni.getStorageSync('token') // 合并header(修复原代码直接覆盖的问题) const header = { token: token, 'content-type': 'application/x-www-form-urlencoded', // 合并用户传入的header,用户传入的会覆盖默认值 ...(promise.header || {}) } // 处理请求数据和URL const data = promise.data || {} const url = joinApiUrl(baseUrl, promise.url || '') return new Promise((resolve, reject) => { // 可选:根据Loading参数显示加载中 if (Loading) { uni.showLoading({ title: '加载中...', mask: true }) } uni.request({ url: url, data: data, timeout: promise.timeout != null ? promise.timeout : 15000, method: promise.method || 'POST', // 简化三元表达式 header: header, sslVerify: false, success: (res) => { const data = res.data // 处理登录失效逻辑(补充完整) if (data.code === -1) { uni.removeStorageSync('token') // 删除token //uni.showToast({ title: '登录已失效,请重新登录', icon: 'none' }) // 可选:跳转到登录页 // uni.redirectTo({ url: '/pages/login/login' }) } resolve(data) }, fail: (e) => { // uni.showToast({ title: '请求失败,请重试', icon: 'none' }) reject(e) }, complete: (e) => { // 关闭加载中 if (Loading) { uni.hideLoading() } // console.log('complete', e) } }) }) } /** * 解析页面参数(支持普通参数和扫码 scene 参数) * @param {Object} options - 页面 options,如 getCurrentPages()[].options * @returns {Object} 解析后的参数对象 */ function parsePageParams(options) { const params = {} if (!options) return params // 如果有 scene 参数(扫码进入),解析 scene if (options.scene) { try { const decodedScene = decodeURIComponent(options.scene) decodedScene.split('&').forEach(item => { const [key, value] = item.split('=') if (key && value) { params[key] = value } }) } catch (error) { console.error('解析scene参数失败:', error) } } // 合并普通参数(普通跳转进入) Object.keys(options).forEach(key => { if (key !== 'scene' && options[key]) { params[key] = options[key] } }) return params } export function createApp() { const app = createSSRApp(App) // Vue3挂载全局属性的方式 app.config.globalProperties.$url = baseUrl app.config.globalProperties.apiUrl = apiUrl app.config.globalProperties.$parsePageParams = parsePageParams return { app } } // #endif