uniapp request请求

import * as authService from "@/service/authService/authService"
import * as Sign from "@/common/Sign.js"
export default class API {

	// static prefix = 'http://www.linglikeji.cn/api'  //旧
	// static prefix = 'http://192.168.6.100:9099'
	static prefix = 'https://www.wait-hope.com'

	static request(url, data, config, method) {
		uni.hideLoading()
		uni.showLoading()
		return new Promise((resolve, reject) => {
			// 判断url 是否是 http 开通的
			let reqUrl = url
			let Timestamp = new Date().getTime()
			let s_sign = Sign.getSign(url, 'APP-ANDROID', data, plus.runtime.channel || 'common',
				Timestamp)
			if (!url.startsWith('http')) {
				reqUrl = `${this.prefix}${url}`
			}
			let customConfig = config || {}
			// 处理不同的请求方式,修改为表单提交方式
			if (customConfig.useForm) {
				customConfig.header = {
					...(customConfig.header || {}),
					'content-type': 'application/x-www-form-urlencoded'
				}
				delete customConfig.useForm
			}
			// 带上tokne
			if (getApp().globalData.Token) {
				customConfig = {
					...customConfig,
					header: {
						...(customConfig.header || {}),
						authorization: getApp().globalData.Token
					}
				}
			}
			customConfig = {
				...customConfig,
				header: {
					...(customConfig.header || {}),
					'X-Timestamp': Timestamp,
					'X-Sign': s_sign
				}
			}
			uni.request({
				url: reqUrl,
				data,
				// timeout: 3000,
				method,
				...this.mergeConfig(customConfig),
				success(res) {
					const {
						code,
						message,
						...data
					} = res.data || {}
					console.log(url, code, data, res);
					if (code === 200) {
						resolve(data.data || {})
					} else {
						uni.showToast({
							title: message || '系统异常',
							position: "top",
							duration: 3000
						})
						reject(message || '系统异常')
						// if (code === 10110) {
						if (code === 11011) {
							// 登录超时逻辑
							authService.reset()
							// 清空缓存
							uni.showModal({
								title: '提示',
								content: '登录超时,是否重新登录?',
								success(res) {
									if (res.confirm) {
										uni.navigateTo({
											url: "/pages/fnPages/user/login/login"
										})
									}
								}
							})
							// 
						}
					}
				},
				fail(e) {
					reject(e)
					uni.showToast({
						title: e.errMsg,
						position: "top",
						duration: 3000
					})
				},
				complete() {
					uni.hideLoading()
				}
			})
		})
	}

	static get(url, data, config) {
		return this.request(url, data, config, 'GET')
	}

	static post(url, data, config) {
		return this.request(url, data, config, 'POST')
	}

	static delete(url, data, config) {
		return this.request(url, data, config, 'DELETE')
	}
	static put(url, data, config) {
		return this.request(url, data, config, 'PUT')
	}

	static defaultConfig = {
		dataType: 'json',
		sslVerify: false,
		header: {
			'content-type': 'application/json',
			'X-Channel': plus.runtime.channel || 'common',
			'X-Version': plus.runtime.version,
			'X-PackageName': `uni.${plus.runtime.appid.replace(/__/g, '')}`,
			'X-Platform': 'APP-ANDROID',
		},
	}

	static mergeConfig(config) {
		if (config) {
			return {
				...this.defaultConfig,
				...config,
				header: {
					...this.defaultConfig.header,
					...(config.header || {})
				}
			}
		}
		return this.defaultConfig
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值