Common.php 14 KB

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