人人商城小程序 getUserInfo换成getUserProfile(方法二)

调整后4月13日之后发布的小程序wx.getUserInfo的接口都将失效,因此导致小程序的登录会出现灰色头像和微信用户

后台也是获取不到数据导致界面一度尴尬。

先用getUserProfile获取  userInfo用户对象信息,在用wx.getUserInfo 获取encryptedData和iv 至此后面的登录流程不变只是提交数据的时候多提交一个userInfo信息到后台

小程序支持库最少升到2.10.4

相关文件

小程序路径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);
    }

 

对人人整个流程不是特别熟悉本着能少改动代码尽量少改动,缺什么补什么吧,这个我朋友上线测试之前没有获取到用户个人数据的账号,在重新登录后头像昵称数据会更新过来。

 

版权所有:《太阳花工作室》 => 《人人商城小程序 getUserInfo换成getUserProfile(方法二)
本文地址:http://bg.artuion.com/代码/372.html
除非注明,文章均为 《太阳花工作室》 原创,欢迎转载!转载请注明本文地址,谢谢。