Common.php 14 KB

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