Bladeren bron

feat(xcx):获取微信授权手机号

geek 4 jaren geleden
bovenliggende
commit
5de9fbd250

+ 26 - 0
application/common/service/wechat/decode/ErrorCode.php

@@ -0,0 +1,26 @@
+<?php
+/**
+ * Author: luzheng.liu
+ * Time: 2020/10/23 22:21
+ */
+
+namespace app\common\service\wechat\decode;
+
+/**
+ * error code 说明.
+ * <ul>
+
+ *    <li>-41001: encodingAesKey 非法</li>
+ *    <li>-41003: aes 解密失败</li>
+ *    <li>-41004: 解密后得到的buffer非法</li>
+ *    <li>-41005: base64加密失败</li>
+ *    <li>-41016: base64解密失败</li>
+ * </ul>
+ */
+class ErrorCode {
+    public static $OK = 0;
+    public static $IllegalAesKey = -41001;
+    public static $IllegalIv = -41002;
+    public static $IllegalBuffer = -41003;
+    public static $DecodeBase64Error = -41004;
+}

+ 61 - 0
application/common/service/wechat/decode/WXBizDataCrypt.php

@@ -0,0 +1,61 @@
+<?php
+/**
+ * Author: luzheng.liu
+ * Time: 2020/10/23 22:21
+ */
+
+namespace app\common\service\wechat\decode;
+
+
+class WXBizDataCrypt {
+
+    private $appid;
+    private $sessionKey;
+
+    /**
+     * 构造函数
+     * @param $sessionKey string 用户在小程序登录后获取的会话密钥
+     * @param $appid string 小程序的appid
+     */
+    public function __construct($appid, $sessionKey) {
+        $this->sessionKey = $sessionKey;
+        $this->appid = $appid;
+    }
+
+
+    /**
+     * 检验数据的真实性,并且获取解密后的明文.
+     * @param $encryptedData string 加密的用户数据
+     * @param $iv string 与用户数据一同返回的初始向量
+     * @param $data string 解密后的原文
+     *
+     * @return int 成功0,失败返回对应的错误码
+     */
+    public function decryptData($encryptedData, $iv, &$data) {
+        if (strlen($this->sessionKey) != 24) {
+            return ErrorCode::$IllegalAesKey;
+        }
+        $aesKey = base64_decode($this->sessionKey);
+
+
+        if (strlen($iv) != 24) {
+            return ErrorCode::$IllegalIv;
+        }
+        $aesIV = base64_decode($iv);
+
+        $aesCipher = base64_decode($encryptedData);
+
+        $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
+
+        $dataObj = json_decode($result);
+        if ($dataObj == NULL) {
+            return ErrorCode::$IllegalBuffer;
+        }
+        if ($dataObj->watermark->appid != $this->appid) {
+            return ErrorCode::$IllegalBuffer;
+        }
+        $data = $result;
+        return ErrorCode::$OK;
+    }
+
+}

+ 48 - 0
application/expand/controller/Xcx.php

@@ -1,6 +1,7 @@
 <?php
 namespace app\expand\controller;
 use app\common\service\HelperService;
+use app\common\service\wechat\decode\WXBizDataCrypt;
 use app\common\service\wechat\UnifiedOrder_pub;
 use think\Config;
 use think\Validate;
@@ -148,6 +149,53 @@ class Xcx extends BaseAuth
         ]);
     }
 
+    public function getMobileInfo() {
+
+        $params = $this->_params;
+
+        $rule = [
+            'code|js获取的code'=>'require',
+            'encryptedData'=>'require',
+            'iv'=>'require'
+        ];
+
+        $validate = new Validate($rule);
+        if(!$validate->check($params)){
+            HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>[]]);
+        }
+
+        if(empty($this->_Account['Xcx_secret'])){
+            HelperService::returnJson(['code'=>400,'msg'=>'Xcx getOpenInfo unauthorized access','data'=>$this->_Account]);
+        }
+
+        $secret = $this->_Account['Xcx_secret'];
+        $appId = empty($this->_SUBAPPID)?$this->_APPID:$this->_SUBAPPID;
+
+        $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$secret}&js_code={$params['code']}&grant_type=authorization_code";
+        $openInfoJson = file_get_contents($url);
+        $openInfo = @json_decode($openInfoJson,true);
+
+        if($openInfo===false || !empty($openInfo['errcode'])){
+            HelperService::returnJson([
+                'data'=>$openInfoJson,'msg'=>'fail','code'=>400
+            ]);
+        }
+
+        $pc = new WXBizDataCrypt($appId, $openInfo['session_key']);
+        $errCode = $pc->decryptData($params['encryptedData'], $params['iv'], $data);
+        if ($errCode == 0) {
+            $data =  json_decode($data,true);
+            HelperService::returnJson([
+                'data'=>  array_merge($openInfo,['appId'=>$appId]),'msg'=>'success','code'=>200
+            ]);
+        }
+
+        HelperService::returnJson([
+            'data'=>[],'msg'=>'获取手机号失败请重试','code'=>400
+        ]);
+
+    }
+
     /**
      * 异步通知小程序支付
      */

+ 2 - 1
application/route.php

@@ -83,7 +83,8 @@ return [
     'v1/notifyXcxPay'=>'expand/Xcx/notifyXcxPay',
     'v1/platformXcxPay'=>'expand/Xcx/platformXcxPay',
     'v1/getOpenInfo'=>'expand/Xcx/getOpenInfo',
-    
+    'v1/getMobileInfo'=>'expand/Xcx/getMobileInfo',
+
     //一维码,二维码
     'v1/Qrcode'=>'expand/Qrcode/QR',
     'v1/Barcode'=>'expand/Barcode/Bar',