ProfileController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <?php
  2. namespace App\Http\Controllers\User;
  3. use App\Exceptions\AlertException;
  4. use App\Http\Controllers\Core\Auth;
  5. use App\Http\Requests\UserRequest;
  6. use App\Jobs\StoreSelfPartnerJob;
  7. use App\Managers\UserManager;
  8. use App\Models\Fpdx\WxkfModel;
  9. use App\Models\Log\AppSignModel;
  10. use App\Models\NoticeModel;
  11. use App\Models\User\AuthKey;
  12. use App\Services\Auth\AuthKeyService;
  13. use App\Services\Goodnight\UserService;
  14. use App\Services\User\PartnerService;
  15. use App\Services\User\ProfileService;
  16. use App\Services\User\SignService;
  17. use Exception;
  18. use Illuminate\Http\Request;
  19. use Illuminate\Support\Facades\Redis;
  20. use App\Http\Controllers\Controller;
  21. use App\Models\User\UserModel;
  22. use App\Models\PartnerModel;
  23. use App\Models\TagModel;
  24. /**
  25. * 用户基础设置
  26. * Class ProfileController
  27. * @package App\Http\Controllers\User
  28. */
  29. class ProfileController extends Controller
  30. {
  31. /**
  32. * 修改某人的用户信息
  33. * @param Request $request
  34. * @return array
  35. * @throws AlertException
  36. * @throws \AlicFeng\IdentityCard\Exception\BirthdayException
  37. * @deprecated User/UpdateProfile
  38. */
  39. public function update(UserRequest $request)
  40. {
  41. $uid = Auth::auth();
  42. /** @var UserModel $user */
  43. $user = UserModel::findOrFail($uid);
  44. // 更新基础数据
  45. $extData = $request->only(['nickname', 'headimgurl', 'sex']);
  46. if (!empty($extData)) {
  47. $pService = new ProfileService();
  48. $pService->updateUser($user, $extData);
  49. }
  50. $app = $request->get('model') ?? "fpdx";
  51. switch ($app) {
  52. case 'goodnight': # 互道晚安
  53. $extData = $request->only(['cover', 'like_sex', 'like_tone_1', 'like_tone_2', 'like_tone_3', 'voice']);
  54. if (!empty($extData)) {
  55. $guService = new UserService();
  56. $guService->updateUser($user, $extData);
  57. }
  58. break;
  59. case 'fpdx': # 72小时恋爱体验
  60. default: # 基础信息
  61. $extData = $request->except(array(
  62. 'nickname',
  63. 'headimgurl',
  64. 'sex',
  65. 'gold_flower',
  66. 'red_flower',
  67. 'task_type',
  68. 'wx_auth',
  69. 'identity_auth',
  70. 'wxkf',
  71. 'login_at',
  72. 'created_at',
  73. 'phone',
  74. ));
  75. if (!empty($extData)) {
  76. $pService = new ProfileService();
  77. $pService->updateUser($user, $extData);
  78. }
  79. break;
  80. }
  81. return $this->user($request);
  82. }
  83. /**
  84. * 修改标签
  85. * @param Request $request
  86. * @return array
  87. * @deprecated User/ToggleTag
  88. */
  89. public function toggleTag(Request $request)
  90. {
  91. $this->validate($request, ['tag_id' => 'required', 'group' => 'required']);
  92. $tag_id = $request->post('tag_id');
  93. $group = $request->post('group');
  94. $uid = Auth::auth();
  95. $ps = new ProfileService();
  96. $ps->toggleTag($uid, $tag_id, $group);
  97. return array(
  98. 'code' => 200,
  99. 'message' => 'success'
  100. );
  101. }
  102. /**
  103. * 获取用户标签
  104. * @return array
  105. */
  106. public function getTags()
  107. {
  108. $uid = Auth::auth();
  109. $user = UserModel::find($uid);
  110. $tagModel = new TagModel();
  111. $tags = $tagModel->getAllTagByUser($user);
  112. return array(
  113. 'code' => 200,
  114. 'message' => 'success',
  115. 'data' => $tags
  116. );
  117. }
  118. /**
  119. * 选择封面
  120. * @param Request $request
  121. * @return array
  122. * @throws AlertException
  123. * @deprecated User/SelectCover
  124. */
  125. public function selectCover(Request $request)
  126. {
  127. $uid = Auth::auth();
  128. $this->validate($request, [
  129. 'field' => 'required|in:photo_1,photo_2,photo_3,photo_4'
  130. ]);
  131. $user = UserModel::findOrFail($uid);
  132. /** @var UserModel $user */
  133. $field = $request->input('field');
  134. if (empty($user->{$field})) {
  135. throw new AlertException("参数错误", 101);
  136. }
  137. $identity_auth = $user->identity_auth;
  138. if (!empty($identity_auth)) {
  139. $identity_auths = explode(",", $identity_auth);
  140. array_walk($identity_auths, function (&$value) use ($field) {
  141. if ($value == "photo_src") {
  142. $value = $field;
  143. } elseif ($value == $field) {
  144. $value = "photo_src";
  145. }
  146. });
  147. $identity_auth = implode(",", $identity_auths);
  148. $user->identity_auth = $identity_auth;
  149. }
  150. $tmp = $user->photo_src;
  151. $user->photo_src = $user->{$field};
  152. $user->{$field} = $tmp;
  153. $user->save();
  154. $ps = new PartnerService();
  155. $ps->selectCover($user->partner_id, $field);
  156. return response([
  157. 'code' => 200,
  158. 'message' => 'success'
  159. ]);
  160. }
  161. /**
  162. * 删除照片
  163. * @param Request $request
  164. * @return array
  165. * @throws Exception
  166. * @deprecated User/DeletePhotos
  167. */
  168. public function deletePhotos(Request $request)
  169. {
  170. $this->validate($request, [
  171. 'field' => 'required|in:photo_src,photo_1,photo_2,photo_3,photo_4'
  172. ]);
  173. $uid = Auth::auth();
  174. $pfs = new ProfileService();
  175. $pfs->deletePhoto($uid, $request->post('field'));
  176. return response([
  177. 'code' => 200,
  178. 'message' => 'success'
  179. ]);
  180. }
  181. /**
  182. * 获取用户信息
  183. * @param Request $request
  184. * @return array
  185. * @deprecated User/Profile
  186. */
  187. public function user(Request $request)
  188. {
  189. $uid = Auth::auth();
  190. $app = $request->get('model') ?? "fpdx";
  191. $manager = new UserManager();
  192. $user = $manager->profile($uid, $app);
  193. return response([
  194. 'code' => 200,
  195. 'message' => 'success',
  196. 'data' => $user,
  197. ]);
  198. }
  199. /**
  200. * 通过uid获取某人信息
  201. * @param int $uid
  202. * @return array
  203. */
  204. public function userByUid(int $uid)
  205. {
  206. /** @var UserModel $user */
  207. $user = UserModel::findOrFail($uid);
  208. try {
  209. /** @var PartnerModel $partner */
  210. $partner = PartnerModel::findOrFail($user->partner_id);
  211. if (1 != $partner->check_photo) {
  212. unset($user->photo_src);
  213. }
  214. if (1 != $partner->photo_1_check) {
  215. unset($user->photo_1);
  216. }
  217. if (1 != $partner->photo_2_check) {
  218. unset($user->photo_2);
  219. }
  220. if (1 != $partner->photo_3_check) {
  221. unset($user->photo_3);
  222. }
  223. if (1 != $partner->photo_4_check) {
  224. unset($user->photo_4);
  225. }
  226. if (1 != $partner->voice_check) {
  227. unset($user->voice);
  228. }
  229. $user->black_at = $partner->black_at < time() ? 0 : $partner->black_at;
  230. $user->is_sell = $partner->is_sell;
  231. } catch (Exception $exception) {
  232. }
  233. return response([
  234. 'code' => 200,
  235. 'message' => 'success',
  236. 'data' => $user
  237. ]);
  238. }
  239. /**
  240. * 创建交友卡片
  241. * @param Request $request
  242. * @return array
  243. * @throws Exception
  244. * @deprecated User/StoreSelfPartner
  245. */
  246. public function storeSelfPartner(Request $request)
  247. {
  248. $uid = Auth::auth();
  249. try {
  250. $user = UserModel::findOrFail($uid);
  251. if ($user->partner_id > 0) {
  252. return array(
  253. 'code' => 200,
  254. 'message' => 'success',
  255. 'data' => $user
  256. );
  257. }
  258. $partnerModel = new PartnerModel();
  259. $data = $user->toArray();
  260. $data['media_id'] = $request->get('media_id', "");
  261. $data['secret'] = bin2hex(openssl_random_pseudo_bytes(4));
  262. $partner = $partnerModel->fill($data);
  263. $partner->created_at = time();
  264. if (!empty($partner->qq) || !empty($partner->weixin)) {
  265. $partner->is_sell = 1;
  266. }
  267. if (!empty($partner->photo_src)) {
  268. $partner->check_photo = 0;
  269. $partner->is_commit_check = 1;
  270. }
  271. if (!empty($partner->voice)) {
  272. $partner->voice_check = 0;
  273. $partner->is_commit_check = 1;
  274. }
  275. if (!empty($partner->photo_1)) {
  276. $partner->photo_1_check = 0;
  277. $partner->is_commit_check = 1;
  278. }
  279. if (!empty($partner->photo_2)) {
  280. $partner->photo_2_check = 0;
  281. $partner->is_commit_check = 1;
  282. }
  283. if (!empty($partner->photo_3)) {
  284. $partner->photo_3_check = 0;
  285. $partner->is_commit_check = 1;
  286. }
  287. if (!empty($partner->photo_4)) {
  288. $partner->photo_4_check = 0;
  289. $partner->is_commit_check = 1;
  290. }
  291. if (Redis::exists("storeselfpartner:{$uid}")) {
  292. throw new AlertException("请求频繁", 104);
  293. }
  294. Redis::setex("storeselfpartner:{$uid}", 5, true);
  295. if ($partner->save()) {
  296. $user->partner_id = $partner->id;
  297. $user->save();
  298. // 系统通知
  299. $sex = 1 == $partner->sex ? "小姐姐" : "小哥哥";
  300. NoticeModel::create([
  301. 'uid' => $uid,
  302. 'title' => '交友卡片上传完成',
  303. 'content' => "你的交友卡片已经完成啦!现在可以去交友大厅邀请你心动的{$sex}啦~ ",
  304. 'type' => 5,
  305. 'type_id' => 0,
  306. 'create_at' => time(),
  307. 'update_at' => time(),
  308. 'tab_content' => '进入大厅',
  309. 'tab_url' => '/pages/index/index',
  310. ]);
  311. }
  312. Redis::del(["storeselfpartner:{$uid}"]);
  313. StoreSelfPartnerJob::dispatch($uid);
  314. } catch (Exception $e) {
  315. if (Redis::exists("storeselfpartner:{$uid}")) {
  316. Redis::del(["storeselfpartner:{$uid}"]);
  317. }
  318. throw $e;
  319. }
  320. return array(
  321. 'code' => 200,
  322. 'message' => 'success',
  323. 'data' => $user
  324. );
  325. }
  326. /**
  327. * 照片活体验证对比
  328. * @param Request $request
  329. * @return array
  330. * @throws AlertException
  331. */
  332. public function facematch(Request $request)
  333. {
  334. $uid = Auth::auth();
  335. $this->validate($request, [
  336. 'field' => "required",
  337. 'face_url' => 'required'
  338. ]);
  339. $pfs = new ProfileService();
  340. $pfs->fatchmatch($uid, $request->post('face_url'), $request->post('field'));
  341. return response([
  342. 'code' => 200,
  343. 'message' => 'success',
  344. 'data' => array(
  345. 'is_pass' => true
  346. )
  347. ]);
  348. }
  349. /**
  350. * 微信验证
  351. * @param Request $request
  352. * @return array
  353. * @throws Exception
  354. */
  355. public function wxverify(Request $request)
  356. {
  357. $this->validate($request, [
  358. 'wxid' => 'required',
  359. 'kf' => 'required'
  360. ]);
  361. $wxid = $request->post('wxid');
  362. $kf = $request->post('kf');
  363. $uid = Auth::auth();
  364. try {
  365. $aks = new AuthKeyService();
  366. $aks->bindKey($uid, $wxid, 'wxid');
  367. } catch (Exception $exception) {
  368. $auth = AuthKey::where([
  369. ['uid', $uid],
  370. ['auth_type', 'wxid']
  371. ])->firstOrFail();
  372. $wxid = $auth->auth_key;
  373. }
  374. $psf = new ProfileService();
  375. return $psf->wxverify($uid, $wxid, $kf);
  376. }
  377. /**
  378. * 微信验证申请人工审核
  379. * @param Request $request
  380. * @return array
  381. * @throws Exception
  382. */
  383. public function wxverifyByArtifical(Request $request)
  384. {
  385. Auth::auth();
  386. $this->validate($request, [
  387. 'wxid' => 'required',
  388. 'kf' => 'required'
  389. ]);
  390. $wxid = $request->get('wxid');
  391. $kf = $request->get('kf');
  392. $auth = AuthKey::where('auth_key', $wxid)->firstOrFail();
  393. $user = UserModel::findOrFail($auth->uid);
  394. if ($user->wx_auth == 1) {
  395. return array(
  396. 'code' => 200,
  397. 'message' => 'success'
  398. );
  399. }
  400. $wxkf = WxkfModel::where('wxid', $kf)->firstOrFail();
  401. $url = "https://m.fenpeiduixiang.com/fpdx-wxauth/wxverify.html?uid={$user->uid}&wxid={$wxid}&kf={$kf}";
  402. $content = [
  403. 'first' => [
  404. 'value' => "用户微信认证失败:\n 原因: 未能获得微信号",
  405. ],
  406. 'keyword1' => [
  407. 'value' => "\nUID:{$user->uid}\n用户昵称:{$user->nickname}\n审核客服:{$wxkf->nickname}",
  408. ],
  409. 'keyword2' => [
  410. 'value' => "\n微信id:{$wxid}\n 点此人工认证:\n{$url}",
  411. ],
  412. 'remark' => [
  413. 'value' => "点此人工认证",
  414. "color" => "#FF7E98",
  415. ],
  416. ];
  417. $psf = new ProfileService();
  418. $psf->wxverifyWarn($content, $url);
  419. return response([
  420. 'code' => 200,
  421. 'message' => 'success'
  422. ]);
  423. }
  424. /**
  425. * app签到
  426. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  427. * @throws Exception
  428. */
  429. public function appSigns(Request $request)
  430. {
  431. $uid = Auth::auth();
  432. try {
  433. $platform = $request->header('platform');
  434. $signService = new SignService();
  435. $signService->sign($uid, $platform);
  436. $flower = $signService->rewardFlower($uid);
  437. } catch (Exception $e) {
  438. $flower = 0;
  439. }
  440. if (AppSignModel::where('uid', $uid)->where('date', date('Y-m-d'))->first()) {
  441. return response([
  442. 'code' => 410001,
  443. 'message' => '今天已经签到,请明天再来'
  444. ]);
  445. }
  446. \DB::beginTransaction();
  447. try {
  448. AppSignModel::create([
  449. 'uid' => $uid,
  450. 'created_at' => time(),
  451. 'date' => date('Y-m-d')
  452. ]);
  453. UserModel::where('uid', $uid)->increment('app_like_unlock_count');
  454. \DB::commit();
  455. return response([
  456. 'code' => 200,
  457. 'message' => 'success',
  458. 'data' => [
  459. 'app_sign_at' => mktime(0, 0, 0) + 86400,
  460. 'flower' => $flower,
  461. ]
  462. ]);
  463. } catch (Exception $e) {
  464. \DB::rollBack();
  465. return response([
  466. 'code' => 40910,
  467. 'message' => '您点的太快啦'
  468. ]);
  469. }
  470. }
  471. }