建站资讯

网站建设根本知识-小程序之无感知刷新token

作者:admin 发布时间:2021-04-16
--------

网站建设根本知识

-------

手机微信小程序受权登陆以后,会成转化成token回到给顾客端,顾客端获得token以后,做为后续一系列插口的启用凭据,随后,服务器回到给顾客端token会有一个合理期,token一但到期,就会致使顾客端后续的插口启用不成功 ... ...


一.插口启用凭据token

手机微信小程序受权登陆以后,会成转化成token回到给顾客端,顾客端获得token以后,做为后续一系列插口的启用凭据,随后,服务器回到给顾客端token会有一个合理期,token一但到期,就会致使顾客端后续的插口启用不成功,因而,在启用插口之前,大家需要确保token是合理的,这样才可以确保顾客端作用不会由于token到期而致使出现异常


二.手机微信受权登陆、token合理合法性及合理性校检

token合理合法性及合理性校检

const Router = require('koa-router')
const router = new Router()
const { verifyToken } = require('../../../../utils/utils')
const User = require('../../../../model/user')
router.post('/api/v1/member/verify', async (ctx, next) = {
 const token = ctx.request.header.token
 //认证token是不是到期
 const decoded = verifyToken(token)
 const res = await User.findByOpenID(decoded.openid)
 if(!res) {
 //客户不存在
 throw new global.errors.UserNotFoundException()
 ctx.body = {
 code: 0,
 msg: "token合理"
module.exports = router
const jwt = require('jsonwebtoken');
const verifyToken = function(token, secret) {
 let decoded
 try {
 decoded = jwt.verify(token, 'com_aykj_gale');
 } catch(error) {
 let err = new global.errors.ForbiddenException()
 if(error.name == 'TokenExpiredError') {
 err.msg = 'token已到期'
 err.errorCode = 10006
 } else {
 err.msg = 'token不符合法'
 err.errorCode = 10005
 throw err
 return decoded
/api/v1/member/code2token: 登陆获得token
/api/v1/member/register: 申请注册
/api/v1/member/verify: token合理合法性及合理性校检
/api/v1/member/member_index: 获得客户信息内容,后续会根据此插口,完成token的无认知更新
三.手机微信小程序中恳求插口

// /project_directory/config.js
const serverUrl = 'api/v1/member/code2token`,
 register: `${serverUrl}/api/v1/member/register`,
 verify: `${serverUrl}/api/v1/member/verify`,
 memberIndex: `${serverUrl}/api/v1/member/member_index`
module.exports = config
// /project_directory/utils/http-service.js
class HttpService {
 constructor(){}
 get(url, params, headers) {
 if(!headers) {
 headers = {}
 return this._request(url, 'GET', params, headers)
 postJson(url, params, headers) {
 if(!headers) {
 headers = {}
 headers['content-type'] = 'application/json'
 return this._request(url, 'POST', params, headers)
 postForm(url, params, headers) {
 if(!headers) {
 headers = {}
 headers['content-type'] = 'application/x-www-form-urlencoded'
 return this._request(url, 'POST', params, headers)
 _request(url, method, params, headers) {
 return new Promise((resolve, reject) = {
 wx.request({
 url: url,
 method: method,
 data: params,
 header: headers,
 complete: function(res) {
 var errMsg = res.errMsg //request:ok;request:fail
 if(errMsg != "request:ok") {
 var reason = {
 code: -1,
 msg: "恳求请求超时"
 reject(reason)
 } else {
 var resData = res.data
 var resDataCode = resData.code
 if(resDataCode != undefined) {
 if(resDataCode == 0) {
 resolve(resData)
 } else {
 reject(resData)
 } else {
 var reason = {
 code: -1,
 msg: resData
 reject(reason)
module.exports = HttpService
// /project_directory/http/request-center.js
import HttpService from "../utils/http-service"
import config from "../config"
const tokenKey = "token"
class RequestCenter {
 constructor() {
 this.httpService = new HttpService()
 register(params, headers) {
 return this.httpService.postJson(config.register, params, headers)
 code2token(params, headers) {
 return this.httpService.postJson(config.code2token, params, headers)
 verifyToken(params, headers) {
 return this.httpService.postJson(config.verify, params, headers)
 memberIndex(params, headers) {
 return this.httpService.postJson(config.memberIndex, params, headers)
 wxLogin() {
 return new Promise((resolve, reject) = {
 wx.login({
 complete: function(res) {
 if(res res.code) {
 resolve(res.code)
 } else {
 reject({
 code: -1,
 msg: "wx.login 不成功"
const requestCenter = new RequestCenter()
module.exports = requestCenter
// /project_directory/pages/index/index.js
const app = getApp()
Page({
 data: {
 onLoad: async function(res) {
 var token = wx.getStorageSync('token')
 if(!token) {
 wx.reLaunch({
 url: '/pages/auth/auth',
 } else {
 var headers = {}
 headers["token"] = wx.getStorageSync('token')
 await getApp().global.requestCenter.memberIndex({}, headers)
四.无认知更新token

// /project_directory/http/request-center.js
import HttpService from "../utils/http-service"
import config from "../config"
const tokenKey = "token"
class RequestCenter {
 memberIndex(params, headers) {
 return this._verify(params, headers, () = {
 return this.httpService.postJson(config.memberIndex, params, headers)
 wxLogin() {
 return new Promise((resolve, reject) = {
 wx.login({
 complete: function(res) {
 if(res res.code) {
 resolve(res.code)
 } else {
 reject({
 code: -1,
 msg: "wx.login 不成功"
 _verify(params, headers, fun) {
 if(headers headers[tokenKey]) {
 return new Promise((resolve, reject) = {
 //认证token是不是到期
 this.verifyToken(params, headers).then((res) = {
 //token未到期,再次上一次恳求
 fun().then((res) = {
 resolve(res)
 }, (reason) = {
 reject(reason)
 }, (reason) = {
 var code = reason.code
 if(code == 10006) {
 //token到期,更新token,再次上一次恳求
 this.wxLogin().then((res) = {
 this.code2token({code:res}, {}).then((res) = {
 var token = res.token
 wx.setStorageSync('token', token)
 headers[tokenKey] = token
 fun().then((res) = {
 resolve(res)
 }, (reason) = {
 reject(reason)
 }, (reason) = {
 reject(reason)
 }, (reason) = {
 reject(reason)
 } else {
 reject(reason)
 } else {
 return fun()
const requestCenter = new RequestCenter()
module.exports = requestCenter
---------

网站建设根本知识

------------

收缩