Common.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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\controller\cashier;
  12. use app\Request;
  13. use app\services\order\OtherOrderServices;
  14. use app\services\order\store\BranchOrderServices;
  15. use app\services\order\StoreOrderServices;
  16. use app\services\other\CityAreaServices;
  17. use app\services\product\branch\StoreBranchProductServices;
  18. use app\services\product\product\StoreProductReplyServices;
  19. use app\services\store\SystemStoreServices;
  20. use app\services\store\SystemStoreStaffServices;
  21. use app\services\system\SystemMenusServices;
  22. use app\services\user\UserRechargeServices;
  23. use app\services\system\attachment\SystemAttachmentServices;
  24. use crmeb\services\CacheService;
  25. use crmeb\services\UploadService;
  26. /**
  27. * 公共接口基类 主要存放公共接口
  28. * Class Common
  29. * @package app\controller\admin
  30. */
  31. class Common extends AuthController
  32. {
  33. /**
  34. * 获取logo
  35. * @param SystemStoreServices $storeServices
  36. * @return mixed
  37. */
  38. public function getLogo(SystemStoreServices $storeServices)
  39. {
  40. $store = $storeServices->get((int)$this->storeId, ['id', 'image', 'name']);
  41. return $this->success([
  42. 'logo' => $store && isset($store['image']) && $store['image'] ? $store['image'] : sys_config('site_logo'),
  43. 'logo_square' => $store && isset($store['image']) && $store['image'] ? $store['image'] : sys_config('site_logo_square'),
  44. 'site_name' => $store && isset($store['name']) && $store['name'] ? $store['name'] : sys_config('site_name')
  45. ]);
  46. }
  47. /**
  48. * 获取版权
  49. * @return mixed
  50. */
  51. public function getCopyright()
  52. {
  53. try {
  54. $copyright = $this->__z6uxyJQ4xYa5ee1mx5();
  55. } catch (\Throwable $e) {
  56. $copyright = ['copyrightContext' => '', 'copyrightImage' => ''];
  57. }
  58. $copyright['version'] = get_crmeb_version();
  59. return $this->success($copyright);
  60. }
  61. /**
  62. * @return mixed
  63. */
  64. public function getConfig()
  65. {
  66. return $this->success([
  67. 'tengxun_map_key' => sys_config('tengxun_map_key'),
  68. 'open_erp' => !!sys_config('erp_open')
  69. ]);
  70. }
  71. /**
  72. * @param CityAreaServices $services
  73. * @return mixed
  74. */
  75. public function city(CityAreaServices $services, $pid = 0)
  76. {
  77. return $this->success($services->getCityTreeList((int)$pid));
  78. }
  79. /**
  80. * 格式化菜单
  81. * @return mixed
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\DbException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. */
  86. public function menusList()
  87. {
  88. /** @var SystemMenusServices $menusServices */
  89. $menusServices = app()->make(SystemMenusServices::class);
  90. $list = $menusServices->getSearchList(3);
  91. $counts = $menusServices->getColumn([
  92. ['type', 3],
  93. ['is_show', '=', 1],
  94. ['auth_type', '=', 1],
  95. ['is_del', '=', 0],
  96. ['is_show_path', '=', 0],
  97. ], 'pid');
  98. $data = [];
  99. foreach ($list as $key => $item) {
  100. $pid = $item->getData('pid');
  101. $data[$key] = json_decode($item, true);
  102. $data[$key]['pid'] = $pid;
  103. if (in_array($item->id, $counts)) {
  104. $data[$key]['type'] = 1;
  105. } else {
  106. $data[$key]['type'] = 0;
  107. }
  108. $data[$key]['menu_path'] = preg_replace('/^\/cashier/', '', $item['menu_path']);
  109. }
  110. return app('json')->success(sort_list_tier($data));
  111. }
  112. /**
  113. * 首页运营头部统计
  114. * @param Request $request
  115. * @param BranchOrderServices $orderServices
  116. * @return mixed
  117. */
  118. public function homeStatics(Request $request, BranchOrderServices $orderServices)
  119. {
  120. [$time] = $request->getMore([
  121. ['data', '', '', 'time']
  122. ], true);
  123. $time = $orderServices->timeHandle($time);
  124. return app('json')->success($orderServices->homeStatics((int)$this->storeId, $time));
  125. }
  126. /**
  127. * 首页营业趋势图表
  128. * @param Request $request
  129. * @param BranchOrderServices $orderServices
  130. * @return mixed
  131. */
  132. public function operateChart(Request $request, BranchOrderServices $orderServices)
  133. {
  134. [$time] = $request->getMore([
  135. ['data', '', '', 'time']
  136. ], true);
  137. $time = $orderServices->timeHandle($time, true);
  138. return app('json')->success($orderServices->operateChart((int)$this->storeId, $time));
  139. }
  140. /**
  141. * 首页交易统计
  142. * @param Request $request
  143. * @param BranchOrderServices $orderServices
  144. * @return mixed
  145. * @throws \think\db\exception\DataNotFoundException
  146. * @throws \think\db\exception\DbException
  147. * @throws \think\db\exception\ModelNotFoundException
  148. */
  149. public function orderChart(Request $request, BranchOrderServices $orderServices)
  150. {
  151. [$time] = $request->getMore([
  152. ['data', '', '', 'time']
  153. ], true);
  154. $time = $orderServices->timeHandle($time);
  155. return $this->success($orderServices->orderChart((int)$this->storeId, $time));
  156. }
  157. /**
  158. * 首页店员统计
  159. * @param Request $request
  160. * @param SystemStoreStaffServices $staffServices
  161. * @return mixed
  162. */
  163. public function staffChart(Request $request, SystemStoreStaffServices $staffServices)
  164. {
  165. [$time] = $request->getMore([
  166. ['data', '', '', 'time']
  167. ], true);
  168. $time = $staffServices->timeHandle($time);
  169. return $this->success($staffServices->staffChart((int)$this->storeId, $time));
  170. }
  171. /**
  172. * 待办事统计
  173. * @return mixed
  174. */
  175. public function jnotice()
  176. {
  177. /** @var StoreOrderServices $orderServices */
  178. $orderServices = app()->make(StoreOrderServices::class);
  179. $data['ordernum'] = $orderServices->storeOrderCount((int)$this->storeId, 7);
  180. $store_stock = sys_config('store_stock');
  181. if ($store_stock < 0) $store_stock = 2;
  182. /** @var StoreBranchProductServices $storeServices */
  183. $storeServices = app()->make(StoreBranchProductServices::class);
  184. $data['inventory'] = $storeServices->count(['store_id' => $this->storeId, 'type' => 5, 'store_stock' => $store_stock]);//警戒库存
  185. /** @var StoreProductReplyServices $replyServices */
  186. $replyServices = app()->make(StoreProductReplyServices::class);
  187. $data['commentnum'] = $replyServices->replyCount(1, (int)$this->storeId);
  188. $value = [];
  189. if ($data['ordernum'] != 0) {
  190. $value[] = [
  191. 'title' => '您有' . $data['ordernum'] . '个待发货的订单',
  192. 'type' => 'bulb',
  193. 'url' => '/order/index?type=7&status=1'
  194. ];
  195. }
  196. if ($data['inventory'] != 0) {
  197. $value[] = [
  198. 'title' => '您有' . $data['inventory'] . '个商品库存预警',
  199. 'type' => 'information',
  200. 'url' => '/product/index?type=5',
  201. ];
  202. }
  203. if ($data['commentnum'] != 0) {
  204. $value[] = [
  205. 'title' => '您有' . $data['commentnum'] . '条评论待回复',
  206. 'type' => 'bulb',
  207. 'url' => '/product/product_reply?is_reply=0'
  208. ];
  209. }
  210. return $this->success($this->noticeData($value));
  211. }
  212. /**
  213. * 消息返回格式
  214. * @param array $data
  215. * @return array
  216. */
  217. public function noticeData(array $data): array
  218. {
  219. // 消息图标
  220. $iconColor = [
  221. // 邮件 消息
  222. 'mail' => [
  223. 'icon' => 'md-mail',
  224. 'color' => '#3391e5'
  225. ],
  226. // 普通 消息
  227. 'bulb' => [
  228. 'icon' => 'md-bulb',
  229. 'color' => '#87d068'
  230. ],
  231. // 警告 消息
  232. 'information' => [
  233. 'icon' => 'md-information',
  234. 'color' => '#fe5c57'
  235. ],
  236. // 关注 消息
  237. 'star' => [
  238. 'icon' => 'md-star',
  239. 'color' => '#ff9900'
  240. ],
  241. // 申请 消息
  242. 'people' => [
  243. 'icon' => 'md-people',
  244. 'color' => '#f06292'
  245. ],
  246. ];
  247. // 消息类型
  248. $type = array_keys($iconColor);
  249. // 默认数据格式
  250. $default = [
  251. 'icon' => 'md-bulb',
  252. 'iconColor' => '#87d068',
  253. 'title' => '',
  254. 'url' => '',
  255. 'type' => 'bulb',
  256. 'read' => 0,
  257. 'time' => 0
  258. ];
  259. $value = [];
  260. foreach ($data as $item) {
  261. $val = array_merge($default, $item);
  262. if (isset($item['type']) && in_array($item['type'], $type)) {
  263. $val['type'] = $item['type'];
  264. $val['iconColor'] = $iconColor[$item['type']]['color'] ?? '';
  265. $val['icon'] = $iconColor[$item['type']]['icon'] ?? '';
  266. }
  267. $value[] = $val;
  268. }
  269. return $value;
  270. }
  271. /**
  272. * 轮询后台扫码订单状态
  273. * @param Request $request
  274. * @param $type
  275. * @return mixed
  276. */
  277. public function checkOrderStatus(Request $request, $type)
  278. {
  279. [$order_id, $end_time] = $request->getMore([
  280. ['order_id', ''],
  281. ['end_time', 0],
  282. ], true);
  283. switch ($type) {
  284. case 1://recharge
  285. /** @var UserRechargeServices $userRecharge */
  286. $userRecharge = app()->make(UserRechargeServices::class);
  287. $data['status'] = (bool)$userRecharge->count(['order_id' => $order_id, 'paid' => 1]);
  288. break;
  289. case 2://svip
  290. /** @var OtherOrderServices $otherOrderServices */
  291. $otherOrderServices = app()->make(OtherOrderServices::class);
  292. $data['status'] = (bool)$otherOrderServices->count(['order_id' => $order_id, 'paid' => 1]);
  293. break;
  294. case 3://订单
  295. $storeOrderServices = app()->make(StoreOrderServices::class);
  296. $data['status'] = (bool)$storeOrderServices->count(['order_id' => $order_id, 'paid' => 1]);
  297. break;
  298. default:
  299. return app('json')->fail('暂不支持该类型订单查询');
  300. }
  301. $time = $end_time - time();
  302. $data['time'] = $time > 0 ? $time : 0;
  303. return app('json')->successful($data);
  304. }
  305. /**
  306. * 图片上传
  307. * @param Request $request
  308. * @return mixed
  309. * @throws \Psr\SimpleCache\InvalidArgumentException
  310. */
  311. public function upload_image(Request $request, SystemAttachmentServices $services)
  312. {
  313. $data = $request->postMore([
  314. ['filename', 'file'],
  315. ]);
  316. if (!$data['filename']) return app('json')->fail('参数有误');
  317. if (CacheService::has('cashier_uploads_' . $request->cashierId()) && CacheService::get('cashier_uploads_' . $request->cashierId()) >= 100) return app('json')->fail('非法操作');
  318. $upload = UploadService::init();
  319. $info = $upload->to('cashier')->validate()->move($data['filename']);
  320. if ($info === false) {
  321. return app('json')->fail($upload->getError());
  322. }
  323. $res = $upload->getUploadInfo();
  324. $services->attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 1, (int)sys_config('upload_type', 1), $res['time'], 3);
  325. if (CacheService::has('cashier_uploads_' . $request->cashierId()))
  326. $start_uploads = (int)CacheService::get('cashier_uploads_' . $request->cashierId());
  327. else
  328. $start_uploads = 0;
  329. $start_uploads++;
  330. CacheService::set('cashier_uploads_' . $request->cashierId(), $start_uploads, 86400);
  331. $res['dir'] = path_to_url($res['dir']);
  332. if (strpos($res['dir'], 'http') === false) $res['dir'] = $request->domain() . $res['dir'];
  333. return app('json')->successful('图片上传成功!', ['name' => $res['name'], 'url' => $res['dir']]);
  334. }
  335. }