UserController.php 7.5 KB

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