User.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\user;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\store\order\StoreOrderRepository;
  14. use app\common\repositories\user\UserBillRepository;
  15. use app\common\repositories\user\UserRepository;
  16. use app\common\repositories\user\UserVisitRepository;
  17. use crmeb\services\YunxinSmsService;
  18. use think\App;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. use think\facade\Cache;
  23. class User extends BaseController
  24. {
  25. public $repository;
  26. public function __construct(App $app, UserRepository $repository)
  27. {
  28. parent::__construct($app);
  29. $this->repository = $repository;
  30. }
  31. /**
  32. * @return mixed
  33. * @author xaboy
  34. * @day 2020/6/22
  35. */
  36. public function spread_image()
  37. {
  38. $type = $this->request->param('type');
  39. $res = $type == 'routine'
  40. ? $this->repository->routineSpreadImage($this->request->userInfo())
  41. : $this->repository->wxSpreadImage($this->request->userInfo());
  42. return app('json')->success($res);
  43. }
  44. public function spread_image_v2()
  45. {
  46. $type = $this->request->param('type');
  47. $user = $this->request->userInfo();
  48. $siteName = systemConfig('site_name');
  49. $qrcode = $type == 'routine'
  50. ? $this->repository->mpQrcode($user)
  51. : $this->repository->wxQrcode($user);
  52. $poster = systemGroupData('spread_banner');
  53. $nickname = $user['nickname'];
  54. $mark = '邀请您加入' . $siteName;
  55. return app('json')->success(compact('qrcode', 'poster', 'nickname', 'mark'));
  56. }
  57. public function spread_info()
  58. {
  59. $user = $this->request->userInfo();
  60. $user->append(['one_level_count', 'lock_brokerage', 'two_level_count', 'spread_total', 'yesterday_brokerage', 'total_extract', 'total_brokerage', 'total_brokerage_price']);
  61. $data = [
  62. 'total_brokerage_price' => $user->total_brokerage_price,
  63. 'lock_brokerage' => $user->lock_brokerage,
  64. 'one_level_count' => $user->one_level_count,
  65. 'two_level_count' => $user->two_level_count,
  66. 'spread_total' => $user->spread_total,
  67. 'yesterday_brokerage' => $user->yesterday_brokerage,
  68. 'total_extract' => $user->total_extract,
  69. 'total_brokerage' => $user->total_brokerage,
  70. 'brokerage_price' => $user->brokerage_price,
  71. 'now_money' => $user->now_money,
  72. 'broken_day' => (int)systemConfig('lock_brokerage_timer'),
  73. 'user_extract_min' => (int)systemConfig('user_extract_min'),
  74. ];
  75. return app('json')->success($data);
  76. }
  77. /**
  78. * @param UserBillRepository $billRepository
  79. * @return mixed
  80. * @author xaboy
  81. * @day 2020/6/22
  82. */
  83. public function bill(UserBillRepository $billRepository)
  84. {
  85. [$page, $limit] = $this->getPage();
  86. return app('json')->success($billRepository->userList([
  87. 'now_money' => $this->request->param('type', 0),
  88. 'status' => 1,
  89. ], $this->request->uid(), $page, $limit));
  90. }
  91. /**
  92. * @param UserBillRepository $billRepository
  93. * @return mixed
  94. * @author xaboy
  95. * @day 2020/6/22
  96. */
  97. public function brokerage_list(UserBillRepository $billRepository)
  98. {
  99. [$page, $limit] = $this->getPage();
  100. return app('json')->success($billRepository->userList([
  101. 'category' => 'brokerage',
  102. ], $this->request->uid(), $page, $limit));
  103. }
  104. /**
  105. * @return mixed
  106. * @throws DataNotFoundException
  107. * @throws DbException
  108. * @throws ModelNotFoundException
  109. * @author xaboy
  110. * @day 2020/6/22
  111. */
  112. public function spread_order()
  113. {
  114. [$page, $limit] = $this->getPage();
  115. return app('json')->success($this->repository->subOrder($this->request->uid(), $page, $limit));
  116. }
  117. /**
  118. * TODO
  119. * @return mixed
  120. * @author Qinii
  121. * @day 2020-06-18
  122. */
  123. public function binding()
  124. {
  125. $data = $this->request->params(['phone', 'sms_code']);
  126. if (!$data['sms_code'] || !(YunxinSmsService::create())->checkSmsCode($data['phone'], $data['sms_code'],'binding')) return app('json')->fail('验证码不正确');
  127. $user = $this->repository->accountByUser($data['phone']);
  128. if ($user) {
  129. $data = ['phone' => $data['phone']];
  130. } else {
  131. $data = ['account' => $data['phone'], 'phone' => $data['phone']];
  132. }
  133. $this->repository->update($this->request->uid(), $data);
  134. return app('json')->success('绑定成功');
  135. }
  136. /**
  137. * @return mixed
  138. * @throws DataNotFoundException
  139. * @throws DbException
  140. * @throws ModelNotFoundException
  141. * @author xaboy
  142. * @day 2020/6/22
  143. */
  144. public function spread_list()
  145. {
  146. [$level, $sort, $nickname] = $this->request->params(['level', 'sort', 'keyword'], true);
  147. $uid = $this->request->uid();
  148. [$page, $limit] = $this->getPage();
  149. return app('json')->success($level == 2
  150. ? $this->repository->getTwoLevelList($uid, $nickname, $sort, $page, $limit)
  151. : $this->repository->getOneLevelList($uid, $nickname, $sort, $page, $limit));
  152. }
  153. /**
  154. * @return mixed
  155. * @author xaboy
  156. * @day 2020/6/22
  157. */
  158. public function spread_top()
  159. {
  160. [$page, $limit] = $this->getPage();
  161. $type = $this->request->param('type', 0);
  162. return app('json')->success($type == 1
  163. ? $this->repository->spreadMonthTop($page, $limit)
  164. : $this->repository->spreadWeekTop($page, $limit));
  165. }
  166. /**
  167. * @return mixed
  168. * @author xaboy
  169. * @day 2020/6/22
  170. */
  171. public function brokerage_top()
  172. {
  173. [$page, $limit] = $this->getPage();
  174. $type = $this->request->param('type', 'week');
  175. $uid = $this->request->uid();
  176. return app('json')->success($type == 'month'
  177. ? $this->repository->brokerageMonthTop($uid, $page, $limit)
  178. : $this->repository->brokerageWeekTop($uid, $page, $limit));
  179. }
  180. public function history(UserVisitRepository $repository)
  181. {
  182. $uid = $this->request->uid();
  183. [$page, $limit] = $this->getPage();
  184. return app('json')->success($repository->getHistory($uid, $page, $limit));
  185. }
  186. public function deleteHistory($id, UserVisitRepository $repository)
  187. {
  188. $uid = $this->request->uid();
  189. if (!$repository->getWhereCount(['user_visit_id' => $id, 'uid' => $uid]))
  190. return app('json')->fail('数据不存在');
  191. $repository->delete($id);
  192. return app('json')->success('删除成功');
  193. }
  194. public function deleteHistoryBatch(UserVisitRepository $repository)
  195. {
  196. $uid = $this->request->uid();
  197. $data = $this->request->param('ids');
  198. if(!empty($data) && is_array($data)){
  199. foreach ($data as $id){
  200. if (!$repository->getWhereCount(['user_visit_id' => $id, 'uid' => $uid]))
  201. return app('json')->fail('数据不存在');
  202. }
  203. $repository->batchDelete($data,null);
  204. }
  205. if($data == 1)
  206. $repository->batchDelete(null,$uid);
  207. return app('json')->success('删除成功');
  208. }
  209. public function account()
  210. {
  211. $user = $this->request->userInfo();
  212. if (!$user->phone) return app('json')->fail('请绑定手机号');
  213. return app('json')->success($this->repository->selfUserList($user->phone));
  214. }
  215. public function switchUser()
  216. {
  217. $uid = (int)$this->request->param('uid');
  218. if (!$uid) return app('json')->fail('用户不存在');
  219. $userInfo = $this->request->userInfo();
  220. if (!$userInfo->phone) return app('json')->fail('请绑定手机号');
  221. $user = $this->repository->switchUser($userInfo, $uid);
  222. $tokenInfo = $this->repository->createToken($user);
  223. $this->repository->loginAfter($user);
  224. return app('json')->success($this->repository->returnToken($user, $tokenInfo));
  225. }
  226. public function edit()
  227. {
  228. $data = $this->request->params(['avatar','nickname']);
  229. $uid = (int)$this->request->param('uid');
  230. if (!$uid) return app('json')->fail('用户不存在');
  231. if(empty($data['avatar'])) unset($data['avatar']);
  232. if(empty($data['nickname'])) unset($data['nickname']);
  233. if(empty($data)) return app('json')->fail('参数丢失');
  234. $this->repository->update($this->request->uid(),$data);
  235. return app('json')->success('修改成功');
  236. }
  237. public function chagePassword()
  238. {
  239. $data = $this->request->params(['password', 'sms_code']);
  240. if (!$data['sms_code'] || !(YunxinSmsService::create())->checkSmsCode($data['phone'], $data['sms_code'],'binding')) return app('json')->fail('验证码不正确');
  241. $user = $this->repository->accountByUser($data['phone']);
  242. if ($user) {
  243. $data = ['phone' => $data['phone']];
  244. } else {
  245. $data = ['account' => $data['phone'], 'phone' => $data['phone']];
  246. }
  247. $this->repository->update($this->request->uid(), $data);
  248. return app('json')->success('绑定成功');
  249. }
  250. }