Auth.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\behaviors;
  8. use app\models\Admin;
  9. use app\models\AdminRole;
  10. use app\models\AuthRole;
  11. use app\models\Option;
  12. use app\models\Store;
  13. use yii\base\ActionFilter;
  14. use Throwable;
  15. use Yii;
  16. use yii\helpers\Json;
  17. use app\models\WechatConfig;
  18. use EasyWeChat\Factory;
  19. /**
  20. * Class Auth
  21. * @package app\modules\admin\behaviors
  22. */
  23. class Auth extends ActionFilter
  24. {
  25. // 接口白名单
  26. public static $allow_list = [
  27. 'admin/alipay/alipay-app/get-service-cats',
  28. // 'admin/user/info',
  29. // 'admin/auth/get-permission',
  30. // 'admin/user/select-list',
  31. 'admin/saas/get-base-setting',
  32. 'admin/marketing/export',
  33. // 'admin/goods/cat-list',
  34. "admin/aggregate-qrcode/ali-config",
  35. "admin/aggregate-qrcode/create-url",
  36. "admin/cloud/supplier/purchase-order-info",
  37. "admin/cloud/platform/export-distribution-info",
  38. 'admin/saas/financia-list'
  39. ];
  40. public function beforeAction($action)
  41. {
  42. $params_r = $_REQUEST['r'];
  43. if (in_array($params_r, self::$allow_list)) {
  44. return true;
  45. }
  46. try {
  47. $payload = Yii::$app->jwt->getPayload();
  48. } catch (Throwable $throwable) {
  49. Yii::$app->getResponse()->statusCode = 401;
  50. return false;
  51. }
  52. $admin_id = $payload['admin_id'];
  53. if ($admin_id) {
  54. $admin = Admin::find()->where([
  55. 'id' => $admin_id,
  56. 'is_delete' => Admin::ADMIN_NORMAL,
  57. ])->one();
  58. if (Yii::$app->isSaas()) {
  59. // 如果是saas版本,那么store_id重置为token中的store_id
  60. $admin->store_id = $payload['store_id'];
  61. }
  62. // 权限问题
  63. // TODO 暂时注释,待解决saas版本权限问题
  64. // if ($admin->username != 'admin') {
  65. // $admin_role = AdminRole::findOne(['admin_id' => $admin_id]);
  66. // if ($admin_role) {
  67. // $auth_role = AuthRole::findOne(['key' => $admin_role->role_key, 'status' => 1]);
  68. // if ($auth_role) {
  69. // $data = Json::decode($auth_role->data);
  70. // if (($res = $this->check($data)) ==s= false) {
  71. // Yii::$app->getResponse()->statusCode = 401;
  72. // return false;
  73. // }
  74. // }
  75. // }
  76. // }
  77. Yii::$app->jwt->setAdmin($admin);
  78. }
  79. list($wechat_config, $wechat) = $this->initWX();
  80. $action->controller->wechat_config = $wechat_config;
  81. $action->controller->wechat = $wechat;
  82. if ($wechat_config) {
  83. list($wechatPay, $wechatMini) = $this->initWXPay($wechat_config);
  84. $action->controller->wechatPay = $wechatPay;
  85. $action->controller->wechatMini = $wechatMini;
  86. }
  87. return parent::beforeAction($action);
  88. }
  89. protected function initWX()
  90. {
  91. // TODO: 小程序为例
  92. // 获取微信配置进行初始化
  93. $wechat = null;
  94. $wechat_config = WechatConfig::findOne(['store_id' => get_store_id(), 'type' => 1]);
  95. if (\Yii::$app->prod_is_dandianpu()) {
  96. $platform_mch_appid = Option::get('one_store_wechat_appid', 0, 'saas')['value'];
  97. $platform_mch_key = Option::get('one_store_wechat_secret', 0, 'saas')['value'];
  98. $wechat_config = (object)[
  99. 'app_id' => $platform_mch_appid,
  100. 'app_secret' => $platform_mch_key
  101. ];
  102. //没有进件走平台账号
  103. if(get_store_id() > 0){
  104. $hasIncoming = \app\models\Store::hasIncoming(get_store_id());
  105. if(!$hasIncoming){
  106. $mch_id = Option::get('one_store_mch_id', 0, 'saas', '')['value'];
  107. $pay_key = Option::get('one_store_pay_key', 0, 'saas', '')['value'];
  108. $apiclient_cert = Option::get('one_store_apiclient_cert', 0, 'saas', '')['value'];
  109. $apiclient_key = Option::get('one_store_apiclient_key', 0, 'saas', '')['value'];
  110. $wechat_config->mch_id = $mch_id;
  111. $wechat_config->pay_key = $pay_key;
  112. $wechat_config->cert_pem = $apiclient_cert;
  113. $wechat_config->key_pem = $apiclient_key;
  114. }
  115. }
  116. }
  117. \Yii::error('store_id => ' . \get_store_id());
  118. if ($wechat_config) {
  119. $config = [
  120. 'app_id' => $wechat_config->app_id,
  121. 'secret' => $wechat_config->app_secret,
  122. 'response_type' => 'array'
  123. ];
  124. $wechat = Factory::miniProgram($config);
  125. }else{
  126. //如果是商盟 获取商盟小程序配置信息
  127. if(get_store_id() == -1){
  128. $keys = [
  129. 'platform_appid',
  130. 'platform_mch_id',
  131. 'platform_key',
  132. 'platform_apiclient_cert',
  133. 'platform_apiclient_key',
  134. 'platform_pay_key',
  135. ];
  136. $data = Option::get($keys, 0, 'saas');
  137. if (empty($data)) {
  138. $data = [
  139. 'platform_appid' => '',
  140. 'platform_mch_id' => '',
  141. 'platform_key' => ' ',
  142. 'platform_apiclient_cert' => '',
  143. 'platform_apiclient_key' => '',
  144. 'platform_pay_key'=> '',
  145. ];
  146. } else {
  147. $arr = [];
  148. foreach ($data as $value) {
  149. $index = array_search($value['name'], $keys);
  150. unset($keys[$index]);
  151. $arr[$value['name']] = $value['value'];
  152. }
  153. foreach ($keys as $key) {
  154. $arr[$key] = '';
  155. }
  156. $data = $arr;
  157. }
  158. $wechat_config =new WechatConfig();
  159. $wechat_config->app_id = $data['platform_appid'];
  160. $wechat_config->app_secret = $data['platform_key'];
  161. $wechat_config->mch_id = $data['platform_mch_id'];
  162. $wechat_config->pay_key = $data['platform_pay_key'];
  163. $wechat_config->cert_pem = $data['platform_apiclient_cert'];
  164. $wechat_config->key_pem = $data['platform_apiclient_key'];
  165. $config =[];
  166. $config['app_id'] = $data['platform_appid'];
  167. $config['secret'] = $data['platform_key'];
  168. $config['response_type'] = 'array';
  169. $wechat = Factory::miniProgram($config);
  170. }
  171. }
  172. return [
  173. $wechat_config,
  174. $wechat,
  175. ];
  176. }
  177. /**
  178. * Undocumented function
  179. *
  180. * @Author LGL 24963@qq.com
  181. * @DateTime 2021-02-03
  182. * @desc: 实例化支付类
  183. * @return void
  184. */
  185. protected function initWXPay($wechat_config)
  186. {
  187. $store_id = get_store_id();
  188. $store = Store::findOne($store_id);
  189. //供应链版本
  190. if (\Yii::$app->prod_is_dandianpu()) {
  191. //未进件情况
  192. if (!Store::hasIncoming($store_id)) {
  193. }
  194. } else {
  195. }
  196. if ((int)$store->business_model === 1) {
  197. }
  198. //saas版本
  199. if ((int)$store->business_model === 1) {
  200. }
  201. // 证书
  202. if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
  203. mkdir(\Yii::$app->runtimePath . '/pem');
  204. file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
  205. }
  206. $cert_pem_file = null;
  207. if (isset($wechat_config->cert_pem) && $wechat_config->cert_pem) {
  208. $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config->cert_pem);
  209. if (!file_exists($cert_pem_file)) {
  210. file_put_contents($cert_pem_file, $wechat_config->cert_pem);
  211. }
  212. }
  213. $key_pem_file = null;
  214. if (isset($wechat_config->key_pem) && $wechat_config->key_pem) {
  215. $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config->key_pem);
  216. if (!file_exists($key_pem_file)) {
  217. file_put_contents($key_pem_file, $wechat_config->key_pem);
  218. }
  219. }
  220. $config = [
  221. 'app_id' => $wechat_config->app_id,
  222. 'secret' => $wechat_config->app_secret,
  223. 'key' => $wechat_config->pay_key ?? '',
  224. 'mch_id' => $wechat_config->mch_id ?? '',
  225. 'cert_path' => $cert_pem_file,
  226. 'key_path' => $key_pem_file,
  227. 'response_type' => 'array'
  228. ];
  229. $wechatPay = Factory::payment($config);
  230. $wechatMini = Factory::miniProgram($config);
  231. return [
  232. $wechatPay,
  233. $wechatMini,
  234. ];
  235. }
  236. /**
  237. * 检测权限
  238. * @param $res
  239. * @return boolean
  240. */
  241. private function check($res) {
  242. if (empty($res)) {
  243. return false;
  244. }
  245. $params = require Yii::$app->basePath . '/config/interface_permission.php';
  246. $params_r = $_REQUEST['r'];
  247. if (in_array($params_r, self::$allow_list)) {
  248. return true;
  249. }
  250. $permission = [];
  251. foreach ($params as $key => $val) {
  252. if (!empty($val)) {
  253. foreach ($val as $r) {
  254. if ($r == $params_r) {
  255. $permission[] = $key;
  256. }
  257. }
  258. }
  259. }
  260. if (empty($permission)) {
  261. return false;
  262. }
  263. foreach ($res as $str) {
  264. foreach ($permission as $item) {
  265. if (strpos($str, $item) !== false) {
  266. return true;
  267. }
  268. }
  269. }
  270. return false;
  271. }
  272. }