UserController.php 35 KB

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