NewFuture / miniprogram-network

Redefine the Network API of Wechat MiniProgram (小程序网络库)
https://miniprogram-network.newfuture.cc
Apache License 2.0
113 stars 11 forks source link

token支持 #50

Closed acegank closed 2 years ago

acegank commented 3 years ago

支持开启token 无感知刷新之类的

NewFuture commented 3 years ago

自动刷新token? 这个通过transformSend 拦截器函数,可以很容易的实现

import { REQUEST, transformRequestSendDefault } from "miniprogram-network";
REQUEST.Defaults.transformSend = async (options) => {
    const token = await getToken(); //获取和维护token的方法
    options.headers = { Authorization: `Bearer ${token}` };
    return transformRequestSendDefault(options); // 默认的参数转换
};

所有request 执行发送之前调用上面的内容