UserController.php 30 KB

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