PHP小程序登录获取用户openid以及用户手机号

  1. (客户端)调用接口wx.login()获取登录凭证(code)。
	wx.login({
	  success (res) {
	    if (res.code) {
	      //发起网络请求
	      wx.request({
	        url: 'https://test.com/onLogin',
	        data: {
	          code: res.code
	        }
	      })
	    } else {
	      console.log('登录失败!' + res.errMsg)
	    }
	  }
	})
  1. 因为需要用户主动触发才能发起获取手机号接口,所以该功能不由 API 来调用,需用 button 组件的点击来触发。
	<button open-type="getPhoneNumber" 	bindgetphonenumber="getPhoneNumber"></button>

		Page({
		  getPhoneNumber (e) {
		    console.log(e.detail.errMsg)
		    console.log(e.detail.iv)
		    console.log(e.detail.encryptedData)
		  }
		})
  1. (客户端)将获取的code,iv,encryptedData参数发送到服务端.
  2. (服务端 php)服务器发送code到微信服务器获取openid(用户唯一标识)和session_key(会话密钥),最后将encryptedData、iv、session_key通过解密获取到用户手机号
    解密需要 引用的文件官网可以下载
    https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html#%E5%8A%A0%E5%AF%86%E6%95%B0%E6%8D%AE%E8%A7%A3%E5%AF%86%E7%AE%97%E6%B3%95
	    /**
     * 用户授权登录
     * code     jscode
     */
    public function login()
    {
        $appid = config('wechat.app_id');//	小程序appid
        $secret = config('wechat.secret');   //  小程序appsecret
        //前端获取的授权登录相关参数信息
        $code = request()->param('code');
        $encryptedData = request()->param('encryptedData');
        $iv = request()->param('iv');

        /**
         *  请求微信官方接口获取用户手机号
         */
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        $user_obj = curlHttp($url);
        $user = $user_obj;
        $pc = new WXBizDataCrypt($appid, $user['session_key']);
        $errCode = $pc->decryptData($encryptedData, $iv, $data);
            if($errCode == 0){
            //根据手机号做判断
            $user_info = Db::name('customer')->where('phone',$data['purePhoneNumber'])->find();
            if(!empty($user_info)){
            	//判断用户是否有openid
                if(empty($user_info['openid'])){
                    Db::name('customer')->where('id',$user_info['id'])->update(['openid'=>$user['openid'],'update_time'=>time()]);
                }
                $uid = $user_info['id'];
            }else{
                $user_data['phone'] = $data['purePhoneNumber'];
                $user_data['openid'] = $user['openid'];
                $user_data['create_time'] = time();
                $user_data['user_source'] = '小程序';
                $uid = Db::name('customer')->insertGetId($user_data);

               //插入用户登录记录
                $record_data['user_id'] = $uid;
                $record_data['type_name'] = '录入时间';
                $record_data['feedback'] = '小程序自主登录';
                $record_data['content'] = date('Y-m-d H:i:s').','.'无'.','.$record_data['feedback'];
                \app\model\RecordModel::create($record_data);
            }
            if($uid){
                 return_json(1,'登录成功',['phone'=> $data['purePhoneNumber'],'user_token'=>user_encrypt($uid)]);
            }else{
                return_json(0,'登录失败');
            }

        }else{
            return_json(2,'登录失败');
        }
    }

你可能感兴趣的:(PHP,小程序登录,获取用户手机号)