UserController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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,$city_id,$city_name,$store_name ) = $request->postMore([
  79. ['avatar', ''],
  80. ['nickname', ''],
  81. ['city_id', ''],
  82. ['city_name', ''],
  83. ['store_name', '']
  84. ], true);
  85. if (!$avatar && $nickname == '') {
  86. return app('json')->fail(410134);
  87. }
  88. $uid = (int)$request->uid();
  89. if ($this->services->eidtNickname($uid, ['avatar' => $avatar, 'nickname' => $nickname,'city_id' => $city_id,'city_name' => $city_name,'store_name' => $store_name ])) {
  90. return app('json')->success(100014);
  91. }
  92. return app('json')->fail(100015);
  93. }
  94. /**
  95. * 推广人排行
  96. * @param Request $request
  97. * @return mixed
  98. * @throws \think\db\exception\DataNotFoundException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. */
  101. public function rank(Request $request)
  102. {
  103. $data = $request->getMore([
  104. ['page', ''],
  105. ['limit', ''],
  106. ['type', '']
  107. ]);
  108. return app('json')->success($this->services->getRankList($data));
  109. }
  110. /**
  111. * 添加访问记录
  112. * @param Request $request
  113. * @return mixed
  114. */
  115. public function set_visit(Request $request)
  116. {
  117. $data = $request->postMore([
  118. ['url', ''],
  119. ['stay_time', 0]
  120. ]);
  121. if ($data['url'] == '') return app('json')->fail(100100);
  122. $data['uid'] = (int)$request->uid();
  123. $data['ip'] = $request->ip();
  124. if ($this->services->setVisit($data)) {
  125. return app('json')->success(100021);
  126. } else {
  127. return app('json')->fail(100022);
  128. }
  129. }
  130. /**
  131. * 静默绑定推广人
  132. * @param Request $request
  133. * @return mixed
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public function spread(Request $request)
  139. {
  140. [$spreadUid, $code] = $request->postMore([
  141. ['puid', 0],
  142. ['code', 0]
  143. ], true);
  144. $uid = (int)$request->uid();
  145. $res = $this->services->spread($uid, (int)$spreadUid, $code);
  146. return app('json')->success($res);
  147. }
  148. /**
  149. * 推荐用户
  150. * @param Request $request
  151. * @return mixed
  152. */
  153. public function spread_people(Request $request)
  154. {
  155. $spreadInfo = $request->postMore([
  156. ['grade', 0],
  157. ['keyword', ''],
  158. ['sort', ''],
  159. ]);
  160. if (!in_array($spreadInfo['grade'], [0, 1])) {
  161. return app('json')->fail(100100);
  162. }
  163. $uid = $request->uid();
  164. return app('json')->success($this->services->getUserSpreadGrade($uid, $spreadInfo['grade'], $spreadInfo['sort'], $spreadInfo['keyword']));
  165. }
  166. /**
  167. * 是否关注
  168. * @param Request $request
  169. * @return mixed
  170. */
  171. public function subscribe(Request $request)
  172. {
  173. if ($request->uid()) {
  174. /** @var WechatUserServices $wechatUserService */
  175. $wechatUserService = app()->make(WechatUserServices::class);
  176. $subscribe = (bool)$wechatUserService->value(['uid' => $request->uid()], 'subscribe');
  177. return app('json')->success(['subscribe' => $subscribe]);
  178. } else {
  179. return app('json')->success(['subscribe' => true]);
  180. }
  181. }
  182. /**
  183. * 用户注销
  184. * @param Request $request
  185. * @return mixed
  186. */
  187. public function SetUserCancel(Request $request)
  188. {
  189. /** @var UserCancelServices $userCancelServices */
  190. $userCancelServices = app()->make(UserCancelServices::class);
  191. $userCancelServices->SetUserCancel($request->uid());
  192. return app('json')->success(410135);
  193. }
  194. /**
  195. * 商品浏览记录
  196. * @param Request $request
  197. * @param StoreProductLogServices $services
  198. * @return mixed
  199. * @throws \think\db\exception\DataNotFoundException
  200. * @throws \think\db\exception\DbException
  201. * @throws \think\db\exception\ModelNotFoundException
  202. */
  203. public function visitList(Request $request, StoreProductLogServices $services)
  204. {
  205. $where['uid'] = (int)$request->uid();
  206. $where['type'] = 'visit';
  207. $result = $services->getList($where, 'product_id', 'id,product_id,max(add_time) as add_time');
  208. $time_data = [];
  209. if ($result['list']) {
  210. foreach ($result['list'] as $key => &$item) {
  211. $add_time = strtotime($item['add_time']);
  212. if (date('Y') == date('Y', $add_time)) {//今年
  213. $item['time_key'] = date('m-d', $add_time);
  214. } else {
  215. $item['time_key'] = date('Y-m-d', $add_time);
  216. }
  217. }
  218. $time_data = array_merge(array_unique(array_column($result['list'], 'time_key')));
  219. }
  220. $result['time'] = $time_data;
  221. return app('json')->success($result);
  222. }
  223. /**
  224. * 商品浏览记录删除
  225. * @param Request $request
  226. * @param StoreProductLogServices $services
  227. * @return mixed
  228. * @throws \think\db\exception\DataNotFoundException
  229. * @throws \think\db\exception\DbException
  230. * @throws \think\db\exception\ModelNotFoundException
  231. */
  232. public function visitDelete(Request $request, StoreProductLogServices $services)
  233. {
  234. $uid = (int)$request->uid();
  235. [$ids] = $request->postMore([
  236. ['ids', []],
  237. ], true);
  238. if ($ids) {
  239. $where = ['uid' => $uid, 'product_id' => $ids];
  240. $services->delete($where);
  241. }
  242. return app('json')->success('删除成功');
  243. }
  244. }