UserController.php 28 KB

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