UserController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\v1\user;
  12. use app\Request;
  13. use app\services\product\product\StoreProductLogServices;
  14. use app\services\user\UserCancelServices;
  15. use app\services\user\UserServices;
  16. use app\services\wechat\WechatUserServices;
  17. /**
  18. * 用户类
  19. * Class UserController
  20. * @package app\api\controller\store
  21. */
  22. class UserController
  23. {
  24. protected $services = NUll;
  25. /**
  26. * UserController constructor.
  27. * @param UserServices $services
  28. */
  29. public function __construct(UserServices $services)
  30. {
  31. $this->services = $services;
  32. }
  33. /**
  34. * 获取用户信息
  35. * @param Request $request
  36. * @return mixed
  37. */
  38. public function userInfo(Request $request)
  39. {
  40. $info = $request->user()->toArray();
  41. return app('json')->success($this->services->userInfo($info));
  42. }
  43. /**
  44. * 用户资金统计
  45. * @param Request $request
  46. * @return mixed
  47. */
  48. public function balance(Request $request)
  49. {
  50. $uid = (int)$request->uid();
  51. return app('json')->success($this->services->balance($uid));
  52. }
  53. /**
  54. * 个人中心
  55. * @param Request $request
  56. * @return mixed
  57. */
  58. public function user(Request $request)
  59. {
  60. $user = $request->user()->toArray();
  61. return app('json')->success($this->services->personalHome($user, $request->tokenData()));
  62. }
  63. /**
  64. * 获取活动状态
  65. * @return mixed
  66. */
  67. public function activity()
  68. {
  69. return app('json')->success($this->services->activity());
  70. }
  71. /**
  72. * 用户修改信息
  73. * @param Request $request
  74. * @return mixed
  75. */
  76. public function edit(Request $request)
  77. {
  78. list($avatar, $nickname) = $request->postMore([
  79. ['avatar', ''],
  80. ['nickname', ''],
  81. ], true);
  82. if (!$avatar && $nickname == '') {
  83. return app('json')->fail(410134);
  84. }
  85. $uid = (int)$request->uid();
  86. if ($this->services->eidtNickname($uid, ['avatar' => $avatar, 'nickname' => $nickname])) {
  87. return app('json')->success(100014);
  88. }
  89. return app('json')->fail(100015);
  90. }
  91. /**
  92. * 推广人排行
  93. * @param Request $request
  94. * @return mixed
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. public function rank(Request $request)
  99. {
  100. $data = $request->getMore([
  101. ['page', ''],
  102. ['limit', ''],
  103. ['type', '']
  104. ]);
  105. return app('json')->success($this->services->getRankList($data));
  106. }
  107. /**
  108. * 添加访问记录
  109. * @param Request $request
  110. * @return mixed
  111. */
  112. public function set_visit(Request $request)
  113. {
  114. $data = $request->postMore([
  115. ['url', ''],
  116. ['stay_time', 0]
  117. ]);
  118. if ($data['url'] == '') return app('json')->fail(100100);
  119. $data['uid'] = (int)$request->uid();
  120. $data['ip'] = $request->ip();
  121. if ($this->services->setVisit($data)) {
  122. return app('json')->success(100021);
  123. } else {
  124. return app('json')->fail(100022);
  125. }
  126. }
  127. /**
  128. * 静默绑定推广人
  129. * @param Request $request
  130. * @return mixed
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\DbException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. */
  135. public function spread(Request $request)
  136. {
  137. [$spreadUid, $code, $agent_id] = $request->postMore([
  138. ['puid', 0],
  139. ['code', 0],
  140. ['agent_id', 0]
  141. ], true);
  142. $uid = (int)$request->uid();
  143. $res = $this->services->spread($uid, (int)$spreadUid, $code, $agent_id);
  144. return app('json')->success($res);
  145. }
  146. /**
  147. * 推荐用户
  148. * @param Request $request
  149. * @return mixed
  150. */
  151. public function spread_people(Request $request)
  152. {
  153. $spreadInfo = $request->postMore([
  154. ['grade', 0],
  155. ['keyword', ''],
  156. ['sort', ''],
  157. ]);
  158. if (!in_array($spreadInfo['grade'], [0, 1])) {
  159. return app('json')->fail(100100);
  160. }
  161. $uid = $request->uid();
  162. return app('json')->success($this->services->getUserSpreadGrade($uid, $spreadInfo['grade'], $spreadInfo['sort'], $spreadInfo['keyword']));
  163. }
  164. /**
  165. * 是否关注
  166. * @param Request $request
  167. * @return mixed
  168. */
  169. public function subscribe(Request $request)
  170. {
  171. if ($request->uid()) {
  172. /** @var WechatUserServices $wechatUserService */
  173. $wechatUserService = app()->make(WechatUserServices::class);
  174. $subscribe = (bool)$wechatUserService->value(['uid' => $request->uid()], 'subscribe');
  175. return app('json')->success(['subscribe' => $subscribe]);
  176. } else {
  177. return app('json')->success(['subscribe' => true]);
  178. }
  179. }
  180. /**
  181. * 用户注销
  182. * @param Request $request
  183. * @return mixed
  184. */
  185. public function SetUserCancel(Request $request)
  186. {
  187. /** @var UserCancelServices $userCancelServices */
  188. $userCancelServices = app()->make(UserCancelServices::class);
  189. $userCancelServices->SetUserCancel($request->uid());
  190. return app('json')->success(410135);
  191. }
  192. /**
  193. * 商品浏览记录
  194. * @param Request $request
  195. * @param StoreProductLogServices $services
  196. * @return mixed
  197. * @throws \think\db\exception\DataNotFoundException
  198. * @throws \think\db\exception\DbException
  199. * @throws \think\db\exception\ModelNotFoundException
  200. */
  201. public function visitList(Request $request, StoreProductLogServices $services)
  202. {
  203. $where['uid'] = (int)$request->uid();
  204. $where['type'] = 'visit';
  205. $result = $services->getList($where, 'product_id', 'id,product_id,max(add_time) as add_time');
  206. $time_data = [];
  207. if ($result['list']) {
  208. foreach ($result['list'] as $key => &$item) {
  209. $add_time = strtotime($item['add_time']);
  210. if (date('Y') == date('Y', $add_time)) {//今年
  211. $item['time_key'] = date('m-d', $add_time);
  212. } else {
  213. $item['time_key'] = date('Y-m-d', $add_time);
  214. }
  215. }
  216. $time_data = array_merge(array_unique(array_column($result['list'], 'time_key')));
  217. }
  218. $result['time'] = $time_data;
  219. return app('json')->success($result);
  220. }
  221. /**
  222. * 商品浏览记录删除
  223. * @param Request $request
  224. * @param StoreProductLogServices $services
  225. * @return mixed
  226. * @throws \think\db\exception\DataNotFoundException
  227. * @throws \think\db\exception\DbException
  228. * @throws \think\db\exception\ModelNotFoundException
  229. */
  230. public function visitDelete(Request $request, StoreProductLogServices $services)
  231. {
  232. $uid = (int)$request->uid();
  233. [$ids] = $request->postMore([
  234. ['ids', []],
  235. ], true);
  236. if ($ids) {
  237. $where = ['uid' => $uid, 'product_id' => $ids];
  238. $services->delete($where);
  239. }
  240. return app('json')->success('删除成功');
  241. }
  242. }