UserController.php 30 KB

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