UserController.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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 app\models\user\WechatUser;
  9. use think\db\exception\DataNotFoundException;
  10. use think\db\exception\DbException;
  11. use think\db\exception\ModelNotFoundException;
  12. use think\exception\ValidateException;
  13. use app\Request;
  14. use app\models\user\UserLevel;
  15. use app\models\user\UserSign;
  16. use app\models\store\StoreBargain;
  17. use app\models\store\StoreCombination;
  18. use app\models\store\StoreCouponUser;
  19. use app\models\store\StoreOrder;
  20. use app\models\store\StoreProductRelation;
  21. use app\models\store\StoreSeckill;
  22. use app\models\user\User;
  23. use app\models\user\UserAddress;
  24. use app\models\user\UserBill;
  25. use app\models\user\UserExtract;
  26. use app\models\user\UserNotice;
  27. use crmeb\services\GroupDataService;
  28. use crmeb\services\UtilService;
  29. use think\facade\Db;
  30. /**
  31. * 用户类
  32. * Class UserController
  33. * @package app\api\controller\store
  34. */
  35. class UserController
  36. {
  37. /**
  38. * 获取用户信息
  39. * @param Request $request
  40. * @return mixed
  41. */
  42. public function userInfo(Request $request)
  43. {
  44. $info = $request->user()->toArray();
  45. $broken_time = intval(sys_config('extract_time'));
  46. $search_time = time() - 86400 * $broken_time;
  47. //返佣 +
  48. $brokerage_commission = UserBill::where(['uid' => $info['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  49. ->where('add_time', '>', $search_time)
  50. ->where('pm', 1)
  51. ->sum('number');
  52. //退款退的佣金 -
  53. $refund_commission = UserBill::where(['uid' => $info['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  54. ->where('add_time', '>', $search_time)
  55. ->where('pm', 0)
  56. ->sum('number');
  57. $info['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  58. if ($info['broken_commission'] < 0)
  59. $info['broken_commission'] = 0;
  60. $info['commissionCount'] = bcsub($info['brokerage_price'], $info['broken_commission'], 2);
  61. if ($info['commissionCount'] < 0)
  62. $info['commissionCount'] = 0;
  63. if ($info['uid'] != 10045){
  64. UserLevel::setLevelComplete($info['uid']);
  65. }
  66. $info['is_enter'] = 0;
  67. if (UserEnter::where('uid', $info['uid'])->where('status', 0)->find()){
  68. $info['is_enter'] = 1;
  69. }elseif (UserEnter::where('uid', $info['uid'])->where('status', 1)->find()){
  70. $info['is_enter'] = 2;
  71. }
  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. * @throws DataNotFoundException
  598. * @throws DbException
  599. * @throws ModelNotFoundException
  600. */
  601. public function dow_user(Request $request)
  602. {
  603. $data = UtilService::postMore([
  604. 'phone',
  605. 'money',
  606. 'type',
  607. ]);
  608. if ($data['type'] == 1){
  609. if (cache($request->uid())) return app('json')->fail('请勿频繁点击');
  610. // if (empty($data['pas'])) return app('json')->fail('请填写支付密码');
  611. // if ($request->user()['payment_pas'] != md5($data['pas'].'sxg')) return app('json')->fail('支付密码不正确');
  612. if ($data['phone'] == $request->user()['account'] || $data['phone'] == $request->uid()) return app('json')->fail('不能给自己转账');
  613. if (empty($data['phone']) or empty($data['money'])) return app('json')->fail('手机号,金额不能未空');
  614. $user = User::where('account|uid', $data['phone'])->find();
  615. if (!$user) return app('json')->fail('没有当前用户');
  616. $userInfo = User::where('uid', $request->uid())->find();
  617. if ($userInfo['now_money'] < $data['money']) return app('json')->fail('余额不足');
  618. User::beginTrans();
  619. $user['now_money'] += $data['money'];
  620. $userInfo['now_money'] -= $data['money'];
  621. try {
  622. $user->save();
  623. $userInfo->save();
  624. UserBill::expend('转账', $userInfo['uid'], 'now_money', 'sub_now_money', $data['money'], $userInfo['uid'], $userInfo['now_money'], '给用户'.$user['nickname'].'转账'.$data['money']);
  625. UserBill::income('收账', $user['uid'], 'now_money', 'add_now_money', $data['money'], $user['uid'], $user['now_money'], '用户'.$userInfo['nickname'].'给你转账'.$data['money']);
  626. cache($request->uid(), 1, 5);
  627. User::commitTrans();
  628. return app('json')->success('成功');
  629. } catch (\Exception $e) {
  630. User::rollbackTrans();
  631. return app('json')->fail('失败');
  632. }
  633. }elseif ($data['type'] == 2){
  634. if (cache($request->uid())) return app('json')->fail('请勿频繁点击');
  635. // if (empty($data['pas'])) return app('json')->fail('请填写支付密码');
  636. // if ($request->user()['payment_pas'] != md5($data['pas'].'sxg')) return app('json')->fail('支付密码不正确');
  637. if ($data['phone'] == $request->user()['account'] || $data['phone'] == $request->uid()) return app('json')->fail('不能给自己转账');
  638. if (empty($data['phone']) or empty($data['money'])) return app('json')->fail('手机号,金额不能未空');
  639. $user = User::where('account|uid', $data['phone'])->find();
  640. if (!$user) return app('json')->fail('没有当前用户');
  641. $userInfo = User::where('uid', $request->uid())->find();
  642. if ($userInfo['business_integral'] < $data['money']) return app('json')->fail('积分不足');
  643. User::beginTrans();
  644. $user['business_integral'] += $data['money'];
  645. $userInfo['business_integral'] -= $data['money'];
  646. try {
  647. $user->save();
  648. $userInfo->save();
  649. UserBill::expend('转账', $userInfo['uid'], 'business_integral', 'sub_business_integral', $data['money'], $userInfo['uid'], $userInfo['now_money'], '给用户'.$user['nickname'].'转商家积分'.$data['money']);
  650. UserBill::income('收账', $user['uid'], 'business_integral', 'add_business_integral', $data['money'], $user['uid'], $user['now_money'], '用户'.$userInfo['nickname'].'给你转商家积分'.$data['money']);
  651. cache($request->uid(), 1, 5);
  652. User::commitTrans();
  653. return app('json')->success('成功');
  654. } catch (\Exception $e) {
  655. User::rollbackTrans();
  656. return app('json')->fail('失败');
  657. }
  658. }
  659. return app('json')->fail('参数传入错误');
  660. }
  661. /**
  662. * 团队下级人数和流水
  663. * @param Request $request
  664. * @return mixed
  665. * @throws DataNotFoundException
  666. * @throws DbException
  667. * @throws ModelNotFoundException
  668. */
  669. public function dow(Request $request)
  670. {
  671. $user = User::select();
  672. $userId = get_downline_dow($user, $request->uid());
  673. $data = [
  674. 'count' => 0,
  675. 'user_list' => [],
  676. 'user_price' => 0,
  677. 'week_price' => 0
  678. ];
  679. $week = date('w');
  680. if (count($userId) > 0){
  681. $count = count($userId);
  682. $userList = User::where('uid', 'in', $userId)
  683. ->field('uid,nickname,avatar,add_time,spread_uid,phone')
  684. ->select();
  685. foreach ($userList as &$item){
  686. $item['zt'] = 0;
  687. $item['phone'] = substr_replace($item['phone'], '****', 3, 4);
  688. if ($item['spread_uid'] == $request->uid()) $item['zt'] = 1;
  689. $item['price'] = ManyOrder::where('uid', $item['uid'])->where('status', 1)->sum('price');//返回额度
  690. $item['cy_price'] = ManyOrder::where('uid', $item['uid'])->where('status', 0)->where('is_return', 0)->sum('price'); // 参与中金额
  691. $item['dfh_price'] = ManyOrder::where('uid', $item['uid'])->where('status', 0)->where('is_return', 1)->sum('price'); // 待返回金额
  692. $item['ztr_price'] = ManyOrder::where('uid', $item['uid'])->sum('price'); // 总投注额度
  693. $item['order_price'] = StoreOrder::where('uid', $item['uid'])->where('paid', 1)->sum('pay_price');
  694. }
  695. $userPrice = ManyOrder::where('uid', 'in', $userId)->where('status', 1)->sum('price');
  696. $weekPrice = ManyOrder::where('uid', 'in', $userId)->whereBetweenTime('create_time', strtotime(date('Y-m-d',strtotime( '+'. 1-$week .' days' ))), strtotime(date('Y-m-d',strtotime( '+'. 7-$week .' days' ))))->sum('price');
  697. $data = [
  698. 'count' => $count,
  699. 'user_price' => $userPrice,
  700. 'week_price' => $weekPrice,
  701. 'user_list' => $userList
  702. ];
  703. }
  704. return app('json')->success($data);
  705. }
  706. /**
  707. * 消息通知
  708. * @param Request $request
  709. * @return mixed
  710. * @throws DataNotFoundException
  711. * @throws DbException
  712. * @throws ModelNotFoundException
  713. */
  714. public function notice(Request $request)
  715. {
  716. $data = UtilService::getMore([
  717. 'type',
  718. 'page',
  719. 'limit'
  720. ]);
  721. if ($data['type']){
  722. $list = UserNotice::where('type', $data['type'])->where('uid', $request->uid())->order('id DESC')->page($data['page'], $data['limit'])->select();//系统消息
  723. $list = empty($list)?[]:$list->toArray();
  724. foreach ($list as &$item)
  725. {
  726. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  727. }
  728. return app('json')->success($list);
  729. }else{
  730. $list['xt'] = UserNotice::where('type', 1)->where('uid', $request->uid())->order('id DESC')->find();//系统消息
  731. if ($list['xt']) $list['xt']['add_time'] = date('Y-m-d H:i:s', $list['xt']['add_time']);
  732. $list['xx'] = UserNotice::where('type', 2)->where('uid', $request->uid())->order('id DESC')->find();//新增下线
  733. if ($list['xx'])$list['xx']['add_time'] = date('Y-m-d H:i:s', $list['xx']['add_time']);
  734. $list['yj'] = UserNotice::where('type', 3)->where('uid', $request->uid())->order('id DESC')->find();//佣金通知
  735. if ($list['yj'])$list['yj']['add_time'] = date('Y-m-d H:i:s', $list['yj']['add_time']);
  736. $list['fh'] = UserNotice::where('type', 4)->where('uid', $request->uid())->order('id DESC')->find();//发货通知
  737. if ($list['fh'])$list['fh']['add_time'] = date('Y-m-d H:i:s', $list['fh']['add_time']);
  738. $list['dg'] = UserNotice::where('type', 5)->where('uid', $request->uid())->order('id DESC')->find();//打怪通知
  739. if ($list['dg'])$list['dg']['add_time'] = date('Y-m-d H:i:s', $list['dg']['add_time']);
  740. return app('json')->success($list);
  741. }
  742. }
  743. public function read(Request $request)
  744. {
  745. $data = UtilService::getMore([
  746. 'id',
  747. ]);
  748. $notice = UserNotice::where('id', $data['id'])->find();
  749. if (!$notice) return app('json')->fail('消息不存在');
  750. $notice['status'] = 1;
  751. $notice->save();
  752. $notice = empty($notice)?[]:$notice->toArray();
  753. return app('json')->success($notice);
  754. }
  755. /**
  756. * 会员紧缩
  757. * @return void
  758. * @throws DataNotFoundException
  759. * @throws DbException
  760. * @throws ModelNotFoundException
  761. */
  762. public function tighten()
  763. {
  764. $user = User::select();
  765. foreach ($user as $item)
  766. {
  767. $time = time() - $item['add_time'];
  768. if ($time > (86400*30)){
  769. $many_order = ManyOrder::where('uid', $item['uid'])->whereBetweenTime('create_time', strtotime('-30 day'), time())->count();
  770. if ($many_order == 0){
  771. User::where('spread_uid', $item['uid'])->update(['spread_uid' => $item['spread_uid']]);
  772. User::where('uid', $item['uid'])->delete();
  773. WechatUser::where('uid', $item['uid'])->delete();
  774. }
  775. }
  776. }
  777. return app('json')->success('成功');
  778. }
  779. /**
  780. * @param Request $request
  781. * @return mixed
  782. * @throws DataNotFoundException
  783. * @throws DbException
  784. * @throws ModelNotFoundException
  785. */
  786. public function turn(Request $request)
  787. {
  788. $data = UtilService::postMore([
  789. ['price', 0],
  790. ['uid', 0]
  791. ]);
  792. if ($data['price'] <= 0) return app('json')->fail('请输入转账额度');
  793. if ($data['uid'] <= 0) return app('json')->fail('请输入转账人手机号或者编号');
  794. $id = $request->uid();
  795. $user = User::where('uid', $id)->find(); // 转账人
  796. $user_e = User::where('uid|phone', $data['uid'])->find(); // 收转人
  797. if ($user['purple_integral'] < $data['price']) return app('json')->fail('转账积分不足');
  798. if (!$user_e) return app('json')->fail('转账用户不存在');
  799. $userList = User::select()->toArray();
  800. $top = getParent($user['spread_uid'], $userList);
  801. $lower = get_downline_dow($userList, $user['uid']);
  802. $spread = array_merge($top, $lower);
  803. if (!in_array($user_e['uid'],$spread)) return app('json')->fail('该用户不是你的同一条线');
  804. try {
  805. Db::startTrans();
  806. User::where('uid', $user['uid'])->dec('purple_integral', $data['price'])->update();
  807. User::where('uid', $user_e['uid'])->inc('white_integral', $data['price'])->update();
  808. UserBill::expend('转账阳光积分', $user['uid'], 'purple_integral', 'turn_purple_integral', $data['price'], $user_e['uid'], $user['purple_integral']-$data['price'], '转账给用户'.$user_e['account'].'阳光积分');
  809. UserBill::income('接收红积分', $user_e['uid'], 'white_integral', 'jies_purple_integral', $data['price'], $user['uid'], $user_e['white_integral']+$data['price'], '接收用户'.$user['account'].'阳光积分转为红积分');
  810. Db::commit();
  811. return app('json')->success('转账成功');
  812. } catch (\Exception $e) {
  813. Db::rollback();
  814. return app('json')->success('操作失败');
  815. }
  816. }
  817. }