PublicController.php 21 KB

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