PublicController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\system\SystemAttachment;
  4. use app\models\game\Lottery;
  5. use app\models\store\StoreCategory;
  6. use app\models\store\StoreCouponIssue;
  7. use app\models\store\StoreOrder;
  8. use app\models\store\StorePink;
  9. use app\models\store\StoreProduct;
  10. use app\models\store\StoreService;
  11. use app\models\system\Express;
  12. use app\models\system\SystemCity;
  13. use app\models\system\SystemStore;
  14. use app\models\system\SystemStoreStaff;
  15. use app\models\tree\Tree;
  16. use app\models\tree\TreeRecommend;
  17. use app\models\user\Achievement;
  18. use app\models\user\User;
  19. use app\models\user\UserBill;
  20. use app\models\user\UserLevel;
  21. use app\models\user\UserSpread;
  22. use app\models\user\WechatUser;
  23. use app\Request;
  24. use crmeb\basic\BaseModel;
  25. use crmeb\services\CacheService;
  26. use crmeb\services\UtilService;
  27. use crmeb\services\workerman\ChannelService;
  28. use think\db\exception\DataNotFoundException;
  29. use think\db\exception\DbException;
  30. use think\db\exception\ModelNotFoundException;
  31. use think\Exception;
  32. use think\facade\Cache;
  33. use crmeb\services\upload\Upload;
  34. /**
  35. * 公共类
  36. * Class PublicController
  37. * @package app\api\controller
  38. */
  39. class PublicController
  40. {
  41. public function test(Request $request)
  42. {
  43. $uid = $request->post('uid');
  44. $way = $request->post('way');
  45. $sp = $request->post('spread_uid');
  46. $userInfo = User::getUserInfo($uid);
  47. BaseModel::beginTrans();
  48. try {
  49. self::addAchievement($uid);
  50. User::where('uid', $userInfo['uid'])->update(['integral' => bcadd($userInfo['integral'], 3000, 2)]);
  51. $res2 = false != UserBill::income('购买商品赠送积分', $uid, 'integral', 'gain', 3000, 0, $userInfo['integral'] + 3000, '购买商品赠送' . floatval(3000) . '积分');
  52. $res = $res2 && self::spreadIntegral($uid) && self::groupIntegral($uid);
  53. TreeRecommend::insertTree($uid, $sp, $way);
  54. self::pinkRecommend($uid);
  55. BaseModel::commitTrans();
  56. return app('json')->success('ok');
  57. } catch (\Exception $e) {
  58. BaseModel::rollbackTrans();
  59. return app('json')->fail($e->getMessage());
  60. }
  61. }
  62. public static function spreadIntegral($uid)
  63. {
  64. $recommend_integral = sys_config('recommend_integral_ratio', 0, true);
  65. $integral = bcmul(3000, bcdiv($recommend_integral, 100, 4), 2);
  66. if ($integral > 0) {
  67. $userInfo = User::getUserInfo($uid);
  68. $spreadInfo = User::getUserInfo($userInfo['spread_uid']);
  69. if (!$spreadInfo) return true;
  70. $res1 = false != User::where('uid', $spreadInfo['uid'])->update(['integral' => bcadd($spreadInfo['integral'], $integral, 2)]);
  71. return false != UserBill::income('推荐用户购买商品赠送积分', $spreadInfo['uid'], 'integral', 'spread_gain', $integral, 0, $spreadInfo['integral'], '推荐用户购买商品赠送' . floatval($integral) . '积分');
  72. }
  73. return true;
  74. }
  75. public static function pinkRecommend($uid)
  76. {
  77. $user = User::where('uid', $uid)->find();
  78. $spread = User::where('uid', $user['spread_uid'])->find();
  79. if (!$spread) return true;
  80. if ($spread['last_recommend_award']) $return = sys_config('recommend_double', 0);
  81. else $return = sys_config('recommend_single', 0);
  82. if ($spread['integral'] < $return) return true;
  83. $res = true;
  84. if ($return > 0) {
  85. $balance_integral = User::where('uid', $spread['uid'])->value('integral');
  86. $balance_brokerage = User::where('uid', $spread['uid'])->value('brokerage_price');
  87. $res = $res && UserBill::expend('礼包推荐奖[积分转换]', $spread['uid'], 'integral', 'pink_recommend_pay', $return, 0, bcsub($balance_integral, $return, 2), '推荐用户购礼包商品,响亮积分转为推荐奖' . $return);
  88. $res = $res && UserBill::income('礼包推荐奖', $spread['uid'], 'now_money', 'brokerage', $return, 0, bcadd($balance_brokerage, $return, 2), '推荐用户购礼包商品' . $return);
  89. $res = $res && User::where('uid', $spread['uid'])->dec('integral', $return)->update();
  90. $res = $res && User::where('uid', $spread['uid'])->inc('brokerage_price', $return)->update();
  91. $res = $res && User::where('uid', $spread['uid'])->update(['last_recommend_award' => ($spread['last_recommend_award'] ? 0 : 1)]);
  92. }
  93. return $res;
  94. }
  95. public static function addAchievement($uid)
  96. {
  97. $res = Achievement::create(
  98. [
  99. 'uid' => $uid,
  100. 'from_uid' => $uid,
  101. 'achievement' => 3000,
  102. 'order_id' => 0,
  103. 'add_time' => time()
  104. ]
  105. ) && User::where('uid', $uid)->inc('achievement', 3000)->inc('self_achievement', 3000)->update();
  106. $spread = User::where('uid', $uid)->value('spread_uid');
  107. while ($spread) {
  108. $res = $res && Achievement::create(
  109. [
  110. 'uid' => $spread,
  111. 'from_uid' => $uid,
  112. 'achievement' => 3000,
  113. 'order_id' => 0,
  114. 'add_time' => time()
  115. ]
  116. ) && User::where('uid', $spread)->inc('achievement', 3000)->update();
  117. UserLevel::setLevelComplete($spread);
  118. $spread = User::where('uid', $spread)->value('spread_uid');
  119. }
  120. return $res;
  121. }
  122. public static function groupIntegral($uid): bool
  123. {
  124. $userInfo = User::getUserInfo($uid);
  125. $spread = User::getUserInfo($userInfo['spread_uid']);
  126. $send = 0;
  127. $res = true;
  128. while ($spread) {
  129. $level = UserLevel::getUserLevelInfo(UserLevel::getUserLevel($spread['uid']));
  130. if ($level) {
  131. $integral = bcmul(3000, bcdiv($level['group_point_award'], 100, 4), 2);
  132. if ($integral > $send) {
  133. $real_send = $integral - $send;
  134. $res = false != UserBill::income('团队用户购买商品赠送积分', $spread['uid'], 'integral', 'group_gain', $real_send, 0, $spread['integral'], '团队用户购买商品赠送' . floatval($real_send) . '积分', 0);
  135. $send = $integral;
  136. }
  137. }
  138. $spread = User::getUserInfo($spread['spread_uid']);
  139. }
  140. return $res;
  141. }
  142. public function open(Request $request)
  143. {
  144. Lottery::openLottery();
  145. return app('json')->success('ok');
  146. }
  147. /**
  148. * @param Request $request
  149. * @return mixed
  150. * @throws DataNotFoundException
  151. * @throws ModelNotFoundException
  152. * @throws \think\exception\DbException
  153. */
  154. public function index(Request $request)
  155. {
  156. $banner = sys_data('routine_home_banner') ?: [];//TODO 首页banner图
  157. $menus = sys_data('routine_home_menus') ?: [];//TODO 首页按钮
  158. $roll = sys_data('routine_home_roll_news') ?: [];//TODO 首页滚动新闻
  159. $activity = sys_data('routine_home_activity', 3) ?: [];//TODO 首页活动区域图片
  160. $site_name = sys_config('site_name');
  161. $routine_index_page = sys_data('routine_index_page');
  162. $info['fastInfo'] = $routine_index_page[0]['fast_info'] ?? '';//sys_config('fast_info');//TODO 快速选择简介
  163. $info['bastInfo'] = $routine_index_page[0]['bast_info'] ?? '';//sys_config('bast_info');//TODO 精品推荐简介
  164. $info['firstInfo'] = $routine_index_page[0]['first_info'] ?? '';//sys_config('first_info');//TODO 首发新品简介
  165. $info['salesInfo'] = $routine_index_page[0]['sales_info'] ?? '';//sys_config('sales_info');//TODO 促销单品简介
  166. $logoUrl = sys_config('routine_index_logo');//TODO 促销单品简介
  167. if (strstr($logoUrl, 'http') === false && $logoUrl) $logoUrl = sys_config('site_url') . $logoUrl;
  168. $logoUrl = str_replace('\\', '/', $logoUrl);
  169. $fastNumber = sys_config('fast_number', 0);//TODO 快速选择分类个数
  170. $bastNumber = sys_config('bast_number', 0);//TODO 精品推荐个数
  171. $firstNumber = sys_config('first_number', 0);//TODO 首发新品个数
  172. $promotionNumber = sys_config('promotion_number', 0);//TODO 首发新品个数
  173. $info['fastList'] = StoreCategory::byIndexList((int)$fastNumber, false);//TODO 快速选择分类个数
  174. $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);//TODO 精品推荐个数
  175. $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);//TODO 首发新品个数
  176. $info['bastBanner'] = sys_data('routine_home_bast_banner') ?? [];//TODO 首页精品推荐图片
  177. $benefit = StoreProduct::getBenefitProduct('id,image,store_name,cate_id,price,ot_price,stock,unit_name', $promotionNumber);//TODO 首页促销单品
  178. $lovely = sys_data('routine_home_new_banner') ?: [];//TODO 首发新品顶部图
  179. $likeInfo = StoreProduct::getHotProduct('id,image,store_name,cate_id,price,ot_price,unit_name', 10);//TODO 热门榜单 猜你喜欢
  180. $giftInfo = StoreProduct::getGiftProduct('id,image,store_name,cate_id,price,ot_price,unit_name', 10);//TODO 热门榜单 猜你喜欢
  181. $couponList = StoreCouponIssue::getIssueCouponList($request->uid(), 3);
  182. if ($request->uid()) {
  183. $subscribe = WechatUser::where('uid', $request->uid())->value('subscribe') ? true : false;
  184. } else {
  185. $subscribe = true;
  186. }
  187. $newGoodsBananr = sys_config('new_goods_bananr');
  188. $tengxun_map_key = sys_config('tengxun_map_key');
  189. return app('json')->successful(compact('giftInfo', 'banner', 'menus', 'roll', 'info', 'activity', 'lovely', 'benefit', 'likeInfo', 'logoUrl', 'couponList', 'site_name', 'subscribe', 'newGoodsBananr', 'tengxun_map_key'));
  190. }
  191. /**
  192. * @param Request $request
  193. * @return mixed
  194. * @throws DataNotFoundException
  195. * @throws DbException
  196. * @throws ModelNotFoundException
  197. */
  198. public function cityArea(Request $request)
  199. {
  200. $province = SystemCity::where('level', 0)->where('parent_id', 0)->field('name,city_id')->select()->each(function ($item) {
  201. $item['city'] = SystemCity::where('level', 1)
  202. ->where('parent_id', $item['city_id'])
  203. ->field('name,city_id')
  204. ->select()->each(function ($sub_item) {
  205. $sub_item['area'] = SystemCity::where('level', 2)
  206. ->where('parent_id', $sub_item['city_id'])
  207. ->column('name');
  208. unset($sub_item['city_id']);
  209. })->toArray();
  210. unset($item['city_id']);
  211. });
  212. return app('json')->success('ok', $province->toArray());
  213. }
  214. /**
  215. * 获取分享配置
  216. * @return mixed
  217. */
  218. public function share()
  219. {
  220. $data['img'] = sys_config('wechat_share_img');
  221. if (strstr($data['img'], 'http') === false) $data['img'] = sys_config('site_url') . $data['img'];
  222. $data['img'] = str_replace('\\', '/', $data['img']);
  223. $data['title'] = sys_config('wechat_share_title');
  224. $data['synopsis'] = sys_config('wechat_share_synopsis');
  225. return app('json')->successful(compact('data'));
  226. }
  227. /**
  228. * 获取个人中心菜单
  229. * @param Request $request
  230. * @return mixed
  231. * @throws DataNotFoundException
  232. * @throws ModelNotFoundException
  233. * @throws \think\exception\DbException
  234. */
  235. public function menu_user(Request $request)
  236. {
  237. $menusInfo = sys_data('routine_my_menus') ?? [];
  238. $user = $request->user();
  239. $vipOpen = sys_config('vip_open');
  240. $vipOpen = is_string($vipOpen) ? (int)$vipOpen : $vipOpen;
  241. foreach ($menusInfo as $key => &$value) {
  242. $value['pic'] = set_file_url($value['pic']);
  243. if ($value['id'] == 137 && !(intval(sys_config('store_brokerage_statu')) == 2 || $user->is_promoter == 1))
  244. unset($menusInfo[$key]);
  245. if ($value['id'] == 174 && !StoreService::orderServiceStatus($user->uid))
  246. unset($menusInfo[$key]);
  247. if (((!StoreService::orderServiceStatus($user->uid)) && (!SystemStoreStaff::verifyStatus($user->uid))) && $value['wap_url'] === '/order/order_cancellation')
  248. unset($menusInfo[$key]);
  249. if (((!StoreService::orderServiceStatus($user->uid)) && (!SystemStoreStaff::verifyStatus($user->uid))) && $value['wap_url'] === '/admin/order_cancellation/index')
  250. unset($menusInfo[$key]);
  251. if ((!StoreService::orderServiceStatus($user->uid)) && $value['wap_url'] === '/admin/order/index')
  252. unset($menusInfo[$key]);
  253. if ($value['wap_url'] == '/user/vip' && !$vipOpen)
  254. unset($menusInfo[$key]);
  255. if ($value['wap_url'] == '/customer/index' && !StoreService::orderServiceStatus($user->uid))
  256. unset($menusInfo[$key]);
  257. }
  258. return app('json')->successful(['routine_my_menus' => $menusInfo]);
  259. }
  260. /**
  261. * 热门搜索关键字获取
  262. * @return mixed
  263. * @throws DataNotFoundException
  264. * @throws ModelNotFoundException
  265. * @throws \think\exception\DbException
  266. */
  267. public function search()
  268. {
  269. $routineHotSearch = sys_data('routine_hot_search') ?? [];
  270. $searchKeyword = [];
  271. if (count($routineHotSearch)) {
  272. foreach ($routineHotSearch as $key => &$item) {
  273. array_push($searchKeyword, $item['title']);
  274. }
  275. }
  276. return app('json')->successful($searchKeyword);
  277. }
  278. /**
  279. * 图片上传
  280. * @param Request $request
  281. * @return mixed
  282. * @throws \Psr\SimpleCache\InvalidArgumentException
  283. */
  284. public function upload_image(Request $request)
  285. {
  286. $data = UtilService::postMore([
  287. ['filename', 'file'],
  288. ], $request);
  289. if (!$data['filename']) return app('json')->fail('参数有误');
  290. if (Cache::has('start_uploads_' . $request->uid()) && Cache::get('start_uploads_' . $request->uid()) >= 100) return app('json')->fail('非法操作');
  291. $upload_type = sys_config('upload_type', 1);
  292. $upload = new Upload((int)$upload_type, [
  293. 'accessKey' => sys_config('accessKey'),
  294. 'secretKey' => sys_config('secretKey'),
  295. 'uploadUrl' => sys_config('uploadUrl'),
  296. 'storageName' => sys_config('storage_name'),
  297. 'storageRegion' => sys_config('storage_region'),
  298. ]);
  299. $info = $upload->to('store/comment')->validate()->move($data['filename']);
  300. if ($info === false) {
  301. return app('json')->fail($upload->getError());
  302. }
  303. $res = $upload->getUploadInfo();
  304. SystemAttachment::attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 1, $upload_type, $res['time'], 2);
  305. if (Cache::has('start_uploads_' . $request->uid()))
  306. $start_uploads = (int)Cache::get('start_uploads_' . $request->uid());
  307. else
  308. $start_uploads = 0;
  309. $start_uploads++;
  310. Cache::set('start_uploads_' . $request->uid(), $start_uploads, 86400);
  311. $res['dir'] = path_to_url($res['dir']);
  312. if (strpos($res['dir'], 'http') === false) $res['dir'] = $request->domain() . $res['dir'];
  313. return app('json')->successful('图片上传成功!', ['name' => $res['name'], 'url' => $res['dir']]);
  314. }
  315. /**
  316. * 物流公司
  317. * @return mixed
  318. */
  319. public function logistics()
  320. {
  321. $expressList = Express::lst();
  322. if (!$expressList) return app('json')->successful([]);
  323. return app('json')->successful($expressList->hidden(['code', 'id', 'sort', 'is_show'])->toArray());
  324. }
  325. /**
  326. * 短信购买异步通知
  327. *
  328. * @param Request $request
  329. * @return mixed
  330. */
  331. public function sms_pay_notify(Request $request)
  332. {
  333. list($order_id, $price, $status, $num, $pay_time, $attach) = UtilService::postMore([
  334. ['order_id', ''],
  335. ['price', 0.00],
  336. ['status', 400],
  337. ['num', 0],
  338. ['pay_time', time()],
  339. ['attach', 0],
  340. ], $request, true);
  341. if ($status == 200) {
  342. ChannelService::instance()->send('PAY_SMS_SUCCESS', ['price' => $price, 'number' => $num], [$attach]);
  343. return app('json')->successful();
  344. }
  345. return app('json')->fail();
  346. }
  347. /**
  348. * 记录用户分享
  349. * @param Request $request
  350. * @return mixed
  351. */
  352. public function user_share(Request $request)
  353. {
  354. return app('json')->successful(UserBill::setUserShare($request->uid()));
  355. }
  356. /**
  357. * 获取图片base64
  358. * @param Request $request
  359. * @return mixed
  360. */
  361. public function get_image_base64(Request $request)
  362. {
  363. list($imageUrl, $codeUrl) = UtilService::postMore([
  364. ['image', ''],
  365. ['code', ''],
  366. ], $request, true);
  367. try {
  368. $codeTmp = $code = $codeUrl ? image_to_base64($codeUrl) : false;
  369. if (!$codeTmp) {
  370. $putCodeUrl = put_image($codeUrl);
  371. $code = $putCodeUrl ? image_to_base64($_SERVER['HTTP_HOST'] . '/' . $putCodeUrl) : false;
  372. $code ?? unlink($_SERVER["DOCUMENT_ROOT"] . '/' . $putCodeUrl);
  373. }
  374. $imageTmp = $image = $imageUrl ? image_to_base64($imageUrl) : false;
  375. if (!$imageTmp) {
  376. $putImageUrl = put_image($imageUrl);
  377. $image = $putImageUrl ? image_to_base64($_SERVER['HTTP_HOST'] . '/' . $putImageUrl) : false;
  378. $image ?? unlink($_SERVER["DOCUMENT_ROOT"] . '/' . $putImageUrl);
  379. }
  380. return app('json')->successful(compact('code', 'image'));
  381. } catch (\Exception $e) {
  382. return app('json')->fail($e->getMessage());
  383. }
  384. }
  385. /**
  386. * 门店列表
  387. * @return mixed
  388. */
  389. public function store_list(Request $request)
  390. {
  391. list($latitude, $longitude, $page, $limit) = UtilService::getMore([
  392. ['latitude', ''],
  393. ['longitude', ''],
  394. ['page', 1],
  395. ['limit', 10]
  396. ], $request, true);
  397. $list = SystemStore::lst($latitude, $longitude, $page, $limit);
  398. if (!$list) $list = [];
  399. $data['list'] = $list;
  400. $data['tengxun_map_key'] = sys_config('tengxun_map_key');
  401. return app('json')->successful($data);
  402. }
  403. /**
  404. * 查找城市数据
  405. * @param Request $request
  406. * @return mixed
  407. */
  408. public function city_list(Request $request)
  409. {
  410. $list = CacheService::get('CITY_LIST', function () {
  411. $list = SystemCity::with('children')->field(['city_id', 'name', 'id', 'parent_id'])->where('parent_id', 0)->order('id asc')->select()->toArray();
  412. $data = [];
  413. foreach ($list as &$item) {
  414. $value = ['v' => $item['city_id'], 'n' => $item['name']];
  415. if ($item['children']) {
  416. foreach ($item['children'] as $key => &$child) {
  417. $value['c'][$key] = ['v' => $child['city_id'], 'n' => $child['name']];
  418. unset($child['id'], $child['area_code'], $child['merger_name'], $child['is_show'], $child['level'], $child['lng'], $child['lat'], $child['lat']);
  419. if (SystemCity::where('parent_id', $child['city_id'])->count()) {
  420. $child['children'] = SystemCity::where('parent_id', $child['city_id'])->field(['city_id', 'name', 'id', 'parent_id'])->select()->toArray();
  421. foreach ($child['children'] as $kk => $vv) {
  422. $value['c'][$key]['c'][$kk] = ['v' => $vv['city_id'], 'n' => $vv['name']];
  423. }
  424. }
  425. }
  426. }
  427. $data[] = $value;
  428. }
  429. return $data;
  430. }, 0);
  431. return app('json')->successful($list);
  432. }
  433. }