UserController.php 37 KB

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