uni-app (安卓、微信小程序)接口封装 token失效自动获取新的token

一、文件路径截图

1、新建一个文件app.js存放接口

复制代码
//这里存放你需要的接口
 
import {request} from '@/utils/request.js' //这个文件是请求逻辑处理
module.exports = {
	// 登录 -- 注册
	perssonRegister: (data) => { // 供应商注册 
		return request({
			url: 'manageWx/Login/perssonRegister',
			method: "post",
			data
		})
	}
},

2、新建一个文件request.js

复制代码
const host = "http://192.168.1.10:8081/jeecg-boot/" //这个是本地地址,项目上线可以换成线上
export const request = (options) => {
	console.log(options, `调用接口`)
	return new Promise((resolve, reject) => {
		const WXUSER = uni.getStorageSync('WXUSER');
		if (!options.url) {
			console.error('请传入URL')
			return;
		}
		if (!WXUSER.rememberToken) {
			wxlogin(options).then(() => {
				sendRequest(options, resolve, reject);
			});
		} else {
			sendRequest(options, resolve, reject);
		}
	})
};
//封装的发送请求函数
function sendRequest(options, resolve, reject) {
	const WXUSER = uni.getStorageSync('WXUSER');
	uni.request({
		url: options.url,
		data: options.data || '',
		method: options.method || 'POST',
		header: {
			"X-Access-Token": WXUSER.rememberToken,
		},
		success: (res) => {
			if (res.data.code == 401) {
				console.log('返回401')
				wxlogin(options).then(() => {
					sendRequest(options, resolve, reject);
				});
			} else resolve(res.data)
		}
	})
};
//  token失效请求微信的无痕登录   app 就换成自己的登录接口
export async function wxlogin(options) {
	return new Promise((resolve, reject) => {
		uni.getUserInfo({
			success: (UserRes) => {
				uni.login({
					desc: 'weixin',
					success: res => {
						wx.request({
							url: `${host}manageWx/Login/queryUserByPhone`, //换成在自己登录接口
							method: 'POST',
							data: {
								weappCode: res.code,
							},
							success: res => {
								if (res.data.success) {
									console.log(res.data.result.wxUser)
									const WXUSER = res.data.result.wxUser
									const SYSUSER = res.data.result.sysUser
									uni.setStorageSync('WXUSER',WXUSER);
									uni.setStorageSync('SYSUSER',SYSUSER);
									resolve(); // 登录成功后,返回resolve
								} else {
									console.log('登录错误', res)
									reject(); // 登录失败时,返回reject
								}
							}
						});
					}
				});
			}
		})
	});
};
module.exports = {
	request,
	host
}

3、页面使用

复制代码
<!--@ 商品列表 -->
<template>
	

</template>
<script>
	import {perssonRegister} from '@/utils/app.js' //调用接口使用
	export default {
		name: "PlaceOrderGoods",
		
		data() {
			return {
				dataSource: [],
				
			};
		},

	
		onLoad() {
		
			this.loadData()
		},
		methods: {
			
			// 单位名称的数据
			async loadData() {
                
                const pames = {}
                
    await queryUnitName(pames).then(res => {
						if (res.code == 200) {
							this.dataSource= res.result
						}
					})
					.catch(err => {
						// 失败执行
					})
					.finally(() => {
						// 执行代码正确、报错都执行	
						
					});

			},
			
			

		}
	};
</script>

<style lang="scss" scoped>
	
</style>
相关推荐
玛雅牛牛5 小时前
多款物流信息哪款更适合
小程序
说私域19 小时前
技术赋能直播运营:开源AI智能名片商城小程序助力个人IP构建与高效运营
人工智能·tcp/ip·小程序·流量运营·私域运营
码云数智-园园19 小时前
uni-app 实现物流进度跟踪功能:从 UI 到数据驱动的完整方案
ui·uni-app
说私域19 小时前
流量思维向长效思维转型:开源链动2+1模式AI智能名片小程序赋能私域电商品牌建设
人工智能·小程序·开源·产品运营·私域运营
说私域2 天前
链动2+1模式AI智能名片S2B2C商城小程序在微商信任重建中的创新应用与价值实现
大数据·人工智能·小程序·私域运营
qq_24218863322 天前
微信小程序AI象棋游戏开发指南
人工智能·微信小程序·小程序
予你@。2 天前
UniApp + Vue3 实现 Tab 点击滚动定位(微信小程序)
微信小程序·小程序·uni-app
大黄说说2 天前
小程序商城哪个平台好?码云数智、有赞、微盟对比
小程序
游戏开发爱好者82 天前
完整教程:App上架苹果App Store全流程指南
android·ios·小程序·https·uni-app·iphone·webview
潆润千川科技3 天前
中老年同城社交小程序功能梳理与应用分析
小程序