UserController.php 14 KB

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