cxfoot/modules/api/models/WxmpAuthMobileForm.php
2023-11-20 20:18:51 +08:00

160 lines
4.7 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2022年6月5日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\api\models;
use app\models\User;
use app\components\auth\AToken;
use app\components\wechat\aes\WxBizDataCrypt;
use app\models\UserOauth;
use app\models\common\CommonUserEditForm;
use app\components\SysConst;
use app\components\FlashStorage;
use app\components\EncryptHelper;
use app\models\sms\SmsMsgHelper;
use app\models\sms\SmsTpl;
class WxmpAuthMobileForm extends ApiModel
{
public $iv;
public $code;
public $encrypted_data;
public $wechat_mp;
public $cx_mch_id;
public $url;
public function rules()
{
return [
[['iv', 'code', 'encrypted_data','code'], 'trim'],
[['iv', 'code', 'encrypted_data', 'code'], 'string'],
[['cx_mch_id',], 'required'],
[['iv', 'code', 'encrypted_data', 'wechat_mp',], 'required'],
];
}
//微信授权手机号
public function auth()
{
$res = $this->code2session($this->code);
if($res['code'] != 0)
return $res;
$session_key = $res['data']['session_key'];
$res = $this->decrypted_data($session_key);
if($res['code'] != 0){
return $res;
}
$res['data'] = json_decode($res['data'],true);
if(!isset($res['data']['purePhoneNumber'])){
return [
'code' => 1,
'msg' => '数据解析失败',
'data' => $res['data']
];
}
$mobile = $res['data']['purePhoneNumber'];//不带区号的手机号
$mobile_prefix = $res['data']['countryCode'];
$data = [
'code' => 0,
'msg' => 'ok',
'data' => [
'mobile' => $mobile,
'mobile_prefix' => $mobile_prefix
]
];
return $data;
}
/***
* 用户数据解密
*/
private function decrypted_data($session_key){
$pc = new WxBizDataCrypt($this->wechat_mp->appId, $session_key);
$errCode = $pc->decryptData($this->encrypted_data, $this->iv, $data );
if ($errCode == 0) {
return [
'code' => 0,
'msg' => 'success',
'data' => $data
];
} else {
return [
'code' => 1,
'msg' => $errCode,
];
}
}
private function code2session($code)
{
$api = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->wechat_mp->appId}&secret={$this->wechat_mp->appSecret}&js_code={$code}&grant_type=authorization_code";
$this->wechat_mp->curl->get($api);
if($this->wechat_mp->curl->error_code != 0){
return [
'code' => 1,
'msg' => "err_code:{$this->wechat_mp->curl->error_code}err_msg:{$this->wechat_mp->curl->error_msg}"
];
}
$resp = $this->wechat_mp->curl->response;
$res = json_decode($resp, true);
if(!isset($res['openid'])){
return [
'code' => 1,
'msg' => isset($res['errmsg']) ? $res['errmsg'] : 'error'
];
}
return [
'code' => 0,
'msg' => 'ok',
'data' => $res
];
}
/**
* @ 获取js-sdk前端所需参数
*/
public function actionGetJssdk(){
$redis_name = "api:{$this->wechat_mp->appId}:actionGetJssdk_1";
$get = \Yii::$app->redis->get($redis_name);
if(empty($get)){
$accessToken = $this->wechat_mp->getAccessToken();
// $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$accessToken;
$this->wechat_mp->curl->get($url);
$res = json_decode($this->wechat_mp->curl->response);
$get = $res->ticket;
\Yii::$app->redis->setex($redis_name,6800,$get);
}
$uniq = uniqid();
$time = time();
$this->url = urldecode($this->url);
$str = "jsapi_ticket={$get}&noncestr={$uniq}&timestamp={$time}&url={$this->url}";
$sign = sha1($str);
$return = [
'appId' => $this->wechat_mp->appId,
'timestamp' => $time,
'nonceStr' => $uniq,
'signature' => $sign,
'jsApiList' => [
'scanQRCode',
],
'url' => $this->url,
];
return $this->apiReturnSuccess('ok',$return);
}
}