Common.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. public function growth($nowValue, $lastValue)
  135. {
  136. if ($lastValue == 0 && $nowValue == 0) return 0;
  137. if ($lastValue == 0) return round($nowValue, 2);
  138. if ($nowValue == 0) return -round($lastValue, 2);
  139. return bcmul(bcdiv((bcsub($nowValue, $lastValue, 2)), $lastValue, 2), 100, 2);
  140. }
  141. /**
  142. * 订单图表
  143. */
  144. public function orderChart()
  145. {
  146. $cycle = $this->request->param('cycle') ?: 'thirtyday';//默认30天
  147. /** @var StoreOrderServices $orderServices */
  148. $orderServices = app()->make(StoreOrderServices::class);
  149. $chartdata = $orderServices->orderCharts($cycle);
  150. return app('json')->success($chartdata);
  151. }
  152. /**
  153. * 用户图表
  154. */
  155. public function userChart()
  156. {
  157. /** @var UserServices $uServices */
  158. $uServices = app()->make(UserServices::class);
  159. $chartdata = $uServices->userChart();
  160. return app('json')->success($chartdata);
  161. }
  162. /**
  163. * 交易额排行
  164. * @return mixed
  165. */
  166. public function purchaseRanking()
  167. {
  168. /** @var StoreProductAttrValueServices $valueServices */
  169. $valueServices = app()->make(StoreProductAttrValueServices::class);
  170. $list = $valueServices->purchaseRanking();
  171. return app('json')->success(compact('list'));
  172. }
  173. /**
  174. * 待办事统计
  175. * @return mixed
  176. */
  177. public function jnotice()
  178. {
  179. /** @var StoreOrderServices $orderServices */
  180. $orderServices = app()->make(StoreOrderServices::class);
  181. $data['ordernum'] = $orderServices->storeOrderCount();
  182. $store_stock = sys_config('store_stock');
  183. if ($store_stock < 0) $store_stock = 2;
  184. /** @var StoreProductServices $storeServices */
  185. $storeServices = app()->make(StoreProductServices::class);
  186. $data['inventory'] = $storeServices->count(['type' => 5, 'store_stock' => $store_stock]);//警戒库存
  187. /** @var StoreProductReplyServices $replyServices */
  188. $replyServices = app()->make(StoreProductReplyServices::class);
  189. $data['commentnum'] = $replyServices->replyCount();
  190. /** @var UserExtractServices $extractServices */
  191. $extractServices = app()->make(UserExtractServices::class);
  192. $data['reflectnum'] = $extractServices->userExtractCount();//提现
  193. $data['msgcount'] = intval($data['ordernum']) + intval($data['inventory']) + intval($data['commentnum']) + intval($data['reflectnum']);
  194. $data['newOrderId'] = $orderServices->newOrderId(1);
  195. if (count($data['newOrderId'])) $orderServices->newOrderUpdate($data['newOrderId']);
  196. $value = [];
  197. if ($data['ordernum'] != 0) {
  198. $value[] = [
  199. 'title' => "您有$data[ordernum]个待发货的订单",
  200. 'type' => 1,
  201. 'url' => '/' . Config::get('app.admin_prefix', 'admin') . '/order/list?status=1'
  202. ];
  203. }
  204. if ($data['inventory'] != 0) {
  205. $value[] = [
  206. 'title' => "您有$data[inventory]个商品库存预警",
  207. 'type' => 2,
  208. 'url' => '/' . Config::get('app.admin_prefix', 'admin') . '/product/product_list?type=5',
  209. ];
  210. }
  211. if ($data['commentnum'] != 0) {
  212. $value[] = [
  213. 'title' => "您有$data[commentnum]条评论待回复",
  214. 'type' => 3,
  215. 'url' => '/' . Config::get('app.admin_prefix', 'admin') . '/product/product_reply?is_reply=0'
  216. ];
  217. }
  218. if ($data['reflectnum'] != 0) {
  219. $value[] = [
  220. 'title' => "您有$data[reflectnum]个提现申请待审核",
  221. 'type' => 4,
  222. 'url' => '/' . Config::get('app.admin_prefix', 'admin') . '/finance/user_extract/index?status=0',
  223. ];
  224. }
  225. return app('json')->success($this->noticeData($value));
  226. }
  227. /**
  228. * 消息返回格式
  229. * @param array $data
  230. * @return array
  231. */
  232. public function noticeData(array $data): array
  233. {
  234. // 消息图标
  235. $iconColor = [
  236. // 邮件 消息
  237. 'mail' => [
  238. 'icon' => 'md-mail',
  239. 'color' => '#3391e5'
  240. ],
  241. // 普通 消息
  242. 'bulb' => [
  243. 'icon' => 'md-bulb',
  244. 'color' => '#87d068'
  245. ],
  246. // 警告 消息
  247. 'information' => [
  248. 'icon' => 'md-information',
  249. 'color' => '#fe5c57'
  250. ],
  251. // 关注 消息
  252. 'star' => [
  253. 'icon' => 'md-star',
  254. 'color' => '#ff9900'
  255. ],
  256. // 申请 消息
  257. 'people' => [
  258. 'icon' => 'md-people',
  259. 'color' => '#f06292'
  260. ],
  261. ];
  262. // 消息类型
  263. $type = array_keys($iconColor);
  264. // 默认数据格式
  265. $default = [
  266. 'icon' => 'md-bulb',
  267. 'iconColor' => '#87d068',
  268. 'title' => '',
  269. 'url' => '',
  270. 'type' => 'bulb',
  271. 'read' => 0,
  272. 'time' => 0
  273. ];
  274. $value = [];
  275. foreach ($data as $item) {
  276. $val = array_merge($default, $item);
  277. if (isset($item['type']) && in_array($item['type'], $type)) {
  278. $val['type'] = $item['type'];
  279. $val['iconColor'] = $iconColor[$item['type']]['color'] ?? '';
  280. $val['icon'] = $iconColor[$item['type']]['icon'] ?? '';
  281. }
  282. $value[] = $val;
  283. }
  284. return $value;
  285. }
  286. /**
  287. * 格式化菜单
  288. * @return mixed
  289. * @throws \think\db\exception\DataNotFoundException
  290. * @throws \think\db\exception\DbException
  291. * @throws \think\db\exception\ModelNotFoundException
  292. */
  293. public function menusList()
  294. {
  295. /** @var SystemMenusServices $menusServices */
  296. $menusServices = app()->make(SystemMenusServices::class);
  297. $list = $menusServices->getSearchList();
  298. $counts = $menusServices->getColumn([
  299. ['is_show', '=', 1],
  300. ['auth_type', '=', 1],
  301. ['is_del', '=', 0],
  302. ['is_show_path', '=', 0],
  303. ], 'pid');
  304. $data = [];
  305. foreach ($list as $key => $item) {
  306. $pid = $item->getData('pid');
  307. $data[$key] = json_decode($item, true);
  308. $data[$key]['pid'] = $pid;
  309. $data[$key]['menu_path'] = '/' . config('app.admin_prefix', 'admin') . $item['menu_path'];
  310. if (in_array($item->id, $counts)) {
  311. $data[$key]['type'] = 1;
  312. } else {
  313. $data[$key]['type'] = 0;
  314. }
  315. }
  316. return app('json')->success(sort_list_tier($data));
  317. }
  318. /**
  319. * 查询购买版权
  320. * @return mixed
  321. */
  322. public function copyright()
  323. {
  324. $copyrightContext = sys_config('nncnL_crmeb_copyright', '');
  325. $copyrightImage = sys_config('nncnL_crmeb_copyright_image', '');
  326. return app('json')->success(compact('copyrightContext', 'copyrightImage'));
  327. }
  328. /**
  329. * 保存版权
  330. * @return mixed
  331. */
  332. public function saveCopyright()
  333. {
  334. [$copyright, $copyrightImg] = $this->request->postMore(['copyright', 'copyright_img',], true);
  335. /** @var SystemConfigServices $services */
  336. $services = app()->make(SystemConfigServices::class);
  337. if ($services->count(['menu_name' => 'nncnL_crmeb_copyright'])) {
  338. $services->update(['menu_name' => 'nncnL_crmeb_copyright'], ['value' => json_encode($copyright)]);
  339. } else {
  340. $services->save([
  341. 'menu_name' => 'nncnL_crmeb_copyright',
  342. 'type' => 'text',
  343. 'input_type' => 'input',
  344. 'config_tab_id' => 1,
  345. 'value' => json_encode($copyright),
  346. 'status' => 2,
  347. 'info' => ''
  348. ]);
  349. }
  350. if ($services->count(['menu_name' => 'nncnL_crmeb_copyright_image'])) {
  351. $services->update(['menu_name' => 'nncnL_crmeb_copyright_image'], ['value' => json_encode($copyrightImg)]);
  352. } else {
  353. $services->save([
  354. 'menu_name' => 'nncnL_crmeb_copyright_image',
  355. 'type' => 'text',
  356. 'input_type' => 'input',
  357. 'config_tab_id' => 1,
  358. 'value' => json_encode($copyrightImg),
  359. 'status' => 2,
  360. 'info' => ''
  361. ]);
  362. }
  363. CacheService::clear();
  364. return app('json')->success(100000);
  365. }
  366. /**
  367. * 系统搜索菜单
  368. * @return \think\Response
  369. * @author 吴汐
  370. * @email 442384644@qq.com
  371. * @date 2024/2/1
  372. */
  373. public function menusSearch()
  374. {
  375. // 从请求中获取关键字
  376. [$keyword] = $this->request->postMore([
  377. ['keyword', ''],
  378. ], true);
  379. // 获取系统菜单服务实例
  380. $menusServices = app()->make(SystemMenusServices::class);
  381. // 查询菜单列表
  382. $menusList = $menusServices->selectList([['menu_name', 'like', '%' . $keyword . '%'], ['auth_type', '=', 1]], 'menu_name as title,menu_path as path,id')->toArray();
  383. // 获取系统配置服务实例
  384. $configServices = app()->make(SystemConfigServices::class);
  385. // 获取系统配置标签服务实例
  386. $configTabServices = app()->make(SystemConfigTabServices::class);
  387. // 查询配置项列表
  388. $configList = $configServices->selectList([['info', 'like', '%' . $keyword . '%']], 'info as title,config_tab_id')->toArray();
  389. $configTabList = $configTabServices->selectList([['title', 'like', '%' . $keyword . '%']], 'title,id as config_tab_id')->toArray();
  390. $configAllList = array_merge($configList, $configTabList);
  391. // 获取配置项对应的标签ID
  392. $tabIds = array_unique(array_column($configAllList, 'config_tab_id'));
  393. // 查询配置项标签列表
  394. $tabList = $configTabServices->getColumn([['id', 'in', $tabIds]], 'menus_id', 'id');
  395. // 将配置项标签列表中的菜单ID与配置项列表中的菜单ID对应起来
  396. foreach ($configAllList as &$item1) {
  397. $item1['menus_id'] = $tabList[$item1['config_tab_id']] ?? 0;
  398. }
  399. // 获取配置项标签对应的菜单ID
  400. $configTabIds = array_values($tabList);
  401. // 查询配置项标签对应的菜单列表
  402. $configMenusList = $menusServices->getColumn([['id', 'in', $configTabIds]], 'menu_name as title,menu_path as path,id', 'id');
  403. // 将配置项列表中的菜单ID与配置项标签对应的菜单ID对应起来
  404. foreach ($configAllList as $item2) {
  405. if ($item2['menus_id'] == 0) {
  406. continue;
  407. }
  408. $menusList[] = [
  409. 'title' => $configMenusList[$item2['menus_id']]['title'] . '-' . $item2['title'],
  410. 'path' => $configMenusList[$item2['menus_id']]['path'] . '?tab_id=' . $item2['config_tab_id'],
  411. 'id' => $configMenusList[$item2['menus_id']]['id']
  412. ];
  413. }
  414. // 将菜单列表中的路径前缀添加到每个菜单项的 path 属性上
  415. foreach ($menusList as &$item) {
  416. $item['path'] = '/' . Config::get('app.admin_prefix', 'admin') . $item['path'];
  417. }
  418. // 返回 JSON 格式的菜单列表
  419. return app('json')->success($menusList);
  420. }
  421. }