Common.php 14 KB

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