PublicController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\system\SystemAttachment;
  4. use app\models\system\Express;
  5. use app\models\system\SystemCity;
  6. use app\models\system\SystemStore;
  7. use app\models\system\SystemStoreStaff;
  8. use app\models\user\UserBill;
  9. use app\models\user\WechatUser;
  10. use app\Request;
  11. use crmeb\services\CacheService;
  12. use crmeb\services\UtilService;
  13. use crmeb\services\workerman\ChannelService;
  14. use think\db\exception\DataNotFoundException;
  15. use think\db\exception\DbException;
  16. use think\db\exception\ModelNotFoundException;
  17. use think\facade\Cache;
  18. use crmeb\services\upload\Upload;
  19. /**
  20. * 公共类
  21. * Class PublicController
  22. * @package app\api\controller
  23. */
  24. class PublicController
  25. {
  26. /**
  27. * @param Request $request
  28. * @return mixed
  29. * @throws DataNotFoundException
  30. * @throws ModelNotFoundException
  31. * @throws \think\exception\DbException
  32. */
  33. public function index(Request $request)
  34. {
  35. $banner = sys_data('routine_home_banner') ?: [];//TODO 首页banner图
  36. $menus = sys_data('routine_home_menus') ?: [];//TODO 首页按钮
  37. $roll = sys_data('routine_home_roll_news') ?: [];//TODO 首页滚动新闻
  38. $activity = sys_data('routine_home_activity', 3) ?: [];//TODO 首页活动区域图片
  39. $site_name = sys_config('site_name');
  40. $routine_index_page = sys_data('routine_index_page');
  41. $info['fastInfo'] = $routine_index_page[0]['fast_info'] ?? '';//sys_config('fast_info');//TODO 快速选择简介
  42. $info['bastInfo'] = $routine_index_page[0]['bast_info'] ?? '';//sys_config('bast_info');//TODO 精品推荐简介
  43. $info['firstInfo'] = $routine_index_page[0]['first_info'] ?? '';//sys_config('first_info');//TODO 首发新品简介
  44. $info['salesInfo'] = $routine_index_page[0]['sales_info'] ?? '';//sys_config('sales_info');//TODO 促销单品简介
  45. $logoUrl = sys_config('routine_index_logo');//TODO 促销单品简介
  46. if (strstr($logoUrl, 'http') === false && $logoUrl) $logoUrl = sys_config('site_url') . $logoUrl;
  47. $logoUrl = str_replace('\\', '/', $logoUrl);
  48. if ($request->uid()) {
  49. $subscribe = WechatUser::where('uid', $request->uid())->value('subscribe') ? true : false;
  50. } else {
  51. $subscribe = true;
  52. }
  53. $newGoodsBananr = sys_config('new_goods_bananr');
  54. $tengxun_map_key = sys_config('tengxun_map_key');
  55. return app('json')->successful(compact('banner', 'menus', 'roll', 'info', 'activity', 'logoUrl', 'site_name', 'subscribe', 'newGoodsBananr', 'tengxun_map_key'));
  56. }
  57. /**
  58. * @param Request $request
  59. * @return mixed
  60. * @throws DataNotFoundException
  61. * @throws DbException
  62. * @throws ModelNotFoundException
  63. */
  64. public function cityArea(Request $request)
  65. {
  66. $province = SystemCity::where('level', 0)->where('parent_id', 0)->field('name,city_id')->select()->each(function ($item) {
  67. $item['city'] = SystemCity::where('level', 1)
  68. ->where('parent_id', $item['city_id'])
  69. ->field('name,city_id')
  70. ->select()->each(function ($sub_item) {
  71. $sub_item['area'] = SystemCity::where('level', 2)
  72. ->where('parent_id', $sub_item['city_id'])
  73. ->column('name');
  74. unset($sub_item['city_id']);
  75. })->toArray();
  76. unset($item['city_id']);
  77. });
  78. return app('json')->success('ok', $province->toArray());
  79. }
  80. /**
  81. * 获取分享配置
  82. * @return mixed
  83. */
  84. public function share()
  85. {
  86. $data['img'] = sys_config('wechat_share_img');
  87. if (strstr($data['img'], 'http') === false) $data['img'] = sys_config('site_url') . $data['img'];
  88. $data['img'] = str_replace('\\', '/', $data['img']);
  89. $data['title'] = sys_config('wechat_share_title');
  90. $data['synopsis'] = sys_config('wechat_share_synopsis');
  91. return app('json')->successful(compact('data'));
  92. }
  93. /**
  94. * 获取个人中心菜单
  95. * @param Request $request
  96. * @return mixed
  97. * @throws DataNotFoundException
  98. * @throws ModelNotFoundException
  99. * @throws \think\exception\DbException
  100. */
  101. public function menu_user(Request $request)
  102. {
  103. $menusInfo = sys_data('routine_my_menus') ?? [];
  104. $user = $request->user();
  105. $vipOpen = sys_config('vip_open');
  106. $vipOpen = is_string($vipOpen) ? (int)$vipOpen : $vipOpen;
  107. foreach ($menusInfo as $key => &$value) {
  108. $value['pic'] = set_file_url($value['pic']);
  109. if ($value['id'] == 137 && !(intval(sys_config('store_brokerage_statu')) == 2 || $user->is_promoter == 1))
  110. unset($menusInfo[$key]);
  111. if ($value['id'] == 174 && !StoreService::orderServiceStatus($user->uid))
  112. unset($menusInfo[$key]);
  113. if (((!StoreService::orderServiceStatus($user->uid)) && (!SystemStoreStaff::verifyStatus($user->uid))) && $value['wap_url'] === '/order/order_cancellation')
  114. unset($menusInfo[$key]);
  115. if (((!StoreService::orderServiceStatus($user->uid)) && (!SystemStoreStaff::verifyStatus($user->uid))) && $value['wap_url'] === '/admin/order_cancellation/index')
  116. unset($menusInfo[$key]);
  117. if ((!StoreService::orderServiceStatus($user->uid)) && $value['wap_url'] === '/admin/order/index')
  118. unset($menusInfo[$key]);
  119. if ($value['wap_url'] == '/user/vip' && !$vipOpen)
  120. unset($menusInfo[$key]);
  121. if ($value['wap_url'] == '/customer/index' && !StoreService::orderServiceStatus($user->uid))
  122. unset($menusInfo[$key]);
  123. }
  124. return app('json')->successful(['routine_my_menus' => $menusInfo]);
  125. }
  126. /**
  127. * 热门搜索关键字获取
  128. * @return mixed
  129. * @throws DataNotFoundException
  130. * @throws ModelNotFoundException
  131. * @throws \think\exception\DbException
  132. */
  133. public function search()
  134. {
  135. $routineHotSearch = sys_data('routine_hot_search') ?? [];
  136. $searchKeyword = [];
  137. if (count($routineHotSearch)) {
  138. foreach ($routineHotSearch as $key => &$item) {
  139. array_push($searchKeyword, $item['title']);
  140. }
  141. }
  142. return app('json')->successful($searchKeyword);
  143. }
  144. /**
  145. * 图片上传
  146. * @param Request $request
  147. * @return mixed
  148. * @throws \Psr\SimpleCache\InvalidArgumentException
  149. */
  150. public function upload_image(Request $request)
  151. {
  152. $data = UtilService::postMore([
  153. ['filename', 'file'],
  154. ], $request);
  155. if (!$data['filename']) return app('json')->fail('参数有误');
  156. if (Cache::has('start_uploads_' . $request->uid()) && Cache::get('start_uploads_' . $request->uid()) >= 100) return app('json')->fail('非法操作');
  157. $upload_type = sys_config('upload_type', 1);
  158. $upload = new Upload((int)$upload_type, [
  159. 'accessKey' => sys_config('accessKey'),
  160. 'secretKey' => sys_config('secretKey'),
  161. 'uploadUrl' => sys_config('uploadUrl'),
  162. 'storageName' => sys_config('storage_name'),
  163. 'storageRegion' => sys_config('storage_region'),
  164. ]);
  165. $info = $upload->to('store/comment')->validate()->move($data['filename']);
  166. if ($info === false) {
  167. return app('json')->fail($upload->getError());
  168. }
  169. $res = $upload->getUploadInfo();
  170. SystemAttachment::attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 1, $upload_type, $res['time'], 2);
  171. if (Cache::has('start_uploads_' . $request->uid()))
  172. $start_uploads = (int)Cache::get('start_uploads_' . $request->uid());
  173. else
  174. $start_uploads = 0;
  175. $start_uploads++;
  176. Cache::set('start_uploads_' . $request->uid(), $start_uploads, 86400);
  177. $res['dir'] = path_to_url($res['dir']);
  178. if (strpos($res['dir'], 'http') === false) $res['dir'] = $request->domain() . $res['dir'];
  179. return app('json')->successful('图片上传成功!', ['name' => $res['name'], 'url' => $res['dir']]);
  180. }
  181. /**
  182. * 物流公司
  183. * @return mixed
  184. */
  185. public function logistics()
  186. {
  187. $expressList = Express::lst();
  188. if (!$expressList) return app('json')->successful([]);
  189. return app('json')->successful($expressList->hidden(['code', 'id', 'sort', 'is_show'])->toArray());
  190. }
  191. /**
  192. * 短信购买异步通知
  193. *
  194. * @param Request $request
  195. * @return mixed
  196. */
  197. public function sms_pay_notify(Request $request)
  198. {
  199. list($order_id, $price, $status, $num, $pay_time, $attach) = UtilService::postMore([
  200. ['order_id', ''],
  201. ['price', 0.00],
  202. ['status', 400],
  203. ['num', 0],
  204. ['pay_time', time()],
  205. ['attach', 0],
  206. ], $request, true);
  207. if ($status == 200) {
  208. ChannelService::instance()->send('PAY_SMS_SUCCESS', ['price' => $price, 'number' => $num], [$attach]);
  209. return app('json')->successful();
  210. }
  211. return app('json')->fail();
  212. }
  213. /**
  214. * 记录用户分享
  215. * @param Request $request
  216. * @return mixed
  217. */
  218. public function user_share(Request $request)
  219. {
  220. return app('json')->successful(UserBill::setUserShare($request->uid()));
  221. }
  222. /**
  223. * 获取图片base64
  224. * @param Request $request
  225. * @return mixed
  226. */
  227. public function get_image_base64(Request $request)
  228. {
  229. list($imageUrl, $codeUrl) = UtilService::postMore([
  230. ['image', ''],
  231. ['code', ''],
  232. ], $request, true);
  233. try {
  234. $codeTmp = $code = $codeUrl ? image_to_base64($codeUrl) : false;
  235. if (!$codeTmp) {
  236. $putCodeUrl = put_image($codeUrl);
  237. $code = $putCodeUrl ? image_to_base64($_SERVER['HTTP_HOST'] . '/' . $putCodeUrl) : false;
  238. $code ?? unlink($_SERVER["DOCUMENT_ROOT"] . '/' . $putCodeUrl);
  239. }
  240. $imageTmp = $image = $imageUrl ? image_to_base64($imageUrl) : false;
  241. if (!$imageTmp) {
  242. $putImageUrl = put_image($imageUrl);
  243. $image = $putImageUrl ? image_to_base64($_SERVER['HTTP_HOST'] . '/' . $putImageUrl) : false;
  244. $image ?? unlink($_SERVER["DOCUMENT_ROOT"] . '/' . $putImageUrl);
  245. }
  246. return app('json')->successful(compact('code', 'image'));
  247. } catch (\Exception $e) {
  248. return app('json')->fail($e->getMessage());
  249. }
  250. }
  251. /**
  252. * 门店列表
  253. * @return mixed
  254. */
  255. public function store_list(Request $request)
  256. {
  257. list($latitude, $longitude, $page, $limit) = UtilService::getMore([
  258. ['latitude', ''],
  259. ['longitude', ''],
  260. ['page', 1],
  261. ['limit', 10]
  262. ], $request, true);
  263. $list = SystemStore::lst($latitude, $longitude, $page, $limit);
  264. if (!$list) $list = [];
  265. $data['list'] = $list;
  266. $data['tengxun_map_key'] = sys_config('tengxun_map_key');
  267. return app('json')->successful($data);
  268. }
  269. /**
  270. * 查找城市数据
  271. * @param Request $request
  272. * @return mixed
  273. */
  274. public function city_list(Request $request)
  275. {
  276. $list = CacheService::get('CITY_LIST', function () {
  277. $list = SystemCity::with('children')->field(['city_id', 'name', 'id', 'parent_id'])->where('parent_id', 0)->order('id asc')->select()->toArray();
  278. $data = [];
  279. foreach ($list as &$item) {
  280. $value = ['v' => $item['city_id'], 'n' => $item['name']];
  281. if ($item['children']) {
  282. foreach ($item['children'] as $key => &$child) {
  283. $value['c'][$key] = ['v' => $child['city_id'], 'n' => $child['name']];
  284. unset($child['id'], $child['area_code'], $child['merger_name'], $child['is_show'], $child['level'], $child['lng'], $child['lat'], $child['lat']);
  285. if (SystemCity::where('parent_id', $child['city_id'])->count()) {
  286. $child['children'] = SystemCity::where('parent_id', $child['city_id'])->field(['city_id', 'name', 'id', 'parent_id'])->select()->toArray();
  287. foreach ($child['children'] as $kk => $vv) {
  288. $value['c'][$key]['c'][$kk] = ['v' => $vv['city_id'], 'n' => $vv['name']];
  289. }
  290. }
  291. }
  292. }
  293. $data[] = $value;
  294. }
  295. return $data;
  296. }, 0);
  297. return app('json')->successful($list);
  298. }
  299. }