授权手机号phone.jsphone.jsPage({ /** * 页面的初始数据 */ data: { }, //授权手机号 getPhoneNumber(e){ var iv = e.detail.iv var encry..._小程序授权手机号代码">
当前位置:   article > 正文

微信小程序授权手机号_小程序授权手机号代码

小程序授权手机号代码

phone.wxml

<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">授权手机号</button>

phone.js

  1. phone.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. },
  8. //授权手机号
  9. getPhoneNumber(e){
  10. var iv = e.detail.iv
  11. var encryptedData = e.detail.encryptedData
  12. var session_key = wx.getStorageSync('session_key')
  13. var openid = wx.getStorageSync('openid')
  14. var that =this
  15. wx.request({
  16. url: 'http://www.lav8.week3.com/api/phone',
  17. data: {
  18. iv:iv,
  19. encryptedData:encryptedData,
  20. session_key:session_key,
  21. openid:openid
  22. },
  23. method:"POST",
  24. header: {
  25. token:wx.getStorageSync('token')
  26. },
  27. success (res) {
  28. console.log(res)
  29. if(res.statusCode==200){
  30. wx.showToast({
  31. title: '手机号授权成功',
  32. })
  33. }
  34. }
  35. })
  36. },

php:

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Login;
  4. use App\Service\phone\WXBizDataCrypt;
  5. use Illuminate\Http\Request;
  6. class PhoneController extends Controller
  7. {
  8. /**
  9. * 授权手机号
  10. */
  11. public function getPhone(request $request)
  12. {
  13. $appid = config('openid.openid');
  14. $sessionKey = $request->post('session_key');
  15. $iv = $request->post('iv');
  16. $encryptedData = $request->post('encryptedData');
  17. $openid = $request->post('openid');
  18. $pc = new WXBizDataCrypt($appid, $sessionKey);
  19. $errCode = $pc->decryptData($encryptedData, $iv, $data );
  20. if ($errCode == 0) {
  21. print($data . "\n");
  22. } else {
  23. print($errCode . "\n");
  24. }
  25. $newData = json_decode($data,true);
  26. if ($errCode==0){
  27. $res = Login::where('openid',$openid)->update(['phone'=>$newData['phoneNumber']]);
  28. if ($res!==false){
  29. return ['code'=>200,'msg'=>'手机号成功','data'=>$newData['phoneNumber']];
  30. }
  31. }
  32. }
  33. }

首先封装一个服务层 

引入三个php文件

  1. demo.php
  2. <?php
  3. namespace App\Service\phone;
  4. $appid = 'wx4f4bc4dec97d474b';
  5. $sessionKey = 'tiihtNczf5v6AKRyjwEUhQ==';
  6. $encryptedData="CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZM
  7. QmRzooG2xrDcvSnxIMXFufNstNGTyaGS
  8. 9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+
  9. 3hVbJSRgv+4lGOETKUQz6OYStslQ142d
  10. NCuabNPGBzlooOmB231qMM85d2/fV6Ch
  11. evvXvQP8Hkue1poOFtnEtpyxVLW1zAo6
  12. /1Xx1COxFvrc2d7UL/lmHInNlxuacJXw
  13. u0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn
  14. /Hz7saL8xz+W//FRAUid1OksQaQx4CMs
  15. 8LOddcQhULW4ucetDf96JcR3g0gfRK4P
  16. C7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB
  17. 6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns
  18. /8wR2SiRS7MNACwTyrGvt9ts8p12PKFd
  19. lqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYV
  20. oKlaRv85IfVunYzO0IKXsyl7JCUjCpoG
  21. 20f0a04COwfneQAGGwd5oa+T8yO5hzuy
  22. Db/XcxxmK01EpqOyuxINew==";
  23. $iv = 'r7BXXKkLb8qrSNn05n0qiA==';
  24. $pc = new WXBizDataCrypt($appid, $sessionKey);
  25. $errCode = $pc->decryptData($encryptedData, $iv, $data );
  26. if ($errCode == 0) {
  27. print($data . "\n");
  28. } else {
  29. print($errCode . "\n");
  30. }
  1. ErrorCode.php
  2. <?php
  3. namespace App\Service\phone;
  4. /**
  5. * error code 说明.
  6. * <ul>
  7. * <li>-41001: encodingAesKey 非法</li>
  8. * <li>-41003: aes 解密失败</li>
  9. * <li>-41004: 解密后得到的buffer非法</li>
  10. * <li>-41005: base64加密失败</li>
  11. * <li>-41016: base64解密失败</li>
  12. * </ul>
  13. */
  14. class ErrorCode
  15. {
  16. public static $OK = 0;
  17. public static $IllegalAesKey = -41001;
  18. public static $IllegalIv = -41002;
  19. public static $IllegalBuffer = -41003;
  20. public static $DecodeBase64Error = -41004;
  21. }
  22. ?>
  1. WXBizDataCrypt.php
  2. <?php
  3. /**
  4. * 对微信小程序用户加密数据的解密示例代码.
  5. *
  6. * @copyright Copyright (c) 1998-2014 Tencent Inc.
  7. */
  8. namespace App\Service\phone;
  9. class WXBizDataCrypt
  10. {
  11. private $appid;
  12. private $sessionKey;
  13. /**
  14. * 构造函数
  15. * @param $sessionKey string 用户在小程序登录后获取的会话密钥
  16. * @param $appid string 小程序的appid
  17. */
  18. public function __construct( $appid, $sessionKey)
  19. {
  20. $this->sessionKey = $sessionKey;
  21. $this->appid = $appid;
  22. }
  23. /**
  24. * 检验数据的真实性,并且获取解密后的明文.
  25. * @param $encryptedData string 加密的用户数据
  26. * @param $iv string 与用户数据一同返回的初始向量
  27. * @param $data string 解密后的原文
  28. *
  29. * @return int 成功0,失败返回对应的错误码
  30. */
  31. public function decryptData( $encryptedData, $iv, &$data )
  32. {
  33. if (strlen($this->sessionKey) != 24) {
  34. return ErrorCode::$IllegalAesKey;
  35. }
  36. $aesKey=base64_decode($this->sessionKey);
  37. if (strlen($iv) != 24) {
  38. return ErrorCode::$IllegalIv;
  39. }
  40. $aesIV=base64_decode($iv);
  41. $aesCipher=base64_decode($encryptedData);
  42. $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
  43. $dataObj=json_decode( $result );
  44. if( $dataObj == NULL )
  45. {
  46. return ErrorCode::$IllegalBuffer;
  47. }
  48. if( $dataObj->watermark->appid != $this->appid )
  49. {
  50. return ErrorCode::$IllegalBuffer;
  51. }
  52. $data = $result;
  53. return ErrorCode::$OK;
  54. }
  55. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/525052
推荐阅读
相关标签
  

闽ICP备14008679号