UserController.php 31 KB

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