微信小程序通過getPhoneNumber后台PHP解密獲取用戶手機號碼



之前做的版本用戶這塊是以獲取用戶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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM