UserController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Params\ProblemParam;
  4. use App\Models\User;
  5. use App\Models\UserInfoModel;
  6. use App\Models\UserInviteLog;
  7. use App\Models\UserPhotoDestroy;
  8. use App\Models\UserVipLimit;
  9. use App\Models\UserVipLogModel;
  10. use App\Models\VipConfig;
  11. use App\Models\VipModel;
  12. use App\Services\UserService;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\Cache;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Validator;
  17. use PHPUnit\Util\Exception;
  18. class UserController extends Controller
  19. {
  20. protected $userService;
  21. public function __construct(){
  22. $this->userService = new UserService();
  23. }
  24. /**
  25. * 设置密码
  26. */
  27. public function setpass(Request $request){
  28. $validator = Validator::make($request->all(), [
  29. 'password' => 'required|string',
  30. ],[
  31. 'password.required'=>"密码必须",
  32. ]);
  33. if ($validator->fails()) {
  34. return $this->response()->errorForbidden($validator->messages()->first());
  35. }
  36. $user = auth('api')->user();
  37. $user->password = $request->password;
  38. if($user->save()){
  39. return response()->json(['message'=>"设置成功"]);
  40. }else{
  41. return $this->response->errorForbidden("设置失败");
  42. }
  43. }
  44. /**
  45. * 选择性别
  46. */
  47. public function checksex(Request $request){
  48. $user = auth('api')->user();
  49. $validator = Validator::make($request->all(), [
  50. 'sex' => 'required'
  51. ], [
  52. 'mobile.required'=>"性别必须",
  53. ]);
  54. if ($validator->fails()) {
  55. return $this->response()->errorForbidden($validator->messages()->first());
  56. }
  57. //dd($user);
  58. $user->sex = $request->sex;
  59. if (!$user->save()) {
  60. return $this->response->errorForbidden('设置性别失败');
  61. }
  62. return response()->json(['message'=>"设置成功"]);
  63. }
  64. /**
  65. * 设置资料(初次登录)
  66. */
  67. public function setinfo(Request $request){
  68. $user = auth('api')->user();
  69. DB::beginTransaction();
  70. try {
  71. $validator = Validator::make($request->all(), [
  72. 'nickname' => 'required|between:2,10',
  73. 'birthday' => 'required',
  74. 'avatar' => 'required',
  75. ], [
  76. 'nickname.required'=>"昵称不能为空",
  77. 'nickname.between'=>"昵称长度必须在2~10之间",
  78. 'birthday.required'=>"生日不能为空",
  79. 'avatar.required'=>"头像不能为空",
  80. ]);
  81. if ($validator->fails()) {
  82. throw new Exception($validator->messages()->first());
  83. }
  84. if($user->sex==2 && empty($request->video)){
  85. throw new Exception("请上传视频");
  86. }
  87. if(UserInfoModel::query()->leftJoin('users','users_info.user_id','=','users.id')
  88. ->where(['users_info.nickname'=>$request->nickname,'users.is_distory'=>0])
  89. ->where('users_info.user_id', '!=' , $user->id)->first()){
  90. throw new Exception("昵称已被使用");
  91. }
  92. //邀请码设置
  93. $pid = 0;
  94. if(isset($request->ycode) && $request->ycode!=""){
  95. if(!$puser = User::where(['ycode'=>$request->ycode])->first()){
  96. throw new Exception("邀请码不存在");
  97. }
  98. $pid = $puser->id;
  99. //赠送会员天数
  100. UserInviteLog::query()->create([
  101. 'user_id'=>$pid,
  102. 'invite_id'=>$user->id,
  103. 'day'=>1,
  104. 'status'=>0,
  105. ]);
  106. }
  107. UserInfoModel::query()->where('user_id',$user->id)->update([
  108. 'avatar' =>$request->avatar,
  109. 'nickname' =>htmlspecialchars($request->nickname),
  110. 'birthday' =>$request->birthday,
  111. 'video' => !empty($request->video)?json_encode($request->video) :[],
  112. ]);
  113. if($pid!=0){
  114. $user->pid = $pid;
  115. $user->save();
  116. }
  117. DB::commit();
  118. }catch (\Exception $exception){
  119. DB::rollBack();
  120. return $this->response()->errorForbidden($exception->getMessage());
  121. }
  122. return response()->json(['message'=>"设置成功"]);
  123. }
  124. /**
  125. * 获取个人资料
  126. */
  127. public function getinfo(){
  128. $user = auth('api')->user();
  129. $userinfo = UserInfoModel::query()->where('user_id', $user->id)->first();
  130. //dd($userinfo);
  131. $userinfo['hobby'] = !empty($userinfo['hobby'])?explode(',',$userinfo['hobby']):[];
  132. $userinfo['photo'] = !empty($userinfo['photo'])?json_decode($userinfo['photo'],true):[];
  133. $video_info = !empty($userinfo['video'])?json_decode($userinfo['video'],true):[];
  134. $userinfo['video'] = array_column($video_info,'url');
  135. $userinfo['like_num'] = $user->like_num;
  136. $userinfo['like_me_num'] = $user->like_me_num;
  137. $userinfo['look_num'] = $user->look_num;
  138. $userinfo['sex'] = $user->sex;
  139. $userinfo['mobile'] = $user->mobile;
  140. $userinfo['is_auth'] = $user->is_auth;
  141. $userinfo['is_vip'] = $user->is_vip;
  142. $userinfo['ycode'] = $user->ycode;
  143. $userinfo['notice_status'] = $user->notice_status;
  144. $userinfo['update_num'] = UserVipLimit::query()->where('user_id',$user->id)->value('user_info');
  145. $userinfo['see_user_num'] = UserVipLimit::query()->where('user_id',$user->id)->value("user_detail");
  146. return response()->json($userinfo);
  147. }
  148. /**
  149. * 修改个人资料
  150. */
  151. public function updateinfo(Request $request){
  152. $user = auth('api')->user();
  153. $validator = Validator::make($request->all(), [
  154. 'nickname' => 'required',
  155. 'birthday' => 'required',
  156. 'avatar' => 'required',
  157. 'height' => 'integer',
  158. 'weight' => 'integer',
  159. ], [
  160. 'nickname.required'=>"昵称必须",
  161. 'birthday.required'=>"请选择生日",
  162. 'avatar.required'=>"请上传头像",
  163. 'height.integer'=>"身高为整数",
  164. 'weight.integer'=>"体重为整数",
  165. ]);
  166. if ($validator->fails()) {
  167. return $this->response()->errorForbidden($validator->messages()->first());
  168. }
  169. $userinfo = UserInfoModel::where('user_id', $user->id)->first();
  170. if($request->nickname!=$userinfo->nickname){
  171. if(UserInfoModel::query()->leftJoin('users','users_info.user_id','=','users.id')
  172. ->where(['users_info.nickname'=>$request->nickname,'users.is_distory'=>0])
  173. ->where('users_info.user_id', '!=' , $user->id)->first()){
  174. return $this->response->errorForbidden('昵称已被使用');
  175. }
  176. }
  177. // if(UserVipLimit::query()->where('user_id',$user->id)->value('user_info')==1){
  178. // return $this->response->errorForbidden("每天只能修改一次");
  179. // }
  180. // $upd = array();
  181. // if(isset($request->nickname)&&!empty($request->nickname)){
  182. // $upd['nickname'] = $request->nickname;
  183. // }
  184. // if(isset($request->nickname)&&!empty($request->nickname)){
  185. // $upd['nickname'] = $request->nickname;
  186. // }
  187. if(UserInfoModel::where('user_id', $user->id)->update($request->input())){
  188. UserVipLimit::query()->where('user_id',$user->id)->increment('user_info',1);
  189. return response()->json(['message'=>"修改成功"]);
  190. }else{
  191. return $this->response->errorForbidden("修改失败");
  192. }
  193. }
  194. /**
  195. * 修改个人更多介绍
  196. */
  197. public function updateext(Request $request){
  198. $user = auth('api')->user();
  199. if(UserInfoModel::where('user_id', $user->id)->update($request->input())){
  200. return response()->json(['message'=>"修改成功"]);
  201. }else{
  202. return $this->response->errorForbidden("修改失败");
  203. }
  204. }
  205. /**
  206. * 获取照片和视频
  207. */
  208. public function get_photo(){
  209. try {
  210. $user = auth('api')->user();
  211. $userinfo = DB::table('users_info')->where(['user_id'=>$user->id])->select('photo','video')->first();
  212. $userinfo->photo = !empty($userinfo->photo)?json_decode($userinfo->photo,true):[];
  213. $video_info = !empty($userinfo->video)?json_decode($userinfo->video,true):[];
  214. $userinfo->video = array_column($video_info,'url');
  215. }catch (\Exception $exception){
  216. return $this->response->errorForbidden($exception->getMessage());
  217. }
  218. return response()->json($userinfo);
  219. }
  220. /**
  221. * 上传照片或者视频
  222. * @param Request $request
  223. * @return \Illuminate\Http\JsonResponse|void
  224. */
  225. public function upload_file(Request $request){
  226. try {
  227. if($request->post('url')==''){
  228. throw new Exception("请选择上传内容");
  229. }
  230. $user = auth('api')->user();
  231. $userinfo = UserInfoModel::query()->where('user_id',$user->id)->first();
  232. if($request->type==1){
  233. $new_arr = [
  234. "url"=>$request->post('url'),
  235. "state"=>$request->post('state',0)
  236. ];
  237. //图片
  238. $user_photo = json_decode($userinfo->photo,true);
  239. $user_photo []= $new_arr;
  240. $userinfo->photo = json_encode($user_photo);
  241. }else{
  242. //视频
  243. $user_video = json_decode($userinfo->video,true);
  244. $user_video []= ['url'=>$request->post('url')];
  245. $userinfo->video = json_encode($user_video);
  246. }
  247. $userinfo->save();
  248. }catch (\Exception $exception){
  249. return $this->response->errorForbidden($exception->getMessage());
  250. }
  251. return response()->json(['message'=>'上传成功']);
  252. }
  253. /**
  254. * 删除图片或者视频
  255. * @param Request $request
  256. * @return \Illuminate\Http\JsonResponse|void
  257. */
  258. public function del_file(Request $request){
  259. try {
  260. $this->userService->del_file($request);
  261. }catch (\Exception $exception){
  262. return $this->response->errorForbidden($exception->getMessage());
  263. }
  264. return response()->json(['message'=>'删除成功']);
  265. }
  266. /**
  267. * 获取VIP
  268. */
  269. public function get_vip(){
  270. try {
  271. $res['list'] = VipModel::query()->orderBy('id','asc')->get();
  272. $res['rights'] = json_decode(VipConfig::query()->where(['id'=>2])->value('rights'),true);
  273. $config = config("filesystems.disks.oss");
  274. foreach ($res['rights'] as $k=>$v){
  275. $res['rights'][$k]['img_url'] = "https://".$config['bucket'].'.'.$config['endpoint'].'/'.$v['img_url'];
  276. $res['rights'][$k]['big_img'] = "https://".$config['bucket'].'.'.$config['endpoint'].'/'.$v['big_img'];
  277. }
  278. }catch (\Exception $exception){
  279. return $this->response->errorForbidden($exception->getMessage());
  280. }
  281. return response()->json($res);
  282. }
  283. /**
  284. * 获取当前vip信息
  285. */
  286. public function get_vip_info(){
  287. try {
  288. $user = auth('api')->user();
  289. $user_info = UserInfoModel::query()->where('user_id',$user->id)->first();
  290. $res['avatar'] = $user_info->avatar;
  291. $res['nickname'] = $user_info->nickname;
  292. $res['is_vip'] = $user->is_vip;
  293. if($user->is_vip>0){
  294. $info = UserVipLogModel::query()->where(['user_id'=>$user->id])->first();
  295. $res['end_day'] = date("Y/m/d",strtotime($info['end_day']));
  296. }else{
  297. $res['end_day'] = "";
  298. }
  299. }catch (\Exception $exception){
  300. return $this->response->errorForbidden($exception->getMessage());
  301. }
  302. return response()->json($res);
  303. }
  304. /**
  305. * 购买/续费vip
  306. * @param Request $request
  307. * @return \Illuminate\Http\JsonResponse|void
  308. */
  309. public function buy_vip(Request $request){
  310. try {
  311. $user = auth('api')->user();
  312. $param['id'] = $request->id; //vip id
  313. $param['user_id'] = $user->id;
  314. $param['payment'] = $request->post('payment',1); //支付方式 1微信 2支付宝
  315. $res = $this->userService->buy_vip($param);
  316. //throw new Exception(json_encode($res));
  317. }catch (\Exception $exception){
  318. return $this->response->errorForbidden($exception->getMessage());
  319. }
  320. return response()->json($res);
  321. }
  322. /**
  323. * 问题反馈
  324. * @param Request $request
  325. * @return \Dingo\Api\Http\Response|void
  326. */
  327. public function problem(Request $request){
  328. try {
  329. $user = auth('api')->user();
  330. $param = new ProblemParam();
  331. $param->user_id = $user->id;
  332. $param->content = $request->post('content');
  333. $param->img_url = $request->post('img_url');
  334. $param->status = 0;
  335. $this->userService->problem($param);
  336. }catch (\Exception $exception){
  337. return $this->response->errorForbidden($exception->getMessage());
  338. }
  339. return response()->json(['message'=>"提交成功"]);
  340. }
  341. /**
  342. * 看过我
  343. * @param Request $request
  344. * @return \Illuminate\Http\JsonResponse|void
  345. */
  346. public function looked_me(Request $request){
  347. try {
  348. $res =$this->userService->looked_me();
  349. }catch (\Exception $exception){
  350. return $this->response->errorForbidden($exception->getMessage());
  351. }
  352. return response()->json($res);
  353. }
  354. /**
  355. * 认证中心
  356. */
  357. public function auth_center(Request $request){
  358. try {
  359. if(empty($request->avatar)){
  360. throw new Exception("请上传头像");
  361. }
  362. if(empty($request->photo)){
  363. throw new Exception("请上传生活照");
  364. }
  365. $user = auth('api')->user();
  366. $userinfo = UserInfoModel::query()->where('user_id',$user->id)->first();
  367. //图片
  368. $user_photo = json_decode($userinfo->photo,true);
  369. $photo = $request->post('photo');
  370. if(is_array($photo) && count($photo)>0){
  371. foreach ($photo as $k=>$v){
  372. $new_arr = [
  373. "url" => $v,
  374. "state" => 0
  375. ];
  376. $user_photo []= $new_arr;
  377. }
  378. }
  379. $userinfo->photo = json_encode($user_photo);
  380. $userinfo->avatar =$request->avatar;
  381. $userinfo->save();
  382. }catch (\Exception $exception){
  383. return $this->response->errorForbidden($exception->getMessage());
  384. }
  385. return response()->json(['message'=>"提交成功"]);
  386. }
  387. /**
  388. * 人脸对比
  389. * @param Request $request
  390. * @return \Illuminate\Http\JsonResponse|void
  391. */
  392. public function check_auth(Request $request){
  393. $apiFace = new \AipFace(env('BAI_DU_YUN_APP_ID'),env('BAI_DU_YUN_API_KEY'),env('BAI_DU_YUN_SECRET_KEY'));
  394. try {
  395. $user = auth('api')->user();
  396. $userinfo = UserInfoModel::query()->where('user_id',$user->id)->first();
  397. if(empty($request->auth_pic)){
  398. throw new Exception("参数错误");
  399. }
  400. $images = array(
  401. array(
  402. 'image' => base64_encode(file_get_contents($userinfo->avatar)),
  403. // 'image' => base64_encode(file_get_contents("https://zhengda.oss-accelerate.aliyuncs.com/tinymce/images/dcdc14c5987ebbb233a6232264bb80e260f4f496e12ba.jpg")),
  404. 'image_type' => 'BASE64',
  405. 'liveness_control'=>'NORMAL',
  406. ),
  407. array(
  408. 'image' => $request->auth_pic,
  409. // 'image' => base64_encode(file_get_contents($request->auth_pic)),
  410. // 'image' => base64_encode(file_get_contents("https://zhengda.oss-accelerate.aliyuncs.com/tinymce/images/cea206a2046b895f253445f35794226860f4f4df1d786.jpg")),
  411. 'image_type' => 'BASE64',
  412. //'liveness_control'=>'NORMAL',
  413. ),
  414. );
  415. //throw new Exception(json_encode($images));
  416. $result = $apiFace->match($images);
  417. // $result = json_decode($result,true);
  418. if($result['error_code']==0){
  419. if($result['result']['score']>80){
  420. $user->is_auth = 1;
  421. $user->save();
  422. }else{
  423. throw new Exception("人脸检测不通过");
  424. }
  425. }else{
  426. throw new Exception($result['error_msg']);
  427. }
  428. }catch (\Exception $exception){
  429. return $this->response->errorForbidden($exception->getMessage());
  430. }
  431. return response()->json(['message'=>"认证成功"]);
  432. }
  433. /**
  434. * 黑名单
  435. * @param Request $request
  436. * @return \Illuminate\Http\JsonResponse|void
  437. */
  438. public function black_list(Request $request){
  439. try {
  440. $res = $this->userService->black_list();
  441. }catch (\Exception $exception){
  442. return $this->response->errorForbidden($exception->getMessage());
  443. }
  444. return response()->json($res);
  445. }
  446. /**
  447. * 移除黑名单
  448. * @param Request $request
  449. * @return \Illuminate\Http\JsonResponse|void
  450. */
  451. public function del_black(Request $request){
  452. try {
  453. $this->userService->del_black($request->id);
  454. }catch (\Exception $exception){
  455. return $this->response->errorForbidden($exception->getMessage());
  456. }
  457. return response()->json(['message'=>'操作成功']);
  458. }
  459. /**
  460. * 设置在线状态和通知开关
  461. * @param Request $request
  462. * @return \Illuminate\Http\JsonResponse|void
  463. */
  464. public function online_status(Request $request){
  465. try {
  466. if($request->type=='get'){
  467. $user = auth('api')->user();
  468. $res = User::query()->find($user->id,['online','notice_status']);
  469. }else{
  470. $res=$this->userService->online_status($request);
  471. }
  472. }catch (\Exception $exception){
  473. return $this->response->errorForbidden($exception->getMessage());
  474. }
  475. return response()->json(['message'=>'操作成功','data'=>$res]);
  476. }
  477. /**
  478. * 获取邀请福利信息
  479. * @param Request $request
  480. * @return \Illuminate\Http\JsonResponse|void
  481. */
  482. public function invite_info(Request $request){
  483. try {
  484. $res=$this->userService->invite_info($request);
  485. }catch (\Exception $exception){
  486. return $this->response->errorForbidden($exception->getMessage());
  487. }
  488. return response()->json($res);
  489. }
  490. /**
  491. * 获取邀请人员列表
  492. * @param Request $request
  493. * @return \Illuminate\Http\JsonResponse|void
  494. */
  495. public function get_invite_list(){
  496. try {
  497. $res=$this->userService->get_invite_list();
  498. }catch (\Exception $exception){
  499. return $this->response->errorForbidden($exception->getMessage());
  500. }
  501. return response()->json($res);
  502. }
  503. /**
  504. * 领取会员天数
  505. * @param Request $request
  506. * @return \Illuminate\Http\JsonResponse|void
  507. */
  508. public function receive_day(){
  509. DB::beginTransaction();
  510. try {
  511. $this->userService->receive_day();
  512. DB::commit();
  513. }catch (\Exception $exception){
  514. DB::rollBack();
  515. return $this->response->errorForbidden($exception->getMessage());
  516. }
  517. return response()->json(['message'=>'操作成功']);
  518. }
  519. /**
  520. * 设置手势锁
  521. */
  522. public function lock_pass(){
  523. }
  524. /**
  525. * 注销账户
  526. */
  527. public function distory_user(){
  528. try {
  529. DB::beginTransaction();
  530. $this->userService->distory_user();
  531. DB::commit();
  532. }catch (\Exception $exception){
  533. DB::rollBack();
  534. return $this->response->errorForbidden($exception->getMessage());
  535. }
  536. return $this->response->errorUnauthorized("注销成功");
  537. }
  538. //分享用户
  539. public function share(Request $request){
  540. $user = auth('api')->user();
  541. // if(isset($request->user_id)){
  542. //
  543. // }
  544. $url = "https://".$_SERVER['HTTP_HOST'].'/web/register.html?invoce='.$user->ycode;
  545. $res['appid'] = env("WEIXIN_OPEN_APPID");
  546. $res['url'] = $url;
  547. return response()->json($res);
  548. }
  549. }