UserController.php 35 KB

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