|
| 1 | +<?php |
| 2 | +namespace lspbupt\wechat; |
| 3 | +use \Yii; |
| 4 | +use yii\helpers\ArrayHelper; |
| 5 | +class WxSmallApp extends \lspbupt\curl\CurlHttp |
| 6 | +{ |
| 7 | + public $appid = ""; |
| 8 | + public $appsecret = ""; |
| 9 | + public $host = "api.weixin.qq.com"; |
| 10 | + public $protocol = "https"; |
| 11 | + |
| 12 | + public function jscode2session($js_code, $grant_type = "authorization_code") |
| 13 | + { |
| 14 | + return $this->setGet()->httpExec("/sns/jscode2session", [ |
| 15 | + 'appid' => $this->appid, |
| 16 | + 'secret' => $this->appsecret, |
| 17 | + 'js_code' => $js_code, |
| 18 | + 'grant_type' => $grant_type, |
| 19 | + ]); |
| 20 | + } |
| 21 | + |
| 22 | + public function checkSign($sessionKey, $rawData, $sign) |
| 23 | + { |
| 24 | + $checkSign = sha1($rawData.$sessionKey); |
| 25 | + return $sign == $checkSign; |
| 26 | + } |
| 27 | + |
| 28 | + public function decrypt($sessionKey, $encryptedData, $iv, &$data) |
| 29 | + { |
| 30 | + if (strlen($sessionKey) != 24) { |
| 31 | + return "AESKEY不正确"; |
| 32 | + } |
| 33 | + $aesKey=base64_decode($sessionKey); |
| 34 | + |
| 35 | + if (strlen($iv) != 24) { |
| 36 | + return "iv不正确"; |
| 37 | + } |
| 38 | + $aesIV=base64_decode($iv); |
| 39 | + |
| 40 | + $aesCipher=base64_decode($encryptedData); |
| 41 | + |
| 42 | + $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV); |
| 43 | + |
| 44 | + $data=json_decode($result, true); |
| 45 | + if(empty($data)) { |
| 46 | + return "数据不正确"; |
| 47 | + } |
| 48 | + $appid = ArrayHelper::getValue($data, "watermark.appid", ""); |
| 49 | + if( $appid != $this->appid ) { |
| 50 | + return "appid不正确"; |
| 51 | + } |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | +} |
0 commit comments