PartnerController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. <?php
  2. namespace App\Http\Controllers\User;
  3. use App\Exceptions\AlertException;
  4. use App\Http\Controllers\Controller;
  5. use App\Http\Controllers\Core\Auth;
  6. use App\Models\Fpdx\ActivityModel;
  7. use App\Models\Deed\FriendsModel;
  8. use App\Models\Fpdx\PairModel;
  9. use App\Models\User\InviteConfigModel;
  10. use App\Models\User\SuperLikeModel;
  11. use App\Services\Home\FeedService;
  12. use App\Services\User\NoticeService;
  13. use App\Services\User\PartnerService;
  14. use App\Models\Deed\InvitationCardModel;
  15. use App\Models\PartnerModel;
  16. use App\Models\PraiseModel;
  17. use App\Models\User\UserModel;
  18. use App\Models\BuyInviteListModel;
  19. use App\Models\BuyInviteModel;
  20. use Illuminate\Http\Request;
  21. use Illuminate\Support\Facades\Redis;
  22. use Tymon\JWTAuth\Exceptions\JWTException;
  23. /**
  24. * 卡片
  25. * Class PartnerController
  26. * @package App\Http\Controllers\User
  27. */
  28. class PartnerController extends Controller
  29. {
  30. /**
  31. * 上传一张卖室友卡片
  32. * @param Request $request
  33. * @throws \Exception
  34. */
  35. public function store(Request $request)
  36. {
  37. }
  38. /**
  39. * 更新卡片信息
  40. * @param int $partner_id
  41. * @param array $data
  42. */
  43. public function updatePartner(int $partner_id, array $data)
  44. {
  45. $partner = PartnerModel::findOrFail($partner_id);
  46. if (isset($data['voice'])) {
  47. $data['voice_check'] = 0;
  48. $data['is_commit_check'] = 1;
  49. }
  50. if (isset($data['photo_src'])) {
  51. $data['check_photo'] = 0;
  52. $data['is_commit_check'] = 1;
  53. }
  54. if (isset($data['photo_1'])) {
  55. $data['photo_1_check'] = 0;
  56. $data['is_commit_check'] = 1;
  57. }
  58. if (isset($data['photo_2'])) {
  59. $data['photo_2_check'] = 0;
  60. $data['is_commit_check'] = 1;
  61. }
  62. if (isset($data['photo_3'])) {
  63. $data['photo_3_check'] = 0;
  64. $data['is_commit_check'] = 1;
  65. }
  66. if (isset($data['photo_4'])) {
  67. $data['photo_4_check'] = 0;
  68. $data['is_commit_check'] = 1;
  69. }
  70. if (isset($data['is_commit_check']) && 1 == $data['is_commit_check']) {
  71. $ings = ActivityModel::getActivitys();
  72. $exists = PairModel::where([
  73. 'uid' => $partner->uid,
  74. 'stage_id' => $ings['ing'] ?: 0
  75. ])->exists();
  76. if ($exists) {
  77. $data['is_fpdx'] = 1;
  78. } else {
  79. $data['is_fpdx'] = 0;
  80. }
  81. }
  82. $data['update_at'] = time();
  83. $partner->fill($data);
  84. if (isset($data['sex']) && 1 == $partner->is_sell) {
  85. $user = UserModel::findOrFail($partner->uid);
  86. if ($partner->sex == 1) {
  87. Redis::zrem("fpdx:user:locations:sell:girl", $partner->uid);
  88. Redis::geoAdd("fpdx:user:locations:sell:boy", $user->lng, $user->lat, $partner->uid);
  89. } else {
  90. Redis::zrem("fpdx:user:locations:sell:boy", $partner->uid);
  91. Redis::geoAdd("fpdx:user:locations:sell:girl", $user->lng, $user->lat, $partner->uid);
  92. }
  93. }
  94. if (1 == $partner->is_sell && empty($partner->qq) && empty($partner->weixin)) {
  95. $partner->is_sell = 0;
  96. Redis::zrem("fpdx:user:locations:sell:boy", $partner->uid);
  97. Redis::zrem("fpdx:user:locations:sell:girl", $partner->uid);
  98. }
  99. if (-1 == $partner->is_sell && 1 == $partner->is_commit_check) {
  100. $partner->is_sell = 0;
  101. Redis::zrem("fpdx:user:locations:sell:boy", $partner->uid);
  102. Redis::zrem("fpdx:user:locations:sell:girl", $partner->uid);
  103. }
  104. $partner->save();
  105. }
  106. /**
  107. * 更新卡片信息接口
  108. * @param int $partner_id
  109. * @param Request $request
  110. * @return array
  111. * @throws AlertException
  112. */
  113. public function update(int $partner_id, Request $request)
  114. {
  115. $data = $request->only([
  116. 'home',
  117. 'address',
  118. 'sex',
  119. 'photo_src',
  120. 'photo_1',
  121. 'photo_2',
  122. 'photo_3',
  123. 'photo_4',
  124. 'voice',
  125. 'school',
  126. 'qq',
  127. 'weixin',
  128. 'subject',
  129. 'grade',
  130. 'education',
  131. 'height',
  132. 'age',
  133. 'star',
  134. 'introduce',
  135. 'expect',
  136. 'allow',
  137. 'auto_sell',
  138. 'is_hide',
  139. 'is_fpdx'
  140. ]);
  141. $ps = new PartnerService();
  142. $ps->updatePartner($partner_id, $data);
  143. return response([
  144. 'code' => 200,
  145. 'message' => 'success'
  146. ]);
  147. }
  148. /**
  149. * 获取卡片信息
  150. * @param int $partner_id
  151. * @return array
  152. */
  153. public function getPartner(int $partner_id)
  154. {
  155. $uid = Auth::auth();
  156. /** @var PartnerModel $partner */
  157. $partner = PartnerModel::findOrFail($partner_id);
  158. $partner->black_at = $partner->black_at < time() ? 0 : $partner->black_at;
  159. try {
  160. /** @var PartnerModel $user */
  161. $user = UserModel::findOrfail($partner->uid, [
  162. 'uid',
  163. 'headimgurl',
  164. 'nickname',
  165. 'age',
  166. 'star',
  167. 'last_life',
  168. 'why',
  169. 'pair_voice',
  170. 'identity_auth',
  171. 'wx_auth',
  172. 'qq',
  173. 'weixin',
  174. 'introduce',
  175. 'expect',
  176. 'height',
  177. 'sex',
  178. 'school',
  179. 'address',
  180. 'home',
  181. 'tag_1',
  182. 'tag_2',
  183. 'tag_3',
  184. 'tag_4',
  185. 'login_at',
  186. 'claim_tag',
  187. 'work_state',
  188. 'be_vip_at',
  189. 'supvip_endat',
  190. 'logoff_at',
  191. 'bgcolor'
  192. ]);
  193. if ($inviteConf = InviteConfigModel::find($partner->uid)) {
  194. $user->task_photo = $inviteConf->task_photo;
  195. $user->task_question = $inviteConf->task_question;
  196. $user->task_sing = $inviteConf->task_sing;
  197. $user->task_question_data = $inviteConf->task_question_data ? true : false;
  198. $user->task_sing_data = $inviteConf->task_sing_data ? true : false;
  199. } else {
  200. $user->task_photo = false;
  201. $user->task_question = false;
  202. $user->task_sing = false;
  203. $user->task_question_data = false;
  204. $user->task_sing_data = false;
  205. }
  206. $user->invite_cnt = InvitationCardModel::where('invite_uid', $user->uid)->count();
  207. $partner->headimgurl = $user->headimgurl;
  208. $partner->nickname = $user->nickname;
  209. } catch (\Exception $e) {
  210. $user = null;
  211. $partner->headimgurl = null;
  212. $partner->nickname = null;
  213. }
  214. if ($uid != $partner->uid) {
  215. if (1 != $partner->check_photo) {
  216. unset($partner->photo_src);
  217. }
  218. if (1 != $partner->photo_1_check) {
  219. unset($partner->photo_1);
  220. }
  221. if (1 != $partner->photo_2_check) {
  222. unset($partner->photo_2);
  223. }
  224. if (1 != $partner->photo_3_check) {
  225. unset($partner->photo_3);
  226. }
  227. if (1 != $partner->photo_4_check) {
  228. unset($partner->photo_4);
  229. }
  230. if (1 != $partner->voice_check) {
  231. unset($partner->voice);
  232. }
  233. if (1 != $partner->introduce_check) {
  234. unset($partner->introduce);
  235. unset($partner->expect);
  236. }
  237. unset($partner->check_photo);
  238. unset($partner->photo_1_check);
  239. unset($partner->photo_2_check);
  240. unset($partner->photo_3_check);
  241. unset($partner->photo_4_check);
  242. unset($partner->voice_check);
  243. unset($partner->check_contact);
  244. unset($partner->introduce_check);
  245. unset($partner->qq);
  246. unset($partner->weixin);
  247. // 与self的关系处理
  248. $self = array(
  249. 'friend' => false,
  250. 'praise' => false,
  251. 'invite' => false,
  252. 'superlike' => false
  253. );
  254. $friend = FriendsModel::where([['uid', $uid], ['friend_uid', $partner->uid]])->first();
  255. if (collect($friend)->isEmpty()) {
  256. !empty($user->weixin) && $user->weixin = substr($user->weixin, 0, 1) . "****" . substr(
  257. $user->weixin,
  258. -1,
  259. 1
  260. );
  261. !empty($user->qq) && $user->qq = substr($user->qq, 0, 1) . "****" . substr($user->qq, -1, 1);
  262. }
  263. $self['friend'] = $friend;
  264. if (PraiseModel::where([['uid', $uid], ['partner_id', $partner->id], ['type', 1]])->exists()) {
  265. $self['praise'] = true;
  266. }
  267. if (
  268. InvitationCardModel::where([
  269. ['uid', $uid],
  270. ['invite_uid', $partner->uid],
  271. ['expired_at', '>', time()]
  272. ])->exists()
  273. ) {
  274. $self['invite'] = true;
  275. }
  276. if (SuperLikeModel::where([['uid', $uid], ['partner_id', $partner->id]])->exists()) {
  277. $self['superlike'] = true;
  278. }
  279. $partner->self = $self;
  280. }
  281. $partner->setAttribute('user', $user);
  282. $praise = PraiseModel::where([['partner_id', $partner->id], ['uid', $uid]])->first();
  283. $partner->praise = isset($praise->type) ? $praise->type : 0;
  284. $buyInviteListModel = new BuyInviteListModel();
  285. $inviteList = $buyInviteListModel->where([['partner_id', $partner->id], ['uid', $uid]])->first();
  286. if (!collect($inviteList)->isEmpty()) {
  287. $partner->invite_id = $inviteList->id;
  288. $buyInviteModel = new BuyInviteModel();
  289. $partner->invite_percent = $buyInviteModel->effectiveInvite($uid, $partner_id);
  290. }
  291. return response([
  292. 'code' => 200,
  293. 'message' => 'success',
  294. 'data' => $partner
  295. ]);
  296. }
  297. /**
  298. * 上下架卡片
  299. * @param int $partner_id
  300. * @param Request $request
  301. * @return array
  302. * @throws AlertException
  303. * @throws \Illuminate\Validation\ValidationException
  304. */
  305. public function sell(int $partner_id, Request $request)
  306. {
  307. $this->validate($request, ['is_sell' => 'in:-1,0,1']);
  308. $is_sell = $request->input('is_sell');
  309. $ups = new PartnerService();
  310. $ups->sell($partner_id, $is_sell);
  311. return response([
  312. 'code' => 200,
  313. 'message' => 'success'
  314. ]);
  315. }
  316. /**
  317. * 判断是否可上架
  318. */
  319. public function isCanSell()
  320. {
  321. $uid = Auth::auth();
  322. /** @var UserModel $user */
  323. $user = UserModel::findOrFail($uid);
  324. $isCanSell = false;
  325. if ($user->partner_id) {
  326. /** @var PartnerModel $partner */
  327. $partner = PartnerModel::findOrFail($user->partner_id);
  328. $ps = new PartnerService();
  329. if ($partner->black_at < time() && $ps->isCanSell($partner)) {
  330. $isCanSell = true;
  331. }
  332. }
  333. return array(
  334. 'code' => 200,
  335. 'message' => 'success',
  336. 'data' => array(
  337. 'is_can_sell' => $isCanSell
  338. )
  339. );
  340. }
  341. /**
  342. * 通过用户获取卡片交友卡片信息
  343. * @param int $user_id
  344. * @return array
  345. */
  346. public function getPartnerByUser(int $user_id)
  347. {
  348. $uid = Auth::auth();
  349. /** @var PartnerModel $partner */
  350. $partner = PartnerModel::where('uid', $user_id)->firstOrFail();
  351. $partner->black_at = $partner->black_at < time() ? 0 : $partner->black_at;
  352. if ($uid == $user_id) {
  353. return $this->getPartner($partner->id);
  354. }
  355. $ps = new PartnerService();
  356. $partner = $ps->getUserSelf($user_id);
  357. // 与self的关系处理
  358. $self = array(
  359. 'friend' => false,
  360. 'praise' => false,
  361. 'invite' => false,
  362. 'superlike' => false
  363. );
  364. $friend = FriendsModel::where([['uid', $uid], ['friend_uid', $partner->uid]])->first();
  365. if (collect($friend)->isEmpty()) {
  366. !empty($partner->weixin) && $partner->weixin = substr(
  367. $partner->weixin,
  368. 0,
  369. 1
  370. ) . "****" . substr($partner->weixin, -1, 1);
  371. !empty($partner->qq) && $partner->qq = substr($partner->qq, 0, 1) . "****" . substr($partner->qq, -1, 1);
  372. }
  373. $self['friend'] = $friend;
  374. if (PraiseModel::where([['uid', $uid], ['partner_id', $partner->id], ['type', 1]])->exists()) {
  375. $self['praise'] = true;
  376. }
  377. if (
  378. InvitationCardModel::where([
  379. ['uid', $uid],
  380. ['invite_uid', $partner->uid],
  381. ['expired_at', '>', time()]
  382. ])->exists()
  383. ) {
  384. $self['invite'] = true;
  385. }
  386. if (SuperLikeModel::where([['uid', $uid], ['partner_id', $partner->id]])->exists()) {
  387. $self['superlike'] = true;
  388. }
  389. if (PraiseModel::where([['uid', $uid], ['partner_id', $partner->id], ['type', 1]])->exists()) {
  390. $praise = true;
  391. } else {
  392. $praise = false;
  393. }
  394. $partner->setAttribute("self", $self);
  395. $partner->setAttribute("praise", $praise);
  396. return response([
  397. 'code' => 200,
  398. 'message' => 'success',
  399. 'data' => $partner
  400. ]);
  401. }
  402. /**
  403. * 点赞/无感 对象卡片
  404. * @param int $partner_id
  405. * @param Request $request
  406. * @return array
  407. * @throws AlertException
  408. */
  409. public function thumb(int $partner_id, Request $request)
  410. {
  411. $uid = Auth::auth();
  412. $thumb = $request->get('thumb', null);
  413. $ps = new PartnerService();
  414. $data = $ps->thumb($uid, $partner_id, $thumb);
  415. return response([
  416. 'code' => 200,
  417. 'message' => 'success',
  418. 'data' => $data
  419. ]);
  420. }
  421. /**
  422. * 超级喜欢某人
  423. * @param int $partner_id
  424. * @return array
  425. */
  426. public function superLike(int $partner_id)
  427. {
  428. $uid = Auth::auth();
  429. $ps = new PartnerService();
  430. $ps->superLike($uid, $partner_id);
  431. return response([
  432. 'code' => 200,
  433. 'message' => 'success'
  434. ]);
  435. }
  436. /**
  437. * 阅读超级喜欢信息
  438. * @param int $like_uid
  439. * @return array
  440. */
  441. public function readSuperlike(int $like_uid)
  442. {
  443. $uid = Auth::auth();
  444. $ns = new NoticeService();
  445. $ns->readSuperlike($uid, $like_uid);
  446. return response([
  447. 'code' => 200,
  448. 'message' => 'success'
  449. ]);
  450. }
  451. /**
  452. * 阅读相互心动信息
  453. * @param int $eachlike_id
  454. * @return array
  455. * @throws AlertException
  456. */
  457. public function readEachlike(int $eachlike_id)
  458. {
  459. $uid = Auth::auth();
  460. $ns = new NoticeService();
  461. $ns->readEachLikeList($uid, $eachlike_id);
  462. return response([
  463. 'code' => 200,
  464. 'message' => 'success'
  465. ]);
  466. }
  467. /**
  468. * 解锁喜欢我的卡片
  469. * @param Request $request
  470. * @param int $partner_id
  471. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  472. * @throws AlertException
  473. * @deprecated Partner/LockLikeMe
  474. */
  475. public function lockLikeme(Request $request, int $partner_id)
  476. {
  477. $uid = Auth::auth();
  478. $origin = $request->input('origin', 'supvip');
  479. if ($origin == 'app_sign') {
  480. $ps = new PartnerService();
  481. $ps->appLockLikeMe($uid, $partner_id);
  482. } else {
  483. $ps = new PartnerService();
  484. $ps->lockLikeme($uid, $partner_id);
  485. }
  486. return response([
  487. 'code' => 200,
  488. 'message' => 'success'
  489. ]);
  490. }
  491. /**
  492. * 通过卡片id获取卡片信息
  493. * @param int $partner_id
  494. * @return array
  495. */
  496. public function getPartnerById(int $partner_id)
  497. {
  498. try {
  499. $uid = Auth::auth();
  500. } catch (JWTException $exception) {
  501. $uid = 0;
  502. }
  503. $fs = new FeedService();
  504. $data = $fs->getPartner($partner_id, $uid);
  505. return response([
  506. 'code' => 200,
  507. 'message' => 'success',
  508. 'data' => $data->toArray()
  509. ]);
  510. }
  511. /**
  512. * 通过卡片ids获取卡片信息
  513. * @param Request $request
  514. * @return array
  515. */
  516. public function getPartnerByIds(Request $request)
  517. {
  518. try {
  519. $uid = Auth::auth();
  520. } catch (JWTException $exception) {
  521. $uid = 0;
  522. }
  523. $ids = $request->post('ids', []);
  524. $fs = new FeedService();
  525. $datas = array();
  526. foreach ($ids as $partner_id) {
  527. $datas[] = $fs->getPartner($partner_id, $uid)->toArray();
  528. }
  529. return response(array(
  530. 'code' => 200,
  531. 'message' => 'success',
  532. 'data' => $datas
  533. ));
  534. }
  535. /**
  536. * 通过uid获取卡片信息
  537. * @param int $user_id
  538. * @return array
  539. * @throws AlertException
  540. */
  541. public function getPartnerByUid(int $user_id)
  542. {
  543. $uid = Auth::auth();
  544. /** @var UserModel $user */
  545. $user = UserModel::findOrFail($user_id);
  546. if (0 == $user->partner_id) {
  547. throw new AlertException("未生成卡片", 401);
  548. }
  549. $fs = new FeedService();
  550. $data = $fs->getPartner($user->partner_id, $uid);
  551. return array(
  552. 'code' => 200,
  553. 'message' => 'success',
  554. 'data' => $data
  555. );
  556. }
  557. }