UserController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\admin\model\system\SystemAdmin;
  4. use app\http\validates\user\AddressValidate;
  5. use app\models\system\SystemCity;
  6. use app\models\system\SystemStore;
  7. use app\models\system\SystemStoreStaff;
  8. use app\models\user\UserVisit;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\services\workerman\Response;
  11. use think\db\exception\DataNotFoundException;
  12. use think\db\exception\DbException;
  13. use think\db\exception\ModelNotFoundException;
  14. use think\exception\ValidateException;
  15. use app\Request;
  16. use app\models\user\UserLevel;
  17. use app\models\user\UserSign;
  18. use app\models\store\StoreBargain;
  19. use app\models\store\StoreCombination;
  20. use app\models\store\StoreCouponUser;
  21. use app\models\store\StoreOrder;
  22. use app\models\store\StoreProductRelation;
  23. use app\models\store\StoreSeckill;
  24. use app\models\user\User;
  25. use app\models\user\UserAddress;
  26. use app\models\user\UserBill;
  27. use app\models\user\UserExtract;
  28. use app\models\user\UserNotice;
  29. use crmeb\services\GroupDataService;
  30. use crmeb\services\UtilService;
  31. /**
  32. * 用户类
  33. * Class UserController
  34. * @package app\api\controller\store
  35. */
  36. class UserController
  37. {
  38. public function userList(Request $request)
  39. {
  40. list($keyword) = UtilService::getMore([['keyword', '']], $request, true);
  41. return app('json')->success('ok', User::where('status', 1)->where('nickname|uid', 'like', "%$keyword%")->field('nickname,uid')->select()->toArray());
  42. }
  43. /**
  44. * 获取用户信息
  45. * @param Request $request
  46. * @return mixed
  47. */
  48. public function userInfo(Request $request)
  49. {
  50. $info = $request->user()->toArray();
  51. $broken_time = intval(sys_config('extract_time'));
  52. $search_time = time() - 86400 * $broken_time;
  53. //返佣 +
  54. $brokerage_commission = UserBill::where(['uid' => $info['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  55. ->where('add_time', '>', $search_time)
  56. ->where('pm', 1)
  57. ->sum('number');
  58. //退款退的佣金 -
  59. $refund_commission = UserBill::where(['uid' => $info['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  60. ->where('add_time', '>', $search_time)
  61. ->where('pm', 0)
  62. ->sum('number');
  63. $info['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  64. if ($info['broken_commission'] < 0)
  65. $info['broken_commission'] = 0;
  66. $info['commissionCount'] = bcsub($info['brokerage_price'], $info['broken_commission'], 2);
  67. if ($info['commissionCount'] < 0)
  68. $info['commissionCount'] = 0;
  69. return app('json')->success($info);
  70. }
  71. /**
  72. * 用户资金统计
  73. * @param Request $request
  74. * @return mixed
  75. * @throws \think\Exception
  76. * @throws DataNotFoundException
  77. * @throws ModelNotFoundException
  78. * @throws \think\exception\DbException
  79. */
  80. public function balance(Request $request)
  81. {
  82. $uid = $request->uid();
  83. $user['now_money'] = User::getUserInfo($uid, 'now_money')['now_money'];//当前总资金
  84. $user['recharge'] = UserBill::getRecharge($uid);//累计充值
  85. $user['orderStatusSum'] = StoreOrder::getOrderStatusSum($uid);//累计消费
  86. return app('json')->successful($user);
  87. }
  88. /**
  89. * 个人中心
  90. * @param Request $request
  91. * @return mixed
  92. */
  93. public function user(Request $request)
  94. {
  95. $user = $request->user();
  96. $user = $user->toArray();
  97. $user['couponCount'] = StoreCouponUser::getUserValidCouponCount($user['uid']);
  98. $user['like'] = StoreProductRelation::getUserIdCollect($user['uid']);
  99. $user['orderStatusNum'] = StoreOrder::getOrderData($user['uid']);
  100. $user['notice'] = UserNotice::getNotice($user['uid']);
  101. // $user['brokerage'] = UserBill::getBrokerage($user['uid']);//获取总佣金
  102. $user['recharge'] = UserBill::getRecharge($user['uid']);//累计充值
  103. $user['orderStatusSum'] = StoreOrder::getOrderStatusSum($user['uid']);//累计消费
  104. $user['extractTotalPrice'] = UserExtract::userExtractTotalPrice($user['uid']);//累计提现
  105. $user['extractPrice'] = $user['brokerage_price'];//可提现
  106. $user['statu'] = (int)sys_config('store_brokerage_statu');
  107. $broken_time = intval(sys_config('extract_time'));
  108. $search_time = time() - 86400 * $broken_time;
  109. if (!$user['is_promoter'] && $user['statu'] == 2) {
  110. $price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'uid' => $user['uid']])->sum('pay_price');
  111. $status = is_brokerage_statu($price);
  112. if ($status) {
  113. User::where('uid', $user['uid'])->update(['is_promoter' => 1]);
  114. $user['is_promoter'] = 1;
  115. } else {
  116. $storeBrokeragePrice = sys_config('store_brokerage_price', 0);
  117. $user['promoter_price'] = bcsub($storeBrokeragePrice, $price, 2);
  118. }
  119. }
  120. //可提现佣金
  121. //返佣 +
  122. $brokerage_commission = UserBill::where(['uid' => $user['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  123. ->where('add_time', '>', $search_time)
  124. ->where('pm', 1)
  125. ->sum('number');
  126. //退款退的佣金 -
  127. $refund_commission = UserBill::where(['uid' => $user['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  128. ->where('add_time', '>', $search_time)
  129. ->where('pm', 0)
  130. ->sum('number');
  131. $user['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  132. if ($user['broken_commission'] < 0)
  133. $user['broken_commission'] = 0;
  134. $user['commissionCount'] = bcsub($user['brokerage_price'], $user['broken_commission'], 2);
  135. if ($user['commissionCount'] < 0)
  136. $user['commissionCount'] = 0;
  137. if (!sys_config('vip_open'))
  138. $user['vip'] = false;
  139. else {
  140. $vipId = UserLevel::getUserLevel($user['uid']);
  141. $user['vip'] = $vipId !== false ? true : false;
  142. if ($user['vip']) {
  143. $user['vip_id'] = $vipId;
  144. $user['vip_icon'] = UserLevel::getUserLevelInfo($vipId, 'icon');
  145. $user['vip_name'] = UserLevel::getUserLevelInfo($vipId, 'name');
  146. }
  147. }
  148. $user['yesterDay'] = UserBill::yesterdayCommissionSum($user['uid']);
  149. $user['recharge_switch'] = (int)sys_config('recharge_switch');//充值开关
  150. $user['adminid'] = (boolean)\app\models\store\StoreService::orderServiceStatus($user['uid']);
  151. if ($user['phone'] && $user['user_type'] != 'h5') {
  152. $user['switchUserInfo'][] = $request->user();
  153. if ($h5UserInfo = User::where('account', $user['phone'])->where('user_type', 'h5')->find()) {
  154. $user['switchUserInfo'][] = $h5UserInfo;
  155. }
  156. } else if ($user['phone'] && $user['user_type'] == 'h5') {
  157. if ($wechatUserInfo = User::where('phone', $user['phone'])->where('user_type', '<>', 'h5')->find()) {
  158. $user['switchUserInfo'][] = $wechatUserInfo;
  159. }
  160. $user['switchUserInfo'][] = $request->user();
  161. } else if (!$user['phone']) {
  162. $user['switchUserInfo'][] = $request->user();
  163. }
  164. return app('json')->successful($user);
  165. }
  166. /**
  167. * 地址 获取单个
  168. * @param Request $request
  169. * @param $id
  170. * @return mixed
  171. * @throws DataNotFoundException
  172. * @throws ModelNotFoundException
  173. * @throws \think\exception\DbException
  174. */
  175. public function address(Request $request, $id)
  176. {
  177. $addressInfo = [];
  178. if ($id && is_numeric($id) && UserAddress::be(['is_del' => 0, 'id' => $id, 'uid' => $request->uid()])) {
  179. $addressInfo = UserAddress::find($id)->toArray();
  180. }
  181. return app('json')->successful($addressInfo);
  182. }
  183. /**
  184. * 地址列表
  185. * @param Request $request
  186. * @param $page
  187. * @param $limit
  188. * @return mixed
  189. */
  190. public function address_list(Request $request)
  191. {
  192. list($page, $limit) = UtilService::getMore([['page', 0], ['limit', 20]], $request, true);
  193. $list = UserAddress::getUserValidAddressList($request->uid(), $page, $limit, '*');
  194. return app('json')->successful($list);
  195. }
  196. /**
  197. * 设置默认地址
  198. *
  199. * @param Request $request
  200. * @return mixed
  201. */
  202. public function address_default_set(Request $request)
  203. {
  204. list($id) = UtilService::getMore([['id', 0]], $request, true);
  205. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
  206. if (!UserAddress::be(['is_del' => 0, 'id' => $id, 'uid' => $request->uid()]))
  207. return app('json')->fail('地址不存在!');
  208. $res = UserAddress::setDefaultAddress($id, $request->uid());
  209. if (!$res)
  210. return app('json')->fail('地址不存在!');
  211. else
  212. return app('json')->successful();
  213. }
  214. /**
  215. * 获取默认地址
  216. * @param Request $request
  217. * @return mixed
  218. */
  219. public function address_default(Request $request)
  220. {
  221. $defaultAddress = UserAddress::getUserDefaultAddress($request->uid(), 'id,real_name,phone,province,city,district,detail,is_default');
  222. if ($defaultAddress) {
  223. $defaultAddress = $defaultAddress->toArray();
  224. return app('json')->successful('ok', $defaultAddress);
  225. }
  226. return app('json')->successful('empty', []);
  227. }
  228. /**
  229. * 修改 添加地址
  230. * @param Request $request
  231. * @return mixed
  232. */
  233. public function address_edit(Request $request)
  234. {
  235. $addressInfo = UtilService::postMore([
  236. ['address', []],
  237. ['is_default', false],
  238. ['real_name', ''],
  239. ['post_code', ''],
  240. ['phone', ''],
  241. ['detail', ''],
  242. ['longitude', 0],
  243. ['latitude', 0],
  244. ['id', 0],
  245. ['type', 0]
  246. ], $request);
  247. if (!isset($addressInfo['address']['province'])) return app('json')->fail('收货地址格式错误!');
  248. if (!isset($addressInfo['address']['city'])) return app('json')->fail('收货地址格式错误!');
  249. if (!isset($addressInfo['address']['district'])) return app('json')->fail('收货地址格式错误!');
  250. if (!isset($addressInfo['address']['city_id']) && $addressInfo['type'] == 0) {
  251. return app('json')->fail('收货地址格式错误!请重新选择!');
  252. } else if ($addressInfo['type'] == 1) {
  253. $city = $addressInfo['address']['city'];
  254. $cityId = SystemCity::where('name', $city)->where('parent_id', '<>', 0)->value('city_id');
  255. if ($cityId) {
  256. $addressInfo['address']['city_id'] = $cityId;
  257. } else {
  258. if (!($cityId = SystemCity::where('parent_id', '<>', 0)->where('name', 'like', "%$city%")->value('city_id'))) {
  259. return app('json')->fail('收货地址格式错误!修改后请重新导入!');
  260. }
  261. $addressInfo['address']['city_id'] = $cityId;
  262. }
  263. }
  264. $addressInfo['province'] = $addressInfo['address']['province'];
  265. $addressInfo['city'] = $addressInfo['address']['city'];
  266. $addressInfo['city_id'] = $addressInfo['address']['city_id'] ?? 0;
  267. $addressInfo['district'] = $addressInfo['address']['district'];
  268. $addressInfo['is_default'] = (int)$addressInfo['is_default'] == true ? 1 : 0;
  269. $addressInfo['uid'] = $request->uid();
  270. unset($addressInfo['address'], $addressInfo['type']);
  271. try {
  272. validate(AddressValidate::class)->check($addressInfo);
  273. } catch (ValidateException $e) {
  274. return app('json')->fail($e->getError());
  275. }
  276. if ($addressInfo['id'] && UserAddress::be(['id' => $addressInfo['id'], 'uid' => $request->uid(), 'is_del' => 0])) {
  277. $id = $addressInfo['id'];
  278. unset($addressInfo['id']);
  279. if (UserAddress::edit($addressInfo, $id, 'id')) {
  280. if ($addressInfo['is_default'])
  281. UserAddress::setDefaultAddress($id, $request->uid());
  282. return app('json')->successful();
  283. } else
  284. return app('json')->fail('编辑收货地址失败!');
  285. } else {
  286. $addressInfo['add_time'] = time();
  287. if ($address = UserAddress::create($addressInfo)) {
  288. if ($addressInfo['is_default']) {
  289. UserAddress::setDefaultAddress($address->id, $request->uid());
  290. }
  291. return app('json')->successful(['id' => $address->id]);
  292. } else {
  293. return app('json')->fail('添加收货地址失败!');
  294. }
  295. }
  296. }
  297. /**
  298. * 删除地址
  299. *
  300. * @param Request $request
  301. * @return mixed
  302. */
  303. public function address_del(Request $request)
  304. {
  305. list($id) = UtilService::postMore([['id', 0]], $request, true);
  306. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
  307. if (!UserAddress::be(['is_del' => 0, 'id' => $id, 'uid' => $request->uid()]))
  308. return app('json')->fail('地址不存在!');
  309. if (UserAddress::edit(['is_del' => '1'], $id, 'id'))
  310. return app('json')->successful();
  311. else
  312. return app('json')->fail('删除地址失败!');
  313. }
  314. /**
  315. * 获取收藏产品
  316. *
  317. * @param Request $request
  318. * @return mixed
  319. */
  320. public function collect_user(Request $request)
  321. {
  322. list($page, $limit) = UtilService::getMore([
  323. ['page', 0],
  324. ['limit', 0]
  325. ], $request, true);
  326. if (!(int)$limit) return app('json')->successful([]);
  327. $productRelationList = StoreProductRelation::getUserCollectProduct($request->uid(), (int)$page, (int)$limit);
  328. return app('json')->successful($productRelationList);
  329. }
  330. /**
  331. * 添加收藏
  332. * @param Request $request
  333. * @param $id
  334. * @param $category
  335. * @return mixed
  336. */
  337. public function collect_add(Request $request)
  338. {
  339. list($id, $category) = UtilService::postMore([['id', 0], ['category', 'product']], $request, true);
  340. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误');
  341. $res = StoreProductRelation::productRelation($id, $request->uid(), 'collect', $category);
  342. if (!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
  343. else return app('json')->successful();
  344. }
  345. /**
  346. * 取消收藏
  347. *
  348. * @param Request $request
  349. * @return mixed
  350. */
  351. public function collect_del(Request $request)
  352. {
  353. list($id, $category) = UtilService::postMore([['id', 0], ['category', 'product']], $request, true);
  354. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误');
  355. $res = StoreProductRelation::unProductRelation($id, $request->uid(), 'collect', $category);
  356. if (!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
  357. else return app('json')->successful();
  358. }
  359. /**
  360. * 批量收藏
  361. * @param Request $request
  362. * @return mixed
  363. */
  364. public function collect_all(Request $request)
  365. {
  366. $collectInfo = UtilService::postMore([
  367. ['id', []],
  368. ['category', 'product'],
  369. ], $request);
  370. if (!count($collectInfo['id'])) return app('json')->fail('参数错误');
  371. $productIdS = $collectInfo['id'];
  372. $res = StoreProductRelation::productRelationAll($productIdS, $request->uid(), 'collect', $collectInfo['category']);
  373. if (!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
  374. else return app('json')->successful('收藏成功');
  375. }
  376. /**
  377. * 添加点赞
  378. *
  379. * @param Request $request
  380. * @return mixed
  381. */
  382. // public function like_add(Request $request)
  383. // {
  384. // list($id, $category) = UtilService::postMore([['id',0], ['category','product']], $request, true);
  385. // if(!$id || !is_numeric($id)) return app('json')->fail('参数错误');
  386. // $res = StoreProductRelation::productRelation($id,$request->uid(),'like',$category);
  387. // if(!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
  388. // else return app('json')->successful();
  389. // }
  390. /**
  391. * 取消点赞
  392. *
  393. * @param Request $request
  394. * @return mixed
  395. */
  396. // public function like_del(Request $request)
  397. // {
  398. // list($id, $category) = UtilService::postMore([['id',0], ['category','product']], $request, true);
  399. // if(!$id || !is_numeric($id)) return app('json')->fail('参数错误');
  400. // $res = StoreProductRelation::unProductRelation($id, $request->uid(),'like',$category);
  401. // if(!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
  402. // else return app('json')->successful();
  403. // }
  404. /**
  405. * 签到 配置
  406. * @return mixed
  407. * @throws DataNotFoundException
  408. * @throws ModelNotFoundException
  409. * @throws \think\exception\DbException
  410. */
  411. public function sign_config()
  412. {
  413. $signConfig = sys_data('sign_day_num') ?? [];
  414. return app('json')->successful($signConfig);
  415. }
  416. /**
  417. * 签到 列表
  418. * @param Request $request
  419. * @param $page
  420. * @param $limit
  421. * @return mixed
  422. */
  423. public function sign_list(Request $request)
  424. {
  425. list($page, $limit) = UtilService::getMore([
  426. ['page', 0],
  427. ['limit', 0]
  428. ], $request, true);
  429. if (!$limit) return app('json')->successful([]);
  430. $signList = UserSign::getSignList($request->uid(), (int)$page, (int)$limit);
  431. if ($signList) $signList = $signList->toArray();
  432. return app('json')->successful($signList);
  433. }
  434. /**
  435. * 签到
  436. * @param Request $request
  437. * @return mixed
  438. */
  439. public function sign_integral(Request $request)
  440. {
  441. $signed = UserSign::getIsSign($request->uid());
  442. if ($signed) return app('json')->fail('已签到');
  443. if (false !== ($integral = UserSign::sign($request->uid())))
  444. return app('json')->successful('签到获得' . floatval($integral) . '积分', ['integral' => $integral]);
  445. return app('json')->fail(UserSign::getErrorInfo('签到失败'));
  446. }
  447. /**
  448. * 签到用户信息
  449. * @param Request $request
  450. * @return mixed
  451. */
  452. public function sign_user(Request $request)
  453. {
  454. list($sign, $integral, $all) = UtilService::postMore([
  455. ['sign', 0],
  456. ['integral', 0],
  457. ['all', 0],
  458. ], $request, true);
  459. $user = $request->user();
  460. //是否统计签到
  461. if ($sign || $all) {
  462. $user['sum_sgin_day'] = UserSign::getSignSumDay($user['uid']);
  463. $user['is_day_sgin'] = UserSign::getIsSign($user['uid']);
  464. $user['is_YesterDay_sgin'] = UserSign::getIsSign($user['uid'], 'yesterday');
  465. if (!$user['is_day_sgin'] && !$user['is_YesterDay_sgin']) {
  466. $user['sign_num'] = 0;
  467. }
  468. }
  469. //是否统计积分使用情况
  470. if ($integral || $all) {
  471. $user['sum_integral'] = (int)UserBill::getRecordCount($user['uid'], 'integral', 'sign,system_add,gain');
  472. $user['deduction_integral'] = (int)UserBill::getRecordCount($user['uid'], 'integral', 'deduction', '', true) ?? 0;
  473. $user['today_integral'] = (int)UserBill::getRecordCount($user['uid'], 'integral', 'sign,system_add,gain', 'today');
  474. }
  475. unset($user['pwd']);
  476. if (!$user['is_promoter']) {
  477. $user['is_promoter'] = (int)sys_config('store_brokerage_statu') == 2 ? true : false;
  478. }
  479. 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());
  480. }
  481. /**
  482. * 签到列表(年月)
  483. *
  484. * @param Request $request
  485. * @return mixed
  486. */
  487. public function sign_month(Request $request)
  488. {
  489. list($page, $limit) = UtilService::getMore([
  490. ['page', 0],
  491. ['limit', 0]
  492. ], $request, true);
  493. if (!$limit) return app('json')->successful([]);
  494. $userSignList = UserSign::getSignMonthList($request->uid(), (int)$page, (int)$limit);
  495. return app('json')->successful($userSignList);
  496. }
  497. /**
  498. * 获取活动状态
  499. * @return mixed
  500. */
  501. public function activity()
  502. {
  503. $data['is_bargin'] = StoreBargain::validBargain() ? true : false;
  504. $data['is_pink'] = StoreCombination::getPinkIsOpen() ? true : false;
  505. $data['is_seckill'] = StoreSeckill::getSeckillCount() ? true : false;
  506. return app('json')->successful($data);
  507. }
  508. /**
  509. * 用户修改信息
  510. * @param Request $request
  511. * @return mixed
  512. */
  513. public function edit(Request $request)
  514. {
  515. list($avatar, $nickname) = UtilService::postMore([
  516. ['avatar', ''],
  517. ['nickname', ''],
  518. ], $request, true);
  519. if (User::editUser($avatar, $nickname, $request->uid())) return app('json')->successful('修改成功');
  520. return app('json')->fail('修改失败');
  521. }
  522. /**
  523. * 推广人排行
  524. * @param Request $request
  525. * @return mixed
  526. * @throws DataNotFoundException
  527. * @throws ModelNotFoundException
  528. * @throws \think\exception\DbException
  529. */
  530. public function rank(Request $request)
  531. {
  532. $data = UtilService::getMore([
  533. ['page', ''],
  534. ['limit', ''],
  535. ['type', '']
  536. ], $request);
  537. $users = User::getRankList($data);
  538. return app('json')->success($users);
  539. }
  540. /**
  541. * 佣金排行
  542. * @param Request $request
  543. * @return mixed
  544. */
  545. public function brokerage_rank(Request $request)
  546. {
  547. $data = UtilService::getMore([
  548. ['page', ''],
  549. ['limit'],
  550. ['type']
  551. ], $request);
  552. return app('json')->success([
  553. 'rank' => User::brokerageRank($data),
  554. 'position' => User::currentUserRank($data['type'], $request->user()['brokerage_price'])
  555. ]);
  556. }
  557. /**
  558. * 添加访问记录
  559. * @param Request $request
  560. * @return mixed
  561. */
  562. public function set_visit(Request $request)
  563. {
  564. $data = UtilService::postMore([
  565. ['url', ''],
  566. ['stay_time', 0]
  567. ], $request);
  568. if ($data['url'] == '') return app('json')->fail('未获取页面路径');
  569. $data['uid'] = $request->uid();
  570. $data['ip'] = $request->ip();
  571. $data['add_time'] = time();
  572. $res = UserVisit::insert($data);
  573. if ($res) {
  574. return app('json')->success('添加访问记录成功');
  575. } else {
  576. return app('json')->fail('添加访问记录失败');
  577. }
  578. }
  579. /**
  580. * 静默绑定推广人
  581. * @param Request $request
  582. * @return mixed
  583. * @throws DataNotFoundException
  584. * @throws DbException
  585. * @throws ModelNotFoundException
  586. */
  587. public function spread(Request $request)
  588. {
  589. $puid = $request->post('puid/d', 0);
  590. return app('json')->success(User::setSpread($puid, $request->uid()));
  591. }
  592. /**
  593. * 绑定后台账号
  594. * @param Request $request
  595. * @return Response
  596. */
  597. public function bindAdmin(Request $request)
  598. {
  599. $admin_account = $request->post('admin_account');
  600. $admin_password = $request->post('admin_password');
  601. $admin = SystemAdmin::where('account', $admin_account)->where('is_del', 0)->where('status', 1)->find();
  602. if (!$admin) {
  603. return app('json')->fail('账号不存在');
  604. }
  605. if (md5($admin_password) !== $admin['pwd']) {
  606. return app('json')->fail('密码错误');
  607. }
  608. if (!$admin['store_id']) {
  609. return app('json')->fail('账号非门店账号');
  610. }
  611. if (User::be(['admin_id' => $admin['id']])) {
  612. return app('json')->fail('门店账号已有绑定用户');
  613. }
  614. if (User::where('uid', $request->uid())->value('admin_id')) {
  615. return app('json')->fail('已有绑定门店账号');
  616. }
  617. BaseModel::beginTrans();
  618. try {
  619. $res = User::where('uid', $request->uid())->update(['admin_id' => $admin['id']]);
  620. //生成客服信息
  621. $old = SystemStoreStaff::where('uid', $request->uid())->find();
  622. if ($old) {
  623. if ($old['store_id'] != $admin['store_id']) {
  624. $data = [
  625. 'uid' => $request->uid(),
  626. 'store_id' => $admin['store_id'],
  627. ];
  628. $data['add_time'] = time();
  629. $res = $res && SystemStoreStaff::edit($data, $old['id']);
  630. }
  631. } else {
  632. $data = [
  633. 'uid' => $request->uid(),
  634. 'avatar' => $request->user()['avatar'],
  635. 'store_id' => $admin['store_id'],
  636. 'staff_name' => $request->user()['nickname'],
  637. 'phone' => $request->user()['phone'],
  638. 'verify_status' => 1,
  639. 'status' => 1
  640. ];
  641. $data['add_time'] = time();
  642. $res = $res && SystemStoreStaff::create($data);
  643. }
  644. if ($res) {
  645. BaseModel::commitTrans();
  646. return app('json')->successful('绑定成功');
  647. } else {
  648. BaseModel::rollbackTrans();
  649. return app('json')->fail('绑定失败');
  650. }
  651. } catch (\Exception $e) {
  652. BaseModel::rollbackTrans();
  653. return app('json')->fail('绑定失败:' . $e->getMessage());
  654. }
  655. }
  656. /**
  657. * 获取我所属的门店
  658. * @param Request $request
  659. * @return mixed
  660. * @throws DataNotFoundException
  661. * @throws DbException
  662. * @throws ModelNotFoundException
  663. *
  664. */
  665. public function getMyStore(Request $request)
  666. {
  667. $admin_indo = $request->admin_info();
  668. return app('json')->success('ok', SystemStore::getStoreDispose($admin_indo['store_id']) ? SystemStore::getStoreDispose($admin_indo['store_id'])->toArray() : []);
  669. }
  670. public function areaAchievementMonth(Request $request)
  671. {
  672. $user = $request->user();
  673. if ($user['area_admin']) {
  674. $where = [];
  675. $where['province'] = $user['area_province'];
  676. if ($user['area_admin'] == 2 || $user['area_admin'] == 1) $where['city'] = $user['area_city'];
  677. if ($user['area_admin'] == 1) $where['district'] = $user['area_district'];
  678. $start = StoreOrder::min('add_time');
  679. $data = [];
  680. while ($start < time()) {
  681. $start_month = date('Y-m', $start);
  682. $end = strtotime('+1month', strtotime($start_month)) - 1;
  683. $frontPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($start, $end, $where);
  684. $frontNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($start, $end, $where);
  685. $start = $end + 1;
  686. $data[$start_month] = compact('frontPrice', 'frontNumber');
  687. }
  688. return app('json')->successful($data);
  689. }
  690. return app('json')->fail('非区域代理');
  691. }
  692. /**
  693. * 订单交易额/订单数量时间统计
  694. * @param Request $request
  695. * @return bool
  696. */
  697. public function areaAchievementTime(Request $request)
  698. {
  699. $user = $request->user();
  700. list($start, $stop, $type) = UtilService::getMore([
  701. ['start', strtotime(date('Y-m'))],
  702. ['stop', time()],
  703. ['type', 1]
  704. ], $request, true);
  705. if ($user['area_admin']) {
  706. $where = [];
  707. $where['province'] = $user['area_province'];
  708. if ($user['area_admin'] == 2 || $user['area_admin'] == 1) $where['city'] = $user['area_city'];
  709. if ($user['area_admin'] == 1) $where['district'] = $user['area_district'];
  710. if ($start == $stop) {
  711. return app('json')->fail('开始时间不能等于结束时间');
  712. }
  713. if ($start > $stop) {
  714. $middle = $stop;
  715. $stop = $start;
  716. $start = $middle;
  717. }
  718. $space = bcsub($stop, $start, 0);//间隔时间段
  719. $front = bcsub($start, $space, 0);//第一个时间段
  720. if ($type == 1) {//销售额
  721. $frontPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($front, $start, $where);
  722. $afterPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($start, $stop, $where);
  723. $chartInfo = StoreOrder::chartTimePrice($start, $stop, $where);
  724. $data['chart'] = $chartInfo;//营业额图表数据
  725. $data['time'] = $afterPrice;//时间区间营业额
  726. $increase = (float)bcsub($afterPrice, $frontPrice, 2); //同比上个时间区间增长营业额
  727. $growthRate = abs($increase);
  728. if ($growthRate == 0) $data['growth_rate'] = 0;
  729. else if ($frontPrice == 0) $data['growth_rate'] = $growthRate;
  730. else $data['growth_rate'] = (int)bcmul(bcdiv($growthRate, $frontPrice, 2), 100, 0);//时间区间增长率
  731. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  732. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  733. } else {//订单数
  734. $frontNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($front, $start, $where);
  735. $afterNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($start, $stop, $where);
  736. $chartInfo = StoreOrder::chartTimeNumber($start, $stop, $where);
  737. $data['chart'] = $chartInfo;//订单数图表数据
  738. $data['time'] = $afterNumber;//时间区间订单数
  739. $increase = (int)bcsub($afterNumber, $frontNumber, 0); //同比上个时间区间增长订单数
  740. $growthRate = abs($increase);
  741. if ($growthRate == 0) $data['growth_rate'] = 0;
  742. else if ($frontNumber == 0) $data['growth_rate'] = $growthRate;
  743. else $data['growth_rate'] = (int)bcmul(bcdiv($growthRate, $frontNumber, 2), 100, 0);//时间区间增长率
  744. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  745. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  746. }
  747. return app('json')->successful($data);
  748. }
  749. return app('json')->fail('非区域代理');
  750. }
  751. }