UserController.php 34 KB

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