AddonsController.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. <?php
  2. namespace backend\modules\mall\controllers;
  3. use backend\controllers\MallController;
  4. use backend\models\AdminRole;
  5. use backend\modules\mall\services\TopAddonsService;
  6. use common\enums\addons\MallAddonsEnum;
  7. use common\enums\OrderStatusEnum;
  8. use common\enums\RedisKeyEnum;
  9. use common\enums\SettingEnum;
  10. use common\enums\StatusEnum;
  11. use common\events\mall\AddonsInstallEvent;
  12. use common\helpers\Auth;
  13. use common\models\addons\AddonsOrder;
  14. use common\models\addons\AiTool;
  15. use common\models\backend\Admin;
  16. use common\models\common\Addons;
  17. use common\models\common\PlatformSetting;
  18. use common\models\common\PosterCode;
  19. use common\models\mall\MallAddons;
  20. use common\models\platform\PlatformOrder;
  21. use common\models\platform\PlatformOrderStatisticsDay;
  22. use common\models\rbac\AuthItem;
  23. use common\models\rbac\AuthItemChild;
  24. use Yii;
  25. use yii\helpers\Json;
  26. class AddonsController extends MallController
  27. {
  28. // 我的应用
  29. public function actionIndex()
  30. {
  31. if (Yii::$app->request->isAjax) {
  32. if (Yii::$app->request->isGet) {
  33. $params = Yii::$app->request->get();
  34. $res = Yii::$app->services->validate->validate($params, [[['keyword'], 'string']]);
  35. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  36. $cate = Yii::$app->params['addonsGroup'];// 获取应用分类
  37. // 搜索
  38. if (!empty($params['keyword'])) {
  39. $addons_names = Addons::getAddonsByTitle($params['keyword'], 'name');
  40. }
  41. // 获取用户拥有的应用列表
  42. $where = ['mall_id' => $this->mall_id,'addons_status' => MallAddonsEnum::INSTALL,'status' => StatusEnum::ENABLED];
  43. $childWhere = [];
  44. !empty($addons_names) && $where['addons_name'] = $addons_names;
  45. $is_compliance = Yii::$app->services->mall->isOpenComplianceMode($this->mall_id);
  46. $is_compliance && $childWhere = ['<>', 'group', 'business'];
  47. $list = MallAddons::getMallAddons([$where], ['addons' => function ($query) use ($childWhere) {
  48. $query->where($childWhere)
  49. ->select('id,title,name,cover,group,brief_introduction,course_url');
  50. }], true, 'id,addons_name,expire_time,status');// 获取已上架的应用列表
  51. // 获取用户有权限的插件
  52. $valid_addons = self::getUserAuth($list);
  53. $valid_addons_menu_arr = self::getUserAuthAddonsDefaultMenu($list);
  54. // 应用重组到分类中
  55. $data = [];// 返回值初始化
  56. $time = time();
  57. $cache = Yii::$app->cache;
  58. // 获取所有置顶插件
  59. $top_addons = Yii::$app->services->setting->mall->get($this->mall_id, 'top_addons.addons_name');
  60. $top_addons = empty($top_addons) ? [] : Json::decode($top_addons);
  61. foreach ($list as $value) {
  62. // 已过期的应用则跳过循环
  63. if ($value['expire_time'] != MallAddonsEnum::EXPIRE && $time >= $value['expire_time']) continue;
  64. if (is_array($valid_addons) && !in_array($value['addons_name'], $valid_addons)) continue;
  65. $addons = $value['addons'];
  66. if (!empty($addons)) {
  67. // 获取首页链接
  68. $cache_key = RedisKeyEnum::suffix(RedisKeyEnum::BACKEND_MENU,$addons['name'],true);
  69. $cacheData = $cache->get($cache_key);
  70. $url = '';
  71. if(!empty($cacheData[0]['url'])){
  72. $url = $cacheData[0]['url'];
  73. }
  74. if(!empty($valid_addons_menu_arr[$addons['name']])){
  75. $url = $valid_addons_menu_arr[$addons['name']];
  76. }
  77. $k = $addons['group'];
  78. $data[$k]['title'] = $cate[$k]['title'];
  79. $data[$k]['list'][] = [
  80. 'id' => $value['id'],
  81. 'title' => $addons['title'],
  82. 'name' => $addons['name'],
  83. 'cover' => $addons['cover'],
  84. 'brief_introduction' => $addons['brief_introduction'],
  85. 'status' => $time > $value['expire_time'] && $value['expire_time'] != -1 ? 2 : $value['status'],
  86. 'url' =>$url,
  87. 'course_url' => $addons['course_url'],
  88. 'is_top' => in_array($addons['name'], $top_addons) ? 1 : 0, // 是否已置顶
  89. ];
  90. }
  91. }
  92. $data = $this->getAiToolList($cate,$data);
  93. $this->success('获取成功', $data);
  94. }
  95. }
  96. $is_compliance = Yii::$app->services->mall->isOpenComplianceMode($this->mall_id);
  97. return $this->render($this->action->id, ['is_compliance', $is_compliance]);
  98. }
  99. public function getAiToolList($cate,$data){
  100. $static_url = \Yii::$app->services->setting->platform->get('system.static_url');
  101. $ai_tool_list = AiTool::lists(['where' => [['status' => StatusEnum::ENABLED], ['is_docking' => 0]], 'order' => 'sort asc,id asc']);
  102. foreach ($ai_tool_list as &$value){
  103. $value['cover'] = $static_url.$value['cover'];
  104. }
  105. $data['aiTool']['title'] = $cate['aiTool']['title'];
  106. if(isset($data['aiTool']['list'])){
  107. foreach ($data['aiTool']['list'] as &$val){
  108. $val['is_docking'] = 1;
  109. }
  110. $data['aiTool']['list']= array_merge($data['aiTool']['list'],$ai_tool_list);
  111. }else{
  112. $data['aiTool']['list'] = $ai_tool_list;
  113. }
  114. return $data;
  115. }
  116. // 插件详情
  117. public function actionDetail()
  118. {
  119. if (Yii::$app->request->isAjax) {
  120. if (Yii::$app->request->get()) {
  121. $params = Yii::$app->request->get();
  122. $res = Yii::$app->services->validate->validate($params, [[['name'], 'required'], [['name'], 'string']]);
  123. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  124. $detail = Addons::getOne([['name' => $params['name']], ['is_online' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED]], true, ['mallAddons' => function($query){
  125. $query->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
  126. }]);
  127. $mallAddons = $detail['mallAddons'] ?? [];
  128. $time = time();
  129. // 判断是否已安装该应用[0=否,1=是]
  130. $isIntall = 1;
  131. if (empty($mallAddons)) {
  132. $isIntall = 0;
  133. } else {
  134. if ($mallAddons['addons_status'] == MallAddonsEnum::TO_BE_INSTALL) $isIntall = 0;
  135. }
  136. $is_renew = $allow_uninstall = $allow_install = 0;
  137. // 判断是否需要续费
  138. if ($mallAddons) {
  139. $expire_time = $mallAddons['expire_time'];
  140. $is_renew = $expire_time == MallAddonsEnum::EXPIRE ? 0 : 1;
  141. if ($mallAddons['addons_status'] != MallAddonsEnum::UNINSTALL) {// 非卸载状态下才可以卸载
  142. $allow_uninstall = $expire_time != MallAddonsEnum::EXPIRE && $expire_time <= $time ? 0 : 1;
  143. }
  144. if (in_array($mallAddons['addons_status'], [MallAddonsEnum::UNINSTALL, MallAddonsEnum::TO_BE_INSTALL])) $allow_install = 1;
  145. }
  146. $data = [
  147. 'title' => $detail['title'],
  148. 'name' => $detail['name'],
  149. 'cover' => $detail['cover'],
  150. 'brief_introduction' => $detail['brief_introduction'],
  151. 'description' => $detail['description'],
  152. 'payment_type' => $detail['payment_type'],
  153. 'price_setting' => Json::decode($detail['price_setting'], true),
  154. 'is_install' => $isIntall,
  155. 'version' => $mallAddons ? $mallAddons['version'] : $detail['version'],// 商城应用版本号
  156. 'allow_install' => $allow_install,// 是否允许安装,0=否,1=是
  157. 'is_renew' => $is_renew,// 续费
  158. 'expire_time' => $mallAddons['expire_time'] ?? 0,// 过期时间
  159. 'now_time' => time(),// 当前时间
  160. 'allow_uninstall' => $allow_uninstall,// 是否允许卸载,0=否,1=是
  161. ];
  162. // 插件已过期并且状态不是已过期则将其状态改为已过期
  163. if (!empty($mallAddons) && $mallAddons['addons_status'] != MallAddonsEnum::EXPIRE) {
  164. if ($mallAddons['expire_time'] > 0 && $mallAddons['expire_time'] < $time) {
  165. MallAddons::setData(['mall_id' => $this->mall_id, 'addons_name' => $mallAddons['addons_name'], 'addons_status' => MallAddonsEnum::EXPIRE]);
  166. }
  167. }
  168. $this->success('获取成功', $data);
  169. }
  170. }
  171. return $this->render($this->action->id);
  172. }
  173. /**
  174. * 已购应用
  175. * @Author: lun
  176. * @DateTime: 2022/2/28 0028 16:08
  177. * @Copyright: copyright (c) 2021 广东七件事集团
  178. */
  179. public function actionPurchased()
  180. {
  181. $request = Yii::$app->request;
  182. if ($request->isAjax && $request->isPost) {
  183. $cate = Yii::$app->params['addonsGroup'];// 获取应用分类
  184. $childWhere = [];
  185. $is_compliance = Yii::$app->services->mall->isOpenComplianceMode($this->mall_id);
  186. $is_compliance && $childWhere = ['<>', 'group', 'business'];
  187. $list = MallAddons::getMallAddons([['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]], ['addons' => function ($query) use ($childWhere) {
  188. $query->where($childWhere)
  189. ->select('id,title,name,cover,group,brief_introduction,course_url');
  190. }], true, 'id,addons_name,expire_time,addons_status,status');// 获取已上架的应用列表
  191. $data = $addons_list = [];// 返回值初始化
  192. $time = time();// 当前时间
  193. $soon_expire_time = 3 * 24 * 60 * 60;// 即将过期时间
  194. // 获取用户有权限的插件
  195. $valid_addons = self::getUserAuth($list);
  196. foreach ($list as $value) {
  197. // 判断已安装的应用中是否存在已过期的应用,移除此代码可暂时全部应用
  198. if ($value['addons_status'] == MallAddonsEnum::INSTALL && $value['expire_time'] == MallAddonsEnum::EXPIRE) continue;// 永久有效应用跳过
  199. if (is_array($valid_addons) && !in_array($value['addons_name'], $valid_addons)) continue;
  200. $addons = $value['addons'];
  201. if (!empty($addons)) {
  202. $k = $addons['group'];
  203. $addons_list[$k]['title'] = $cate[$k]['title'];
  204. $addons_list[$k]['list'][] = [
  205. 'id' => $value['id'],
  206. 'title' => $addons['title'],
  207. 'name' => $addons['name'],
  208. 'cover' => $addons['cover'],
  209. 'brief_introduction' => $addons['brief_introduction'],
  210. 'addons_status' => $value['expire_time'] != MallAddonsEnum::EXPIRE && $value['expire_time'] <= $time ? MallAddonsEnum::EXPIRE : $value['addons_status'],
  211. 'is_renew' => $value['expire_time'] == MallAddonsEnum::EXPIRE ? 0 : 1,// 是否需要续费,0=否,1=是
  212. 'soon_expire' => $value['expire_time'] > 0 && ($time + $soon_expire_time) > $value['expire_time'] ? 1 : 0,// 即将过期,0=否,1=是
  213. 'expire_time' => $value['expire_time'],
  214. 'course_url' => $addons['course_url'],
  215. ];
  216. }
  217. }
  218. $data['now_time'] = $time;
  219. $data['addons_list'] = $addons_list;
  220. $this->success('获取成功', $data);
  221. } else {
  222. return $this->render($this->action->id);
  223. }
  224. }
  225. // 应用市场
  226. public function actionMarket()
  227. {
  228. if (Yii::$app->request->isAjax) {
  229. if (Yii::$app->request->isPost) {
  230. $params = Yii::$app->request->post();
  231. $res = Yii::$app->services->validate->validate($params, [[['keyword'], 'string']]);
  232. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  233. $cate = Yii::$app->params['addonsGroup'];// 获取应用分类
  234. // 搜索
  235. if (!empty($params['keyword'])) {
  236. $addons_names = Addons::getAddonsByTitle($params['keyword'], 'name');
  237. }
  238. $Where = [['is_online' => 1]];
  239. $is_compliance = Yii::$app->services->mall->isOpenComplianceMode($this->mall_id);
  240. $is_compliance && $Where[] = ['<>', 'group', 'business'];
  241. // 获取用户拥有的应用列表
  242. !empty($addons_names) && $Where[] = ['in', 'name', $addons_names];
  243. $field = 'id,title,name,cover,group,brief_introduction';
  244. $list = Addons::getAddons($Where, ['mallAddons' => function($query){
  245. $query->where(['mall_id' => $this->mall_id]);
  246. }], true, $field, $this->mall_id);// 获取已上架的应用列表
  247. // 获取用户有权限的插件
  248. $valid_addons = self::getUserAuth($list, 'name');
  249. // 应用重组到分类中
  250. $data = [];// 返回值初始化
  251. foreach ($list as $value) {
  252. // if ($value['mallAddons'] && $value['mallAddons']['status'] == StatusEnum::ENABLED) continue;
  253. if (is_array($valid_addons) && !in_array($value['name'], $valid_addons)) continue;
  254. $k = $value['group'];
  255. $data[$k]['title'] = $cate[$k]['title'];
  256. $data[$k]['list'][] = [
  257. 'title' => $value['title'],
  258. 'name' => $value['name'],
  259. 'cover' => $value['cover'],
  260. 'brief_introduction' => $value['brief_introduction'],
  261. 'is_install' => $value['is_install'],
  262. ];
  263. }
  264. $this->success('获取成功', $data);
  265. }
  266. } else {
  267. return $this->render($this->action->id);
  268. }
  269. }
  270. // 安装插件
  271. public function actionInstall()
  272. {
  273. if (Yii::$app->request->isAjax) {
  274. if (Yii::$app->request->isPost) {
  275. $params = Yii::$app->request->post();
  276. $res = Yii::$app->services->validate->validate($params, [[['name'], 'required'], [['name'], 'string']]);
  277. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  278. try {
  279. // 判断商城是否已安装此插件
  280. $market_addons = Addons::getOne([['name' => $params['name'],'is_online' => 1]], true,['mallAddons' => function($query){
  281. $query->where(['mall_id' => $this->mall_id,'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  282. }]);
  283. if (empty($market_addons['mallAddons'])) return $this->error('您并未购买此应用,请前往购买');
  284. if ($market_addons['mallAddons']['status'] != StatusEnum::ENABLED) return $this->error('应用已下架,暂不可安装');
  285. // 安装插入数据
  286. $params['mall_id'] = $this->mall_id;
  287. $params['addons_name'] = $params['name'];
  288. $params['addons_status'] = MallAddonsEnum::INSTALL;
  289. $res = MallAddons::setData($params);
  290. if ($res == false) throw new \Exception(MallAddons::getStaticError());
  291. // 初始化安装插件
  292. $install_key = "addons\\" . $params['name'] . "\Install";
  293. $install = new $install_key();
  294. $install->runApp($this->mall_id, $params['name']);
  295. // 初始化商城事件
  296. //Yii::$app->services->events->mallEventBind($params['name'],$this->mall_id);
  297. // 异步初始化商城
  298. $event = new AddonsInstallEvent($this->mall_id);
  299. $install_addons_data = [];
  300. $install_addons_data['mall_id'] = $this->mall_id;
  301. $install_addons_data['addons_name'] = $params['addons_name'];
  302. Yii::$app->services->events->dispatch($event,$install_addons_data,$event->listeners);
  303. $this->success('安装成功');
  304. } catch (\Exception $e) {
  305. $this->error('安装失败,原因:'.$e->getMessage());
  306. }
  307. }
  308. }
  309. }
  310. // 卸载插件
  311. public function actionUninstall()
  312. {
  313. if (Yii::$app->request->isAjax) {
  314. if (Yii::$app->request->isPost) {
  315. $params = Yii::$app->request->post();
  316. $res = Yii::$app->services->validate->validate($params, [[['name'], 'required'], [['name'], 'string']]);
  317. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  318. try {
  319. // 判断商城是否已安装此插件
  320. $market_addons = Addons::getOne([['is_online' => 1]], true,['mallAddons' => function($query){
  321. $query->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
  322. }]);
  323. if (empty($market_addons)) return $this->error('暂无插件可卸载');
  324. // 卸载数据
  325. $res = MallAddons::Uninstall($this->mall_id, $params['name']);
  326. if ($res == false) throw new \Exception(MallAddons::getStaticError());
  327. if ($params['name'] == 'StoreSteward') {
  328. // 如果卸载插件为门店管家。这删除该商城下所有二维码链接
  329. PosterCode::updateAll(['applet_code' => ''], ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
  330. }
  331. $this->success('卸载成功');
  332. } catch (\Exception $e) {
  333. $this->error('卸载失败,原因:' . $e->getMessage());
  334. }
  335. }
  336. }
  337. }
  338. /**
  339. * 生成扫码订单
  340. * @Author: lun
  341. * @DateTime: 2022/2/25 0025 14:41
  342. * @Copyright: copyright (c) 2021 广东七件事集团
  343. */
  344. public function actionCreateOrder()
  345. {
  346. $request = Yii::$app->request;
  347. if ($request->isAjax && $request->isPost) {
  348. $params = $request->post();
  349. $res = Yii::$app->services->validate->validate($params, [
  350. [['name','setting_index'], 'required'],
  351. [['name'], 'string'],
  352. [['setting_index'], 'integer'],
  353. ]);
  354. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  355. try {
  356. // 判断应用是否可购买
  357. $addons = Addons::getOne([['name' => $params['name'],'is_online' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED]], true);
  358. if (empty($addons)) return $this->error('该应用已下架');
  359. if (empty($addons['price_setting'][$params['setting_index']])) return $this->error('支付价格有误');
  360. $price_setting = Json::decode($addons['price_setting'],true);
  361. // 创建订单
  362. $addons['consumer_name'] = $this->mall['name'];
  363. $order = AddonsOrder::createOrder($this->mall_id, $addons, $price_setting[$params['setting_index']]);
  364. if ($order == false) throw new \Exception(AddonsOrder::getStaticError());
  365. $data['order'] = $order['order'];
  366. if ($order['order']['pay_money'] > 0) {
  367. // 获取交易订单信息
  368. $trade = Yii::$app->services->pay->getByTradeNo($order['trade']['trade_no']);
  369. // 获取支付方式
  370. if (!empty($trade)) {
  371. $payment_arr = AddonsOrder::getPaymentArr($addons['payment_type'], $trade);
  372. $data['payment_arr'] = $payment_arr;
  373. }
  374. }
  375. // 修改订单
  376. $res = AddonsOrder::setData([
  377. 'id' => $order['order']['order_id'],
  378. 'out_trade_no' => $order['trade']['trade_no'],
  379. 'operator_id' => $this->admin->id,
  380. 'operator_username' => $this->admin->account,
  381. 'extra_info' => Json::encode(['payment_arr' => $payment_arr ?? []]),
  382. ]);
  383. if ($res == false) throw new \Exception(AddonsOrder::getStaticError());
  384. $this->success('获取成功', $data);
  385. } catch (\Exception $e) {
  386. $this->error('文件:' . $e->getFile() . ',第'. $e->getLine() .'行,原因:'. $e->getMessage());
  387. }
  388. }
  389. }
  390. /**
  391. * 确认订单
  392. * @Author: ltm
  393. * @DateTime: 2022/3/2 0002 9:27
  394. * @Copyright: copyright (c) 2021 广东七件事集团
  395. * @param $data
  396. * @return string
  397. */
  398. public function actionConfirmOrder(){
  399. $request = Yii::$app->request;
  400. $params = $request->get();
  401. if ($request->isAjax && $request->isGet) {
  402. $res = Yii::$app->services->validate->validate($params, [
  403. [['name','setting_index'], 'required'],
  404. [['name'], 'string'],
  405. [['setting_index'], 'integer'],
  406. [['order_id'],'safe'],
  407. ]);
  408. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  409. // 查询插件信息
  410. $addons = Addons::getOne([['name' => $params['name'],'is_online' => 1]], true);
  411. $addons['mall_name'] = $this->mall['name'];
  412. $addons['setting_index'] = $params['setting_index'];
  413. if (empty($addons)) return $this->error('该应用已下架');
  414. if (empty($addons['price_setting'][$params['setting_index']])) return $this->error('该应用未开放购买');
  415. $addons['price_setting'] = Json::decode($addons['price_setting'],true);
  416. //订单信息
  417. if($params['order_id']){
  418. $order = AddonsOrder::getOne([['id'=>$params['order_id'],'status' => StatusEnum::ENABLED]],true);
  419. $order['mall_name'] = $this->mall['name'];
  420. if (empty($addons)) return $this->error('订单不存在或已经删除');
  421. $order['extra_info'] = Json::decode($addons['extra_info'],true);
  422. $addons['order'] = $order;
  423. }
  424. //获取应用服务协议
  425. $payagreement = PlatformSetting::getConfByGroup(['addons'], true);
  426. $addons['payagreement'] = $payagreement['addons']['payagreement']['value'];
  427. $this->success('获取成功', $addons);
  428. }
  429. return $this->render($this->action->id);
  430. }
  431. /**
  432. * 关闭订单
  433. * @Author: lun
  434. * @DateTime: 2022/3/3 0003 19:48
  435. * @Copyright: copyright (c) 2021 广东七件事集团
  436. * @return mixed|void
  437. * @throws \Exception
  438. */
  439. public function actionOrderClose(){
  440. $request = Yii::$app->request;
  441. $params = $request->get();
  442. if ($request->isAjax && $request->isGet) {
  443. $res = Yii::$app->services->validate->validate($params, [
  444. [['id'], 'required'],
  445. [['id'], 'integer'],
  446. ]);
  447. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  448. // 查询插件信息
  449. try {
  450. $params['order_status'] = OrderStatusEnum::REPEAL;
  451. $params['mall_id'] = $this->mall_id;
  452. $params['close_time'] = time();
  453. $order = AddonsOrder::setData($params);
  454. $platformOrder = PlatformOrder::findOne(['order_id' => $params['id']]);
  455. $platformOrder -> order_status = OrderStatusEnum::REPEAL;
  456. $platformOrder -> updated_at = time();
  457. if($platformOrder) $platformOrder->save();
  458. if ($order == false) throw new \Exception(AddonsOrder::getStaticError());
  459. // 统计订单数据
  460. $res = PlatformOrderStatisticsDay::setData([
  461. 'cancel_order_num' => 1,
  462. 'cancel_order_money' => $order['order_price'],
  463. 'cancel_goods_num' => 1,
  464. ]);
  465. if ($res == false) throw new \Exception(PlatformOrderStatisticsDay::getStaticError());
  466. $this->success('操作成功');
  467. } catch (\Exception $e) {
  468. $this->error('文件:' . $e->getFile() . ',第'. $e->getLine() .'行,原因:'. $e->getMessage());
  469. }
  470. }
  471. }
  472. /**
  473. * @Author: ltm 订单详情
  474. * @DateTime: 2022/3/3 0003 9:57
  475. * @Copyright: copyright (c) 2021 广东七件事集团
  476. * @param $data
  477. * @return string
  478. */
  479. public function actionOrderDetail(){
  480. $request = Yii::$app->request;
  481. $params = $request->get();
  482. if ($request->isAjax && $request->isGet) {
  483. $res = Yii::$app->services->validate->validate($params, [
  484. [['order_id'], 'required'],
  485. [['order_id'], 'integer'],
  486. ]);
  487. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  488. // 查询插件信息
  489. $with = ['addons' => function($query){
  490. $query->select('title,id,brief_introduction,cover');
  491. }];
  492. $addons = AddonsOrder::getOne([['id'=>$params['order_id'],'status' => StatusEnum::ENABLED]],true,$with);
  493. $addons['mall_name'] = $this->mall['name'];
  494. if (empty($addons)) return $this->error('订单不存在或已经删除');
  495. $addons['extra_info'] = Json::decode($addons['extra_info'],true);
  496. $this->success('获取成功', $addons);
  497. }
  498. return $this->render($this->action->id);
  499. }
  500. /**
  501. * @Author: ltm 订单支付
  502. * @DateTime: 2022/3/2 0002 16:37
  503. * @Copyright: copyright (c) 2021 广东七件事集团
  504. * @param $data
  505. * @return string
  506. */
  507. public function actionConfirmPay(){
  508. $request = Yii::$app->request;
  509. $params = $request->get();
  510. if ($request->isAjax && $request->isGet) {
  511. $res = Yii::$app->services->validate->validate($params, [
  512. [['order_id'], 'required'],
  513. [['order_id'], 'integer'],
  514. ]);
  515. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  516. // 查询插件信息
  517. $with = ['addons' => function($query){
  518. $query->select('title,id,brief_introduction,cover');
  519. }];
  520. $addons = AddonsOrder::getOne([['id'=>$params['order_id'],'status' => StatusEnum::ENABLED]],true,$with);
  521. $addons['mall_name'] = $this->mall['name'];
  522. if (empty($addons)) return $this->error('订单不存在或已经删除');
  523. $addons['extra_info'] = Json::decode($addons['extra_info'],true);
  524. $this->success('获取成功', $addons);
  525. }
  526. return $this->render($this->action->id);
  527. }
  528. /**
  529. * 支付成功页面
  530. * @Author: ltm
  531. * @DateTime: 2022/3/4 0004 14:29
  532. * @Copyright: copyright (c) 2021 广东七件事集团
  533. * @return mixed|string|void
  534. */
  535. public function actionConfirmSuccess(){
  536. $request = Yii::$app->request;
  537. $params = $request->get();
  538. if ($request->isAjax && $request->isGet) {
  539. $res = Yii::$app->services->validate->validate($params, [
  540. [['order_id'], 'required'],
  541. [['order_id'], 'integer'],
  542. ]);
  543. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  544. // 查询插件信息
  545. $addons = AddonsOrder::getOne([['id'=>$params['order_id'],'status' => StatusEnum::ENABLED]],true);
  546. $addons['mall_name'] = $this->mall['name'];
  547. if (empty($addons)) return $this->error('订单不存在或已经删除');
  548. $addons['extra_info'] = Json::decode($addons['extra_info'],true);
  549. $this->success('获取成功', $addons);
  550. }
  551. return $this->render($this->action->id);
  552. }
  553. /**
  554. * 获取订单列表
  555. * @Author: lun
  556. * @DateTime: 2022/3/2 0002 15:21
  557. * @Copyright: copyright (c) 2021 广东七件事集团
  558. * @return mixed|void
  559. */
  560. public function actionOrderList()
  561. {
  562. $request = Yii::$app->request;
  563. if ($request->isAjax && $request->isPost) {
  564. $params = $request->post();
  565. $res = Yii::$app->services->validate->validate($params, [
  566. [['datetime'], 'each', 'rule' => ['string']],
  567. [['order_status'], 'integer'],
  568. ]);
  569. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  570. $params = AddonsOrder::exceptNullVal($params);
  571. $where[] = ['=', 'mall_id', $this->mall_id];
  572. $where[] = ['=', 'status', StatusEnum::ENABLED];
  573. isset($params['order_status']) && $where[] = ['=', 'order_status', $params['order_status']];
  574. if (!empty($params['datetime'])) {
  575. $start_time = strtotime($params['datetime'][0]);
  576. $end_time = strtotime($params['datetime'][1]);
  577. $where[] = ['between', 'created_at', $start_time, $end_time];
  578. }
  579. $with = ['addons' => function($query){
  580. $query->select("title,name,cover,brief_introduction");
  581. }];
  582. $order = 'id desc,created_at desc';
  583. $select = "id,order_no,addons_name,unit,duration,pay_money,order_price,order_status,pay_status,payment_type,operator_username,expire_time,created_at";
  584. $params = compact('where','order','select','with');
  585. $list = AddonsOrder::listPage($params, false, true);
  586. $this->success('获取成功', $list);
  587. } else {
  588. return $this->render($this->action->id);
  589. }
  590. }
  591. // 获取用户有效的插件
  592. private function getUserAuth($list, $column = 'addons_name')
  593. {
  594. // 获取用户权限
  595. $admin = Yii::$app->user->identity;
  596. // 超级管理员,管理员无需校验
  597. if (in_array($admin->admin_type, [Admin::ADMIN_TYPE_SUPER])) return true;
  598. // 商场管理员无需校验
  599. if (Yii::$app->services->rbacAuthRole->isMallAdmin()) return true;
  600. // 获取角色id
  601. $role_id = AdminRole::find()->select('role_id')->where(['admin_id' => $admin->id])->scalar();
  602. // 获取插件名称
  603. $addons_names = array_column($list, $column);
  604. $item_ids = AuthItem::getAddonsItemIds($addons_names);// 获取插件对应的权限路径item_id
  605. // 获取有效插件
  606. $valid_addons = AuthItemChild::find()->select('addons_name')->where(['role_id' => $role_id, 'item_id' => $item_ids])->column();
  607. return $valid_addons;
  608. }
  609. private function getUserAuthAddonsDefaultMenu($list, $column = 'addons_name')
  610. {
  611. // 获取用户权限
  612. $admin = Yii::$app->user->identity;
  613. // 超级管理员,管理员无需校验
  614. if (in_array($admin->admin_type, [Admin::ADMIN_TYPE_SUPER, Admin::ADMIN_TYPE_AGENT])) return true;
  615. // 获取角色id
  616. $role_id = AdminRole::find()->select('role_id')->where(['admin_id' => $admin->id])->scalar();
  617. // 获取插件名称
  618. $addons_names = array_column($list, $column);
  619. $item_id_arr = AuthItem::find()->select('id')->where(['addons_name' => $addons_names ,'is_addon' => 1, 'level' => 2, 'status' => StatusEnum::ENABLED])->column();
  620. // 获取有效插件
  621. $valid_addons_list = AuthItemChild::find()->select('*')->where(['role_id' => $role_id, 'item_id' => $item_id_arr])
  622. ->orderBy('item_id asc')
  623. ->asArray()->all();
  624. $valid_addons_menu_arr = [];
  625. foreach ($valid_addons_list as $val){
  626. if(!isset($valid_addons_menu_arr[$val['addons_name']])&&strpos($val['name'],'default')!==false){
  627. $valid_addons_menu_arr[$val['addons_name']] = $val['name'];
  628. }
  629. }
  630. return $valid_addons_menu_arr;
  631. }
  632. /**
  633. * 设置应用置顶
  634. *
  635. * @return void
  636. */
  637. public function actionToTop()
  638. {
  639. if ($this->request->isAjax) {
  640. $name = $this->request->post('name');
  641. $service = new TopAddonsService(['mall_id' => $this->mall_id]);
  642. return $service->setTopAddons($name)
  643. ? $this->success('ok')
  644. : $this->error($service->error);
  645. }
  646. }
  647. /**
  648. * 取消应用置顶
  649. *
  650. * @return void
  651. */
  652. public function actionToBottom()
  653. {
  654. if ($this->request->isAjax) {
  655. $name = $this->request->post('name');
  656. $service = new TopAddonsService(['mall_id' => $this->mall_id]);
  657. return $service->setBottomAddons($name)
  658. ? $this->success('ok')
  659. : $this->error($service->error);
  660. }
  661. }
  662. }