UserController.php 31 KB

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