微信小程序通过getPhoneNumber后台PHP解密获取用户手机号码

╄→гoц情女王★ 提交于 2021-01-31 05:32:57


之前做的版本用户这块是以获取用户openid为凭证,最近改版重新整理了一下,新增注册登录以手机号码为主,

两种(正常注册手机号码-密码+一键获取当前用户手机号码)

getPhoneNumber这个组件要通过button来实现。将button中的open-type=“getPhoneNumber”,并且绑定bindgetphonenumber事件获取回调。

在使用这个组件之前必须先调用 login 接口

然后传递code,iv,encryptedData参数到后台,后台解密

示例

 

 

<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"
 hover-class="none">一键自动注册</button>

  

getPhoneNumber: function (e) {
      console.log(e.detail.errMsg)
      if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: '未授权',
          success: function (res) { }
        })
      } else {
        wx.login({
          success: function (res) {
            var code = res.code;
            if (res.code) {
              //发起网络请求  
              console.log(res.code)
            } else {
              console.log('获取用户登录态失败!' + res.errMsg)
            }
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: '同意授权',
              success: function (res) {
                var that = this;
                console.log(123)
                wx.request({
                  url: '/wxapplet/wx/wechat/phone',
                  data: {
                    code: code,
                    iv: e.detail.iv,
                    encryptedData: e.detail.encryptedData
                  },
                  method: 'GET',
                  header: {
                    'content-type': 'application/json'
                  },
                  success: function (res) {
                    wx.setStorageSync('user', res.data.data);
                    if(res.data.code == "200"){
                      console.log(res.data.data)
                      wx.showToast({
                        title: '一键绑定成功',
                        icon: 'success',
                        duration: 2000,
                        success: function(){
                          wx.switchTab({ url: '../user-center/index' });
                        }
                      })
                    }else{
                      wx.showModal({
                        title: '提示',
                        content: '一键绑定失败,请重新尝试',
                        success: function (res) {
                          if (res.confirm) {
                            console.log('用户点击确定')
                          } else if (res.cancel) {
                            console.log('用户点击取消')
                          }
                        }
                      })
                    }                    
                  },
                });
              }
            })
          }
        });        
      }
}

  

  • 后台是php 框架是laravel
<?php
namespace App\Http\Controllers\WxApplet;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repository\WxUserRepository;

include_once app_path('/Http/Controllers/WxApplet/Php/wxBizDataCrypt.php');
class WechatController extends Controller
{
  /**
   * constructer.
   *
   * @param WxUserRepository  $wxUser
   */
   public function __construct
   (
        WxUserRepository $WxUserRepository
   )
   {
        $this->WxUserRepository  = $WxUserRepository;
   }
    /**
     * 获取用户手机号码
     *
     * @return response
     */
    public function getWechatUserPhone(Request $request)
    {
        $code   =   $request->get('code');
        $encryptedData   =   $request->get('encryptedData');
        $iv   =  $request->get('iv');
        //小程序开发账户
        $appid  =  "*******" ;
        $secret =   "*******";
        $URL = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        $apiData=file_get_contents($URL);
           if(!isset(json_decode($apiData)->errcode))
           {
              $sessionKey = json_decode($apiData)->session_key;
              $info = new \WXBizDataCrypt($appid, $sessionKey);
              $errCode = $info->decryptData($encryptedData, $iv, $data );
              if ($errCode == 0)
              {
                  $phone = json_decode($data)->phoneNumber;
                  $single_phone=$this->WxUserRepository->Single($phone);
                  if ($single_phone == null)
                  {
                     $wx_user = $this->WxUserRepository->Create($phone,$password);
                  }
                  return $this->Success(200003);
              }
              else {
                return $this->fail(420004);
             }
          }
    }
 }

  

 

 

 

原文链接:https://blog.csdn.net/qq_34827048/article/details/78878121

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!