UserController.php 33 KB

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