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; } }