UserController.php 11 KB

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