commit | author | age
|
90c639
|
1 |
<?php |
Z |
2 |
/** |
|
3 |
* Created by IntelliJ IDEA. |
|
4 |
* User: luwei |
|
5 |
* Date: 2019/7/10 |
|
6 |
* Time: 16:28 |
|
7 |
*/ |
|
8 |
|
|
9 |
namespace app\core; |
|
10 |
|
|
11 |
|
|
12 |
use app\core\cloud\Cloud; |
|
13 |
use app\core\currency\Currency; |
|
14 |
use app\core\exceptions\ClassNotFoundException; |
|
15 |
use app\core\express\ExpressFactory; |
|
16 |
use app\core\kdOrder\KdOrder; |
|
17 |
use app\core\newsms\Sms; |
|
18 |
use app\core\payment\Payment; |
|
19 |
use app\forms\common\CommonUser; |
|
20 |
use app\forms\permission\branch\BaseBranch; |
|
21 |
use app\forms\permission\branch\IndBranch; |
|
22 |
use app\forms\permission\branch\OfflineBranch; |
|
23 |
use app\forms\permission\branch\We7Branch; |
|
24 |
use app\forms\permission\role\AdminRole; |
|
25 |
use app\forms\permission\role\BaseRole; |
|
26 |
use app\forms\permission\role\MchRole; |
|
27 |
use app\forms\permission\role\OperatorRole; |
|
28 |
use app\forms\permission\role\SuperAdminRole; |
|
29 |
use app\handlers\HandlerBase; |
|
30 |
use app\handlers\HandlerRegister; |
|
31 |
use app\models\Mall; |
|
32 |
use app\models\UserIdentity; |
|
33 |
use luweiss\Wechat\Wechat; |
|
34 |
use yii\base\Module; |
|
35 |
use yii\queue\Queue; |
|
36 |
use yii\redis\Connection; |
|
37 |
use yii\web\Controller; |
|
38 |
use yii\web\NotFoundHttpException; |
|
39 |
use yii\web\Response; |
|
40 |
|
|
41 |
/** |
|
42 |
* Trait Application |
|
43 |
* @package app\core |
|
44 |
* @property \app\core\Plugin $plugin |
|
45 |
* @property Serializer $serializer |
|
46 |
* @property Mall $mall |
|
47 |
* @property integer $mchId |
|
48 |
* @property string $appPlatform |
|
49 |
* @property string $appVersion |
|
50 |
* @property Payment $payment |
|
51 |
* @property Cloud $cloud |
|
52 |
* @property Connection $redis |
|
53 |
* @property Queue $queue |
|
54 |
* @property Queue $queue3 |
|
55 |
* @property Queue $queue4 |
|
56 |
* @property Currency $currency |
|
57 |
* @property AppMessage $appMessage |
|
58 |
* @property Sms $sms |
|
59 |
* @property Wechat $wechat |
|
60 |
* @property $alipay |
|
61 |
* @property BaseRole $role |
|
62 |
* @property BaseBranch $branch |
|
63 |
* @property string $hostInfo |
|
64 |
* @property string $baseUrl |
|
65 |
*/ |
|
66 |
trait Application |
|
67 |
{ |
|
68 |
private $mallId; |
|
69 |
protected $mall; |
|
70 |
protected $mchId; |
|
71 |
private $xAppPlatform; |
|
72 |
private $payment; |
|
73 |
private $xCloud; |
|
74 |
private $currency; |
|
75 |
private $kdOrder; |
|
76 |
private $appMessage; |
|
77 |
private $sms; |
|
78 |
private $wechat; |
|
79 |
private $alipay; |
|
80 |
private $role; |
|
81 |
private $branch; |
|
82 |
private $appVersion; |
|
83 |
private $expressTrack; |
|
84 |
private $hostInfo; |
|
85 |
private $baseUrl; |
|
86 |
|
|
87 |
protected function setInitParams() |
|
88 |
{ |
|
89 |
ini_set('max_execution_time', 300); |
|
90 |
ini_set('memory_limit', '512M'); |
|
91 |
return $this; |
|
92 |
} |
|
93 |
|
|
94 |
|
|
95 |
/** |
|
96 |
* Load .env file |
|
97 |
* |
|
98 |
* @return self |
|
99 |
*/ |
|
100 |
protected function loadDotEnv() |
|
101 |
{ |
|
102 |
try { |
|
103 |
$dotenv = new \Dotenv\Dotenv(dirname(__DIR__)); |
|
104 |
$dotenv->load(); |
|
105 |
} catch (\Dotenv\Exception\InvalidPathException $ex) { |
|
106 |
} |
|
107 |
return $this; |
|
108 |
} |
|
109 |
|
|
110 |
/** |
|
111 |
* Define some constants |
|
112 |
* |
|
113 |
* @return self |
|
114 |
*/ |
|
115 |
protected function defineConstants() |
|
116 |
{ |
|
117 |
define_once('IN_IA', true); |
|
118 |
define_once('APP_PLATFORM_WXAPP', 'wxapp'); |
|
119 |
define_once('APP_PLATFORM_ALIAPP', 'aliapp'); |
|
120 |
define_once('APP_PLATFORM_BDAPP', 'bdapp'); |
|
121 |
define_once('APP_PLATFORM_TTAPP', 'ttapp'); |
|
122 |
define_once('APP_PLATFORM_MOBILE', 'mobile'); |
|
123 |
define_once('APP_PLATFORM_WECHAT', 'wechat'); |
|
124 |
$this->defineEnvConstants(['YII_DEBUG', 'YII_ENV']); |
|
125 |
return $this; |
|
126 |
} |
|
127 |
|
|
128 |
/** |
|
129 |
* Define some constants via `env()` |
|
130 |
* |
|
131 |
* @param array $names |
|
132 |
* @return self |
|
133 |
*/ |
|
134 |
protected function defineEnvConstants($names = []) |
|
135 |
{ |
|
136 |
foreach ($names as $name) { |
|
137 |
if ((!defined($name)) && ($value = env($name))) { |
|
138 |
define($name, $value); |
|
139 |
} |
|
140 |
} |
|
141 |
return $this; |
|
142 |
} |
|
143 |
|
|
144 |
/** |
|
145 |
* Enable JSON response if controller returns Array or Object |
|
146 |
*/ |
|
147 |
protected function enableObjectResponse() |
|
148 |
{ |
|
149 |
$this->response->on( |
|
150 |
Response::EVENT_BEFORE_SEND, |
|
151 |
function ($event) { |
|
152 |
/** @var \yii\web\Response $response */ |
|
153 |
$response = $event->sender; |
|
154 |
if (is_array($response->data) || is_object($response->data)) { |
|
155 |
$response->format = \yii\web\Response::FORMAT_JSON; |
|
156 |
} |
|
157 |
} |
|
158 |
); |
|
159 |
return $this; |
|
160 |
} |
|
161 |
|
|
162 |
/** |
|
163 |
* Enable full error reporting if using debug mode |
|
164 |
* |
|
165 |
* @return self |
|
166 |
*/ |
|
167 |
protected function enableErrorReporting() |
|
168 |
{ |
|
169 |
if (YII_DEBUG) { |
|
170 |
error_reporting(E_ALL); |
|
171 |
} else { |
|
172 |
error_reporting(E_ALL ^ E_NOTICE); |
|
173 |
} |
|
174 |
return $this; |
|
175 |
} |
|
176 |
|
|
177 |
/** |
|
178 |
* 重写runAction方法使之可运行插件代码 |
|
179 |
* |
|
180 |
* @param string $route |
|
181 |
* @param array $params |
|
182 |
* @return mixed |
|
183 |
* @throws NotFoundHttpException |
|
184 |
* @throws \yii\base\InvalidConfigException |
|
185 |
* @throws \yii\base\InvalidRouteException |
|
186 |
* @throws \Exception |
|
187 |
*/ |
|
188 |
public function runAction($route, $params = []) |
|
189 |
{ |
|
190 |
bcscale(2);//配置BC函数小数精度 |
|
191 |
|
|
192 |
$route = ltrim($route, '/'); |
|
193 |
$pattern = '/^plugin\/.*/'; |
|
194 |
preg_match($pattern, $route, $matches); |
|
195 |
if ($matches) { |
|
196 |
$originRoute = $matches[0]; |
|
197 |
$originRouteArray = mb_split('/', $originRoute); |
|
198 |
|
|
199 |
$pluginId = !empty($originRouteArray[1]) ? $originRouteArray[1] : null; |
|
200 |
if (!$pluginId) { |
|
201 |
throw new NotFoundHttpException(); |
|
202 |
} |
|
203 |
if (!$this->plugin->getInstalledPlugin($pluginId)) { |
|
204 |
throw new NotFoundHttpException(); |
|
205 |
} |
|
206 |
$controllerId = 'index'; |
|
207 |
$controllerClass = "app\\plugins\\{$pluginId}\\controllers\\IndexController"; |
|
208 |
$actionId = 'index'; |
|
209 |
$appendNamespace = ''; |
|
210 |
for ($i = 2; $i < count($originRouteArray); $i++) { |
|
211 |
$controllerId = !empty($originRouteArray[$i]) ? $originRouteArray[$i] : 'index'; |
|
212 |
$controllerName = preg_replace_callback('/\-./', function ($e) { |
|
213 |
return ucfirst(trim($e[0], '-')); |
|
214 |
}, $controllerId); |
|
215 |
$controllerName = ucfirst($controllerName); |
|
216 |
$controllerName .= 'Controller'; |
|
217 |
$controllerClass = "app\\plugins\\{$pluginId}\\controllers\\{$appendNamespace}{$controllerName}"; |
|
218 |
$actionId = !empty($originRouteArray[$i + 1]) ? $originRouteArray[$i + 1] : 'index'; |
|
219 |
if (class_exists($controllerClass)) { |
|
220 |
break; |
|
221 |
} |
|
222 |
$appendNamespace .= $originRouteArray[$i] . '\\'; |
|
223 |
} |
|
224 |
|
|
225 |
try { |
|
226 |
/** @var Controller $controller */ |
|
227 |
$controller = \Yii::createObject($controllerClass, [$controllerId, $this]); |
|
228 |
$module = new Module($pluginId, $this); |
|
229 |
$controller->module = $module; |
|
230 |
$this->controller = $controller; |
|
231 |
\Yii::$app->plugin->setCurrentPlugin(\Yii::$app->plugin->getPlugin($pluginId)); |
|
232 |
return $controller->runAction($actionId, $params); |
|
233 |
} catch (\ReflectionException $e) { |
|
234 |
throw new NotFoundHttpException(\Yii::t('yii', 'Page not found.'), 0, $e); |
|
235 |
} |
|
236 |
} |
|
237 |
return parent::runAction($route, $params); |
|
238 |
} |
|
239 |
|
|
240 |
/** |
|
241 |
* @return $this |
|
242 |
*/ |
|
243 |
protected function loadAppHandler() |
|
244 |
{ |
|
245 |
$register = new HandlerRegister(); |
|
246 |
$HandlerClasses = $register->getHandlers(); |
|
247 |
foreach ($HandlerClasses as $HandlerClass) { |
|
248 |
$handler = new $HandlerClass(); |
|
249 |
if ($handler instanceof HandlerBase) { |
|
250 |
/** @var HandlerBase $handler */ |
|
251 |
$handler->register(); |
|
252 |
} |
|
253 |
} |
|
254 |
return $this; |
|
255 |
} |
|
256 |
|
|
257 |
/** |
|
258 |
* @return $this |
|
259 |
* @throws \Exception |
|
260 |
*/ |
|
261 |
protected function loadPluginsHandler() |
|
262 |
{ |
|
263 |
if (!\Yii::$app->db->username) { |
|
264 |
return $this; |
|
265 |
} |
|
266 |
$corePluginTableName = \Yii::$app->db->tablePrefix . 'core_plugin'; |
|
267 |
if (!table_exists($corePluginTableName)) { |
|
268 |
return $this; |
|
269 |
} |
|
270 |
$corePlugins = \Yii::$app->plugin->list; |
|
271 |
foreach ($corePlugins as $corePlugin) { |
|
272 |
try { |
|
273 |
$plugin = \Yii::$app->plugin->getPlugin($corePlugin->name); |
|
274 |
$plugin->handler(); |
|
275 |
} catch (ClassNotFoundException $exception) { |
|
276 |
continue; |
|
277 |
} |
|
278 |
} |
|
279 |
return $this; |
|
280 |
} |
|
281 |
|
|
282 |
/** |
|
283 |
* @return $this |
|
284 |
*/ |
|
285 |
protected function loadAppLogger() |
|
286 |
{ |
|
287 |
return $this; |
|
288 |
} |
|
289 |
|
|
290 |
|
|
291 |
public function getMallId() |
|
292 |
{ |
|
293 |
return $this->mallId; |
|
294 |
} |
|
295 |
|
|
296 |
public function setMallId($mallId) |
|
297 |
{ |
|
298 |
$this->mallId = $mallId; |
|
299 |
} |
|
300 |
|
|
301 |
/** |
|
302 |
* @return Mall |
|
303 |
* @throws \Exception |
|
304 |
*/ |
|
305 |
public function getMall() |
|
306 |
{ |
|
307 |
if (!$this->mall || !$this->mall->id) { |
|
308 |
throw new \Exception('mall is Null'); |
|
309 |
} |
|
310 |
return $this->mall; |
|
311 |
} |
|
312 |
|
|
313 |
|
|
314 |
/** |
|
315 |
* @param Mall $mall |
|
316 |
*/ |
|
317 |
public function setMall(Mall $mall) |
|
318 |
{ |
|
319 |
$this->mall = $mall; |
|
320 |
} |
|
321 |
|
|
322 |
/** |
|
323 |
* @return mixed |
|
324 |
*/ |
|
325 |
public function getMchId() |
|
326 |
{ |
|
327 |
return $this->mchId ?: 0; |
|
328 |
} |
|
329 |
|
|
330 |
/** |
|
331 |
* @param $mchId |
|
332 |
*/ |
|
333 |
public function setMchId($mchId) |
|
334 |
{ |
|
335 |
$this->mchId = $mchId; |
|
336 |
} |
|
337 |
|
|
338 |
/** |
|
339 |
* @return mixed |
|
340 |
* @throws \Exception |
|
341 |
*/ |
|
342 |
public function getAppPlatform() |
|
343 |
{ |
|
344 |
if ($this->xAppPlatform) { |
|
345 |
return $this->xAppPlatform; |
|
346 |
} |
|
347 |
if (!empty(\Yii::$app->request->headers['x-app-platform'])) { |
|
348 |
$this->xAppPlatform = \Yii::$app->request->headers['x-app-platform']; |
|
349 |
if ($this->xAppPlatform == 'wx') { |
|
350 |
$this->xAppPlatform = 'wxapp'; |
|
351 |
} |
|
352 |
} |
|
353 |
if (!$this->xAppPlatform) { |
|
354 |
$this->xAppPlatform = 'webapp'; |
|
355 |
} |
|
356 |
return $this->xAppPlatform; |
|
357 |
} |
|
358 |
|
|
359 |
public function setAppPlatform($xAppPlatform) |
|
360 |
{ |
|
361 |
$this->xAppPlatform = $xAppPlatform; |
|
362 |
} |
|
363 |
|
|
364 |
/** |
|
365 |
* @return Payment |
|
366 |
*/ |
|
367 |
public function getPayment() |
|
368 |
{ |
|
369 |
if ($this->payment) { |
|
370 |
return $this->payment; |
|
371 |
} |
|
372 |
$this->payment = new Payment(); |
|
373 |
return $this->payment; |
|
374 |
} |
|
375 |
|
|
376 |
/** |
|
377 |
* @return Cloud |
|
378 |
*/ |
|
379 |
public function getCloud() |
|
380 |
{ |
|
381 |
if ($this->xCloud) { |
|
382 |
return $this->xCloud; |
|
383 |
} |
|
384 |
$this->xCloud = new Cloud(); |
|
385 |
return $this->xCloud; |
|
386 |
} |
|
387 |
|
|
388 |
public function getCurrency() |
|
389 |
{ |
|
390 |
if ($this->currency) { |
|
391 |
return $this->currency; |
|
392 |
} |
|
393 |
$this->currency = new Currency(); |
|
394 |
return $this->currency; |
|
395 |
} |
|
396 |
|
|
397 |
public function getExpressTrack() |
|
398 |
{ |
|
399 |
if ($this->expressTrack) { |
|
400 |
return $this->expressTrack; |
|
401 |
} |
|
402 |
$this->expressTrack = new ExpressFactory(); |
|
403 |
return $this->expressTrack; |
|
404 |
} |
|
405 |
|
|
406 |
public function getKdOrder() |
|
407 |
{ |
|
408 |
if ($this->kdOrder) { |
|
409 |
return $this->kdOrder; |
|
410 |
} |
|
411 |
$this->kdOrder = new KdOrder(); |
|
412 |
return $this->kdOrder; |
|
413 |
} |
|
414 |
|
|
415 |
private $loadedViewComponents = []; |
|
416 |
|
|
417 |
/** |
|
418 |
* 加载vue组件 |
|
419 |
* @param string $component 组件id |
|
420 |
* @param string $basePath 文件目录,默认/views/components |
|
421 |
* @param bool $singleLoad 只加载一次 |
|
422 |
*/ |
|
423 |
public function loadViewComponent($component, $basePath = null, $singleLoad = true) |
|
424 |
{ |
|
425 |
if (!$basePath) { |
|
426 |
$basePath = \Yii::$app->viewPath . '/components'; |
|
427 |
} |
|
428 |
$file = "{$basePath}/{$component}.php"; |
|
429 |
if (isset($this->loadedViewComponents[$file]) && $singleLoad) { |
|
430 |
return; |
|
431 |
} |
|
432 |
$this->loadedViewComponents[$file] = true; |
|
433 |
echo $this->getView()->renderFile($file) . "\n"; |
|
434 |
} |
|
435 |
|
|
436 |
/** |
|
437 |
* @return AppMessage |
|
438 |
*/ |
|
439 |
public function getAppMessage() |
|
440 |
{ |
|
441 |
if (!$this->appMessage) { |
|
442 |
$this->appMessage = new AppMessage(); |
|
443 |
} |
|
444 |
return $this->appMessage; |
|
445 |
} |
|
446 |
|
|
447 |
public function getSms() |
|
448 |
{ |
|
449 |
if (!$this->sms) { |
|
450 |
$this->sms = new Sms(); |
|
451 |
} |
|
452 |
return $this->sms; |
|
453 |
} |
|
454 |
|
|
455 |
public function getWechat() |
|
456 |
{ |
|
457 |
if (!$this->wechat) { |
|
458 |
/** @var \app\plugins\wxapp\Plugin $plugin */ |
|
459 |
$plugin = $this->plugin->getPlugin('wxapp'); |
|
460 |
$this->wechat = $plugin->getWechat(); |
|
461 |
} |
|
462 |
return $this->wechat; |
|
463 |
} |
|
464 |
|
|
465 |
public function getAlipay() |
|
466 |
{ |
|
467 |
throw new \Exception('尚未支持此功能。'); |
|
468 |
} |
|
469 |
|
|
470 |
/** |
|
471 |
* @return BaseRole |
|
472 |
* @throws \Exception |
|
473 |
* 获取登录用户的角色 |
|
474 |
*/ |
|
475 |
public function getRole() |
|
476 |
{ |
|
477 |
if (!$this->role) { |
|
478 |
if (\Yii::$app->user->isGuest) { |
|
479 |
throw new \Exception('用户未登录'); |
|
480 |
} |
|
481 |
/* @var UserIdentity $userIdentity */ |
|
482 |
$userIdentity = CommonUser::getUserIdentity(); |
|
483 |
$config = [ |
|
484 |
'userIdentity' => $userIdentity, |
|
485 |
'user' => \Yii::$app->user->identity, |
|
486 |
'mall' => \Yii::$app->mall |
|
487 |
]; |
|
488 |
if ($userIdentity->is_super_admin == 1) { |
|
489 |
// 总管理员 |
|
490 |
$this->role = new SuperAdminRole($config); |
|
491 |
} elseif ($userIdentity->is_admin == 1) { |
|
492 |
// 子管理员 |
|
493 |
$this->role = new AdminRole($config); |
|
494 |
} elseif ($userIdentity->is_operator == 1) { |
|
495 |
// 员工 |
|
496 |
$this->role = new OperatorRole($config); |
|
497 |
} elseif (\Yii::$app->user->identity->mch_id > 0) { |
|
498 |
// 商户 |
|
499 |
$this->role = new MchRole($config); |
|
500 |
} else { |
|
501 |
throw new \Exception('未知用户权限'); |
|
502 |
} |
|
503 |
} |
|
504 |
return $this->role; |
|
505 |
} |
|
506 |
|
|
507 |
public function setRole($role) |
|
508 |
{ |
|
509 |
$this->role = $role; |
|
510 |
} |
|
511 |
|
|
512 |
// 获取登录商城的分支版本 |
|
513 |
public function getBranch() |
|
514 |
{ |
|
515 |
if (!$this->branch) { |
|
516 |
if (is_we7()) { |
|
517 |
$this->branch = new We7Branch(); |
|
518 |
} elseif (is_we7_offline()) { |
|
519 |
$this->branch = new OfflineBranch(); |
|
520 |
} else { |
|
521 |
$this->branch = new IndBranch(); |
|
522 |
} |
|
523 |
} |
|
524 |
return $this->branch; |
|
525 |
} |
|
526 |
|
|
527 |
public function createForm($class) |
|
528 |
{ |
|
529 |
if (!is_string($class)) { |
|
530 |
throw new \Exception("{$class}不是有效的Class"); |
|
531 |
} |
|
532 |
return new $class(); |
|
533 |
} |
|
534 |
|
|
535 |
public function getAppVersion() |
|
536 |
{ |
|
537 |
if ($this->appVersion) { |
|
538 |
return $this->appVersion; |
|
539 |
} |
|
540 |
if (!empty(\Yii::$app->request->headers['x-app-version'])) { |
|
541 |
$this->appVersion = \Yii::$app->request->headers['x-app-version']; |
|
542 |
} |
|
543 |
if (!$this->appVersion) { |
|
544 |
$this->appVersion = '4.0.0'; |
|
545 |
} |
|
546 |
return $this->appVersion; |
|
547 |
} |
|
548 |
|
|
549 |
public function setAppVersion($appVersion) |
|
550 |
{ |
|
551 |
$this->appVersion = $appVersion; |
|
552 |
} |
|
553 |
|
|
554 |
public function getHostInfo() |
|
555 |
{ |
|
556 |
if ($this->hostInfo) { |
|
557 |
return $this->hostInfo; |
|
558 |
} |
|
559 |
return ''; |
|
560 |
} |
|
561 |
|
|
562 |
public function setHostInfo($hostInfo) |
|
563 |
{ |
|
564 |
$this->hostInfo = $hostInfo; |
|
565 |
} |
|
566 |
|
|
567 |
public function getBaseUrl() |
|
568 |
{ |
|
569 |
if ($this->baseUrl) { |
|
570 |
return $this->baseUrl; |
|
571 |
} |
|
572 |
return ''; |
|
573 |
} |
|
574 |
|
|
575 |
public function setBaseUrl($baseUrl) |
|
576 |
{ |
|
577 |
$this->baseUrl = $baseUrl; |
|
578 |
} |
|
579 |
} |