h5/api/config.js

74 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2024-05-14 10:44:28 +08:00
import {
toast
} from "/tool/index.js"
export default {
baseUrl: 'https://syjk.yhy.ren/xcx-test/', // 生产
post(url, param) {
return this.ajax('post', url, param)
},
get(url, param) {
return this.ajax('get', url, param)
},
put(url, param) {
return this.ajax('put', url, param)
},
getUrl(url) {
return `${this.baseUrl}${url}`
},
setHeader(url) {
let header = {
'content-type': 'application/x-www-form-urlencoded',
'clientType': 'h5'
}
if (url.indexOf('application') != -1) header['content-type'] = 'application/json'
return header
},
setParams(param) {
const openId = uni.getStorageSync('openId') || ''
const userId = param.userId || uni.getStorageSync('userId') || ''
param['openId'] = openId
param['token'] = openId
param['userId'] = userId
return param
},
setUrlParams(isUrlParam, param, url) {
if (!isUrlParam) return ''
let str = []
for (let i in param) {
str.push(i + '=' + param[i])
}
return `${(url.indexOf('?')!=-1?'&':'?')}${str.join('&')}`
},
ajax(type, url, param = {}) {
return new Promise((resolve, reject) => {
let requestData = this.setParams(param.data || {})
let urlObj = this.setUrlParams(param.isUrlParam, requestData, url)
let request = {
url: `${this.baseUrl}${url}${urlObj}`,
method: type,
header: this.setHeader(url),
sslVerify: false,
dataType: 'json',
timeout: 300000,
data: requestData,
success: (response) => {
if (response.data.code == 500) {
return toast(response.data.msg)
}
let is = typeof response === 'object'
let data = response.data
resolve(is ? data : response)
},
fail: (err) => {
reject(err)
},
}
uni.request(request)
})
},
}