PublicController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. namespace app\api\controller;
  3. use app\models\system\SystemAttachment;
  4. use app\models\store\StoreCategory;
  5. use app\models\store\StoreCouponIssue;
  6. use app\models\store\StoreProduct;
  7. use app\models\store\StoreService;
  8. use app\models\store\StoreOrder;
  9. use app\models\store\StoreAssistanceActive;
  10. use app\models\system\Express;
  11. use app\models\system\SystemCity;
  12. use app\models\system\SystemStore;
  13. use app\models\system\SystemStoreStaff;
  14. use app\models\user\UserBill;
  15. use app\models\user\WechatUser;
  16. use app\models\user\UserBrokerage;
  17. use app\models\user\UserRecharge;
  18. use app\models\wechat\WechatTemplate;
  19. use app\Request;
  20. use crmeb\services\CacheService;
  21. use crmeb\services\MiniProgramService;
  22. use crmeb\services\UploadService;
  23. use crmeb\services\UtilService;
  24. use crmeb\subscribes\OrderSubscribe;
  25. use crmeb\repositories\PaymentRepositories;
  26. use crmeb\services\workerman\ChannelService;
  27. use crmeb\jobs\PaySuccessJob;
  28. use Exception;
  29. use Psr\SimpleCache\InvalidArgumentException;
  30. use think\db\exception\DataNotFoundException;
  31. use think\db\exception\DbException;
  32. use think\db\exception\ModelNotFoundException;
  33. use think\facade\Cache;
  34. use app\models\store\StorePink;
  35. use app\models\user\User;
  36. /**
  37. * 公共类
  38. * Class PublicController
  39. * @package app\api\controller
  40. */
  41. class PublicController
  42. {
  43. /**
  44. * @param Request $request
  45. * @throws Exception
  46. */
  47. public function test()
  48. {
  49. dump(MiniProgramService::profit_sharing(
  50. '4200000744202011046045636579',
  51. 'wx160447244549420541',
  52. [
  53. [
  54. 'type' => 'PERSONAL_OPENID',
  55. 'account' => 'oSAh95RkTKmfzT_s1KaAkIuDMDP0',
  56. 'amount' => 2,
  57. 'description' => '测试分账'
  58. ],
  59. [
  60. 'type' => 'PERSONAL_OPENID',
  61. 'account' => 'oSAh95ZJfUnDWWdZ-z7wfBkUz_B0',
  62. 'amount' => 1,
  63. 'description' => '测试分账'
  64. ]
  65. ],
  66. 11
  67. ));
  68. }
  69. public function test111()
  70. {
  71. $res = StoreOrder::where('mer_id', 11)->where('transaction_id', '<>', '')->where('assistance_id', '=', '')->select();
  72. foreach($res as $value){
  73. dump(MiniProgramService::profit_sharing_finish(
  74. $value['transaction_id'],
  75. $value['order_id'],
  76. $value['mer_id']
  77. ));
  78. }
  79. }
  80. public function test222()
  81. {
  82. $res = UserRecharge::where('mer_id', 11)->where('transaction_id', '<>', '')->select();
  83. foreach($res as $value){
  84. dump(MiniProgramService::profit_sharing_finish(
  85. $value['transaction_id'],
  86. $value['order_id'],
  87. $value['mer_id']
  88. ));
  89. }
  90. }
  91. public function order_pay_success()
  92. {
  93. $orderId = 'wx160679322986954873';
  94. $order = StoreOrder::where('order_id', $orderId)->find();
  95. OrderSubscribe::onOrderPaySuccess($order);
  96. }
  97. public function profitSharingFinish(Request $request)
  98. {
  99. list($order_id) = UtilService::getMore([
  100. ['order_id', ''],
  101. ], $request, true);
  102. $order = StoreOrder::where('order_id', $order_id)->find();
  103. PaySuccessJob::profitSharingFinish($order);
  104. }
  105. public function test_profit_sharing_finish()
  106. {
  107. dump(MiniProgramService::profit_sharing_finish(
  108. '4200000753202011045607878421',
  109. 'wx160445808633819879',
  110. 11
  111. ));
  112. }
  113. public function test_recharge(Request $request)
  114. {
  115. list($order_id) = UtilService::getMore([
  116. ['order_id', ''],
  117. ], $request, true);
  118. UserRecharge::rechargeSuccess($order_id);
  119. }
  120. public function test_refund()
  121. {
  122. $refund_no = UserBrokerage::getNewRefundNo();
  123. dump(MiniProgramService::payOrderRefund(
  124. 'wx2020102914273410024',
  125. [
  126. 'pay_price' => 0.1,
  127. 'refund_price' => 0.01,
  128. 'refund_id' => $refund_no
  129. ],
  130. 11
  131. ));
  132. }
  133. public function wechat_user_recharge()
  134. {
  135. $orderNo = 'wx2020110616055410365';
  136. PaymentRepositories::wechatUserRechargeCard($orderNo);
  137. }
  138. public function pay_success(Request $request)
  139. {
  140. list($orderId) = UtilService::postMore([
  141. ['order_id', ''],
  142. ], $request, true);
  143. PaymentRepositories::wechatProduct($orderId);
  144. }
  145. public function test2($order_id)
  146. {
  147. $order = StoreOrder::where('order_id', $orderId)->find();
  148. if ($order->assistance_id && $res1 && !$order->refund_status) $resAssistance = StoreAssistanceActive::createAssistanceActive($order);//创建助力
  149. }
  150. public function test3(Request $request)
  151. {
  152. $mer_id = 11;
  153. list($uni) = UtilService::postMore([
  154. ['uni', ''],
  155. ], $request, true);
  156. $uid = 24;
  157. $order = StoreOrder::getUserOrderDetail($uid, $uni, $mer_id);
  158. $res = User::backOrderBrokerage($order);
  159. return $res;
  160. }
  161. public function clearCache(Request $request)
  162. {
  163. $config_name = UtilService::postMore([
  164. ['name', ''],
  165. ], $request);
  166. $cacheName = "data_{$config_name['name']}";
  167. return CacheService::delete($cacheName);
  168. }
  169. /**
  170. * @param Request $request
  171. * @return mixed
  172. * @throws DataNotFoundException
  173. * @throws ModelNotFoundException
  174. * @throws DbException
  175. */
  176. public function index(Request $request)
  177. {
  178. $mer_id = $request->mer_id() ?: '';
  179. $banner = sys_data('routine_home_banner', 0, $mer_id) ?: [];//TODO 首页banner图
  180. $menus = sys_data('routine_home_menus', 0, $mer_id) ?: [];//TODO 首页按钮
  181. $roll = sys_data('routine_home_roll_news', 0, $mer_id) ?: [];//TODO 首页滚动新闻
  182. $activity = sys_data('routine_home_activity', 5, $mer_id) ?: [];//TODO 首页活动区域图片
  183. $explosive_money = sys_data('index_categy_images', 0, $mer_id) ?: [];//TODO 首页超值爆款
  184. $site_name = sys_config('site_name', '', $mer_id);
  185. $routine_index_page = sys_data('routine_index_page', 0, $mer_id);
  186. $info['fastInfo'] = $routine_index_page[0]['fast_info'] ?? '';//sys_config('fast_info');//TODO 快速选择简介
  187. $info['bastInfo'] = $routine_index_page[0]['bast_info'] ?? '';//sys_config('bast_info');//TODO 精品推荐简介
  188. $info['firstInfo'] = $routine_index_page[0]['first_info'] ?? '';//sys_config('first_info');//TODO 首发新品简介
  189. $info['salesInfo'] = $routine_index_page[0]['sales_info'] ?? '';//sys_config('sales_info');//TODO 促销单品简介
  190. $logoUrl = sys_config('routine_index_logo', '', $mer_id);
  191. if (strstr($logoUrl, 'http') === false && $logoUrl) $logoUrl = sys_config('site_url', '', $mer_id) . $logoUrl;
  192. $logoUrl = str_replace('\\', '/', $logoUrl);
  193. $fastNumber = sys_config('fast_number', 0, $mer_id);//TODO 快速选择分类个数
  194. $bastNumber = sys_config('bast_number', 0, $mer_id);//TODO 精品推荐个数
  195. $firstNumber = sys_config('first_number', 0, $mer_id);//TODO 首发新品个数
  196. $promotionNumber = sys_config('promotion_number', 0, $mer_id);//TODO 首发新品个数
  197. $info['fastList'] = StoreCategory::byIndexList((int)$fastNumber, false, $mer_id);//TODO 快速选择分类个数
  198. $info['bastList'] = StoreProduct::getBestProduct('id,image,store_name,cate_id,price,ot_price,IFNULL(sales,0) + IFNULL(ficti,0) as sales,unit_name', (int)$bastNumber, $request->uid(), false, 0, 0, $mer_id);//TODO 精品推荐个数
  199. $info['firstList'] = StoreProduct::getNewProduct('id,image,store_name,cate_id,price,unit_name,IFNULL(sales,0) + IFNULL(ficti,0) as sales', (int)$firstNumber, $request->uid(), false, 0, 0, $mer_id);//TODO 首发新品个数
  200. $info['bastBanner'] = sys_data('routine_home_bast_banner', 0, $mer_id) ?? [];//TODO 首页精品推荐图片
  201. $benefit = StoreProduct::getBenefitProduct('id,image,store_name,cate_id,price,ot_price,stock,unit_name', $promotionNumber, 0, 0, $mer_id);//TODO 首页促销单品
  202. $lovely = sys_data('routine_home_new_banner', 0, $mer_id) ?: [];//TODO 首发新品顶部图
  203. $likeInfo = StoreProduct::getHotProduct('id,image,store_name,cate_id,price,ot_price,unit_name', 3, $request->uid(), 0, 0, $mer_id);//TODO 热门榜单 猜你喜欢
  204. $couponList = StoreCouponIssue::getIssueCouponList($request->uid(), 3);
  205. if ($request->uid()) {
  206. $subscribe = WechatUser::where('uid', $request->uid())->value('subscribe') ? true : false;
  207. } else {
  208. $subscribe = true;
  209. }
  210. $newGoodsBananr = sys_config('new_goods_bananr', '', $mer_id);
  211. $tengxun_map_key = sys_config('tengxun_map_key', '', $mer_id);
  212. return app('json')->successful(compact('banner', 'menus', 'roll', 'info', 'activity', 'lovely', 'benefit', 'likeInfo', 'logoUrl', 'couponList', 'site_name', 'subscribe', 'newGoodsBananr', 'tengxun_map_key', 'explosive_money'));
  213. }
  214. /**
  215. * 获取门店商品
  216. */
  217. public function getStoreProduct(Request $request)
  218. {
  219. $data = UtilService::getMore([
  220. ['page', 1],
  221. ['limit', 4],
  222. ], $request);
  223. $res = SystemStore::getStoreProduct($data['page'], $data['limit'], $request->mer_id());
  224. return app('json')->successful($res);
  225. }
  226. /**
  227. * 获取分享配置
  228. * @param Request $request
  229. * @return mixed
  230. */
  231. public function share(Request $request)
  232. {
  233. $mer_id = $request->mer_id();
  234. $data['img'] = sys_config('wechat_share_img', '', $mer_id);
  235. if (strstr($data['img'], 'http') === false) $data['img'] = sys_config('site_url', '', $mer_id) . $data['img'];
  236. $data['img'] = str_replace('\\', '/', $data['img']);
  237. $data['title'] = sys_config('wechat_share_title', '', $mer_id);
  238. $data['synopsis'] = sys_config('wechat_share_synopsis', '', $mer_id);
  239. return app('json')->successful(compact('data'));
  240. }
  241. /**
  242. * 获取个人中心菜单
  243. * @param Request $request
  244. * @return mixed
  245. */
  246. public function menu_user(Request $request)
  247. {
  248. $menusInfo = sys_data('routine_my_menus', 0, $request->mer_id()) ?? [];
  249. $user = $request->user();
  250. $vipOpen = sys_config('vip_open', '', $request->mer_id());
  251. $vipOpen = is_string($vipOpen) ? (int)$vipOpen : $vipOpen;
  252. foreach ($menusInfo as $key => &$value) {
  253. $value['pic'] = set_file_url($value['pic']);
  254. if ($value['id'] == 137 && !(intval(sys_config('store_brokerage_statu', '', $request->mer_id())) == 2 || $user->is_promoter == 1))
  255. unset($menusInfo[$key]);
  256. if ($value['id'] == 174 && !StoreService::orderServiceStatus($user->uid))
  257. unset($menusInfo[$key]);
  258. if (((!StoreService::orderServiceStatus($user->uid)) && (!SystemStoreStaff::verifyStatus($user->uid))) && $value['url'] === '/pages/admin/order_cancellation/index')
  259. unset($menusInfo[$key]);
  260. if ((!StoreService::orderServiceStatus($user->uid)) && $value['url'] === '/pages/admin/order/index')
  261. unset($menusInfo[$key]);
  262. if ($value['url'] == '/pages/users/user_vip/index' && !$vipOpen)
  263. unset($menusInfo[$key]);
  264. }
  265. $bannerInfo = sys_data('routine_my_banner', 0, $request->mer_id()) ?? [];
  266. return app('json')->successful(['routine_my_menus' => $menusInfo, 'routine_my_banner' => $bannerInfo]);
  267. }
  268. /**
  269. * 热门搜索关键字获取
  270. * @return mixed
  271. * @throws DataNotFoundException
  272. * @throws ModelNotFoundException
  273. * @throws \think\exception\DbException
  274. */
  275. public function search(Request $request)
  276. {
  277. $routineHotSearch = sys_data('routine_hot_search', 0, $request->mer_id()) ?? [];
  278. $searchKeyword = [];
  279. if (count($routineHotSearch)) {
  280. foreach ($routineHotSearch as $key => &$item) {
  281. array_push($searchKeyword, $item['title']);
  282. }
  283. }
  284. return app('json')->successful($searchKeyword);
  285. }
  286. /**
  287. * 图片上传
  288. * @param Request $request
  289. * @return mixed
  290. * @throws InvalidArgumentException
  291. */
  292. public function upload_image(Request $request)
  293. {
  294. $data = UtilService::postMore([
  295. ['filename', 'file'],
  296. ], $request);
  297. if (!$data['filename']) return app('json')->fail('参数有误');
  298. if (Cache::has('start_uploads_' . $request->uid()) && Cache::get('start_uploads_' . $request->uid()) >= 100) return app('json')->fail('非法操作');
  299. $upload = UploadService::init(null, $request->mer_id());
  300. $info = $upload->to('store/comment')->validate()->move($data['filename']);
  301. if ($info === false) {
  302. return app('json')->fail($upload->getError());
  303. }
  304. $res = $upload->getUploadInfo();
  305. SystemAttachment::attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 1, (int)sys_config('upload_type', 1), $res['time'], 2);
  306. if (Cache::has('start_uploads_' . $request->uid()))
  307. $start_uploads = (int)Cache::get('start_uploads_' . $request->uid());
  308. else
  309. $start_uploads = 0;
  310. $start_uploads++;
  311. Cache::set('start_uploads_' . $request->uid(), $start_uploads, 86400);
  312. $res['dir'] = path_to_url($res['dir']);
  313. if (strpos($res['dir'], 'http') === false) $res['dir'] = $request->domain() . $res['dir'];
  314. return app('json')->successful('图片上传成功!', ['name' => $res['name'], 'url' => $res['dir']]);
  315. }
  316. /**
  317. * 物流公司
  318. * @return mixed
  319. */
  320. public function logistics()
  321. {
  322. $expressList = Express::lst();
  323. if (!$expressList) return app('json')->successful([]);
  324. return app('json')->successful($expressList->hidden(['code', 'id', 'sort', 'is_show'])->toArray());
  325. }
  326. /**
  327. * 短信购买异步通知
  328. *
  329. * @param Request $request
  330. * @return mixed
  331. * @throws Exception
  332. */
  333. public function sms_pay_notify(Request $request)
  334. {
  335. list($order_id, $price, $status, $num, $pay_time, $attach) = UtilService::postMore([
  336. ['order_id', ''],
  337. ['price', 0.00],
  338. ['status', 400],
  339. ['num', 0],
  340. ['pay_time', time()],
  341. ['attach', 0],
  342. ], $request, true);
  343. if ($status == 200) {
  344. try {
  345. ChannelService::instance()->send('PAY_SMS_SUCCESS', ['price' => $price, 'number' => $num], [$attach]);
  346. } catch (\Throwable $e) {
  347. }
  348. return app('json')->successful();
  349. }
  350. return app('json')->fail();
  351. }
  352. /**
  353. * 记录用户分享
  354. * @param Request $request
  355. * @return mixed
  356. * @throws InvalidArgumentException
  357. * @throws DataNotFoundException
  358. * @throws DbException
  359. * @throws ModelNotFoundException
  360. */
  361. public function user_share(Request $request)
  362. {
  363. return app('json')->successful(UserBill::setUserShare($request->uid(), 300, $request->mer_id()));
  364. }
  365. /**
  366. * 获取图片base64
  367. * @param Request $request
  368. * @return mixed
  369. * @throws Exception
  370. */
  371. public function get_image_base64(Request $request)
  372. {
  373. list($imageUrl, $codeUrl) = UtilService::postMore([
  374. ['image', ''],
  375. ['code', ''],
  376. ], $request, true);
  377. try {
  378. $codeTmp = $code = $codeUrl ? image_to_base64($codeUrl) : false;
  379. if (!$codeTmp) {
  380. $putCodeUrl = put_image($codeUrl);
  381. $code = $putCodeUrl ? image_to_base64($_SERVER['HTTP_HOST'] . '/' . $putCodeUrl) : false;
  382. $code ?? unlink($_SERVER["DOCUMENT_ROOT"] . '/' . $putCodeUrl);
  383. }
  384. $imageTmp = $image = $imageUrl ? image_to_base64($imageUrl) : false;
  385. if (!$imageTmp) {
  386. $putImageUrl = put_image($imageUrl);
  387. $image = $putImageUrl ? image_to_base64($_SERVER['HTTP_HOST'] . '/' . $putImageUrl) : false;
  388. $image ?? unlink($_SERVER["DOCUMENT_ROOT"] . '/' . $putImageUrl);
  389. }
  390. return app('json')->successful(compact('code', 'image'));
  391. } catch (Exception $e) {
  392. return app('json')->fail($e->getMessage());
  393. }
  394. }
  395. /**
  396. * 门店列表
  397. * @param Request $request
  398. * @return mixed
  399. * @throws Exception
  400. */
  401. public function store_list(Request $request)
  402. {
  403. list($latitude, $longitude, $page, $limit, $product_id) = UtilService::getMore([
  404. ['latitude', ''],
  405. ['longitude', ''],
  406. [['page', 'd'], 1],
  407. [['limit', 'd'], 10],
  408. ['product_id', '']
  409. ], $request, true);
  410. $list = SystemStore::lst($latitude, $longitude, $page, $limit, '', 0, $request->mer_id(), $product_id);
  411. if (!$list) $list = [];
  412. $data['list'] = $list;
  413. $data['tengxun_map_key'] = sys_config('tengxun_map_key', '', $request->mer_id());
  414. return app('json')->successful($data);
  415. }
  416. /**
  417. * 查找城市数据
  418. * @param Request $request
  419. * @return mixed
  420. * @throws \throwable
  421. */
  422. public function city_list(Request $request)
  423. {
  424. $list = CacheService::get('CITY_LIST', function () {
  425. $list = SystemCity::with('children')->field(['city_id', 'name', 'id', 'parent_id'])->where('parent_id', 0)->order('id asc')->select()->toArray();
  426. $data = [];
  427. foreach ($list as &$item) {
  428. $value = ['v' => $item['city_id'], 'n' => $item['name']];
  429. if ($item['children']) {
  430. foreach ($item['children'] as $key => &$child) {
  431. $value['c'][$key] = ['v' => $child['city_id'], 'n' => $child['name']];
  432. unset($child['id'], $child['area_code'], $child['merger_name'], $child['is_show'], $child['level'], $child['lng'], $child['lat'], $child['lat']);
  433. if (SystemCity::where('parent_id', $child['city_id'])->count()) {
  434. $child['children'] = SystemCity::where('parent_id', $child['city_id'])->field(['city_id', 'name', 'id', 'parent_id'])->select()->toArray();
  435. foreach ($child['children'] as $kk => $vv) {
  436. $value['c'][$key]['c'][$kk] = ['v' => $vv['city_id'], 'n' => $vv['name']];
  437. }
  438. }
  439. }
  440. }
  441. $data[] = $value;
  442. }
  443. return $data;
  444. }, 0);
  445. return app('json')->successful($list);
  446. }
  447. /**
  448. * 获取拼团数据
  449. * @return mixed
  450. */
  451. public function pink(Request $request)
  452. {
  453. $data['pink_count'] = StorePink::merSet($request->mer_id())->where(['status' => 2, 'is_refund' => 0])->count();
  454. $data['avatars'] = User::whereIn('uid', function ($query) {
  455. $query->name('store_pink')->where(['status' => 2, 'is_refund' => 0])->field(['uid'])->select();
  456. })->limit(3)->order('uid desc')->column('avatar');
  457. return app('json')->successful($data);
  458. }
  459. }