程序中 request 封装的详细流程及相关探讨

GS 250 2024-06-26

背景

之前小程序代码混乱,所以新项目一开始就准备弄个微信小程序的 request 的封装

流程

先来说说整个流程:

1.appjs 里面已进入就去获取用户信息,如果没有登录则默认登录,这里不做错误处理

2.用户必须同意授权才能进行操作,如果不同意授权则会一直跳转到授权页面

3.在授权页面点击授权登录后,调用登录接口,成功后返回调起授权的页面,

app.js

onLaunch 里面获取用户信息

图片.png

httpUtils.js

request 的封装

图片.png

login

const login = function () {
    try {
        wx.removeStorageSync(USER_ID_KEY)
        wx.removeStorageSync(TOKEN_KEY)
    } catch (e) {}
    return new Promise((resolve, reject) => {
        wx.login({
            success: res => {
                let code = res.code;
                // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
                wx.getUserInfo({
                    withCredentials: true,
                    success: res => {
                        let userInfo = res.userInfo;
                        let name = userInfo.nickName;
                        let avatar = userInfo.avatarUrl;
                        let sex = userInfo.gender;
                        let data = {
                            code: code,
                            encryptedData: res.encryptedData,
                            iv: res.iv,
                            name: name,
                            avatar: avatar,
                            sex: sex,
                            from: FROM,
                        };
                        request("/api/user/login/byWeChatApplet", "POST", data).then( (res)=>{
                            if (!res.code) {
                                try {
                                    wx.setStorageSync(USER_ID_KEY, res.user_id);
                                    wx.setStorageSync(TOKEN_KEY, res.token)
                                } catch (e) {
                                    reject(JSON.stringify(e));
                                }
                            }
                            resolve(res)
                        }).catch( (errMsg)=>{
                            reject(errMsg)
                        });
                    },
                    fail: function (res) {
                        console.log(res);

                        if (res.errMsg && res.errMsg.startsWith("getUserInfo:fail") && res.errMsg.search("unauthorized") != -1) {
                            // 跳转授权页面
                            wx.navigateTo({
                                url: '/pages/auth/auth'
                            })
                            return;
                        }
                        wx.getSetting({
                            success: (res) => {
                                if (!res.authSetting["scope.userInfo"]) {
                                    // 跳转授权页面
                                    wx.navigateTo({
                                        url: '/pages/auth/auth'
                                    })
                                }
                            }
                        });
                    }
                })
            }
        })
    });
};

auth.js

授权页面 js

图片.png

项目地址

https://github.com/lmxdawn/wx...

一个 vue + thinkphp5.1 搭建的后台管理:https://github.com/lmxdawn/vu...

演示:<br>

图片.png

图片.png

以上就是小程序中 request 封装的详细流程及相关探讨的详细内容!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:探究利用小程序生成海报的方法及插件推荐
下一篇:百度小程序富文本解析工具bdParse的使用方法介绍
相关文章

 发表评论

暂时没有评论,来抢沙发吧~