Common.php 17 KB

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