134 lines
4.2 KiB
PHP
134 lines
4.2 KiB
PHP
<?php
|
||
|
||
/**
|
||
* @author Any
|
||
* @description KISS
|
||
* @date 2020-12-1
|
||
* @version 1.0.0
|
||
*
|
||
* _____LOG_____
|
||
*
|
||
*/
|
||
namespace app\modules\api\controllers;
|
||
|
||
use Yii;
|
||
use app\models\UserToken;
|
||
use app\models\wechat\WechatApp;
|
||
use Wechat\Wechat;
|
||
use app\components\LockUtil;
|
||
|
||
|
||
class Controller extends \app\controllers\Controller
|
||
{
|
||
//@TODO平台类型,wxmp=微信小程序,wxoa=微信公众号,app=APP,h5=移动网页端
|
||
public $_cx_platform;
|
||
//token类型,根据平台类型生成
|
||
public $_cx_token_type;
|
||
//应用版本
|
||
public $_cx_version;
|
||
//客户端ID
|
||
public $_cx_client_id;
|
||
|
||
/**
|
||
* 微信小程序
|
||
* @var Wechat
|
||
*/
|
||
public $wechat_mp;
|
||
|
||
/**
|
||
* 锁
|
||
* @Var LockUtil
|
||
*/
|
||
public $lock;
|
||
|
||
|
||
public function init()
|
||
{
|
||
parent::init();
|
||
//取消session认证
|
||
\Yii::$app->user->enableSession = false;
|
||
//应用版本
|
||
$this->_cx_version = $this->getRequestParamByKey('X-App-Version',$this->getRequestParamByKey('x-app-version'));
|
||
//平台商户ID
|
||
$this->cx_mch_id = $this->getRequestParamByKey('X-Mch-Id',$this->getRequestParamByKey('x-mch-id',0));
|
||
//获取来源平台
|
||
$_cx_platform = $this->getRequestParamByKey('X-App-Platform',$this->getRequestParamByKey('x-app-platform'));
|
||
$this->_cx_platform = empty($_cx_platform) ? 'h5' : $_cx_platform;
|
||
//根据平台获取token类型
|
||
$this->_cx_token_type = UserToken::getTypeByPlatform($this->_cx_platform);
|
||
//客户端ID
|
||
$this->_cx_client_id = $this->getRequestParamByKey('X-App-Client-Id',$this->getRequestParamByKey('x-app-client-id'));
|
||
//微信小程序
|
||
$wechat_app = WechatApp::findOne([
|
||
'cx_mch_id' => $this->cx_mch_id,
|
||
'is_delete' => 0
|
||
]);
|
||
if($wechat_app){
|
||
if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
|
||
mkdir(\Yii::$app->runtimePath . '/pem');
|
||
file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
|
||
}
|
||
$cert_pem_file = null;
|
||
if ($wechat_app->cert_pem) {
|
||
$cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_app->cert_pem);
|
||
if (!file_exists($cert_pem_file))
|
||
file_put_contents($cert_pem_file, $wechat_app->cert_pem);
|
||
}
|
||
$key_pem_file = null;
|
||
if ($wechat_app->key_pem) {
|
||
$key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_app->key_pem);
|
||
if (!file_exists($key_pem_file))
|
||
file_put_contents($key_pem_file, $wechat_app->key_pem);
|
||
}
|
||
$this->wechat_mp = new Wechat([
|
||
'appId' => $wechat_app->app_id,
|
||
'appSecret' => $wechat_app->app_secret,
|
||
'mchId' => $wechat_app->mch_id,
|
||
'apiKey' => $wechat_app->key,
|
||
'certPem' => $cert_pem_file,
|
||
'keyPem' => $key_pem_file,
|
||
]);
|
||
}
|
||
//锁初始化
|
||
$this->lock = new LockUtil($this->cx_mch_id,$this->_cx_client_id);
|
||
}
|
||
|
||
|
||
public function beforeAction($action)
|
||
{
|
||
//关闭_it_csrf
|
||
$app_csrf = $this->getRequestParamByKey('X-Csrf',$this->getRequestParamByKey('x-csrf'));
|
||
//@CX INIT PARAM
|
||
if($app_csrf == "ag7x1Afy1r0q8j4y05vk3s4118hx7124"){
|
||
//跨域关闭csrf
|
||
$action->controller->enableCsrfValidation = false;
|
||
}
|
||
if (parent::beforeAction($action))
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取请求参数,优选获取请求头
|
||
* @param string $key 键值
|
||
* @param string $default 默认值
|
||
*/
|
||
public function getRequestParamByKey($key, $default = null)
|
||
{
|
||
$headers = \Yii::$app->request->headers;
|
||
if(isset($headers[$key]) && !empty($headers[$key]))
|
||
return $headers[$key];
|
||
$val = \Yii::$app->request->get($key);
|
||
if(!empty($val))
|
||
return $val;
|
||
$val = \Yii::$app->request->post($key);
|
||
if(!empty($val))
|
||
return $val;
|
||
return $default;
|
||
}
|
||
} |