• WAP手机版 保存到桌面加入收藏设为首页
故障排除

人人商城小程序 用户登录授权接口 wx.getUserProfile后 ,修复登录

时间:2022-07-07 10:50:45   作者:612459.com   阅读:682   评论:0
内容摘要:小程序路径pages/auth/index.wxml立即登录按钮需要修改{{shop...
小程序路径     pages/auth/index.wxml    立即登录按钮需要修改

 

<image class='logo' src='{{shop_logo}}'></image>
<view class='title'><text class='title-underline'>{{shop_name}}</text></view>
<view class='message'><text>您尚未登录需要获取您的授权后进入商城</text></view><button bindtap='close'
  class='cancelBtn'>暂不登录</button>
  <button bindtap='bindGetUserInfobu' class='authBtn' >立即登录</button>
 

小程序路径pages/auth/index.js 多加了一个方法bindGetUserInfobu() 

然后获取到userInfo用户对象信息在用wx.getUserInfo 

把对象结构拼装一下,这样基本没有太多改动

提交时多提交一个userInfo用户对象信息

// 登录修正
  bindGetUserInfobu(){
    //getUserProfile获取用户信息
   wx.getUserProfile({
    lang:'zh_CN',
    desc: '用于完善会员资料',
    success: (res) =>{
      console.log(res)
      //getUserInfo获取认证oid解密
      wx.getUserInfo({
        lang: 'zh_CN',
      }).then(r=>{
        console.log(r)
       r.userInfo=res.userInfo
        let n={
          detail:r
         }
         this.bindGetUserInfo(n)
      })
     
    }
   })
  },
//登录修正
  bindGetUserInfo: function (o) {
    console.log(o)
    wx.showLoading({
      title: "加载中"
    }) 
    wx.login({
      success: function (t) {
        console.log(t)
        n.post("wxapp/login", {
          code: t.code
        }, function (tt) {
          console.log(tt)
          tt.error ? n.alert("获取用户登录态失败:" + tt.message) : n.get("wxapp/auth", {
            data: o.detail.encryptedData,
            iv: o.detail.iv,
            sessionKey: tt.session_key,
            userInfo:o.detail.userInfo// 登录修正
          }, function (n) {
            console.log(n)
            1 == n.isblack && wx.showModal({
              title: "无法访问",
              content: "您在商城的黑名单中,无权访问!",
              success: function (n) {
                n.confirm && e.close(), n.cancel && e.close()
              }
            }), o.detail.userInfo.openid = n.openId, o.detail.userInfo.id = n.id, o.detail.userInfo.uniacid = n.uniacid, e.setCache("userinfo", o.detail.userInfo), e.setCache("userinfo_openid", o.detail.userInfo.openid), e.setCache("userinfo_id", n.id), e.getSet(), wx.navigateBack({
              changed: !0
            })
          })
        })
      },
      fail: function () {
        n.alert("获取用户信息失败!")
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  },
后台代码 ewei_shopv2/plugin/app/core/mobile/wxapp.php

只需要修改 auth()  加4行代码 见注释 登录修正

/**
     * 微信小程序登录
     */
    public function auth()
    {
        global $_GPC;
        global $_W;
        $encryptedData = trim($_GPC["data"]);
        $iv = trim($_GPC["iv"]);
        $sessionKey = trim($_GPC["sessionKey"]);
       //登录修正
        $userInfo=$_GPC["userInfo"];
      //登录修正
        if (empty($encryptedData) || empty($iv)) {
            return app_error(AppError::$ParamsError);
        }
        $pc = new WXBizDataCrypt($this->appid, $sessionKey);
        $errCode = $pc->decryptData($encryptedData, $iv, $data);
        
        if ($errCode == 0) {
            $data = json_decode($data, true);
            $this->refine($data["openId"]);
            $member = m("member")->getMember("sns_wa_" . $data["openId"]);
            
            //登录修正
            if(is_array($userInfo)){
                $data=array_merge($data, $userInfo);
            }
            //登录修正
            if (empty($member)) {
                $member = array("uniacid" => $_W["uniacid"], "uid" => 0, "openid" => "sns_wa_" . $data["openId"], "nickname"=>!empty($data["nickName"]) ? $data["nickName"] : "", "avatar" => !empty($data["avatarUrl"]) ? $data["avatarUrl"] : "", "gender" => !empty($data["gender"]) ? $data["gender"] : "-1", "openid_wa" => $data["openId"], "comefrom" => "sns_wa", "createtime" => time(), "status" => 0);
                pdo_insert("ewei_shop_member", $member);
                $id = pdo_insertid();
                $data["id"] = $id;
                $data["uniacid"] = $_W["uniacid"];
                if (method_exists(m("member"), "memberRadisCountDelete")) {
                    m("member")->memberRadisCountDelete();
                }
            } else {
                $updateData = array("nickname" => !empty($data["nickName"]) ? $data["nickName"] : "", "avatar" => !empty($data["avatarUrl"]) ? $data["avatarUrl"] : "", "gender" => !empty($data["gender"]) ? $data["gender"] : "-1");
                pdo_update("ewei_shop_member", $updateData, array("id" => $member["id"], "uniacid" => $member["uniacid"]));
                $data["id"] = $member["id"];
                $data["uniacid"] = $member["uniacid"];
                $data["isblack"] = $member["isblack"];
            }
            if (p("commission")) {
                p("commission")->checkAgent($member["openid"]);
            }
            return app_json($data, $data["openId"]);
        }
        return app_error(AppError::$WxAppError, "登录错误, 错误代码: " . $errCode);
    }
相关文档:https://developers.weixin.qq.com/community/develop/doc/000cacfa20ce88df04cb468bc52801?highLine=login
————————————————

   版权声明,所有转载都有注明出处,本站不负责承担任何法律责往。若有侵权,请联系我。我会及时删除。

电脑维护,系统安装,软 、硬件维修,电脑配件,零售业务,网站建设,路由器安装设置 服务器维护,电脑、网络维护,智能手机刷机,安装WIFI 调试!郴州网站建设 小程序搭建 郴州电脑维修

        咨询电话:18175576644  点击这里给我发消 息
   扫描二维码。关注公众号,小程序
       享受星级服务   

手机点击二维码关注
      


手机点击打开小程序
      


相关评论
免责申明:本站部分资料来源互联网,如果侵犯了您的版权,请作者速来电或QQ与本站联系,我们将第一时间给予以改正或删除。

Copyright © 2020 哎丫丫电脑 All Rights Reserved 
 工信部备案:湘ICP备14010293号-1