UserController.php 27 KB

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