UserController.php 11 KB

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