UserController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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\v1\user;
  12. use app\jobs\integral\IntegralJob;
  13. use app\Request;
  14. use app\services\product\product\StoreProductLogServices;
  15. use app\services\user\UserAwardIntegralServices;
  16. use app\services\user\UserServices;
  17. use app\services\wechat\WechatUserServices;
  18. use crmeb\services\CacheService;
  19. /**
  20. * 用户类
  21. * Class UserController
  22. * @package app\controller\api\store
  23. */
  24. class UserController
  25. {
  26. protected $services = NUll;
  27. /**
  28. * UserController constructor.
  29. * @param UserServices $services
  30. */
  31. public function __construct(UserServices $services)
  32. {
  33. $this->services = $services;
  34. }
  35. /**
  36. * 获取用户信息
  37. * @param Request $request
  38. * @return mixed
  39. */
  40. public function userInfo(Request $request)
  41. {
  42. $info = $request->user()->toArray();
  43. return app('json')->success($this->services->userInfo($info));
  44. }
  45. /**
  46. * 用户资金统计
  47. * @param Request $request
  48. * @return mixed
  49. * @throws \think\Exception
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. * @throws \think\exception\DbException
  53. */
  54. public function balance(Request $request)
  55. {
  56. $uid = (int)$request->uid();
  57. return app('json')->successful($this->services->balance($uid));
  58. }
  59. /**
  60. * 个人中心
  61. * @param Request $request
  62. * @return mixed
  63. */
  64. public function user(Request $request)
  65. {
  66. $user = $request->user()->toArray();
  67. return app('json')->success($this->services->personalHome($user, $request->tokenData()));
  68. }
  69. /**
  70. * 获取活动状态
  71. * @return mixed
  72. */
  73. public function activity()
  74. {
  75. return app('json')->successful($this->services->activity());
  76. }
  77. /**
  78. * 用户修改信息
  79. * @param Request $request
  80. * @return mixed
  81. */
  82. public function edit(Request $request)
  83. {
  84. [$avatar, $nickname, $extend_info] = $request->postMore([
  85. ['avatar', ''],
  86. ['nickname', ''],
  87. ['extend_info', []]
  88. ], true);
  89. if (!$avatar && $nickname == '') {
  90. return app('json')->fail('请输入昵称或者选择头像');
  91. }
  92. $uid = (int)$request->uid();
  93. $this->services->saveExtendForm($uid, $extend_info, ['avatar' => $avatar, 'nickname' => $nickname], true);
  94. return app('json')->success('修改成功');
  95. }
  96. /**
  97. * 推广人排行
  98. * @param Request $request
  99. * @return mixed
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\ModelNotFoundException
  102. * @throws \think\exception\DbException
  103. */
  104. public function rank(Request $request)
  105. {
  106. $data = $request->getMore([
  107. ['page', ''],
  108. ['limit', ''],
  109. ['type', '']
  110. ]);
  111. $data['uid'] = $request->uid();
  112. $info = $this->services->getRankList($data);
  113. $info['avatar'] = $request->user()['avatar'];
  114. return app('json')->success($info);
  115. }
  116. /**
  117. * 添加访问记录
  118. * @param Request $request
  119. * @return mixed
  120. */
  121. public function set_visit(Request $request)
  122. {
  123. $data = $request->postMore([
  124. ['url', ''],
  125. ['stay_time', 0]
  126. ]);
  127. if ($data['url'] == '') return app('json')->fail('未获取页面路径');
  128. $data['uid'] = (int)$request->uid();
  129. $data['ip'] = $request->ip();
  130. if ($this->services->setVisit($data)) {
  131. return app('json')->success('添加访问记录成功');
  132. } else {
  133. return app('json')->fail('添加访问记录失败');
  134. }
  135. }
  136. /**
  137. * 静默绑定推广人
  138. * @param Request $request
  139. * @return mixed
  140. */
  141. public function spread(Request $request)
  142. {
  143. [$spreadUid, $code] = $request->postMore([
  144. ['puid', 0],
  145. ['code', 0]
  146. ], true);
  147. $uid = (int)$request->uid();
  148. $this->services->spread($uid, (int)$spreadUid, $code);
  149. return app('json')->success();
  150. }
  151. /**
  152. * 推荐用户
  153. * @param Request $request
  154. * @return mixed
  155. *
  156. * grade == 0 获取一级推荐人
  157. * grade == 1 获取二级推荐人
  158. *
  159. * keyword 会员名称查询
  160. *
  161. * sort childCount ASC/DESC 团队排序 numberCount ASC/DESC 金额排序 orderCount ASC/DESC 订单排序
  162. */
  163. public function spread_people(Request $request)
  164. {
  165. $spreadInfo = $request->postMore([
  166. ['grade', 0],
  167. ['keyword', ''],
  168. ['sort', ''],
  169. ['start', 0],
  170. ['stop', 0],
  171. ]);
  172. if (!in_array($spreadInfo['grade'], [0, 1])) {
  173. return app('json')->fail('等级错误');
  174. }
  175. $spreadInfo['time'] = [$spreadInfo['start'], $spreadInfo['stop']];
  176. $uid = $request->uid();
  177. return app('json')->successful($this->services->getUserSpreadGrade($uid, $spreadInfo['grade'], $spreadInfo['sort'], $spreadInfo['keyword'], $spreadInfo['time']));
  178. }
  179. /**
  180. * 是否关注
  181. * @param Request $request
  182. * @return mixed
  183. */
  184. public function subscribe(Request $request)
  185. {
  186. if ($request->uid()) {
  187. /** @var WechatUserServices $wechatUserService */
  188. $wechatUserService = app()->make(WechatUserServices::class);
  189. $subscribe = (bool)$wechatUserService->value(['uid' => $request->uid()], 'subscribe');
  190. return app('json')->success(['subscribe' => $subscribe]);
  191. } else {
  192. return app('json')->success(['subscribe' => true]);
  193. }
  194. }
  195. /**
  196. * 用户付款code
  197. * @param Request $request
  198. * @return mixed
  199. */
  200. public function randCode(Request $request)
  201. {
  202. $uid = (int)$request->uid();
  203. $code = $this->services->getRandCode((int)$uid);
  204. return app('json')->success(['code' => $code]);
  205. }
  206. /**
  207. * 商品浏览记录
  208. * @param Request $request
  209. * @param StoreProductLogServices $services
  210. * @return mixed
  211. * @throws \think\db\exception\DataNotFoundException
  212. * @throws \think\db\exception\DbException
  213. * @throws \think\db\exception\ModelNotFoundException
  214. */
  215. public function visitList(Request $request, StoreProductLogServices $services)
  216. {
  217. $where['uid'] = (int)$request->uid();
  218. $where['type'] = 'visit';
  219. $result = $services->getList($where, 'product_id', 'id,product_id,max(add_time) as add_time');
  220. $time_data = [];
  221. if ($result['list']) {
  222. foreach ($result['list'] as $key => &$item) {
  223. $add_time = strtotime($item['add_time']);
  224. if (date('Y') == date('Y', $add_time)) {//今年
  225. $item['time_key'] = date('m月d日', $add_time);
  226. } else {
  227. $item['time_key'] = date('Y年m月d日', $add_time);
  228. }
  229. }
  230. $time_data = array_merge(array_unique(array_column($result['list'], 'time_key')));
  231. }
  232. $result['time'] = $time_data;
  233. return app('json')->success($result);
  234. }
  235. /**
  236. * 商品浏览记录删除
  237. * @param Request $request
  238. * @param StoreProductLogServices $services
  239. * @return mixed
  240. * @throws \think\db\exception\DataNotFoundException
  241. * @throws \think\db\exception\DbException
  242. * @throws \think\db\exception\ModelNotFoundException
  243. */
  244. public function visitDelete(Request $request, StoreProductLogServices $services)
  245. {
  246. $uid = (int)$request->uid();
  247. [$ids] = $request->postMore([
  248. ['ids', []],
  249. ], true);
  250. if ($ids) {
  251. $where = ['uid' => $uid, 'product_id' => $ids];
  252. $services->destroy($where);
  253. }
  254. return app('json')->success('删除成功');
  255. }
  256. public function awardIntegralList(Request $request, UserAwardIntegralServices $services, $type = 0)
  257. {
  258. $where = ['uid' => $request->uid()];
  259. if ($type <= 1) {
  260. $where['type'] = ($type == 1 ? 1 : 0);
  261. }
  262. return app('json')->success($services->getIntegralList($where));
  263. }
  264. public function extractIntegral(Request $request, UserAwardIntegralServices $services, $id)
  265. {
  266. $info = $services->getIntegral($id);
  267. if (!$info || $info['uid'] != $request->uid()) {
  268. return app('json')->fail('记录不存在');
  269. }
  270. if ($info['status'] != 0) {
  271. return app('json')->fail('记录已出局');
  272. }
  273. if ($info['handle'] != 0) {
  274. return app('json')->fail('记录处理中');
  275. }
  276. $res = $services->update($id, ['handle' => 1]);
  277. if ($res) {
  278. IntegralJob::dispatchDo('extract', [$id]);
  279. return app('json')->success('记录已提交出局处理');
  280. } else {
  281. return app('json')->fail('记录提交出局处理失败');
  282. }
  283. }
  284. }