UserController.php 31 KB

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