UserController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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] = $request->postMore([
  138. ['puid', 0],
  139. ['code', 0]
  140. ], true);
  141. $uid = (int)$request->uid();
  142. $res = $this->services->spread($uid, (int)$spreadUid, $code);
  143. return app('json')->success($res);
  144. }
  145. /**
  146. * 推荐用户
  147. * @param Request $request
  148. * @return mixed
  149. */
  150. public function spread_people(Request $request)
  151. {
  152. $spreadInfo = $request->postMore([
  153. ['grade', 0],
  154. ['keyword', ''],
  155. ['sort', ''],
  156. ]);
  157. if (!in_array($spreadInfo['grade'], [0, 1])) {
  158. return app('json')->fail(100100);
  159. }
  160. $uid = $request->uid();
  161. return app('json')->success($this->services->getUserSpreadGrade($uid, $spreadInfo['grade'], $spreadInfo['sort'], $spreadInfo['keyword']));
  162. }
  163. /**
  164. * 是否关注
  165. * @param Request $request
  166. * @return mixed
  167. */
  168. public function subscribe(Request $request)
  169. {
  170. if ($request->uid()) {
  171. /** @var WechatUserServices $wechatUserService */
  172. $wechatUserService = app()->make(WechatUserServices::class);
  173. $subscribe = (bool)$wechatUserService->value(['uid' => $request->uid()], 'subscribe');
  174. return app('json')->success(['subscribe' => $subscribe]);
  175. } else {
  176. return app('json')->success(['subscribe' => true]);
  177. }
  178. }
  179. /**
  180. * 用户注销
  181. * @param Request $request
  182. * @return mixed
  183. */
  184. public function SetUserCancel(Request $request)
  185. {
  186. /** @var UserCancelServices $userCancelServices */
  187. $userCancelServices = app()->make(UserCancelServices::class);
  188. $userCancelServices->SetUserCancel($request->uid());
  189. return app('json')->success(410135);
  190. }
  191. /**
  192. * 商品浏览记录
  193. * @param Request $request
  194. * @param StoreProductLogServices $services
  195. * @return mixed
  196. * @throws \think\db\exception\DataNotFoundException
  197. * @throws \think\db\exception\DbException
  198. * @throws \think\db\exception\ModelNotFoundException
  199. */
  200. public function visitList(Request $request, StoreProductLogServices $services)
  201. {
  202. $where['uid'] = (int)$request->uid();
  203. $where['type'] = 'visit';
  204. $result = $services->getList($where, 'product_id', 'id,product_id,max(add_time) as add_time');
  205. $time_data = [];
  206. if ($result['list']) {
  207. foreach ($result['list'] as $key => &$item) {
  208. $add_time = strtotime($item['add_time']);
  209. if (date('Y') == date('Y', $add_time)) {//今年
  210. $item['time_key'] = date('m-d', $add_time);
  211. } else {
  212. $item['time_key'] = date('Y-m-d', $add_time);
  213. }
  214. }
  215. $time_data = array_merge(array_unique(array_column($result['list'], 'time_key')));
  216. }
  217. $result['time'] = $time_data;
  218. return app('json')->success($result);
  219. }
  220. /**
  221. * 商品浏览记录删除
  222. * @param Request $request
  223. * @param StoreProductLogServices $services
  224. * @return mixed
  225. * @throws \think\db\exception\DataNotFoundException
  226. * @throws \think\db\exception\DbException
  227. * @throws \think\db\exception\ModelNotFoundException
  228. */
  229. public function visitDelete(Request $request, StoreProductLogServices $services)
  230. {
  231. $uid = (int)$request->uid();
  232. [$ids] = $request->postMore([
  233. ['ids', []],
  234. ], true);
  235. if ($ids) {
  236. $where = ['uid' => $uid, 'product_id' => $ids];
  237. $services->delete($where);
  238. }
  239. return app('json')->success('删除成功');
  240. }
  241. }