UserController.php 44 KB

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