151 lines
3.4 KiB
JavaScript
151 lines
3.4 KiB
JavaScript
import App from './App'
|
|
|
|
var baseUrl = 'https://admin.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
|
|
resolve(data)
|
|
},
|
|
fail(e) {
|
|
reject(e)
|
|
},
|
|
complete(e) {},
|
|
})
|
|
})
|
|
}
|
|
App.mpType = 'app'
|
|
const app = new Vue({
|
|
...App,
|
|
})
|
|
app.$mount()
|
|
// #endif
|
|
|
|
// #ifdef VUE3
|
|
import { createSSRApp } from 'vue'
|
|
|
|
function apiUrl(promise, Loading = true) {
|
|
const token = uni.getStorageSync('token')
|
|
|
|
const header = {
|
|
token: token,
|
|
'content-type': 'application/x-www-form-urlencoded',
|
|
...(promise.header || {}),
|
|
}
|
|
|
|
const data = promise.data || {}
|
|
const url = joinApiUrl(baseUrl, promise.url || '')
|
|
|
|
return new Promise((resolve, reject) => {
|
|
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')
|
|
}
|
|
|
|
resolve(data)
|
|
},
|
|
fail: (e) => {
|
|
reject(e)
|
|
},
|
|
complete: (e) => {
|
|
if (Loading) {
|
|
uni.hideLoading()
|
|
}
|
|
},
|
|
})
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 解析页面参数(支持普通参数和扫码 scene 参数)
|
|
* @param {Object} options - 页面 options
|
|
* @returns {Object} 解析后的参数对象
|
|
*/
|
|
function parsePageParams(options) {
|
|
const params = {}
|
|
if (!options) return params
|
|
|
|
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)
|
|
|
|
app.config.globalProperties.$url = baseUrl
|
|
app.config.globalProperties.apiUrl = apiUrl
|
|
app.config.globalProperties.$parsePageParams = parsePageParams
|
|
|
|
return {
|
|
app,
|
|
}
|
|
}
|
|
// #endif
|