UserController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\admin\model\store\ProductCode;
  4. use app\admin\model\ump\WriteOff;
  5. use app\http\validates\user\AddressValidate;
  6. use app\models\store\StoreCoupon;
  7. use app\models\store\StoreProduct;
  8. use app\models\system\SystemCity;
  9. use app\models\system\SystemStoreStaff;
  10. use app\models\user\UserVisit;
  11. use think\db\exception\DataNotFoundException;
  12. use think\db\exception\DbException;
  13. use think\db\exception\ModelNotFoundException;
  14. use think\exception\ValidateException;
  15. use app\Request;
  16. use app\models\user\UserLevel;
  17. use app\models\user\UserSign;
  18. use app\models\store\StoreBargain;
  19. use app\models\store\StoreCombination;
  20. use app\models\store\StoreCouponUser;
  21. use app\models\store\StoreOrder;
  22. use app\models\store\StoreProductRelation;
  23. use app\models\store\StoreSeckill;
  24. use app\models\user\User;
  25. use app\models\user\UserAddress;
  26. use app\models\user\UserBill;
  27. use app\models\user\UserExtract;
  28. use app\models\user\UserNotice;
  29. use crmeb\services\GroupDataService;
  30. use crmeb\services\UtilService;
  31. use think\facade\Db;
  32. /**
  33. * 用户类
  34. * Class UserController
  35. * @package app\api\controller\store
  36. */
  37. class UserController
  38. {
  39. /**
  40. * 获取用户信息
  41. * @param Request $request
  42. * @return mixed
  43. */
  44. public function userInfo(Request $request)
  45. {
  46. $info = $request->user()->toArray();
  47. StoreCouponUser::checkInvalidCoupon();
  48. $broken_time = intval(sys_config('extract_time'));
  49. $search_time = time() - 86400 * $broken_time;
  50. //返佣 +
  51. $brokerage_commission = UserBill::where(['uid' => $info['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  52. ->where('add_time', '>', $search_time)
  53. ->where('pm', 1)
  54. ->sum('number');
  55. //退款退的佣金 -
  56. $refund_commission = UserBill::where(['uid' => $info['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  57. ->where('add_time', '>', $search_time)
  58. ->where('pm', 0)
  59. ->sum('number');
  60. $info['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  61. if ($info['broken_commission'] < 0)
  62. $info['broken_commission'] = 0;
  63. $info['commissionCount'] = bcsub($info['brokerage_price'], $info['broken_commission'], 2);
  64. if ($info['commissionCount'] < 0)
  65. $info['commissionCount'] = 0;
  66. if ($info['level'] == 1){
  67. $info['level_name'] = '会员';
  68. }elseif ($info['level'] == 2){
  69. $info['level_name'] = '创业合伙人';
  70. }else{
  71. $info['level_name'] = '普通用户';
  72. }
  73. $info['spread_nickname'] = User::where('uid', $info['spread_uid'])->value('nickname')?? '';
  74. $info['clerk_type'] = 0;
  75. if (SystemStoreStaff::where('uid', $info['uid'])->find()) $info['clerk_type'] = 1;
  76. return app('json')->success($info);
  77. }
  78. /**
  79. * 用户资金统计
  80. * @param Request $request
  81. * @return mixed
  82. * @throws \think\Exception
  83. * @throws DataNotFoundException
  84. * @throws ModelNotFoundException
  85. * @throws \think\exception\DbException
  86. */
  87. public function balance(Request $request)
  88. {
  89. $uid = $request->uid();
  90. $user['now_money'] = User::getUserInfo($uid, 'now_money')['now_money'];//当前总资金
  91. $user['recharge'] = UserBill::getRecharge($uid);//累计充值
  92. $user['orderStatusSum'] = StoreOrder::getOrderStatusSum($uid);//累计消费
  93. return app('json')->successful($user);
  94. }
  95. /**
  96. * 个人中心
  97. * @param Request $request
  98. * @return mixed
  99. */
  100. public function user(Request $request)
  101. {
  102. $user = $request->user();
  103. $user = $user->toArray();
  104. $user['couponCount'] = StoreCouponUser::getUserValidCouponCount($user['uid']);
  105. $user['like'] = StoreProductRelation::getUserIdCollect($user['uid']);
  106. $user['orderStatusNum'] = StoreOrder::getOrderData($user['uid']);
  107. $user['notice'] = UserNotice::getNotice($user['uid']);
  108. // $user['brokerage'] = UserBill::getBrokerage($user['uid']);//获取总佣金
  109. $user['recharge'] = UserBill::getRecharge($user['uid']);//累计充值
  110. $user['orderStatusSum'] = StoreOrder::getOrderStatusSum($user['uid']);//累计消费
  111. $user['extractTotalPrice'] = UserExtract::userExtractTotalPrice($user['uid']);//累计提现
  112. $user['extractPrice'] = $user['brokerage_price'];//可提现
  113. $user['statu'] = (int)sys_config('store_brokerage_statu');
  114. $broken_time = intval(sys_config('extract_time'));
  115. $search_time = time() - 86400 * $broken_time;
  116. if (!$user['is_promoter'] && $user['statu'] == 2) {
  117. $price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'uid' => $user['uid']])->sum('pay_price');
  118. $status = is_brokerage_statu($price);
  119. if ($status) {
  120. User::where('uid', $user['uid'])->update(['is_promoter' => 1]);
  121. $user['is_promoter'] = 1;
  122. } else {
  123. $storeBrokeragePrice = sys_config('store_brokerage_price', 0);
  124. $user['promoter_price'] = bcsub($storeBrokeragePrice, $price, 2);
  125. }
  126. }
  127. //可提现佣金
  128. //返佣 +
  129. $brokerage_commission = UserBill::where(['uid' => $user['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  130. ->where('add_time', '>', $search_time)
  131. ->where('pm', 1)
  132. ->sum('number');
  133. //退款退的佣金 -
  134. $refund_commission = UserBill::where(['uid' => $user['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  135. ->where('add_time', '>', $search_time)
  136. ->where('pm', 0)
  137. ->sum('number');
  138. $user['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  139. if ($user['broken_commission'] < 0)
  140. $user['broken_commission'] = 0;
  141. $user['commissionCount'] = bcsub($user['brokerage_price'], $user['broken_commission'], 2);
  142. if ($user['commissionCount'] < 0)
  143. $user['commissionCount'] = 0;
  144. if (!sys_config('vip_open'))
  145. $user['vip'] = false;
  146. else {
  147. $vipId = UserLevel::getUserLevel($user['uid']);
  148. $user['vip'] = $vipId !== false ? true : false;
  149. if ($user['vip']) {
  150. $user['vip_id'] = $vipId;
  151. $user['vip_icon'] = UserLevel::getUserLevelInfo($vipId, 'icon');
  152. $user['vip_name'] = UserLevel::getUserLevelInfo($vipId, 'name');
  153. }
  154. }
  155. $user['yesterDay'] = UserBill::yesterdayCommissionSum($user['uid']);
  156. $user['recharge_switch'] = (int)sys_config('recharge_switch');//充值开关
  157. $user['adminid'] = (boolean)\app\models\store\StoreService::orderServiceStatus($user['uid']);
  158. if ($user['phone'] && $user['user_type'] != 'h5') {
  159. $user['switchUserInfo'][] = $request->user();
  160. if ($h5UserInfo = User::where('account', $user['phone'])->where('user_type', 'h5')->find()) {
  161. $user['switchUserInfo'][] = $h5UserInfo;
  162. }
  163. } else if ($user['phone'] && $user['user_type'] == 'h5') {
  164. if ($wechatUserInfo = User::where('phone', $user['phone'])->where('user_type', '<>', 'h5')->find()) {
  165. $user['switchUserInfo'][] = $wechatUserInfo;
  166. }
  167. $user['switchUserInfo'][] = $request->user();
  168. } else if (!$user['phone']) {
  169. $user['switchUserInfo'][] = $request->user();
  170. }
  171. return app('json')->successful($user);
  172. }
  173. /**
  174. * 地址 获取单个
  175. * @param Request $request
  176. * @param $id
  177. * @return mixed
  178. * @throws DataNotFoundException
  179. * @throws ModelNotFoundException
  180. * @throws \think\exception\DbException
  181. */
  182. public function address(Request $request, $id)
  183. {
  184. $addressInfo = [];
  185. if ($id && is_numeric($id) && UserAddress::be(['is_del' => 0, 'id' => $id, 'uid' => $request->uid()])) {
  186. $addressInfo = UserAddress::find($id)->toArray();
  187. }
  188. return app('json')->successful($addressInfo);
  189. }
  190. /**
  191. * 地址列表
  192. * @param Request $request
  193. * @param $page
  194. * @param $limit
  195. * @return mixed
  196. */
  197. public function address_list(Request $request)
  198. {
  199. list($page, $limit) = UtilService::getMore([['page', 0], ['limit', 20]], $request, true);
  200. $list = UserAddress::getUserValidAddressList($request->uid(), $page, $limit, 'id,real_name,phone,province,city,district,detail,is_default');
  201. return app('json')->successful($list);
  202. }
  203. /**
  204. * 设置默认地址
  205. *
  206. * @param Request $request
  207. * @return mixed
  208. */
  209. public function address_default_set(Request $request)
  210. {
  211. list($id) = UtilService::getMore([['id', 0]], $request, true);
  212. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
  213. if (!UserAddress::be(['is_del' => 0, 'id' => $id, 'uid' => $request->uid()]))
  214. return app('json')->fail('地址不存在!');
  215. $res = UserAddress::setDefaultAddress($id, $request->uid());
  216. if (!$res)
  217. return app('json')->fail('地址不存在!');
  218. else
  219. return app('json')->successful();
  220. }
  221. /**
  222. * 获取默认地址
  223. * @param Request $request
  224. * @return mixed
  225. */
  226. public function address_default(Request $request)
  227. {
  228. $defaultAddress = UserAddress::getUserDefaultAddress($request->uid(), 'id,real_name,phone,province,city,district,detail,is_default');
  229. if ($defaultAddress) {
  230. $defaultAddress = $defaultAddress->toArray();
  231. return app('json')->successful('ok', $defaultAddress);
  232. }
  233. return app('json')->successful('empty', []);
  234. }
  235. /**
  236. * 修改 添加地址
  237. * @param Request $request
  238. * @return mixed
  239. */
  240. public function address_edit(Request $request)
  241. {
  242. $addressInfo = UtilService::postMore([
  243. ['address', []],
  244. ['is_default', false],
  245. ['real_name', ''],
  246. ['post_code', ''],
  247. ['phone', ''],
  248. ['detail', ''],
  249. ['id', 0],
  250. ['type', 0]
  251. ], $request);
  252. if (!isset($addressInfo['address']['province'])) return app('json')->fail('收货地址格式错误!');
  253. if (!isset($addressInfo['address']['city'])) return app('json')->fail('收货地址格式错误!');
  254. if (!isset($addressInfo['address']['district'])) return app('json')->fail('收货地址格式错误!');
  255. if (!isset($addressInfo['address']['city_id']) && $addressInfo['type'] == 0) {
  256. return app('json')->fail('收货地址格式错误!请重新选择!');
  257. } else if ($addressInfo['type'] == 1) {
  258. $city = $addressInfo['address']['city'];
  259. $cityId = SystemCity::where('name', $city)->where('parent_id', '<>', 0)->value('city_id');
  260. if ($cityId) {
  261. $addressInfo['address']['city_id'] = $cityId;
  262. } else {
  263. if (!($cityId = SystemCity::where('parent_id', '<>', 0)->where('name', 'like', "%$city%")->value('city_id'))) {
  264. return app('json')->fail('收货地址格式错误!修改后请重新导入!');
  265. }
  266. $addressInfo['address']['city_id'] = $cityId;
  267. }
  268. }
  269. $addressInfo['province'] = $addressInfo['address']['province'];
  270. $addressInfo['city'] = $addressInfo['address']['city'];
  271. $addressInfo['city_id'] = $addressInfo['address']['city_id'] ?? 0;
  272. $addressInfo['district'] = $addressInfo['address']['district'];
  273. $addressInfo['is_default'] = (int)$addressInfo['is_default'] == true ? 1 : 0;
  274. $addressInfo['uid'] = $request->uid();
  275. unset($addressInfo['address'], $addressInfo['type']);
  276. try {
  277. validate(AddressValidate::class)->check($addressInfo);
  278. } catch (ValidateException $e) {
  279. return app('json')->fail($e->getError());
  280. }
  281. if ($addressInfo['id'] && UserAddress::be(['id' => $addressInfo['id'], 'uid' => $request->uid(), 'is_del' => 0])) {
  282. $id = $addressInfo['id'];
  283. unset($addressInfo['id']);
  284. if (UserAddress::edit($addressInfo, $id, 'id')) {
  285. if ($addressInfo['is_default'])
  286. UserAddress::setDefaultAddress($id, $request->uid());
  287. return app('json')->successful();
  288. } else
  289. return app('json')->fail('编辑收货地址失败!');
  290. } else {
  291. $addressInfo['add_time'] = time();
  292. if ($address = UserAddress::create($addressInfo)) {
  293. if ($addressInfo['is_default']) {
  294. UserAddress::setDefaultAddress($address->id, $request->uid());
  295. }
  296. return app('json')->successful(['id' => $address->id]);
  297. } else {
  298. return app('json')->fail('添加收货地址失败!');
  299. }
  300. }
  301. }
  302. /**
  303. * 删除地址
  304. *
  305. * @param Request $request
  306. * @return mixed
  307. */
  308. public function address_del(Request $request)
  309. {
  310. list($id) = UtilService::postMore([['id', 0]], $request, true);
  311. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
  312. if (!UserAddress::be(['is_del' => 0, 'id' => $id, 'uid' => $request->uid()]))
  313. return app('json')->fail('地址不存在!');
  314. if (UserAddress::edit(['is_del' => '1'], $id, 'id'))
  315. return app('json')->successful();
  316. else
  317. return app('json')->fail('删除地址失败!');
  318. }
  319. /**
  320. * 获取收藏产品
  321. *
  322. * @param Request $request
  323. * @return mixed
  324. */
  325. public function collect_user(Request $request)
  326. {
  327. list($page, $limit) = UtilService::getMore([
  328. ['page', 0],
  329. ['limit', 0]
  330. ], $request, true);
  331. if (!(int)$limit) return app('json')->successful([]);
  332. $productRelationList = StoreProductRelation::getUserCollectProduct($request->uid(), (int)$page, (int)$limit);
  333. return app('json')->successful($productRelationList);
  334. }
  335. /**
  336. * 添加收藏
  337. * @param Request $request
  338. * @param $id
  339. * @param $category
  340. * @return mixed
  341. */
  342. public function collect_add(Request $request)
  343. {
  344. list($id, $category) = UtilService::postMore([['id', 0], ['category', 'product']], $request, true);
  345. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误');
  346. $res = StoreProductRelation::productRelation($id, $request->uid(), 'collect', $category);
  347. if (!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
  348. else return app('json')->successful();
  349. }
  350. /**
  351. * 取消收藏
  352. *
  353. * @param Request $request
  354. * @return mixed
  355. */
  356. public function collect_del(Request $request)
  357. {
  358. list($id, $category) = UtilService::postMore([['id', 0], ['category', 'product']], $request, true);
  359. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误');
  360. $res = StoreProductRelation::unProductRelation($id, $request->uid(), 'collect', $category);
  361. if (!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
  362. else return app('json')->successful();
  363. }
  364. /**
  365. * 批量收藏
  366. * @param Request $request
  367. * @return mixed
  368. */
  369. public function collect_all(Request $request)
  370. {
  371. $collectInfo = UtilService::postMore([
  372. ['id', []],
  373. ['category', 'product'],
  374. ], $request);
  375. if (!count($collectInfo['id'])) return app('json')->fail('参数错误');
  376. $productIdS = $collectInfo['id'];
  377. $res = StoreProductRelation::productRelationAll($productIdS, $request->uid(), 'collect', $collectInfo['category']);
  378. if (!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
  379. else return app('json')->successful('收藏成功');
  380. }
  381. /**
  382. * 添加点赞
  383. *
  384. * @param Request $request
  385. * @return mixed
  386. */
  387. // public function like_add(Request $request)
  388. // {
  389. // list($id, $category) = UtilService::postMore([['id',0], ['category','product']], $request, true);
  390. // if(!$id || !is_numeric($id)) return app('json')->fail('参数错误');
  391. // $res = StoreProductRelation::productRelation($id,$request->uid(),'like',$category);
  392. // if(!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
  393. // else return app('json')->successful();
  394. // }
  395. /**
  396. * 取消点赞
  397. *
  398. * @param Request $request
  399. * @return mixed
  400. */
  401. // public function like_del(Request $request)
  402. // {
  403. // list($id, $category) = UtilService::postMore([['id',0], ['category','product']], $request, true);
  404. // if(!$id || !is_numeric($id)) return app('json')->fail('参数错误');
  405. // $res = StoreProductRelation::unProductRelation($id, $request->uid(),'like',$category);
  406. // if(!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
  407. // else return app('json')->successful();
  408. // }
  409. /**
  410. * 签到 配置
  411. * @return mixed
  412. * @throws DataNotFoundException
  413. * @throws ModelNotFoundException
  414. * @throws \think\exception\DbException
  415. */
  416. public function sign_config()
  417. {
  418. $signConfig = sys_data('sign_day_num') ?? [];
  419. return app('json')->successful($signConfig);
  420. }
  421. /**
  422. * 签到 列表
  423. * @param Request $request
  424. * @param $page
  425. * @param $limit
  426. * @return mixed
  427. */
  428. public function sign_list(Request $request)
  429. {
  430. list($page, $limit) = UtilService::getMore([
  431. ['page', 0],
  432. ['limit', 0]
  433. ], $request, true);
  434. if (!$limit) return app('json')->successful([]);
  435. $signList = UserSign::getSignList($request->uid(), (int)$page, (int)$limit);
  436. if ($signList) $signList = $signList->toArray();
  437. return app('json')->successful($signList);
  438. }
  439. /**
  440. * 签到
  441. * @param Request $request
  442. * @return mixed
  443. */
  444. public function sign_integral(Request $request)
  445. {
  446. $signed = UserSign::getIsSign($request->uid());
  447. if ($signed) return app('json')->fail('已签到');
  448. if (false !== ($integral = UserSign::sign($request->uid())))
  449. return app('json')->successful('签到获得' . floatval($integral) . '积分', ['integral' => $integral]);
  450. return app('json')->fail(UserSign::getErrorInfo('签到失败'));
  451. }
  452. /**
  453. * 签到用户信息
  454. * @param Request $request
  455. * @return mixed
  456. */
  457. public function sign_user(Request $request)
  458. {
  459. list($sign, $integral, $all) = UtilService::postMore([
  460. ['sign', 0],
  461. ['integral', 0],
  462. ['all', 0],
  463. ], $request, true);
  464. $user = $request->user();
  465. //是否统计签到
  466. if ($sign || $all) {
  467. $user['sum_sgin_day'] = UserSign::getSignSumDay($user['uid']);
  468. $user['is_day_sgin'] = UserSign::getIsSign($user['uid']);
  469. $user['is_YesterDay_sgin'] = UserSign::getIsSign($user['uid'], 'yesterday');
  470. if (!$user['is_day_sgin'] && !$user['is_YesterDay_sgin']) {
  471. $user['sign_num'] = 0;
  472. }
  473. }
  474. //是否统计积分使用情况
  475. if ($integral || $all) {
  476. $user['sum_integral'] = (int)UserBill::getRecordCount($user['uid'], 'integral', 'sign,system_add,gain');
  477. $user['deduction_integral'] = (int)UserBill::getRecordCount($user['uid'], 'integral', 'deduction', '', true) ?? 0;
  478. $user['today_integral'] = (int)UserBill::getRecordCount($user['uid'], 'integral', 'sign,system_add,gain', 'today');
  479. }
  480. unset($user['pwd']);
  481. if (!$user['is_promoter']) {
  482. $user['is_promoter'] = (int)sys_config('store_brokerage_statu') == 2 ? true : false;
  483. }
  484. return app('json')->successful($user->hidden(['account', 'real_name', 'birthday', 'card_id', 'mark', 'partner_id', 'group_id', 'add_time', 'add_ip', 'phone', 'last_time', 'last_ip', 'spread_uid', 'spread_time', 'user_type', 'status', 'level', 'clean_time', 'addres'])->toArray());
  485. }
  486. /**
  487. * 签到列表(年月)
  488. *
  489. * @param Request $request
  490. * @return mixed
  491. */
  492. public function sign_month(Request $request)
  493. {
  494. list($page, $limit) = UtilService::getMore([
  495. ['page', 0],
  496. ['limit', 0]
  497. ], $request, true);
  498. if (!$limit) return app('json')->successful([]);
  499. $userSignList = UserSign::getSignMonthList($request->uid(), (int)$page, (int)$limit);
  500. return app('json')->successful($userSignList);
  501. }
  502. /**
  503. * 获取活动状态
  504. * @return mixed
  505. */
  506. public function activity()
  507. {
  508. $data['is_bargin'] = StoreBargain::validBargain() ? true : false;
  509. $data['is_pink'] = StoreCombination::getPinkIsOpen() ? true : false;
  510. $data['is_seckill'] = StoreSeckill::getSeckillCount() ? true : false;
  511. return app('json')->successful($data);
  512. }
  513. /**
  514. * 用户修改信息
  515. * @param Request $request
  516. * @return mixed
  517. */
  518. public function edit(Request $request)
  519. {
  520. list($avatar, $nickname) = UtilService::postMore([
  521. ['avatar', ''],
  522. ['nickname', ''],
  523. ], $request, true);
  524. if (User::editUser($avatar, $nickname, $request->uid())) return app('json')->successful('修改成功');
  525. return app('json')->fail('修改失败');
  526. }
  527. /**
  528. * 推广人排行
  529. * @param Request $request
  530. * @return mixed
  531. * @throws DataNotFoundException
  532. * @throws ModelNotFoundException
  533. * @throws \think\exception\DbException
  534. */
  535. public function rank(Request $request)
  536. {
  537. $data = UtilService::getMore([
  538. ['page', ''],
  539. ['limit', ''],
  540. ['type', '']
  541. ], $request);
  542. $users = User::getRankList($data);
  543. return app('json')->success($users);
  544. }
  545. /**
  546. * 佣金排行
  547. * @param Request $request
  548. * @return mixed
  549. */
  550. public function brokerage_rank(Request $request)
  551. {
  552. $data = UtilService::getMore([
  553. ['page', ''],
  554. ['limit'],
  555. ['type']
  556. ], $request);
  557. return app('json')->success([
  558. 'rank' => User::brokerageRank($data),
  559. 'position' => User::currentUserRank($data['type'], $request->user()['brokerage_price'])
  560. ]);
  561. }
  562. /**
  563. * 添加访问记录
  564. * @param Request $request
  565. * @return mixed
  566. */
  567. public function set_visit(Request $request)
  568. {
  569. $data = UtilService::postMore([
  570. ['url', ''],
  571. ['stay_time', 0]
  572. ], $request);
  573. if ($data['url'] == '') return app('json')->fail('未获取页面路径');
  574. $data['uid'] = $request->uid();
  575. $data['ip'] = $request->ip();
  576. $data['add_time'] = time();
  577. $res = UserVisit::insert($data);
  578. if ($res) {
  579. return app('json')->success('添加访问记录成功');
  580. } else {
  581. return app('json')->fail('添加访问记录失败');
  582. }
  583. }
  584. /**
  585. * 静默绑定推广人
  586. * @param Request $request
  587. * @return mixed
  588. * @throws DataNotFoundException
  589. * @throws DbException
  590. * @throws ModelNotFoundException
  591. */
  592. public function spread(Request $request)
  593. {
  594. $puid = $request->post('puid/d', 0);
  595. return app('json')->success(User::setSpread($puid, $request->uid()));
  596. }
  597. /**
  598. * 核销记录
  599. * @param Request $request
  600. * @return mixed
  601. */
  602. public function write_off(Request $request)
  603. {
  604. $data = UtilService::postMore([
  605. ['page', 1],
  606. ['limit', 10]
  607. ], $request);
  608. $model = new WriteOff();
  609. $list = $model->alias('a')
  610. ->field('a.*,us.nickname,c.coupon_title,c.coupon_price,d.name as d_name')
  611. ->leftJoin('user b', 'b.uid = a.uid')
  612. ->leftJoin('store_coupon_user c', 'c.id = a.coupon_id')
  613. ->leftJoin('user us', 'us.uid = c.uid')
  614. ->leftJoin('system_store d', 'd.id = c.store_id')
  615. ->where('a.uid', $request->uid())
  616. ->page($data['page'], $data['limit'])
  617. ->select();
  618. if ($list){
  619. $list = $list->toArray();
  620. foreach ($list as &$v){
  621. $v['add_time'] = $v['add_time'] > 0 ? date('Y-m-d H:i:s', $v['add_time']) : 0;
  622. }
  623. }
  624. return app('json')->success($list);
  625. }
  626. /**
  627. * 用户优惠券
  628. * @param Request $request
  629. * @return mixed
  630. * @throws DataNotFoundException
  631. * @throws DbException
  632. * @throws ModelNotFoundException
  633. */
  634. public function coupon_user(Request $request)
  635. {
  636. $data = UtilService::postMore([
  637. ['status', 0],
  638. ['page', 1],
  639. ['limit', 10]
  640. ], $request);
  641. $coupon = StoreCouponUser::alias('a')
  642. ->field('a.*,c.id as c_id,c.name,c.image,c.latitude,c.longitude,c.detailed_address')
  643. ->order('a.id DESC')
  644. ->leftJoin('store_coupon b', 'a.cid = b.id')
  645. ->leftJoin('system_store c', 'b.store_id = c.id')
  646. ->where('a.uid', $request->uid())
  647. ->where('a.status', $data['status'])
  648. ->select();
  649. $coupon = count($coupon) > 0?$coupon->toArray() : [];
  650. return app('json')->success($coupon);
  651. }
  652. /**
  653. * 分享领取优惠券
  654. * @param Request $request
  655. * @return mixed
  656. * @throws DataNotFoundException
  657. * @throws DbException
  658. * @throws ModelNotFoundException
  659. */
  660. public function share(Request $request)
  661. {
  662. $data = UtilService::postMore([
  663. ['code', ''],
  664. ], $request);
  665. if (!$data['code']) return app('json')->fail('优惠券不存在');
  666. $coupon = StoreCouponUser::where('code', $data['code'])->find();
  667. if ($coupon['status'] == 1) return app('json')->fail('优惠券已使用');
  668. if ($coupon['status'] == 2) return app('json')->fail('优惠券已过期');
  669. if ($coupon['uid'] == $request->uid()) return app('json')->fail('自己不能领取自己的优惠券');
  670. if ($coupon['share_status'] == 1) return app('json')->fail('优惠券已分享');
  671. $coupon['uid'] = $request->uid();
  672. $coupon['share_status'] = 1;
  673. $order = StoreOrder::where('order_id', $coupon['order_id'])->find();
  674. if ($order['use'] < 1){
  675. $order_coupon = StoreCouponUser::where('order_id', $coupon['order_id'])->where('uid', $order['uid'])->where('code', '<>', $data['code'])->where('status', '<', 1)->count();
  676. // 如果购买用户本人优惠券分享出去后没有可用的优惠券订单变为已完成
  677. if ($order_coupon == 0) StoreOrder::where('order_id', $coupon['order_id'])->update(['status' => 3, 'use' => 1]);
  678. }
  679. $res = $coupon->save();
  680. if ($res) return app('json')->success('领取成功');
  681. return app('json')->fail('领取失败');
  682. }
  683. public function coupon_datalis(Request $request)
  684. {
  685. $data = UtilService::postMore([
  686. ['code', ''],
  687. ], $request);
  688. $coupon = StoreCouponUser::where('code', $data['code'])->find();
  689. if (!$coupon) return app('json')->fail('优惠券不存在');
  690. return app('json')->success($coupon->toArray());
  691. }
  692. /**
  693. * 兑换码兑换
  694. * @param Request $request
  695. * @return mixed
  696. * @throws DataNotFoundException
  697. * @throws DbException
  698. * @throws ModelNotFoundException
  699. */
  700. public function exchange(Request $request)
  701. {
  702. $data = UtilService::postMore([
  703. ['code'],
  704. ], $request);
  705. if (!$data['code']) return app('json')->fail('请输入兑换码');
  706. $product_code = ProductCode::where('code', $data['code'])->find();
  707. if (!$product_code) return app('json')->fail('兑换码不存在');
  708. if ($product_code['status'] == 1) return app('json')->fail('兑换码已使用');
  709. $product = StoreProduct::where('id', $product_code['product_id'])->find();
  710. $coupon = StoreCoupon::where('id', 'in', $product['coupon'])->select();
  711. if ($coupon) {
  712. foreach ($coupon as $value) {
  713. $save[] = [
  714. 'cid' => $value['id'],
  715. 'uid' => $request->uid(),
  716. 'order_id' => '',
  717. 'coupon_title' => $value['title'],
  718. 'coupon_price' => $value['coupon_price'],
  719. 'use_min_price' => $value['use_min_price'],
  720. 'add_time' => $value['start_time'] == 0 ? 0 : $value['start_time'],
  721. 'end_time' => $value['end_time'] == 0 ? 0 : $value['end_time'],
  722. 'code' => generate_promotion_code(1),
  723. 'store_id' => $value['store_id'],
  724. ];
  725. }
  726. }
  727. $model = new StoreCouponUser();
  728. try {
  729. Db::startTrans();
  730. $product_code['status'] = 1;
  731. $product_code['end_time'] = time();
  732. $product_code->save();
  733. $model->saveAll($save);
  734. Db::commit();
  735. return app('json')->success('兑换成功');
  736. } catch (\Exception $e) {
  737. Db::rollback();
  738. return app('json')->fail('兑换失败');
  739. }
  740. }
  741. }