UserController.php 35 KB

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