wanmeiyizhan/utils/testApi.js
2025-03-06 22:51:16 +08:00

131 lines
4.5 KiB
JavaScript

import Store from '../store/index'
// 静态图片地址
let url = 'http://10.104.1.8:3001' // 移动业务请求路由
let coopUrl = 'https://mp.eshangtech.com/Coop.Merchant/Handler/handler_ajax.ashx' // 合作商户请求路由
let mpUrl = 'https://eshangtech.com/Coop.Merchant/Handler/handler_ajax.ashx' // 合作商户请求路由
let webUrl = 'https://dev.eshangtech.com:10818' // webapi
// let webUrl = 'http://192.168.10.125:8900/' // webapi
let staticImagePath = 'https://eshangtech.com/ShopICO'
let type = uni.getSystemInfoSync();
let loginType = ''
if (type.platform === "android" && type.uniPlatform === "app") {
loginType = "android"
} else if (type.uniPlatform === "mp-weixin") {
loginType = "min"
} else if (type.osName === 'ios') {
loginType = "ios"
}
const api = {
url: url,
staticImagePath,
request(method, data, control, isWebApi) {
if (method === 'POST') {
uni.showLoading({
title: '加载中...',
mask: true
})
}
return new Promise((resolve, reject) => {
data.appId = loginType ? loginType === "android" || loginType === "ios" ? data.appId || '' : data.appId ? data.appId : uni.getAccountInfoSync().miniProgram.appId : '' // 'wx4c497eddcec4a0e7'
data.weChatAppId = loginType ? loginType === "android" || loginType === "ios" ? data.weChatAppId || '' : data.appId ? data.appId : uni.getAccountInfoSync().miniProgram.appId : '' // 'wx4c497eddcec4a0e7'
data.wechatAppAppId = loginType ? loginType === "android" || loginType === "ios" ? data.wechatAppAppId || '' : data.appId ? data.appId : uni.getAccountInfoSync().miniProgram.appId : '' // 'wx4c497eddcec4a0e7'
data.WECHATAPP_APPID = loginType ? loginType === "android" || loginType === "ios" ? data.WECHATAPP_APPID || '' : data.appId ? data.appId : uni.getAccountInfoSync().miniProgram.appId : '' // 'wx4c497eddcec4a0e7'
data.ownerUnitId = Store.getters.ownerUnitId || data.ownerUnitId || ''
data.Membership_Id = Store.getters.user.MEMBERSHIP_ID || Store.getters.user.MemberShipID || data.MEMBERSHIP_ID || ''
data.MembershipId = Store.getters.user.MEMBERSHIP_ID || Store.getters.user.MemberShipID || data.MEMBERSHIP_ID || ''
data.MEMBERSHIP_ID = Store.getters.user.MEMBERSHIP_ID || Store.getters.user.MemberShipID || data.MEMBERSHIP_ID || ''
let requstOptions = {
url: isWebApi ? this.url + control : this.url,
data: data,
method: method
}
if (method === 'POST') {
if (data.requestType === 'application/x-www-form-urlencoded') {
requstOptions.header = {
'content-type': 'application/x-www-form-urlencoded'
}
} else {
requstOptions.header = {
'content-type': 'application/json'
// application/json
}
}
}
requstOptions.success = function (res) {
// success
if (res.statusCode !== 200 && res.statusCode !== 201) {
uni.showToast({
title: '网络出错,稍后再试',
icon: 'none'
})
return false
}
if (method === 'POST') {
uni.hideLoading()
if (res.data.ResultCode !== '999' && res.data.ResultCode !== '100') {
if (res.data.ResultDesc) {
console.log(res.data.ResultDesc)
// uni.showModal({
// title: '温馨提示',
// content: res.data.ResultDesc,
// showCancel: false,
// confirmText: '确定',
// confirmColor: '#3CC51F'
// })
}
}
}
resolve(res.data)
}
requstOptions.fail = function (error) {
// fail
if (method === 'POST') {
uni.hideLoading()
}
reject(error) // 请求失败
return false
}
requstOptions.complete = function () {
// complete
if (method === 'POST') {
uni.hideLoading()
}
}
uni.request(requstOptions)
})
},
getCoop(data) {
this.url = coopUrl
return this.request('GET', data)
},
postCoop(data) {
this.url = coopUrl
return this.request('POST', data)
},
mpPost(data) {
this.url = mpUrl
return this.request('POST', data)
},
$get(control, data) {
this.url = webUrl
return this.request('GET', data, control, true)
},
$post(control, data) {
this.url = webUrl
return this.request('POST', data, control, true)
}
}
export default api // 暴露出来供其他文件引用