UserController.php 30 KB

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