微信小程序更新token

  • Post author:
  • Post category:小程序


// 更新token
			refreshToken() {
				let that = this
				// token过期时间 后台设置18000秒 判定17000秒
				if (uni.getStorageSync("userInfo").token) {
					// access_token 过期时间剩余
					let expires_in = uni.getStorageSync("userInfo").token.expires_in - Date.now()
					// refresh_token 过期时间固定3天
					let expires_refresh = uni.getStorageSync("userInfo").token.expires_refresh - Date.now()

					console.log(expires_in, expires_refresh, "token过期时间:" + expires_in * 0.0000167 + "分钟" + expires_in *
						0.0000167 * 0.0166667 + "小时")

					if (expires_in < 3000 && expires_refresh > 3000) { //access_token过期  expires_refresh未过期  3000秒 == 50分钟
						let json = {
							refreshToken: uni.getStorageSync('userInfo').token.refresh_token
						}
						that.$api.user.refresh(json).then(res => {
							if (res.code == 200) {
								let userInfo = uni.getStorageSync("userInfo")
								userInfo.token.refresh_token = res.data.refresh_token
								userInfo.token.access_token = res.data.access_token
								userInfo.user.tenantCode = res.data.tenant_code
								// 重置token过期时间
								userInfo.token.expires_in = res.data.expires_in * 1000 + Date.now()
								// refresh_token 过期时间固定3天
								userInfo.token.expires_refresh = 259200000 + Date.now()
								// token重置成功
								console.log("token重置成功")
								uni.setStorageSync("userInfo", userInfo);
							}
						})
					} else if (expires_refresh < 3000) { //expires_refresh过期
						let code = ''
						uni.login({
							success: response => {
								code = response.code
							}
						})
						uni.showModal({
							title:"提示",
							content:"登录状态已过期,请重新登录!",
							showCancel:false,
							confirmText:"重新登录",
							success(res) {
								if (res.confirm) {
									wx.getUserProfile({
										desc: '用于完善会员资料',
										success: (res) => {
											console.log("重新登录")
											let json = {
												code: code,
												encryptedData: res.encryptedData,
												iv: res.iv
											};
											console.log("token过期")
											that.$api.user.wxLogin(json).then(res => {
												if (res.code == 500) {
													that.toast("登陆失败,请联系管理员!")
												} else {
													let userInfo = res.data
													if (!userInfo.user.authType || userInfo
														.user.authName == "") {
														userInfo.user.authName = userInfo.user
															.nickName
													}
													// 重置token过期时间
													userInfo.token.expires_in = userInfo.token
														.expires_in * 1000 + Date.now()
													// refresh_token 过期时间固定3天
													userInfo.token.expires_refresh =
														259200000 + Date.now()
													uni.removeStorageSync("userInfo")
													uni.setStorageSync("userInfo", userInfo);
													uni.showToast({
														icon: "success",
														title: "重新授权成功!",
														duration: 1000
													})
												}
											});
										}
									})
								}
							}
						})
					}
				}
			},



版权声明:本文为weixin_46325225原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。