| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\behaviors;
- use app\models\Admin;
- use app\models\AdminRole;
- use app\models\AuthRole;
- use app\models\Option;
- use app\models\Store;
- use yii\base\ActionFilter;
- use Throwable;
- use Yii;
- use yii\helpers\Json;
- use app\models\WechatConfig;
- use EasyWeChat\Factory;
- /**
- * Class Auth
- * @package app\modules\admin\behaviors
- */
- class Auth extends ActionFilter
- {
- // 接口白名单
- public static $allow_list = [
- 'admin/alipay/alipay-app/get-service-cats',
- // 'admin/user/info',
- // 'admin/auth/get-permission',
- // 'admin/user/select-list',
- 'admin/saas/get-base-setting',
- 'admin/marketing/export',
- // 'admin/goods/cat-list',
- "admin/aggregate-qrcode/ali-config",
- "admin/aggregate-qrcode/create-url",
- "admin/cloud/supplier/purchase-order-info",
- "admin/cloud/platform/export-distribution-info",
- 'admin/saas/financia-list'
- ];
- public function beforeAction($action)
- {
- $params_r = $_REQUEST['r'];
- if (in_array($params_r, self::$allow_list)) {
- return true;
- }
- try {
- $payload = Yii::$app->jwt->getPayload();
- } catch (Throwable $throwable) {
- Yii::$app->getResponse()->statusCode = 401;
- return false;
- }
- $admin_id = $payload['admin_id'];
- if ($admin_id) {
- $admin = Admin::find()->where([
- 'id' => $admin_id,
- 'is_delete' => Admin::ADMIN_NORMAL,
- ])->one();
- if (Yii::$app->isSaas()) {
- // 如果是saas版本,那么store_id重置为token中的store_id
- $admin->store_id = $payload['store_id'];
- }
- // 权限问题
- // TODO 暂时注释,待解决saas版本权限问题
- // if ($admin->username != 'admin') {
- // $admin_role = AdminRole::findOne(['admin_id' => $admin_id]);
- // if ($admin_role) {
- // $auth_role = AuthRole::findOne(['key' => $admin_role->role_key, 'status' => 1]);
- // if ($auth_role) {
- // $data = Json::decode($auth_role->data);
- // if (($res = $this->check($data)) ==s= false) {
- // Yii::$app->getResponse()->statusCode = 401;
- // return false;
- // }
- // }
- // }
- // }
- Yii::$app->jwt->setAdmin($admin);
- }
- list($wechat_config, $wechat) = $this->initWX();
- $action->controller->wechat_config = $wechat_config;
- $action->controller->wechat = $wechat;
- if ($wechat_config) {
- list($wechatPay, $wechatMini) = $this->initWXPay($wechat_config);
- $action->controller->wechatPay = $wechatPay;
- $action->controller->wechatMini = $wechatMini;
- }
- return parent::beforeAction($action);
- }
- protected function initWX()
- {
- // TODO: 小程序为例
- // 获取微信配置进行初始化
- $wechat = null;
- $wechat_config = WechatConfig::findOne(['store_id' => get_store_id(), 'type' => 1]);
- if (\Yii::$app->prod_is_dandianpu()) {
- $platform_mch_appid = Option::get('one_store_wechat_appid', 0, 'saas')['value'];
- $platform_mch_key = Option::get('one_store_wechat_secret', 0, 'saas')['value'];
- $wechat_config = (object)[
- 'app_id' => $platform_mch_appid,
- 'app_secret' => $platform_mch_key
- ];
- //没有进件走平台账号
- if(get_store_id() > 0){
- $hasIncoming = \app\models\Store::hasIncoming(get_store_id());
- if(!$hasIncoming){
- $mch_id = Option::get('one_store_mch_id', 0, 'saas', '')['value'];
- $pay_key = Option::get('one_store_pay_key', 0, 'saas', '')['value'];
- $apiclient_cert = Option::get('one_store_apiclient_cert', 0, 'saas', '')['value'];
- $apiclient_key = Option::get('one_store_apiclient_key', 0, 'saas', '')['value'];
- $wechat_config->mch_id = $mch_id;
- $wechat_config->pay_key = $pay_key;
- $wechat_config->cert_pem = $apiclient_cert;
- $wechat_config->key_pem = $apiclient_key;
- }
- }
- }
- \Yii::error('store_id => ' . \get_store_id());
- if ($wechat_config) {
- $config = [
- 'app_id' => $wechat_config->app_id,
- 'secret' => $wechat_config->app_secret,
- 'response_type' => 'array'
- ];
- $wechat = Factory::miniProgram($config);
- }else{
- //如果是商盟 获取商盟小程序配置信息
- if(get_store_id() == -1){
- $keys = [
- 'platform_appid',
- 'platform_mch_id',
- 'platform_key',
- 'platform_apiclient_cert',
- 'platform_apiclient_key',
- 'platform_pay_key',
- ];
- $data = Option::get($keys, 0, 'saas');
- if (empty($data)) {
- $data = [
- 'platform_appid' => '',
- 'platform_mch_id' => '',
- 'platform_key' => ' ',
- 'platform_apiclient_cert' => '',
- 'platform_apiclient_key' => '',
- 'platform_pay_key'=> '',
- ];
- } else {
- $arr = [];
- foreach ($data as $value) {
- $index = array_search($value['name'], $keys);
- unset($keys[$index]);
- $arr[$value['name']] = $value['value'];
- }
- foreach ($keys as $key) {
- $arr[$key] = '';
- }
- $data = $arr;
- }
- $wechat_config =new WechatConfig();
- $wechat_config->app_id = $data['platform_appid'];
- $wechat_config->app_secret = $data['platform_key'];
- $wechat_config->mch_id = $data['platform_mch_id'];
- $wechat_config->pay_key = $data['platform_pay_key'];
- $wechat_config->cert_pem = $data['platform_apiclient_cert'];
- $wechat_config->key_pem = $data['platform_apiclient_key'];
- $config =[];
- $config['app_id'] = $data['platform_appid'];
- $config['secret'] = $data['platform_key'];
- $config['response_type'] = 'array';
- $wechat = Factory::miniProgram($config);
- }
- }
- return [
- $wechat_config,
- $wechat,
- ];
- }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-02-03
- * @desc: 实例化支付类
- * @return void
- */
- protected function initWXPay($wechat_config)
- {
- $store_id = get_store_id();
- $store = Store::findOne($store_id);
- //供应链版本
- if (\Yii::$app->prod_is_dandianpu()) {
- //未进件情况
- if (!Store::hasIncoming($store_id)) {
- }
- } else {
- }
- if ((int)$store->business_model === 1) {
- }
- //saas版本
- if ((int)$store->business_model === 1) {
- }
- // 证书
- 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 (isset($wechat_config->cert_pem) && $wechat_config->cert_pem) {
- $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config->cert_pem);
- if (!file_exists($cert_pem_file)) {
- file_put_contents($cert_pem_file, $wechat_config->cert_pem);
- }
- }
- $key_pem_file = null;
- if (isset($wechat_config->key_pem) && $wechat_config->key_pem) {
- $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config->key_pem);
- if (!file_exists($key_pem_file)) {
- file_put_contents($key_pem_file, $wechat_config->key_pem);
- }
- }
- $config = [
- 'app_id' => $wechat_config->app_id,
- 'secret' => $wechat_config->app_secret,
- 'key' => $wechat_config->pay_key ?? '',
- 'mch_id' => $wechat_config->mch_id ?? '',
- 'cert_path' => $cert_pem_file,
- 'key_path' => $key_pem_file,
- 'response_type' => 'array'
- ];
- $wechatPay = Factory::payment($config);
- $wechatMini = Factory::miniProgram($config);
- return [
- $wechatPay,
- $wechatMini,
- ];
- }
- /**
- * 检测权限
- * @param $res
- * @return boolean
- */
- private function check($res) {
- if (empty($res)) {
- return false;
- }
- $params = require Yii::$app->basePath . '/config/interface_permission.php';
- $params_r = $_REQUEST['r'];
- if (in_array($params_r, self::$allow_list)) {
- return true;
- }
- $permission = [];
- foreach ($params as $key => $val) {
- if (!empty($val)) {
- foreach ($val as $r) {
- if ($r == $params_r) {
- $permission[] = $key;
- }
- }
- }
- }
- if (empty($permission)) {
- return false;
- }
- foreach ($res as $str) {
- foreach ($permission as $item) {
- if (strpos($str, $item) !== false) {
- return true;
- }
- }
- }
- return false;
- }
- }
|