unniapp微信小程序的登录流程?

uniApp微信小程序的登录流程

  • 先使用uni.getSetting来判断用户是否曾经授权过,获取到res.authSetting[‘scope.userInfo’],来判断是否有进行过授权操作,授权过为true,未授权为false
//获取用户授权结果(是否允许授权)
    async checkWxAuth({ state, dispatch }): Promise<{ auth: boolean }> {
        return await new Promise((resolve, reject) => {
            uni.getSetting({
                success(res) {
                    if (res.authSetting["scope.userInfo"]) {
                        console.log("--------scope-----", res)
                        //授权过
                        resolve({ auth: true });
                    } else {
                    //未授权
                        resolve({ auth: false });
                    }
                },
            });
        });
    },
  • 检查登录状态,查看是否登录超时或是否为未登录
//检查授权状态
    async checkLogin({ state, dispatch, commit }): Promise<boolean> {
		//dispatch异步操作,使用action的方法
        let wxAuth = await dispatch('checkWxAuth')
        
        const token: string | null = uni.getStorageSync("token");
        const expire_time: number = uni.getStorageSync("expire_time");
        const hasUserInfo: boolean = uni.getStorageSync("hasUserInfo");
		//上一个的授权判断是否为已授权
        if (!wxAuth.auth) {
			// commit同步操作,使用mutations的方法
            commit('loginOut')
            return false;
        }
        //查看是否保存有token(是否已登录)
        if (!token) {
            commit('loginOut')
            return false;
        }
        const curTimeStamp: number = (new Date()).getTime();
        //查看登录是否已超时,expire_time是从后端获取的一个时间戳
        if ((curTimeStamp > expire_time)) {
            commit('loginOut')
            return false
        }
        if (!state.isLogin) {
            return false
        }
        if (!String(hasUserInfo)) {
            return false
        }
        return true
    },
  • 如果为未授权未登录状态,则调用事件getUserProfile来进行授权,会弹出一个授权接口,同意授权后使用uni.login获取用户的code值,来与后端发起请求换取CacheKey,再使用uni.login来获取用户的基本信息,向后端发起请求将所需内容保存到后端
//授权登录
 wx.getUserProfile({
      desc: " 授权登录",
      success: (profile) => {
        console.log("--------getUserProfile-----", profile);
        //登录
        uni.login({
          success: async (res) => {
            console.log("--------loginAction/uni.login-----", res);
            console.log(res);
            let keyObject = await this.getCacheKey(res.code);
            if (keyObject) {
              let { cacheKey } = keyObject;
              let openid = keyObject.wechatUser.routineOpenId;
              //获取用户的基本信息
              uni.getUserInfo({
                success: async (res) => {
                  console.log("--------loginAction/uni.getUserInfo-----", res);
                  //将信息保存到后端
                  let loginRes = await this.wxLogin({
                  
                  });
                  console.log("--------loginRes-----", loginRes);
                  uni.hideLoading();
                  this.close();
	console.log('用户信息',this.userInfo)
                },
                fail: () => {
                  uni.hideLoading();
                  this.open();
                },
              });
            }
          },
        });
      },
      fail: () => {
        uni.hideLoading();

        this.open();
      },
    });
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值