更新
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
import App from './App'
|
||||
|
||||
// #ifndef VUE3
|
||||
import Vue from 'vue'
|
||||
import './uni.promisify.adaptor'
|
||||
Vue.config.productionTip = false
|
||||
Vue.prototype.$url = 'http://api.zzzhengyangtang.cn'
|
||||
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=Vue.prototype.$url+promise.url
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
uni.request({
|
||||
url: url,
|
||||
data: data,
|
||||
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
|
||||
const baseUrl = 'http://api.zzzhengyangtang.cn'
|
||||
|
||||
// 封装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 = baseUrl + (promise.url || '')
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
// 可选:根据Loading参数显示加载中
|
||||
if (Loading) {
|
||||
uni.showLoading({ title: '加载中...', mask: true })
|
||||
}
|
||||
|
||||
uni.request({
|
||||
url: url,
|
||||
data: data,
|
||||
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)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function createApp() {
|
||||
const app = createSSRApp(App)
|
||||
|
||||
// Vue3挂载全局属性的方式
|
||||
app.config.globalProperties.$url = baseUrl
|
||||
app.config.globalProperties.apiUrl = apiUrl
|
||||
|
||||
return {
|
||||
app
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
Reference in New Issue
Block a user