UserController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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\model\user\UserBill;
  14. use app\Request;
  15. use app\services\product\product\StoreProductLogServices;
  16. use app\services\user\AwardIntegralServices;
  17. use app\services\user\UserAwardIntegralServices;
  18. use app\services\user\UserBillServices;
  19. use app\services\user\UserServices;
  20. use app\services\wechat\WechatUserServices;
  21. use crmeb\services\AliAuthService;
  22. use crmeb\services\CacheService;
  23. use crmeb\services\WithdrawService;
  24. use function Swoole\Coroutine\Http\get;
  25. /**
  26. * 用户类
  27. * Class UserController
  28. * @package app\controller\api\store
  29. */
  30. class UserController
  31. {
  32. protected $services = NUll;
  33. /**
  34. * UserController constructor.
  35. * @param UserServices $services
  36. */
  37. public function __construct(UserServices $services)
  38. {
  39. $this->services = $services;
  40. }
  41. /**
  42. * 获取用户信息
  43. * @param Request $request
  44. * @return mixed
  45. */
  46. public function userInfo(Request $request)
  47. {
  48. $info = $request->user()->toArray();
  49. return app('json')->success($this->services->userInfo($info));
  50. }
  51. /**
  52. * 用户资金统计
  53. * @param Request $request
  54. * @return mixed
  55. * @throws \think\Exception
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. * @throws \think\exception\DbException
  59. */
  60. public function balance(Request $request)
  61. {
  62. $uid = (int)$request->uid();
  63. return app('json')->successful($this->services->balance($uid));
  64. }
  65. /**
  66. * 个人中心
  67. * @param Request $request
  68. * @return mixed
  69. */
  70. public function user(Request $request)
  71. {
  72. $user = $request->user()->toArray();
  73. return app('json')->success($this->services->personalHome($user, $request->tokenData()));
  74. }
  75. public function bankInfo(Request $request)
  76. {
  77. $user = $request->uid();
  78. return app('json')->success($this->services->bankInfo($user));
  79. }
  80. /**
  81. * 获取活动状态
  82. * @return mixed
  83. */
  84. public function activity()
  85. {
  86. return app('json')->successful($this->services->activity());
  87. }
  88. /**
  89. * 用户修改信息
  90. * @param Request $request
  91. * @return mixed
  92. */
  93. public function edit(Request $request)
  94. {
  95. [$avatar, $nickname, $extend_info] = $request->postMore([
  96. ['avatar', ''],
  97. ['nickname', ''],
  98. ['extend_info', []]
  99. ], true);
  100. if (!$avatar && $nickname == '') {
  101. return app('json')->fail('请输入昵称或者选择头像');
  102. }
  103. $uid = (int)$request->uid();
  104. $this->services->saveExtendForm($uid, $extend_info, ['avatar' => $avatar, 'nickname' => $nickname], true);
  105. return app('json')->success('修改成功');
  106. }
  107. /**
  108. * 推广人排行
  109. * @param Request $request
  110. * @return mixed
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. * @throws \think\exception\DbException
  114. */
  115. public function rank(Request $request)
  116. {
  117. $data = $request->getMore([
  118. ['page', ''],
  119. ['limit', ''],
  120. ['type', '']
  121. ]);
  122. $data['uid'] = $request->uid();
  123. $info = $this->services->getRankList($data);
  124. $info['avatar'] = $request->user()['avatar'];
  125. return app('json')->success($info);
  126. }
  127. /**
  128. * 添加访问记录
  129. * @param Request $request
  130. * @return mixed
  131. */
  132. public function set_visit(Request $request)
  133. {
  134. $data = $request->postMore([
  135. ['url', ''],
  136. ['stay_time', 0]
  137. ]);
  138. if ($data['url'] == '') return app('json')->fail('未获取页面路径');
  139. $data['uid'] = (int)$request->uid();
  140. $data['ip'] = $request->ip();
  141. if ($this->services->setVisit($data)) {
  142. return app('json')->success('添加访问记录成功');
  143. } else {
  144. return app('json')->fail('添加访问记录失败');
  145. }
  146. }
  147. /**
  148. * 静默绑定推广人
  149. * @param Request $request
  150. * @return mixed
  151. */
  152. public function spread(Request $request)
  153. {
  154. [$spreadUid, $code] = $request->postMore([
  155. ['puid', 0],
  156. ['code', 0]
  157. ], true);
  158. $uid = (int)$request->uid();
  159. $this->services->spread($uid, (int)$spreadUid, $code);
  160. return app('json')->success();
  161. }
  162. /**
  163. * 推荐用户
  164. * @param Request $request
  165. * @return mixed
  166. *
  167. * grade == 0 获取一级推荐人
  168. * grade == 1 获取二级推荐人
  169. *
  170. * keyword 会员名称查询
  171. *
  172. * sort childCount ASC/DESC 团队排序 numberCount ASC/DESC 金额排序 orderCount ASC/DESC 订单排序
  173. */
  174. public function spread_people(Request $request)
  175. {
  176. $spreadInfo = $request->postMore([
  177. ['grade', 0],
  178. ['keyword', ''],
  179. ['sort', ''],
  180. ['start', 0],
  181. ['stop', 0],
  182. ]);
  183. if (!in_array($spreadInfo['grade'], [0, 1])) {
  184. return app('json')->fail('等级错误');
  185. }
  186. $spreadInfo['time'] = [$spreadInfo['start'], $spreadInfo['stop']];
  187. $uid = $request->uid();
  188. return app('json')->successful($this->services->getUserSpreadGrade($uid, $spreadInfo['grade'], $spreadInfo['sort'], $spreadInfo['keyword'], $spreadInfo['time']));
  189. }
  190. /**
  191. * 是否关注
  192. * @param Request $request
  193. * @return mixed
  194. */
  195. public function subscribe(Request $request)
  196. {
  197. if ($request->uid()) {
  198. /** @var WechatUserServices $wechatUserService */
  199. $wechatUserService = app()->make(WechatUserServices::class);
  200. $subscribe = (bool)$wechatUserService->value(['uid' => $request->uid()], 'subscribe');
  201. return app('json')->success(['subscribe' => $subscribe]);
  202. } else {
  203. return app('json')->success(['subscribe' => true]);
  204. }
  205. }
  206. /**
  207. * 用户付款code
  208. * @param Request $request
  209. * @return mixed
  210. */
  211. public function randCode(Request $request)
  212. {
  213. $uid = (int)$request->uid();
  214. $code = $this->services->getRandCode((int)$uid);
  215. return app('json')->success(['code' => $code]);
  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 visitList(Request $request, StoreProductLogServices $services)
  227. {
  228. $where['uid'] = (int)$request->uid();
  229. $where['type'] = 'visit';
  230. $result = $services->getList($where, 'product_id', 'id,product_id,max(add_time) as add_time');
  231. $time_data = [];
  232. if ($result['list']) {
  233. foreach ($result['list'] as $key => &$item) {
  234. $add_time = strtotime($item['add_time']);
  235. if (date('Y') == date('Y', $add_time)) {//今年
  236. $item['time_key'] = date('m月d日', $add_time);
  237. } else {
  238. $item['time_key'] = date('Y年m月d日', $add_time);
  239. }
  240. }
  241. $time_data = array_merge(array_unique(array_column($result['list'], 'time_key')));
  242. }
  243. $result['time'] = $time_data;
  244. return app('json')->success($result);
  245. }
  246. /**
  247. * 商品浏览记录删除
  248. * @param Request $request
  249. * @param StoreProductLogServices $services
  250. * @return mixed
  251. * @throws \think\db\exception\DataNotFoundException
  252. * @throws \think\db\exception\DbException
  253. * @throws \think\db\exception\ModelNotFoundException
  254. */
  255. public function visitDelete(Request $request, StoreProductLogServices $services)
  256. {
  257. $uid = (int)$request->uid();
  258. [$ids] = $request->postMore([
  259. ['ids', []],
  260. ], true);
  261. if ($ids) {
  262. $where = ['uid' => $uid, 'product_id' => $ids];
  263. $services->destroy($where);
  264. }
  265. return app('json')->success('删除成功');
  266. }
  267. public function awardIntegralList(Request $request, AwardIntegralServices $services)
  268. {
  269. $where = ['uid' => $request->uid()];
  270. // if ($type <= 1) {
  271. // $where['type'] = ($type == 1 ? 1 : 0);
  272. // }
  273. return app('json')->success($services->getIntegralList($where));
  274. }
  275. public function extractIntegral(Request $request, AwardIntegralServices $services, $id)
  276. {
  277. return app('json')->fail('暂不支持');
  278. $info = $services->getIntegral($id);
  279. $password = $request->post('password', '');
  280. $num = $request->post('num', 0);
  281. $user = $this->services->get($request->uid());
  282. if ($user->pwd !== md5((string)$password))
  283. return app('json')->fail('密码错误');
  284. if (!$info || $info['uid'] != $request->uid()) {
  285. return app('json')->fail('记录不存在');
  286. }
  287. if ($info['status'] != 0) {
  288. return app('json')->fail('记录已出局');
  289. }
  290. $res = $services->transaction(function () use ($services, $id, $num) {
  291. return $services->extract($id, $num);
  292. });
  293. // $res = $services->update($id, ['handle' => 1]);
  294. if ($res) {
  295. return app('json')->success('已提取');
  296. } else {
  297. return app('json')->fail('提取失败');
  298. }
  299. }
  300. public function addEmployee(Request $request)
  301. {
  302. $user = $this->services->get($request->uid());
  303. list($name, $id_card, $bank_code, $mobile, $front_image, $back_image) = $request->postMore([
  304. ['name', ''],
  305. ['id_card', ''],
  306. ['bank_code', ''],
  307. ['mobile', ''],
  308. ['front_image', ''],
  309. ['back_image', ''],
  310. ], true);
  311. if ($user['enterprise_professional_facilitator_id']) {
  312. $bank_info = WithdrawService::init()::contractInfo($user['enterprise_professional_facilitator_id']);
  313. return app('json')->success('已添加用户', ["enterprise_professional_facilitator_id" => $user['enterprise_professional_facilitator_id'],
  314. "professional_id" => $user['professional_id'], 'info' => $bank_info]);
  315. } else {
  316. $res = WithdrawService::init()::addEmployee($name, $id_card, $bank_code, $mobile, $front_image, $back_image);
  317. $this->services->update($request->uid(), ["enterprise_professional_facilitator_id" => $res['enterprise_professional_facilitator_id'],
  318. "professional_id" => $res['professional_id']], 'uid');
  319. return app('json')->success('已添加用户', $res);
  320. }
  321. }
  322. public function applySignUrl(Request $request)
  323. {
  324. $user = $this->services->get($request->uid());
  325. if ($user['professional_id']) {
  326. $bank_info = WithdrawService::init()::contractInfo($user['enterprise_professional_facilitator_id']);
  327. if ($bank_info['sign_img']) {
  328. return app('json')->fail('签约已完成');
  329. }
  330. return app('json')->success('ok', WithdrawService::init()::applySignUrl($user['professional_id']));
  331. } else {
  332. return app('json')->fail('请先添加收款信息');
  333. }
  334. }
  335. /**
  336. * 实名信息提交认证
  337. * @param Request $request
  338. * @return mixed
  339. */
  340. public function rname(Request $request)
  341. {
  342. $info = $request->postMore([
  343. ['rname', ''],
  344. ['cid', ''],
  345. ]);
  346. if (!$info['rname'] || !$info['cid']) return app('json')->fail('请输入身份证号码和真实姓名');
  347. $ress = $this->services->search()->where('uid', $request->uid())->find();
  348. if ($ress['is_auth'] == 1 || $ress['is_auth'] == 2) {
  349. return app('json')->fail('您已提交认证,不可重复提交');
  350. }
  351. $res = $this->services->cidUser($info['rname'], $info['cid'], $request->uid());
  352. if ($res) return app('json')->success('上传成功');
  353. return app('json')->fail('上传失败');
  354. }
  355. public function check(Request $request)
  356. {
  357. // 查询是否已审核
  358. $res1 = $this->services->getUserInfo($request->uid());
  359. if ($res1['is_auth'] == 0) {
  360. return app('json')->fail('请填写真实姓名和身份证');
  361. }
  362. if ($res1['is_auth'] == 2 || $res1['is_auth'] == 3) {
  363. return app('json')->fail('该信息已审核!不可操作');
  364. }
  365. $metaInfo = $request->post('metaInfo', '');
  366. $url = $request->post('url', '');
  367. if (!$metaInfo) app('json')->fail('请传入环境信息');
  368. $res = AliAuthService::check($request->uid(), $metaInfo, $url);
  369. if ($res) {
  370. return app('json')->success('ok', $res);
  371. } else {
  372. $this->services->update($request->uid(), ['is_auth' => 3, 'off' => AliAuthService::getError()], 'uid');
  373. return app('json')->fail(AliAuthService::getError());
  374. }
  375. }
  376. /**
  377. * 实名信息查询
  378. * @param Request $request
  379. * @return mixed
  380. */
  381. public function rate(Request $request)
  382. {
  383. $res = $this->services->get($request->uid());
  384. $data = [
  385. 'is_auth' => $res['is_auth'],
  386. 'off' => $res['off']
  387. ];
  388. return app('json')->success($data);
  389. }
  390. public function trade(Request $request, UserBillServices $server)
  391. {
  392. [$to_uid, $num] = $request->postMore([
  393. ['to_uid', 0],
  394. ['num', 0],
  395. ], true);
  396. $uid = (int)$request->uid();
  397. if ($server->shop_integral_trade($uid, $to_uid, $num))
  398. return app('json')->successful('转账成功!');
  399. else
  400. return app('json')->fail('转账失败');
  401. }
  402. }