TypechoJoeTheme

Dcr163的博客

统计

小程序更新getUserProfile授权用户信息,人人商城小程序更新授权登陆

2021-04-13
/
0 评论
/
1,863 阅读
/
正在检测是否收录...
04/13

小程序又更新更新Api了

没错,而且还是获取用户信息的Api,万恶的TX呀,程序员何必为难程序员:joy:,受伤群体100%
这里不多做介绍,更新的详情在这里 https://developers.weixin.qq.com/community/develop/doc/000cacfa20ce88df04cb468bc52801
获取用户信息的API: wx.getUserInfo
更新为新的Api: wx.getUserProfile
注意最低版本要求:基础库 2.10.4 开始支持,推荐2.16.0


因为项目中用到了人人商城的小程序,所以需要更新人人授权登陆的功能,直接进入主题:

首选修改前端

文件:/pages/message/auth/index.js 在底部新增一个方法

/**
     * 重新调用用户信息,2021年4月13日正式启用 geUserProfile 
     */
    getUserProfile:function(){
       let _this = this;
       wx.getUserProfile({
       lang:'zh_CN',
       desc:'为了体验更好的服务',
       success:function(res){
       console.log('profile:',res);
       var s = e.getCache("routeData"), n = s.url, o = s.params, i = "";
       o && Object.keys(o).forEach(function(e) {
       i += e + "=" + o[e] + "&";
       });
       var r = "/" + n + "?" + (o = i.substring(0, i.length - 1));
       t.loading('登陆中...');
       var core = t;
       wx.login({
       success: function(s) {
       console.log('login:',s);
       t.post("wxapp/loginDcr163", {
       code: s.code,
       user_info:res.userInfo
       }, function(s) {
       if( s.error ) return t.alert("获取用户登录态失败:" + s.message);
       console.log('ok loginn');
       console.log(s), 1 == s.isblack && wx.showModal({
       title: "无法访问",
       content: "您在商城的黑名单中,无权访问!",
       success: function(t) {
       t.confirm && e.close(), t.cancel && e.close();
       }
       }), res.userInfo.openid = s.openid, res.userInfo.id = s.id, res.userInfo.uniacid = s.uniacid, 
       e.setCache("userinfo", res.userInfo), e.setCache("userinfo_openid", res.userInfo.openid), 
       e.setCache("userinfo_id", s.id),e.getSet();
       console.log('ss:',res);
       //增加个定时器,防止已登陆但是还是未登陆成功的状态
       setTimeout(function(){
       core.hideLoading();
       wx.reLaunch({
       url: r === '/undefined?' ? '/pages/index/index' :r
       });
       },800)
       });
       },
       fail: function() {
       t.alert("获取用户信息失败!");
       }
       });
       },
       fail:function(res){
       t.alert("请先授权您的信息");
       }
       });
       return;
        
    }

修改wxml页面

打开文件:/pages/message/auth/index.wxml
把下面的代码

<button bindgetuserinfo="bindGetUserInfo" class="btn" openType="getUserInfo" wx:if="{{!close}}">授权微信公开信息</button>

替换成

<button class="btn" bindtap="getUserProfile"  wx:if="{{!close}}">授权微信公开信息</button>

开始修改后台文件

打开文件:/addons/vcshop/plugin/app/core/mobile/wxapp.php
在底部新增一个新的登陆函数

    /**
     * dcr163
     * 20210413之后使用这个接口登陆,
     * @return mixed|string
     */
    public function loginDcr163()
    {
        global $_GPC;
        global $_W;
        $code = trim($_GPC['code']);

        if (empty($code)) {
            return app_error(AppError::$ParamsError);
        }

        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $this->appid . '&secret=' . $this->appsecret . '&js_code=' . $code . '&grant_type=authorization_code';
        load()->func('communication');
        $resp = ihttp_request($url);

        if (is_error($resp)) {
            return app_error(AppError::$SystemError, $resp['message']);
        }

        $arr = @json_decode($resp['content'], true);

        $arr['isclose'] = $_W['shopset']['app']['isclose'];

        if (!empty($_W['shopset']['app']['isclose'])) {
            $arr['closetext'] = $_W['shopset']['app']['closetext'];
        }

        if (!is_array($arr) || !isset($arr['openid'])) {
            return app_error(AppError::$WxAppLoginError);
        }

        $this->refine($arr['openid']);
        $data = $_GPC['user_info'];
        $data['openid'] =  $arr['openid'];
        $data['openid_wa'] = 'sns_wa_'. $arr['openid'];
        $data['unionid'] = isset($arr['unionid'])?$arr['unionid']:'';
        $member = m('member')->getMember($data['openid_wa']);
        if (empty($member)) {
            $member = array('uniacid' => $_W['uniacid'], 'uid' => 0, 'openid' => $data['openid_wa'], '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,'unionid'=>$data['unionid']);
            pdo_insert('vcshop_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('vcshop_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']);
        }

        //dcr163 更新用户的session_key表,暂时用来做获取手机号使用 s
        $data['openid'] = $data['openid_wa']; //设置为系统的openid
        $openid =  $data['openid'];
        if( $arr['session_key'] ){
            $sql = 'SELECT id FROM '.tablename('vcshop_member_session_key').' WHERE openid=:openid AND uniacid=:uniacid';
            $isExist = pdo_fetchcolumn($sql,array(':openid'=>$openid,':uniacid'=>$_W['uniacid']));
            if( $isExist ){
                $upData = array('session_key'=>$arr['session_key'],'update_time'=>time());
                pdo_update('vcshop_member_session_key',$upData,array('id'=>$isExist,'uniacid'=>$_W['uniacid']));
            } else {
                $saveData = array(
                    'openid' => $openid,
                    'uniacid'    => $_W['uniacid'],
                    'session_key'    => $arr['session_key'],
                    'addtime'        => time()
                );
                pdo_insert('vcshop_member_session_key',$saveData);
            }
        }
        //dcr163 更新用户的session_key表,暂时用来做获取手机号使用 e
        $data['openid'] = $arr['openid'];//还原前端使用
        return app_json($data, $data['openId']);

    }

到这一步基本就更新了最新的wx.getUserProfile 授权登陆信息了!

朗读
赞(1)
版权属于:

Dcr163的博客

本文链接:

https://www.dcr163.cn/448.html(转载时请注明本文出处及文章链接)

评论 (0)

人生倒计时

今日已经过去小时
这周已经过去
本月已经过去
今年已经过去个月

最新回复

  1. https://Sites.google.com/view/vavada-online-casino
    2025-04-12
  2. https://x.com/jeetbuzzcom
    2025-04-07
  3. prozone.cc alternative
    2025-03-19
  4. urgent delivery
    2025-03-16
  5. faster indexing
    2025-03-05

标签云