UserController.php 42 KB

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