PublicController.php 21 KB

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