PublicController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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\api\controller\v1;
  12. use app\services\activity\StorePinkServices;
  13. use app\services\diy\DiyServices;
  14. use app\services\message\service\StoreServiceServices;
  15. use app\services\order\DeliveryServiceServices;
  16. use app\services\product\product\StoreCategoryServices;
  17. use app\services\product\product\StoreProductServices;
  18. use app\services\shipping\ExpressServices;
  19. use app\services\shipping\SystemCityServices;
  20. use app\services\system\attachment\SystemAttachmentServices;
  21. use app\services\system\config\SystemConfigServices;
  22. use app\services\system\store\SystemStoreServices;
  23. use app\services\system\store\SystemStoreStaffServices;
  24. use app\services\user\UserBillServices;
  25. use app\services\user\UserInvoiceServices;
  26. use app\services\user\UserServices;
  27. use app\services\wechat\WechatUserServices;
  28. use app\Request;
  29. use crmeb\services\CacheService;
  30. use crmeb\services\UploadService;
  31. use crmeb\services\workerman\ChannelService;
  32. use think\facade\Cache;
  33. /**
  34. * 公共类
  35. * Class PublicController
  36. * @package app\api\controller
  37. */
  38. class PublicController
  39. {
  40. /**
  41. * 主页获取
  42. * @param Request $request
  43. * @return mixed
  44. */
  45. public function index(Request $request)
  46. {
  47. $banner = sys_data('routine_home_banner') ?: [];//TODO 首页banner图
  48. $menus = sys_data('routine_home_menus') ?: [];//TODO 首页按钮
  49. $roll = sys_data('routine_home_roll_news') ?: [];//TODO 首页滚动新闻
  50. $activity = sys_data('routine_home_activity', 3) ?: [];//TODO 首页活动区域图片
  51. $explosive_money = sys_data('index_categy_images') ?: [];//TODO 首页超值爆款
  52. $site_name = sys_config('site_name');
  53. $routine_index_page = sys_data('routine_index_page');
  54. $info['fastInfo'] = $routine_index_page[0]['fast_info'] ?? '';//TODO 快速选择简介
  55. $info['bastInfo'] = $routine_index_page[0]['bast_info'] ?? '';//TODO 精品推荐简介
  56. $info['firstInfo'] = $routine_index_page[0]['first_info'] ?? '';//TODO 首发新品简介
  57. $info['salesInfo'] = $routine_index_page[0]['sales_info'] ?? '';//TODO 促销单品简介
  58. $logoUrl = sys_config('routine_index_logo');//TODO 促销单品简介
  59. if (strstr($logoUrl, 'http') === false && $logoUrl) {
  60. $logoUrl = sys_config('site_url') . $logoUrl;
  61. }
  62. $logoUrl = str_replace('\\', '/', $logoUrl);
  63. $fastNumber = (int)sys_config('fast_number', 0);//TODO 快速选择分类个数
  64. $bastNumber = (int)sys_config('bast_number', 0);//TODO 精品推荐个数
  65. $firstNumber = (int)sys_config('first_number', 0);//TODO 首发新品个数
  66. $promotionNumber = (int)sys_config('promotion_number', 0);//TODO 首发新品个数
  67. /** @var StoreCategoryServices $categoryService */
  68. $categoryService = app()->make(StoreCategoryServices::class);
  69. $info['fastList'] = $fastNumber ? $categoryService->byIndexList($fastNumber, 'id,cate_name,pid,pic') : [];//TODO 快速选择分类个数
  70. /** @var StoreProductServices $storeProductServices */
  71. $storeProductServices = app()->make(StoreProductServices::class);
  72. $info['bastList'] = $bastNumber ? $storeProductServices->getRecommendProduct($request->uid(), 'is_best', $bastNumber) : [];//TODO 精品推荐个数
  73. $info['firstList'] = $firstNumber ? $storeProductServices->getRecommendProduct($request->uid(), 'is_new', $firstNumber) : [];//TODO 首发新品个数
  74. $info['bastBanner'] = sys_data('routine_home_bast_banner') ?? [];//TODO 首页精品推荐图片
  75. $benefit = $promotionNumber ? $storeProductServices->getRecommendProduct($request->uid(), 'is_benefit', $promotionNumber) : [];//TODO 首页促销单品
  76. $lovely = sys_data('routine_home_new_banner') ?: [];//TODO 首发新品顶部图
  77. $likeInfo = $storeProductServices->getRecommendProduct($request->uid(), 'is_hot', 3);//TODO 热门榜单 猜你喜欢
  78. if ($request->uid()) {
  79. /** @var WechatUserServices $wechatUserService */
  80. $wechatUserService = app()->make(WechatUserServices::class);
  81. $subscribe = $wechatUserService->value(['uid' => $request->uid()], 'subscribe') ? true : false;
  82. } else {
  83. $subscribe = true;
  84. }
  85. $newGoodsBananr = sys_config('new_goods_bananr');
  86. $tengxun_map_key = sys_config('tengxun_map_key');
  87. return app('json')->successful(compact('banner', 'menus', 'roll', 'info', 'activity', 'lovely', 'benefit', 'likeInfo', 'logoUrl', 'site_name', 'subscribe', 'newGoodsBananr', 'tengxun_map_key', 'explosive_money'));
  88. }
  89. /**
  90. * 获取分享配置
  91. * @return mixed
  92. */
  93. public function share()
  94. {
  95. $data['img'] = sys_config('wechat_share_img');
  96. if (strstr($data['img'], 'http') === false) {
  97. $data['img'] = sys_config('site_url') . $data['img'];
  98. }
  99. $data['img'] = str_replace('\\', '/', $data['img']);
  100. $data['title'] = sys_config('wechat_share_title');
  101. $data['synopsis'] = sys_config('wechat_share_synopsis');
  102. return app('json')->successful(compact('data'));
  103. }
  104. /**
  105. * 获取网站配置
  106. * @return mixed
  107. */
  108. public function getSiteConfig()
  109. {
  110. $data['record_No'] = sys_config('record_No');
  111. return app('json')->success($data);
  112. }
  113. /**
  114. * 获取个人中心菜单
  115. * @param Request $request
  116. * @return mixed
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\ModelNotFoundException
  119. * @throws \think\exception\DbException
  120. */
  121. public function menu_user(Request $request)
  122. {
  123. $menusInfo = sys_data('routine_my_menus') ?? [];
  124. $uid = 0;
  125. $userInfo = [];
  126. if ($request->hasMacro('user')) $userInfo = $request->user();
  127. if ($request->hasMacro('uid')) $uid = $request->uid();
  128. $vipOpen = sys_config('member_func_status');
  129. $brokerageFuncStatus = sys_config('brokerage_func_status');
  130. $balanceFuncStatus = sys_config('balance_func_status');
  131. $userService = $invoiceStatus = $deliveryUser = $isUserPromoter = $userVerifyStatus = $userOrder = true;
  132. if ($uid && $userInfo) {
  133. /** @var StoreServiceServices $storeService */
  134. $storeService = app()->make(StoreServiceServices::class);
  135. $userService = $storeService->checkoutIsService(['uid' => $uid, 'status' => 1]);
  136. $userOrder = $storeService->checkoutIsService(['uid' => $uid, 'status' => 1, 'customer' => 1]);
  137. /** @var SystemStoreStaffServices $systemStoreStaff */
  138. $systemStoreStaff = app()->make(SystemStoreStaffServices::class);
  139. /** @var UserServices $user */
  140. $user = app()->make(UserServices::class);
  141. /** @var UserInvoiceServices $userInvoice */
  142. $userInvoice = app()->make(UserInvoiceServices::class);
  143. $invoiceStatus = $userInvoice->invoiceFuncStatus(false);
  144. /** @var DeliveryServiceServices $deliveryService */
  145. $deliveryService = app()->make(DeliveryServiceServices::class);
  146. $deliveryUser = $deliveryService->checkoutIsService($uid);
  147. $isUserPromoter = $user->checkUserPromoter($uid, $userInfo);
  148. $userVerifyStatus = $systemStoreStaff->verifyStatus($uid);
  149. }
  150. $auth = [];
  151. $auth['/pages/users/user_vip/index'] = !$vipOpen;
  152. $auth['/pages/users/user_spread_user/index'] = !$brokerageFuncStatus || !$isUserPromoter || $uid == 0;
  153. $auth['/pages/users/user_money/index'] = !$balanceFuncStatus;
  154. $auth['/pages/admin/order/index'] = !$userOrder || $uid == 0;
  155. $auth['/pages/admin/order_cancellation/index'] = (!$userVerifyStatus && !$deliveryUser) || $uid == 0;
  156. $auth['/pages/users/user_invoice_list/index'] = !$invoiceStatus;
  157. $auth['/kefu/mobile_list'] = !$userService || $uid == 0;
  158. foreach ($menusInfo as $key => &$value) {
  159. // $value['pic'] = set_file_url($value['pic']);
  160. if (isset($auth[$value['url']]) && $auth[$value['url']]) {
  161. unset($menusInfo[$key]);
  162. continue;
  163. }
  164. if ($value['url'] == '/kefu/mobile_list') {
  165. $value['url'] = sys_config('site_url') . $value['url'];
  166. if ($request->isRoutine()) {
  167. $value['url'] = str_replace('http://', 'https://', $value['url']);
  168. }
  169. }
  170. }
  171. /** @var SystemConfigServices $systemConfigServices */
  172. $systemConfigServices = app()->make(SystemConfigServices::class);
  173. $bannerInfo = $systemConfigServices->getSpreadBanner() ?? [];
  174. $my_banner = sys_data('routine_my_banner');
  175. $routine_contact_type = sys_config('routine_contact_type', 0);
  176. return app('json')->successful(['routine_my_menus' => array_merge($menusInfo), 'routine_my_banner' => $my_banner, 'routine_spread_banner' => $bannerInfo, 'routine_contact_type' => $routine_contact_type]);
  177. }
  178. /**
  179. * 热门搜索关键字获取
  180. * @return mixed
  181. * @throws \think\db\exception\DataNotFoundException
  182. * @throws \think\db\exception\ModelNotFoundException
  183. * @throws \think\exception\DbException
  184. */
  185. public function search()
  186. {
  187. $routineHotSearch = sys_data('routine_hot_search') ?? [];
  188. $searchKeyword = [];
  189. if (count($routineHotSearch)) {
  190. foreach ($routineHotSearch as $key => &$item) {
  191. array_push($searchKeyword, $item['title']);
  192. }
  193. }
  194. return app('json')->successful($searchKeyword);
  195. }
  196. /**
  197. * 图片上传
  198. * @param Request $request
  199. * @return mixed
  200. * @throws \Psr\SimpleCache\InvalidArgumentException
  201. */
  202. public function upload_image(Request $request, SystemAttachmentServices $services)
  203. {
  204. $data = $request->postMore([
  205. ['filename', 'file'],
  206. ]);
  207. if (!$data['filename']) return app('json')->fail('参数有误');
  208. if (Cache::has('start_uploads_' . $request->uid()) && Cache::get('start_uploads_' . $request->uid()) >= 100) return app('json')->fail('非法操作');
  209. $upload = UploadService::init();
  210. $info = $upload->to('store/comment')->validate()->move($data['filename']);
  211. if ($info === false) {
  212. return app('json')->fail($upload->getError());
  213. }
  214. $res = $upload->getUploadInfo();
  215. $services->attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 1, (int)sys_config('upload_type', 1), $res['time'], 3);
  216. if (Cache::has('start_uploads_' . $request->uid()))
  217. $start_uploads = (int)Cache::get('start_uploads_' . $request->uid());
  218. else
  219. $start_uploads = 0;
  220. $start_uploads++;
  221. Cache::set('start_uploads_' . $request->uid(), $start_uploads, 86400);
  222. $res['dir'] = path_to_url($res['dir']);
  223. if (strpos($res['dir'], 'http') === false) $res['dir'] = $request->domain() . $res['dir'];
  224. return app('json')->successful('图片上传成功!', ['name' => $res['name'], 'url' => $res['dir']]);
  225. }
  226. /**
  227. * 物流公司
  228. * @return mixed
  229. */
  230. public function logistics(ExpressServices $services)
  231. {
  232. $expressList = $services->expressList();
  233. return app('json')->successful($expressList ?? []);
  234. }
  235. /**
  236. * 短信购买异步通知
  237. *
  238. * @param Request $request
  239. * @return mixed
  240. */
  241. public function sms_pay_notify(Request $request)
  242. {
  243. [$order_id, $price, $status, $num, $pay_time, $attach] = $request->postMore([
  244. ['order_id', ''],
  245. ['price', 0.00],
  246. ['status', 400],
  247. ['num', 0],
  248. ['pay_time', time()],
  249. ['attach', 0],
  250. ], true);
  251. if ($status == 200) {
  252. try {
  253. ChannelService::instance()->send('PAY_SMS_SUCCESS', ['price' => $price, 'number' => $num], [$attach]);
  254. } catch (\Throwable $e) {
  255. }
  256. return app('json')->successful();
  257. }
  258. return app('json')->fail();
  259. }
  260. /**
  261. * 记录用户分享
  262. * @param Request $request
  263. * @return mixed
  264. */
  265. public function user_share(Request $request, UserBillServices $services)
  266. {
  267. $uid = (int)$request->uid();
  268. return app('json')->successful($services->setUserShare($uid));
  269. }
  270. /**
  271. * 获取图片base64
  272. * @param Request $request
  273. * @return mixed
  274. */
  275. public function get_image_base64(Request $request)
  276. {
  277. [$imageUrl, $codeUrl] = $request->postMore([
  278. ['image', ''],
  279. ['code', ''],
  280. ], true);
  281. try {
  282. $code = CacheService::get($codeUrl, function () use ($codeUrl) {
  283. $codeTmp = $code = $codeUrl ? image_to_base64($codeUrl) : false;
  284. if (!$codeTmp) {
  285. $putCodeUrl = put_image($codeUrl);
  286. $code = $putCodeUrl ? image_to_base64(app()->request->domain(true) . '/' . $putCodeUrl) : false;
  287. $code ?? unlink($_SERVER["DOCUMENT_ROOT"] . '/' . $putCodeUrl);
  288. }
  289. return $code;
  290. });
  291. $image = CacheService::get($imageUrl, function () use ($imageUrl) {
  292. $imageTmp = $image = $imageUrl ? image_to_base64($imageUrl) : false;
  293. if (!$imageTmp) {
  294. $putImageUrl = put_image($imageUrl);
  295. $image = $putImageUrl ? image_to_base64(app()->request->domain(true) . '/' . $putImageUrl) : false;
  296. $image ?? unlink($_SERVER["DOCUMENT_ROOT"] . '/' . $putImageUrl);
  297. }
  298. return $image;
  299. });
  300. return app('json')->successful(compact('code', 'image'));
  301. } catch (\Exception $e) {
  302. return app('json')->fail($e->getMessage());
  303. }
  304. }
  305. /**
  306. * 门店列表
  307. * @return mixed
  308. */
  309. public function store_list(Request $request, SystemStoreServices $services)
  310. {
  311. list($latitude, $longitude) = $request->getMore([
  312. ['latitude', ''],
  313. ['longitude', ''],
  314. ], true);
  315. $data['list'] = $services->getStoreList(['type' => 0], ['id', 'name', 'phone', 'address', 'detailed_address', 'image', 'latitude', 'longitude'], $latitude, $longitude);
  316. $data['tengxun_map_key'] = sys_config('tengxun_map_key');
  317. return app('json')->successful($data);
  318. }
  319. /**
  320. * 查找城市数据
  321. * @param Request $request
  322. * @return mixed
  323. */
  324. public function city_list(Request $request)
  325. {
  326. /** @var SystemCityServices $systemCity */
  327. $systemCity = app()->make(SystemCityServices::class);
  328. return app('json')->successful($systemCity->cityList());
  329. }
  330. /**
  331. * 获取拼团数据
  332. * @return mixed
  333. */
  334. public function pink(StorePinkServices $pink, UserServices $user)
  335. {
  336. $data['pink_count'] = $pink->getCount(['status' => 2, 'is_refund' => 0]);
  337. $uids = array_flip($pink->getColumn(['status' => 2, 'is_refund' => 0], 'uid'));
  338. if (count($uids)) {
  339. $uids = array_rand($uids, count($uids) < 3 ? count($uids) : 3);
  340. }
  341. $data['avatars'] = $uids ? $user->getColumn(is_array($uids) ? [['uid', 'in', $uids]] : ['uid' => $uids], 'avatar') : [];
  342. return app('json')->successful($data);
  343. }
  344. /**
  345. * 复制口令接口
  346. * @return mixed
  347. */
  348. public function copy_words()
  349. {
  350. $data['words'] = sys_config('copy_words');
  351. return app('json')->successful($data);
  352. }
  353. /**生成口令关键字
  354. * @param Request $request
  355. * @return mixed
  356. * @throws \think\db\exception\DataNotFoundException
  357. * @throws \think\db\exception\DbException
  358. * @throws \think\db\exception\ModelNotFoundException
  359. */
  360. public function copy_share_words(Request $request)
  361. {
  362. list($productId) = $request->getMore([
  363. ['product_id', ''],
  364. ], true);
  365. /** @var StoreProductServices $productService */
  366. $productService = app()->make(StoreProductServices::class);
  367. $keyWords['key_words'] = $productService->getProductWords($productId);
  368. return app('json')->successful($keyWords);
  369. }
  370. /**
  371. * 获取页面数据
  372. * @return mixed
  373. * @throws \think\db\exception\DataNotFoundException
  374. * @throws \think\db\exception\DbException
  375. * @throws \think\db\exception\ModelNotFoundException
  376. */
  377. public function getDiy(DiyServices $services, $id = 0)
  378. {
  379. return app('json')->successful($services->getDiyInfo((int)$id));
  380. }
  381. /**
  382. * 获取底部导航
  383. * @param DiyServices $services
  384. * @param string $template_name
  385. * @return mixed
  386. */
  387. public function getNavigation(DiyServices $services, string $template_name = '')
  388. {
  389. return app('json')->success($services->getNavigation($template_name));
  390. }
  391. /**
  392. * 首页商品数据
  393. * @param Request $request
  394. */
  395. public function home_products_list(Request $request, DiyServices $services)
  396. {
  397. $data = $request->getMore([
  398. ['priceOrder', ''],
  399. ['newsOrder', ''],
  400. ['salesOrder', ''],
  401. [['type', 0], 0],
  402. ['ids', ''],
  403. ['selectId', ''],
  404. ['selectType', 0],
  405. ['isType', 0],
  406. ]);
  407. $where = [];
  408. $where['is_show'] = 1;
  409. $where['is_del'] = 0;
  410. if ($data['selectType'] == 1) {
  411. if (!$data['ids']) {
  412. return app('json')->success([]);
  413. }
  414. $where['ids'] = $data['ids'] ? explode(',', $data['ids']) : [];
  415. if ($data['type'] != 2 && $data['type'] != 3 && $data['type'] != 8) {
  416. $where['type'] = 0;
  417. } else {
  418. $where['type'] = $data['type'];
  419. }
  420. } else {
  421. $where['priceOrder'] = $data['priceOrder'];
  422. $where['newsOrder'] = $data['newsOrder'];
  423. $where['salesOrder'] = $data['salesOrder'];
  424. $where['type'] = $data['type'];
  425. if ($data['selectId']) {
  426. /** @var StoreCategoryServices $storeCategoryServices */
  427. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  428. if ($storeCategoryServices->value(['id' => $data['selectId']], 'pid')) {
  429. $where['sid'] = $data['selectId'];
  430. } else {
  431. $where['cid'] = $data['selectId'];
  432. }
  433. }
  434. }
  435. return app('json')->success($services->homeProductList($where, $request->uid()));
  436. }
  437. /**
  438. * 获取系统信息
  439. * @return mixed
  440. */
  441. public function getVersion()
  442. {
  443. $data['site_url'] = sys_config('site_url');
  444. $data['version'] = get_crmeb_version();
  445. $data['code'] = get_crmeb_version_code();
  446. $data['server'] = php_uname('s');
  447. return app('json')->success($data);
  448. }
  449. }