Common.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller;
  12. use app\services\system\config\SystemConfigServices;
  13. use app\services\system\config\SystemConfigTabServices;
  14. use app\services\system\SystemAuthServices;
  15. use app\services\order\StoreOrderServices;
  16. use app\services\product\product\StoreProductServices;
  17. use app\services\product\product\StoreProductReplyServices;
  18. use app\services\system\UpgradeServices;
  19. use app\services\user\UserExtractServices;
  20. use app\services\product\sku\StoreProductAttrValueServices;
  21. use app\services\system\SystemMenusServices;
  22. use app\services\user\UserServices;
  23. use crmeb\services\CacheService;
  24. use crmeb\services\HttpService;
  25. use think\facade\Config;
  26. /**
  27. * 公共接口基类 主要存放公共接口
  28. * Class Common
  29. * @package app\adminapi\controller
  30. */
  31. class Common extends AuthController
  32. {
  33. /**
  34. * 获取logo
  35. * @return mixed
  36. */
  37. public function getLogo()
  38. {
  39. return app('json')->success([
  40. 'logo' => sys_config('site_logo'),
  41. 'logo_square' => sys_config('site_logo_square'),
  42. 'site_name' => sys_config('site_name')
  43. ]);
  44. }
  45. /**
  46. * 获取授权信息
  47. * @return mixed
  48. */
  49. public function auth()
  50. {
  51. $version = get_crmeb_version();
  52. $host = $this->request->host();
  53. // 正常域名
  54. $res = HttpService::request('http://authorize.crmeb.net/api/auth_cert_query', 'post', [
  55. 'domain_name' => $host,
  56. 'label' => 34,
  57. 'version' => $version
  58. ]);
  59. $res = $res ? json_decode($res, true) : [];
  60. //兼容test.
  61. if (!isset($res['data']['status']) || $res['data']['status'] !== 1) {
  62. $host = str_replace('test.', '', $host);
  63. $res = HttpService::request('http://authorize.crmeb.net/api/auth_cert_query', 'post', [
  64. 'domain_name' => $host,
  65. 'label' => 34,
  66. 'version' => $version
  67. ]);
  68. $res = $res ? json_decode($res, true) : [];
  69. }
  70. //如果是主域名兼容www.
  71. if (!isset($res['data']['status']) || $res['data']['status'] !== 1) {
  72. $host = str_replace('www.', '', $host);
  73. $res = HttpService::request('http://authorize.crmeb.net/api/auth_cert_query', 'post', [
  74. 'domain_name' => $host,
  75. 'label' => 34,
  76. 'version' => $version
  77. ]);
  78. $res = $res ? json_decode($res, true) : [];
  79. }
  80. //升级状态
  81. // /** @var UpgradeServices $upgradeServices */
  82. // $upgradeServices = app()->make(UpgradeServices::class);
  83. // $upgradeStatus = $upgradeServices->getUpgradeStatus();
  84. $status = $res['data']['status'] ?? -9;
  85. switch ((int)$status) {
  86. case 1:
  87. //审核成功
  88. $authCode = $res['data']['auth_code'] ?? '';
  89. $autoContent = $res['data']['auto_content'] ?? '';
  90. try {
  91. /** @var SystemConfigServices $services */
  92. $services = app()->make(SystemConfigServices::class);
  93. if ($services->count(['menu_name' => 'cert_crmeb'])) {
  94. $services->update(['menu_name' => 'cert_crmeb'], ['value' => json_encode($autoContent . ',' . $authCode)]);
  95. } else {
  96. $services->save([
  97. 'menu_name' => 'cert_crmeb',
  98. 'type' => 'text',
  99. 'input_type' => 'input',
  100. 'config_tab_id' => 1,
  101. 'value' => json_encode($autoContent . ',' . $authCode),
  102. 'status' => 2,
  103. 'info' => '授权密钥'
  104. ]);
  105. }
  106. } catch (\Throwable $e) {
  107. return app('json')->fail(400330);
  108. }
  109. return app('json')->success(['status' => 1, 'copyright' => $res['data']['copyright'], 'authCode' => $authCode, 'day' => 0, 'force_reminder' => $upgradeStatus['force_reminder'] ?? 0]);
  110. default:
  111. return app('json')->success(['status' => -9, 'force_reminder' => $upgradeStatus['force_reminder'] ?? 0]);
  112. }
  113. }
  114. /**
  115. * 申请授权
  116. * @return mixed
  117. */
  118. public function auth_apply(SystemAuthServices $services)
  119. {
  120. $data = $this->request->postMore([
  121. ['company_name', ''],
  122. ['domain_name', ''],
  123. ['order_id', ''],
  124. ['phone', ''],
  125. ['label', 19],
  126. ['captcha', ''],
  127. ]);
  128. if (!$data['company_name']) {
  129. return app('json')->fail(400331);
  130. }
  131. if (!$data['domain_name']) {
  132. return app('json')->fail(400332);
  133. }
  134. if (!$data['phone']) {
  135. return app('json')->fail(400333);
  136. }
  137. if (!$data['order_id']) {
  138. return app('json')->fail(400334);
  139. }
  140. if (!$data['captcha']) {
  141. return app('json')->fail(400137);
  142. }
  143. $services->authApply($data);
  144. return app('json')->success(400335);
  145. }
  146. /**
  147. * 首页头部统计数据
  148. * @return mixed
  149. */
  150. public function homeStatics()
  151. {
  152. /** @var StoreOrderServices $orderServices */
  153. $orderServices = app()->make(StoreOrderServices::class);
  154. $info = $orderServices->homeStatics();
  155. return app('json')->success(compact('info'));
  156. }
  157. //增长率
  158. public function growth($nowValue, $lastValue)
  159. {
  160. if ($lastValue == 0 && $nowValue == 0) return 0;
  161. if ($lastValue == 0) return round($nowValue, 2);
  162. if ($nowValue == 0) return -round($lastValue, 2);
  163. return bcmul(bcdiv((bcsub($nowValue, $lastValue, 2)), $lastValue, 2), 100, 2);
  164. }
  165. /**
  166. * 订单图表
  167. */
  168. public function orderChart()
  169. {
  170. $cycle = $this->request->param('cycle') ?: 'thirtyday';//默认30天
  171. /** @var StoreOrderServices $orderServices */
  172. $orderServices = app()->make(StoreOrderServices::class);
  173. $chartdata = $orderServices->orderCharts($cycle);
  174. return app('json')->success($chartdata);
  175. }
  176. /**
  177. * 用户图表
  178. */
  179. public function userChart()
  180. {
  181. /** @var UserServices $uServices */
  182. $uServices = app()->make(UserServices::class);
  183. $chartdata = $uServices->userChart();
  184. return app('json')->success($chartdata);
  185. }
  186. /**
  187. * 交易额排行
  188. * @return mixed
  189. */
  190. public function purchaseRanking()
  191. {
  192. /** @var StoreProductAttrValueServices $valueServices */
  193. $valueServices = app()->make(StoreProductAttrValueServices::class);
  194. $list = $valueServices->purchaseRanking();
  195. return app('json')->success(compact('list'));
  196. }
  197. /**
  198. * 待办事统计
  199. * @return mixed
  200. */
  201. public function jnotice()
  202. {
  203. /** @var StoreOrderServices $orderServices */
  204. $orderServices = app()->make(StoreOrderServices::class);
  205. $data['ordernum'] = $orderServices->storeOrderCount();
  206. $store_stock = sys_config('store_stock');
  207. if ($store_stock < 0) $store_stock = 2;
  208. /** @var StoreProductServices $storeServices */
  209. $storeServices = app()->make(StoreProductServices::class);
  210. $data['inventory'] = $storeServices->count(['type' => 5, 'store_stock' => $store_stock]);//警戒库存
  211. /** @var StoreProductReplyServices $replyServices */
  212. $replyServices = app()->make(StoreProductReplyServices::class);
  213. $data['commentnum'] = $replyServices->replyCount();
  214. /** @var UserExtractServices $extractServices */
  215. $extractServices = app()->make(UserExtractServices::class);
  216. $data['reflectnum'] = $extractServices->userExtractCount();//提现
  217. $data['msgcount'] = intval($data['ordernum']) + intval($data['inventory']) + intval($data['commentnum']) + intval($data['reflectnum']);
  218. $data['newOrderId'] = $orderServices->newOrderId(1);
  219. if (count($data['newOrderId'])) $orderServices->newOrderUpdate($data['newOrderId']);
  220. $value = [];
  221. if ($data['ordernum'] != 0) {
  222. $value[] = [
  223. 'title' => "您有$data[ordernum]个待发货的订单",
  224. 'type' => 1,
  225. 'url' => '/' . Config::get('app.admin_prefix', 'admin') . '/order/list?status=1'
  226. ];
  227. }
  228. if ($data['inventory'] != 0) {
  229. $value[] = [
  230. 'title' => "您有$data[inventory]个商品库存预警",
  231. 'type' => 2,
  232. 'url' => '/' . Config::get('app.admin_prefix', 'admin') . '/product/product_list?type=5',
  233. ];
  234. }
  235. if ($data['commentnum'] != 0) {
  236. $value[] = [
  237. 'title' => "您有$data[commentnum]条评论待回复",
  238. 'type' => 3,
  239. 'url' => '/' . Config::get('app.admin_prefix', 'admin') . '/product/product_reply?is_reply=0'
  240. ];
  241. }
  242. if ($data['reflectnum'] != 0) {
  243. $value[] = [
  244. 'title' => "您有$data[reflectnum]个提现申请待审核",
  245. 'type' => 4,
  246. 'url' => '/' . Config::get('app.admin_prefix', 'admin') . '/finance/user_extract/index?status=0',
  247. ];
  248. }
  249. return app('json')->success($this->noticeData($value));
  250. }
  251. /**
  252. * 消息返回格式
  253. * @param array $data
  254. * @return array
  255. */
  256. public function noticeData(array $data): array
  257. {
  258. // 消息图标
  259. $iconColor = [
  260. // 邮件 消息
  261. 'mail' => [
  262. 'icon' => 'md-mail',
  263. 'color' => '#3391e5'
  264. ],
  265. // 普通 消息
  266. 'bulb' => [
  267. 'icon' => 'md-bulb',
  268. 'color' => '#87d068'
  269. ],
  270. // 警告 消息
  271. 'information' => [
  272. 'icon' => 'md-information',
  273. 'color' => '#fe5c57'
  274. ],
  275. // 关注 消息
  276. 'star' => [
  277. 'icon' => 'md-star',
  278. 'color' => '#ff9900'
  279. ],
  280. // 申请 消息
  281. 'people' => [
  282. 'icon' => 'md-people',
  283. 'color' => '#f06292'
  284. ],
  285. ];
  286. // 消息类型
  287. $type = array_keys($iconColor);
  288. // 默认数据格式
  289. $default = [
  290. 'icon' => 'md-bulb',
  291. 'iconColor' => '#87d068',
  292. 'title' => '',
  293. 'url' => '',
  294. 'type' => 'bulb',
  295. 'read' => 0,
  296. 'time' => 0
  297. ];
  298. $value = [];
  299. foreach ($data as $item) {
  300. $val = array_merge($default, $item);
  301. if (isset($item['type']) && in_array($item['type'], $type)) {
  302. $val['type'] = $item['type'];
  303. $val['iconColor'] = $iconColor[$item['type']]['color'] ?? '';
  304. $val['icon'] = $iconColor[$item['type']]['icon'] ?? '';
  305. }
  306. $value[] = $val;
  307. }
  308. return $value;
  309. }
  310. /**
  311. * 格式化菜单
  312. * @return mixed
  313. * @throws \think\db\exception\DataNotFoundException
  314. * @throws \think\db\exception\DbException
  315. * @throws \think\db\exception\ModelNotFoundException
  316. */
  317. public function menusList()
  318. {
  319. /** @var SystemMenusServices $menusServices */
  320. $menusServices = app()->make(SystemMenusServices::class);
  321. $list = $menusServices->getSearchList();
  322. $counts = $menusServices->getColumn([
  323. ['is_show', '=', 1],
  324. ['auth_type', '=', 1],
  325. ['is_del', '=', 0],
  326. ['is_show_path', '=', 0],
  327. ], 'pid');
  328. $data = [];
  329. foreach ($list as $key => $item) {
  330. $pid = $item->getData('pid');
  331. $data[$key] = json_decode($item, true);
  332. $data[$key]['pid'] = $pid;
  333. $data[$key]['menu_path'] = '/' . config('app.admin_prefix', 'admin') . $item['menu_path'];
  334. if (in_array($item->id, $counts)) {
  335. $data[$key]['type'] = 1;
  336. } else {
  337. $data[$key]['type'] = 0;
  338. }
  339. }
  340. return app('json')->success(sort_list_tier($data));
  341. }
  342. /**
  343. * 查询购买版权
  344. * @return mixed
  345. */
  346. public function copyright()
  347. {
  348. $copyrightContext = sys_config('nncnL_crmeb_copyright', '');
  349. $copyrightImage = sys_config('nncnL_crmeb_copyright_image', '');
  350. return app('json')->success(compact('copyrightContext', 'copyrightImage'));
  351. }
  352. /**
  353. * 保存版权
  354. * @return mixed
  355. */
  356. public function saveCopyright()
  357. {
  358. [$copyright, $copyrightImg] = $this->request->postMore(['copyright', 'copyright_img',], true);
  359. /** @var SystemConfigServices $services */
  360. $services = app()->make(SystemConfigServices::class);
  361. if ($services->count(['menu_name' => 'nncnL_crmeb_copyright'])) {
  362. $services->update(['menu_name' => 'nncnL_crmeb_copyright'], ['value' => json_encode($copyright)]);
  363. } else {
  364. $services->save([
  365. 'menu_name' => 'nncnL_crmeb_copyright',
  366. 'type' => 'text',
  367. 'input_type' => 'input',
  368. 'config_tab_id' => 1,
  369. 'value' => json_encode($copyright),
  370. 'status' => 2,
  371. 'info' => ''
  372. ]);
  373. }
  374. if ($services->count(['menu_name' => 'nncnL_crmeb_copyright_image'])) {
  375. $services->update(['menu_name' => 'nncnL_crmeb_copyright_image'], ['value' => json_encode($copyrightImg)]);
  376. } else {
  377. $services->save([
  378. 'menu_name' => 'nncnL_crmeb_copyright_image',
  379. 'type' => 'text',
  380. 'input_type' => 'input',
  381. 'config_tab_id' => 1,
  382. 'value' => json_encode($copyrightImg),
  383. 'status' => 2,
  384. 'info' => ''
  385. ]);
  386. }
  387. CacheService::clear();
  388. return app('json')->success(100000);
  389. }
  390. /**
  391. * 系统搜索菜单
  392. * @return \think\Response
  393. * @author 吴汐
  394. * @email 442384644@qq.com
  395. * @date 2024/2/1
  396. */
  397. public function menusSearch()
  398. {
  399. // 从请求中获取关键字
  400. [$keyword] = $this->request->postMore([
  401. ['keyword', ''],
  402. ], true);
  403. // 获取系统菜单服务实例
  404. $menusServices = app()->make(SystemMenusServices::class);
  405. // 查询菜单列表
  406. $menusList = $menusServices->selectList([['menu_name', 'like', '%' . $keyword . '%'], ['auth_type', '=', 1]], 'menu_name as title,menu_path as path,id')->toArray();
  407. // 获取系统配置服务实例
  408. $configServices = app()->make(SystemConfigServices::class);
  409. // 获取系统配置标签服务实例
  410. $configTabServices = app()->make(SystemConfigTabServices::class);
  411. // 查询配置项列表
  412. $configList = $configServices->selectList([['info', 'like', '%' . $keyword . '%']], 'info as title,config_tab_id')->toArray();
  413. $configTabList = $configTabServices->selectList([['title', 'like', '%' . $keyword . '%']], 'title,id as config_tab_id')->toArray();
  414. $configAllList = array_merge($configList, $configTabList);
  415. // 获取配置项对应的标签ID
  416. $tabIds = array_unique(array_column($configAllList, 'config_tab_id'));
  417. // 查询配置项标签列表
  418. $tabList = $configTabServices->getColumn([['id', 'in', $tabIds]], 'menus_id', 'id');
  419. // 将配置项标签列表中的菜单ID与配置项列表中的菜单ID对应起来
  420. foreach ($configAllList as &$item1) {
  421. $item1['menus_id'] = $tabList[$item1['config_tab_id']] ?? 0;
  422. }
  423. // 获取配置项标签对应的菜单ID
  424. $configTabIds = array_values($tabList);
  425. // 查询配置项标签对应的菜单列表
  426. $configMenusList = $menusServices->getColumn([['id', 'in', $configTabIds]], 'menu_name as title,menu_path as path,id', 'id');
  427. // 将配置项列表中的菜单ID与配置项标签对应的菜单ID对应起来
  428. foreach ($configAllList as $item2) {
  429. $menusList[] = [
  430. 'title' => $configMenusList[$item2['menus_id']]['title'] . '-' . $item2['title'],
  431. 'path' => $configMenusList[$item2['menus_id']]['path'] . '?tab_id=' . $item2['config_tab_id'],
  432. 'id' => $configMenusList[$item2['menus_id']]['id']
  433. ];
  434. }
  435. // 将菜单列表中的路径前缀添加到每个菜单项的 path 属性上
  436. foreach ($menusList as &$item) {
  437. $item['path'] = '/' . Config::get('app.admin_prefix', 'admin') . $item['path'];
  438. }
  439. // 返回 JSON 格式的菜单列表
  440. return app('json')->success($menusList);
  441. }
  442. }