User.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. namespace app\controller\api\user;
  3. use app\common\repositories\store\IntegralRepository;
  4. use app\common\repositories\store\service\StoreServiceRepository;
  5. use app\common\repositories\system\CacheRepository;
  6. use app\common\repositories\user\UserBrokerageRepository;
  7. use ln\basic\BaseController;
  8. use app\common\repositories\store\order\StoreOrderRepository;
  9. use app\common\repositories\user\UserBillRepository;
  10. use app\common\repositories\user\UserRepository;
  11. use app\common\repositories\user\UserVisitRepository;
  12. use ln\services\YunxinSmsService;
  13. use think\App;
  14. use think\db\exception\DataNotFoundException;
  15. use think\db\exception\DbException;
  16. use think\db\exception\ModelNotFoundException;
  17. use think\facade\Cache;
  18. class User extends BaseController
  19. {
  20. public $repository;
  21. public function __construct(App $app, UserRepository $repository)
  22. {
  23. parent::__construct($app);
  24. $this->repository = $repository;
  25. }
  26. /**
  27. * @return mixed
  28. * @author zfy
  29. * @day 2020/6/22
  30. */
  31. public function spread_image()
  32. {
  33. $type = $this->request->param('type');
  34. $res = $type == 'routine'
  35. ? $this->repository->routineSpreadImage($this->request->userInfo())
  36. : $this->repository->wxSpreadImage($this->request->userInfo());
  37. return app('json')->success($res);
  38. }
  39. public function spread_image_v2()
  40. {
  41. $type = $this->request->param('type');
  42. $user = $this->request->userInfo();
  43. $siteName = systemConfig('site_name');
  44. $qrcode = $type == 'routine'
  45. ? $this->repository->mpQrcode($user)
  46. : $this->repository->wxQrcode($user);
  47. $poster = systemGroupData('spread_banner');
  48. $nickname = $user['nickname'];
  49. $mark = '邀请您加入' . $siteName;
  50. return app('json')->success(compact('qrcode', 'poster', 'nickname', 'mark'));
  51. }
  52. public function spread_info()
  53. {
  54. $user = $this->request->userInfo();
  55. $make = app()->make(UserBrokerageRepository::class);
  56. $user->append(['one_level_count', 'lock_brokerage', 'two_level_count', 'spread_total', 'yesterday_brokerage', 'total_extract', 'total_brokerage', 'total_brokerage_price']);
  57. $show_brokerage = (bool)$make->search([])->count();
  58. $data = [
  59. 'total_brokerage_price' => $user->total_brokerage_price,
  60. 'lock_brokerage' => $user->lock_brokerage,
  61. 'one_level_count' => $user->one_level_count,
  62. 'two_level_count' => $user->two_level_count,
  63. 'spread_total' => $user->spread_total,
  64. 'yesterday_brokerage' => $user->yesterday_brokerage,
  65. 'total_extract' => $user->total_extract,
  66. 'total_brokerage' => $user->total_brokerage,
  67. 'brokerage_price' => $user->brokerage_price,
  68. 'show_brokerage' => $show_brokerage,
  69. 'brokerage' => $show_brokerage ? ($user->brokerage ?: ['brokerage_level' => 0, 'brokerage_name' => '普通分销员']) : null,
  70. 'now_money' => $user->now_money,
  71. 'broken_day' => (int)systemConfig('lock_brokerage_timer'),
  72. 'user_extract_min' => (int)systemConfig('user_extract_min'),
  73. ];
  74. return app('json')->success($data);
  75. }
  76. public function brokerage_all()
  77. {
  78. return app('json')->success(app()->make(UserBrokerageRepository::class)->all());
  79. }
  80. public function brokerage_info()
  81. {
  82. $make = app()->make(UserBrokerageRepository::class);
  83. $user = $this->request->userInfo();
  84. $brokerage = $user->brokerage;
  85. $next_brokerage = $make->getNextLevel($user->brokerage_level) ?: $brokerage;
  86. $brokerage_rate = null;
  87. if ($next_brokerage || $brokerage) {
  88. $brokerage_rate = $make->getLevelRate($user, $next_brokerage);
  89. }
  90. $down_brokerage = null;
  91. if ($next_brokerage) {
  92. $down_brokerage = $make->getNextLevel($next_brokerage->brokerage_level);
  93. }
  94. $brokerage = $brokerage ?: ['brokerage_level' => 0, 'brokerage_name' => '普通分销员'];
  95. return app('json')->success(compact('brokerage', 'next_brokerage', 'brokerage_rate', 'down_brokerage'));
  96. }
  97. public function brokerage_notice()
  98. {
  99. $user = $this->request->userInfo();
  100. if (!$user->brokerage_level) {
  101. return app('json')->fail('无需通知');
  102. }
  103. $make = app()->make(CacheRepository::class);
  104. $key = 'notice_' . $user->uid . '_' . $user->brokerage_level;
  105. if ($make->getResult($key)) {
  106. return app('json')->fail('已通知');
  107. }
  108. $make->create(['key' => $key, 'result' => 1, 'expire_time' => 0]);
  109. $userBrokerageRepository = app()->make(UserBrokerageRepository::class);
  110. return app('json')->success(['type' => $userBrokerageRepository->getNextLevel($user->brokerage_level) ? 'level' : 'top']);
  111. }
  112. /**
  113. * @param UserBillRepository $billRepository
  114. * @return mixed
  115. * @author zfy
  116. * @day 2020/6/22
  117. */
  118. public function bill(UserBillRepository $billRepository)
  119. {
  120. [$page, $limit] = $this->getPage();
  121. return app('json')->success($billRepository->userList([
  122. 'now_money' => $this->request->param('type', 0),
  123. 'status' => 1,
  124. ], $this->request->uid(), $page, $limit));
  125. }
  126. /**
  127. * @param UserBillRepository $billRepository
  128. * @return mixed
  129. * @author zfy
  130. * @day 2020/6/22
  131. */
  132. public function brokerage_list(UserBillRepository $billRepository)
  133. {
  134. [$page, $limit] = $this->getPage();
  135. return app('json')->success($billRepository->userList([
  136. 'category' => 'brokerage',
  137. ], $this->request->uid(), $page, $limit));
  138. }
  139. /**
  140. * @return mixed
  141. * @throws DataNotFoundException
  142. * @throws DbException
  143. * @throws ModelNotFoundException
  144. * @author zfy
  145. * @day 2020/6/22
  146. */
  147. public function spread_order()
  148. {
  149. [$page, $limit] = $this->getPage();
  150. return app('json')->success($this->repository->subOrder($this->request->uid(), $page, $limit));
  151. }
  152. /**
  153. * TODO
  154. * @return mixed
  155. * @author Qinii
  156. * @day 2020-06-18
  157. */
  158. public function binding()
  159. {
  160. $data = $this->request->params(['phone', 'sms_code']);
  161. if (!$data['sms_code'] || !(YunxinSmsService::create())->checkSmsCode($data['phone'], $data['sms_code'],'binding')) return app('json')->fail('验证码不正确');
  162. $user = $this->repository->accountByUser($data['phone']);
  163. if ($user) {
  164. $data = ['phone' => $data['phone']];
  165. } else {
  166. $data = ['account' => $data['phone'], 'phone' => $data['phone']];
  167. }
  168. $this->repository->update($this->request->uid(), $data);
  169. return app('json')->success('绑定成功');
  170. }
  171. /**
  172. * @return mixed
  173. * @throws DataNotFoundException
  174. * @throws DbException
  175. * @throws ModelNotFoundException
  176. * @author zfy
  177. * @day 2020/6/22
  178. */
  179. public function spread_list()
  180. {
  181. [$level, $sort, $nickname] = $this->request->params(['level', 'sort', 'keyword'], true);
  182. $uid = $this->request->uid();
  183. [$page, $limit] = $this->getPage();
  184. return app('json')->success($level == 2
  185. ? $this->repository->getTwoLevelList($uid, $nickname, $sort, $page, $limit)
  186. : $this->repository->getOneLevelList($uid, $nickname, $sort, $page, $limit));
  187. }
  188. /**
  189. * @return mixed
  190. * @author zfy
  191. * @day 2020/6/22
  192. */
  193. public function spread_top()
  194. {
  195. [$page, $limit] = $this->getPage();
  196. $type = $this->request->param('type', 0);
  197. return app('json')->success($type == 1
  198. ? $this->repository->spreadMonthTop($page, $limit)
  199. : $this->repository->spreadWeekTop($page, $limit));
  200. }
  201. /**
  202. * @return mixed
  203. * @author zfy
  204. * @day 2020/6/22
  205. */
  206. public function brokerage_top()
  207. {
  208. [$page, $limit] = $this->getPage();
  209. $type = $this->request->param('type', 'week');
  210. $uid = $this->request->uid();
  211. return app('json')->success($type == 'month'
  212. ? $this->repository->brokerageMonthTop($uid, $page, $limit)
  213. : $this->repository->brokerageWeekTop($uid, $page, $limit));
  214. }
  215. public function history(UserVisitRepository $repository)
  216. {
  217. $uid = $this->request->uid();
  218. [$page, $limit] = $this->getPage();
  219. return app('json')->success($repository->getHistory($uid, $page, $limit));
  220. }
  221. public function deleteHistory($id, UserVisitRepository $repository)
  222. {
  223. $uid = $this->request->uid();
  224. if (!$repository->getWhereCount(['user_visit_id' => $id, 'uid' => $uid]))
  225. return app('json')->fail('数据不存在');
  226. $repository->delete($id);
  227. return app('json')->success('删除成功');
  228. }
  229. public function deleteHistoryBatch(UserVisitRepository $repository)
  230. {
  231. $uid = $this->request->uid();
  232. $data = $this->request->param('ids');
  233. if(!empty($data) && is_array($data)){
  234. foreach ($data as $id){
  235. if (!$repository->getWhereCount(['user_visit_id' => $id, 'uid' => $uid]))
  236. return app('json')->fail('数据不存在');
  237. }
  238. $repository->batchDelete($data,null);
  239. }
  240. if($data == 1)
  241. $repository->batchDelete(null,$uid);
  242. return app('json')->success('删除成功');
  243. }
  244. public function account()
  245. {
  246. $user = $this->request->userInfo();
  247. if (!$user->phone) return app('json')->fail('请绑定手机号');
  248. return app('json')->success($this->repository->selfUserList($user->phone));
  249. }
  250. public function switchUser()
  251. {
  252. $uid = (int)$this->request->param('uid');
  253. if (!$uid) return app('json')->fail('用户不存在');
  254. $userInfo = $this->request->userInfo();
  255. if (!$userInfo->phone) return app('json')->fail('请绑定手机号');
  256. $user = $this->repository->switchUser($userInfo, $uid);
  257. $tokenInfo = $this->repository->createToken($user);
  258. $this->repository->loginAfter($user);
  259. return app('json')->success($this->repository->returnToken($user, $tokenInfo));
  260. }
  261. public function edit()
  262. {
  263. $data = $this->request->params(['avatar','nickname']);
  264. $uid = (int)$this->request->param('uid');
  265. if (!$uid) return app('json')->fail('用户不存在');
  266. if(empty($data['avatar'])) unset($data['avatar']);
  267. if(empty($data['nickname'])) unset($data['nickname']);
  268. if(empty($data)) return app('json')->fail('参数丢失');
  269. $this->repository->update($this->request->uid(),$data);
  270. return app('json')->success('修改成功');
  271. }
  272. public function chagePassword()
  273. {
  274. $data = $this->request->params(['password', 'sms_code']);
  275. if (!$data['sms_code'] || !(YunxinSmsService::create())->checkSmsCode($data['phone'], $data['sms_code'],'binding')) return app('json')->fail('验证码不正确');
  276. $user = $this->repository->accountByUser($data['phone']);
  277. if ($user) {
  278. $data = ['phone' => $data['phone']];
  279. } else {
  280. $data = ['account' => $data['phone'], 'phone' => $data['phone']];
  281. }
  282. $this->repository->update($this->request->uid(), $data);
  283. return app('json')->success('绑定成功');
  284. }
  285. public function getAgree($key)
  286. {
  287. $make = app()->make(CacheRepository::class);
  288. $data = $make->getResult($key);
  289. return app('json')->success($data);
  290. }
  291. public function integralInfo(UserBillRepository $make)
  292. {
  293. if (!systemConfig('integral_status')) {
  294. return app('json')->fail('积分功能未开启');
  295. }
  296. $user = $this->request->userInfo();
  297. $integral = $user->integral;
  298. $lockIntegral = $make->lockIntegral($user->uid);
  299. $deductionIntegral = $make->deductionIntegral($user->uid);
  300. $totalGainIntegral = $make->totalGainIntegral($user->uid);
  301. $make1 = app()->make(IntegralRepository::class);
  302. $nextClearDay = $make1->getTimeoutDay();
  303. $status = $nextClearDay < strtotime('+20 day');
  304. $invalidDay = $make1->getInvalidDay();
  305. if ($status && $integral > 0 && $invalidDay) {
  306. $validIntegral = $make->validIntegral($user->uid, date('Y-m-d H:i:s', $invalidDay), date('Y-m-d H:i:s', $nextClearDay));
  307. if ($integral > $validIntegral) {
  308. $nextClearIntegral = (int)bcsub($integral, $validIntegral, 0);
  309. } else {
  310. $nextClearIntegral = 0;
  311. }
  312. } else {
  313. $nextClearIntegral = 0;
  314. }
  315. $nextClearDay = date('m月d日', $nextClearDay);
  316. $clear = compact('nextClearDay', 'status', 'nextClearIntegral');
  317. return app('json')->success(compact('integral', 'lockIntegral', 'deductionIntegral', 'totalGainIntegral', 'clear'));
  318. }
  319. public function integralList(UserBillRepository $repository)
  320. {
  321. if (!systemConfig('integral_status')) {
  322. return app('json')->fail('积分功能未开启');
  323. }
  324. [$page, $limit] = $this->getPage();
  325. $data = $repository->userList(['category' => 'integral'], $this->request->userInfo()->uid, $page, $limit);
  326. return app('json')->success($data);
  327. }
  328. public function services()
  329. {
  330. $uid = $this->request->userInfo()->uid;
  331. $where = $this->request->params(['is_verify', 'customer']);
  332. return app('json')->success(app()->make(StoreServiceRepository::class)->getServices($uid, $where));
  333. }
  334. }