PartnerService.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. <?php
  2. namespace App\Services\User;
  3. use App\Events\AuditMedia;
  4. use App\Events\CheckPartner;
  5. use App\Events\EachLike;
  6. use App\Events\HidePartner;
  7. use App\Events\ThumbUser;
  8. use App\Events\UnThumbUser;
  9. use App\Exceptions\AlertException;
  10. use App\Jobs\AppearanceUserJob;
  11. use App\Jobs\StoreSelfPartnerJob;
  12. use App\Models\Deed\FriendsModel;
  13. use App\Models\Fpdx\ActivityModel;
  14. use App\Models\Fpdx\PairModel;
  15. use App\Models\Friends\FriendContact;
  16. use App\Models\NoticeModel;
  17. use App\Models\Partner\ThumbActionModel;
  18. use App\Models\PartnerModel;
  19. use App\Models\PraiseModel;
  20. use App\Models\User\FeedcheckLogModel;
  21. use App\Models\User\InviteConfigModel;
  22. use App\Models\User\LocklikemeModel;
  23. use App\Models\User\SuperLikeModel;
  24. use App\Models\User\UserModel;
  25. use App\Services\Deed\FriendService;
  26. use App\Services\Partner\NoticeService;
  27. use App\Services\Service;
  28. use Illuminate\Support\Facades\DB;
  29. use Illuminate\Support\Facades\Redis;
  30. class PartnerService extends Service
  31. {
  32. public function storeSelfPartner(int $uid)
  33. {
  34. try {
  35. $user = UserModel::findOrFail($uid);
  36. if ($user->partner_id > 0) {
  37. return array(
  38. 'code' => 200,
  39. 'message' => 'success',
  40. 'data' => $user,
  41. );
  42. }
  43. $partnerModel = new PartnerModel();
  44. $data = $user->toArray();
  45. $data['media_id'] = config("wechat.fpdx.public_id");
  46. $data['secret'] = bin2hex(\openssl_random_pseudo_bytes(4));
  47. $partner = $partnerModel->fill($data);
  48. $partner->created_at = time();
  49. if (!empty($partner->photo_src)) {
  50. $partner->check_photo = 0;
  51. $partner->is_commit_check = 1;
  52. }
  53. if (!empty($partner->qq) || !empty($partner->weixin)) {
  54. $partner->check_contact = 0;
  55. $partner->is_commit_check = 1;
  56. }
  57. if (!empty($partner->introduce) || !empty($partner->expect)) {
  58. $partner->intro_check = 0;
  59. $partner->is_commit_check = 1;
  60. }
  61. if (!empty($partner->voice)) {
  62. $partner->voice_check = 0;
  63. $partner->is_commit_check = 1;
  64. }
  65. if (!empty($partner->photo_1)) {
  66. $partner->photo_1_check = 0;
  67. $partner->is_commit_check = 1;
  68. }
  69. if (!empty($partner->photo_2)) {
  70. $partner->photo_2_check = 0;
  71. $partner->is_commit_check = 1;
  72. }
  73. if (!empty($partner->photo_3)) {
  74. $partner->photo_3_check = 0;
  75. $partner->is_commit_check = 1;
  76. }
  77. if (!empty($partner->photo_4)) {
  78. $partner->photo_4_check = 0;
  79. $partner->is_commit_check = 1;
  80. }
  81. if ($this->isCanSell($partner)) {
  82. $partner->is_sell = 1;
  83. $partner->upload_at = time();
  84. }
  85. if (Redis::exists("storeselfpartner:{$uid}")) {
  86. throw new AlertException("请求频繁", 104);
  87. }
  88. Redis::setex("storeselfpartner:{$uid}", 5, true);
  89. if ($partner->save()) {
  90. $user->partner_id = $partner->id;
  91. $user->save();
  92. // 系统通知
  93. $sex = 1 == $partner->sex ? "小姐姐" : "小哥哥";
  94. NoticeModel::create([
  95. 'uid' => $uid,
  96. 'title' => '交友卡片上传完成',
  97. 'content' => "你的交友卡片已经完成啦!现在可以去交友大厅邀请你心动的{$sex}啦~ ",
  98. 'type' => 5,
  99. 'type_id' => 0,
  100. 'create_at' => time(),
  101. 'update_at' => time(),
  102. 'tab_content' => '进入大厅',
  103. 'tab_url' => '/pages/index/index',
  104. ]);
  105. }
  106. Redis::del(["storeselfpartner:{$uid}"]);
  107. StoreSelfPartnerJob::dispatch($uid);
  108. } catch (\Exception $e) {
  109. if (Redis::exists("storeselfpartner:{$uid}")) {
  110. Redis::del(["storeselfpartner:{$uid}"]);
  111. }
  112. throw $e;
  113. }
  114. }
  115. public function isCanSell(PartnerModel $partner)
  116. {
  117. if (empty($partner->qq) && empty($partner->weixin)) {
  118. return false;
  119. }
  120. if (
  121. 1 == $partner->check_photo
  122. || 1 == $partner->photo_1_check
  123. || 1 == $partner->photo_2_check
  124. || 1 == $partner->photo_3_check
  125. || 1 == $partner->photo_4_check
  126. || 1 == $partner->voice_check
  127. ) {
  128. return true;
  129. }
  130. /** @var UserModel $user */
  131. $user = UserModel::findOrFail($partner->uid);
  132. if (!empty($user->introduce) || !empty($user->claim_tag)) {
  133. return true;
  134. }
  135. if (0 < $user->height || !empty($user->school) || !empty($user->home) || !empty($user->address)) {
  136. return true;
  137. }
  138. if (!empty($user->tag_2) || !empty($user->tag_3) || !empty($user->tag_3)) {
  139. return true;
  140. }
  141. return false;
  142. }
  143. /**
  144. * 审核用户个人交友卡片
  145. * @param int $partner_id
  146. * @param string $field
  147. * @param int $check
  148. * @return bool
  149. * @throws AlertException
  150. */
  151. public function check(int $partner_id, string $field, int $check)
  152. {
  153. /** @var PartnerModel $partner */
  154. $partner = PartnerModel::findOrFail($partner_id);
  155. $user = UserModel::find($partner->uid);
  156. // 卡片之前是否审核通过
  157. $before_photo_check = $partner->$field;
  158. // 语音之前是否审核
  159. $before_voice_check = $partner->voice_check;
  160. // 之前是否认证
  161. $before_auth = ($user->identity_auth && $user->wx_auth == 1) ? 1 : 0;
  162. // 记录审核失败次数
  163. $checks = ['check_photo', 'photo_1_check', 'photo_2_check', 'photo_3_check', 'photo_4_check', 'voice_check'];
  164. if (in_array($field, $checks) && $check == -1) {
  165. PartnerModel::where('id', $partner_id)->increment('dechecks', 1);
  166. }
  167. $partner->{$field} = $check;
  168. // 检查审核态
  169. if (
  170. $partner->check_photo != 0
  171. && $partner->photo_1_check != 0
  172. && $partner->photo_2_check != 0
  173. && $partner->photo_3_check != 0
  174. && $partner->photo_4_check != 0
  175. && $partner->voice_check != 0
  176. ) {
  177. $partner->is_commit_check = 0;
  178. }
  179. $partner->save();
  180. // 卡片审核事件
  181. event(new CheckPartner($partner->uid, $field, $check));
  182. // 修正上架状态
  183. // 上架=照片或语音审核通过+QQ或微信
  184. if (1 == $partner->is_sell && !$this->isCanSell($partner)) {
  185. $partner->is_sell = 0;
  186. Redis::zrem("fpdx:user:locations:sell:boy", $partner->uid);
  187. Redis::zrem("fpdx:user:locations:sell:girl", $partner->uid);
  188. $partner->save();
  189. }
  190. AppearanceUserJob::dispatch($partner->uid)->onQueue('{appearance:user}');
  191. if ($check == 1) {
  192. $this->sell($partner_id, 1);
  193. }
  194. $user = UserModel::find($partner->uid);
  195. $partner = PartnerModel::findOrFail($partner_id);
  196. // 卡片之前是否审核通过
  197. $after_photo_check = $partner->$field;
  198. // 语音之前是否审核
  199. $after_voice_check = $partner->voice_check;
  200. // 之前是否认证
  201. $after_auth = ($user->identity_auth && $user->wx_auth == 1) ? 1 : 0;
  202. // 卡片审核通知
  203. $noticeService = new NoticeService();
  204. // 审核卡片
  205. if (in_array($field, ['check_photo', 'photo_1_check', 'photo_2_check', 'photo_3_check', 'photo_4_check'])) {
  206. if ($before_photo_check != 1 && $after_photo_check == 1) {
  207. $noticeService->checkPartnerSuccess($partner->uid);
  208. } elseif ($before_photo_check != -1 && $after_photo_check == -1) {
  209. $noticeService->checkPartnerFail($partner->uid);
  210. }
  211. }
  212. // 审核语音
  213. if (in_array($field, ['voice_check'])) {
  214. if ($before_voice_check != 1 && $after_voice_check == 1) {
  215. $noticeService->checkVoiceSuccess($partner->uid);
  216. } elseif ($before_voice_check != -1 && $after_voice_check == -1) {
  217. $noticeService->checkVoiceFail($partner->uid);
  218. }
  219. }
  220. // 发送认证通知
  221. if ($before_auth == 0 && $after_auth == 1) {
  222. $noticeService->authenticationSuccess($partner->uid);
  223. }
  224. return true;
  225. }
  226. /**
  227. * 上下架卡片
  228. * @param int $partner_id
  229. * @param int $is_sell
  230. * @return bool
  231. * @throws AlertException
  232. */
  233. public function sell(int $partner_id, int $is_sell)
  234. {
  235. /** @var PartnerModel $partner */
  236. $partner = PartnerModel::findOrFail($partner_id);
  237. if (1 == $is_sell && ($partner->black_at > time() || 0 != $partner->is_sell)) {
  238. if ($partner->black_at > time()) {
  239. throw new AlertException("卡片被封禁", 102);
  240. }
  241. if (0 != $partner->is_sell) {
  242. throw new AlertException("卡片被封禁,请修改信息", 102);
  243. }
  244. }
  245. if (1 == $is_sell && $partner->is_sell != 1) {
  246. if ($this->isCanSell($partner)) {
  247. $partner->is_sell = 1;
  248. $partner->upload_at = time();
  249. } else {
  250. throw new AlertException("不满足上架条件「没有练习方式」", 103);
  251. }
  252. }
  253. if (1 == $is_sell) {
  254. $partner->auto_sell = 1;
  255. // 加入redis
  256. /** @var UserModel $user */
  257. $user = UserModel::findOrFail($partner->uid);
  258. if ($partner->sex == 1) {
  259. Redis::geoAdd("fpdx:user:locations:sell:boy", $user->lng, $user->lat, $partner->uid);
  260. } else {
  261. Redis::geoAdd("fpdx:user:locations:sell:girl", $user->lng, $user->lat, $partner->uid);
  262. }
  263. } else {
  264. Redis::zrem("fpdx:user:locations:sell:boy", $partner->uid);
  265. Redis::zrem("fpdx:user:locations:sell:girl", $partner->uid);
  266. }
  267. $partner->is_sell = $is_sell;
  268. $partner->save();
  269. AppearanceUserJob::dispatch($partner->uid)->onQueue('{appearance:user}');
  270. try {
  271. if (1 == $is_sell) {
  272. $ts = new TaskService();
  273. $ts->task($partner->uid, 3);
  274. $noticeService = new NoticeService();
  275. $noticeService->partnerSell($partner->uid);
  276. } else {
  277. event(new HidePartner($partner->id));
  278. }
  279. } catch (\Exception $exception) {
  280. app('sentry')->captureException($exception);
  281. }
  282. return true;
  283. }
  284. /**
  285. * 更新卡片信息
  286. * @param int $partner_id
  287. * @param array $data
  288. * @throws AlertException
  289. */
  290. public function updatePartner(int $partner_id, array $data)
  291. {
  292. /** @var PartnerModel $partner */
  293. $partner = PartnerModel::findOrFail($partner_id);
  294. // 是否上架
  295. $sell = false;
  296. if ((array_key_exists('qq', $data) || array_key_exists('weixin', $data))) {
  297. if ($partner->is_sell == 0 && empty($partner->qq) && empty($partner->weixin)) {
  298. $sell = true;
  299. }
  300. }
  301. if (array_key_exists('voice', $data) && !empty($data['voice'])) {
  302. $data['voice_check'] = 0;
  303. $data['is_commit_check'] = 1;
  304. }
  305. if (array_key_exists('voice', $data) && empty($data['voice'])) {
  306. $data['voice_check'] = -1;
  307. }
  308. if (array_key_exists('photo_src', $data) && !empty($data['photo_src'])) {
  309. $data['check_photo'] = 0;
  310. $data['is_commit_check'] = 1;
  311. }
  312. if (array_key_exists('photo_src', $data) && empty($data['photo_src'])) {
  313. $data['check_photo'] = -1;
  314. }
  315. if (array_key_exists('photo_1', $data) && !empty($data['photo_1'])) {
  316. $data['photo_1_check'] = 0;
  317. $data['is_commit_check'] = 1;
  318. }
  319. if (array_key_exists('photo_1', $data) && empty($data['photo_1'])) {
  320. $data['photo_1_check'] = -1;
  321. }
  322. if (array_key_exists('photo_2', $data) && !empty($data['photo_2'])) {
  323. $data['photo_2_check'] = 0;
  324. $data['is_commit_check'] = 1;
  325. }
  326. if (array_key_exists('photo_2', $data) && empty($data['photo_2'])) {
  327. $data['photo_2_check'] = -1;
  328. }
  329. if (array_key_exists('photo_3', $data) && !empty($data['photo_3'])) {
  330. $data['photo_3_check'] = 0;
  331. $data['is_commit_check'] = 1;
  332. }
  333. if (array_key_exists('photo_3', $data) && empty($data['photo_3'])) {
  334. $data['photo_3_check'] = -1;
  335. }
  336. if (array_key_exists('photo_4', $data) && !empty($data['photo_4'])) {
  337. $data['photo_4_check'] = 0;
  338. $data['is_commit_check'] = 1;
  339. }
  340. if (array_key_exists('photo_4', $data) && empty($data['photo_4'])) {
  341. $data['photo_4_check'] = -1;
  342. }
  343. // 是否属于72小时活动卡片
  344. if (isset($data['is_commit_check']) && 1 == $data['is_commit_check']) {
  345. $ings = ActivityModel::getActivitys();
  346. $exists = PairModel::where([
  347. 'uid' => $partner->uid,
  348. 'stage_id' => $ings['ing'] ?: 0,
  349. ])->exists();
  350. if ($exists) {
  351. $data['is_fpdx'] = 1;
  352. } else {
  353. $data['is_fpdx'] = 0;
  354. }
  355. }
  356. try {
  357. if (
  358. 5 <= $partner->feedcheck_cnt && !empty(array_intersect(
  359. array_keys($data),
  360. ['photo_src', 'photo_1', 'photo_2', 'photo_3', 'photo_4', 'voice']
  361. ))
  362. ) {
  363. $data['feedcheck_cnt'] = 0;
  364. $data['is_push_feed'] = 1;
  365. FeedcheckLogModel::where([
  366. ['partner_id', $partner->id],
  367. ['expire_at', 0],
  368. ])->update(['expire_at' => time()]);
  369. }
  370. } catch (\Exception $exception) {
  371. }
  372. $updatekeys = array(
  373. 'photo_src',
  374. 'photo_1',
  375. 'photo_2',
  376. 'photo_3',
  377. 'photo_4',
  378. 'voice',
  379. 'sex',
  380. 'age',
  381. 'weixin',
  382. 'qq',
  383. 'school',
  384. 'height',
  385. 'home',
  386. 'address',
  387. 'introduce',
  388. );
  389. if (!empty(array_intersect(array_keys($data), $updatekeys))) {
  390. $data['update_at'] = time();
  391. }
  392. $partner->fill($data);
  393. $this->auditDiscribute($partner, $data);
  394. // 修正首页feed流缓存
  395. if (isset($data['sex']) && 1 == $partner->is_sell) {
  396. /** @var UserModel $user */
  397. $user = UserModel::findOrFail($partner->uid);
  398. if ($partner->sex == 1) {
  399. Redis::zrem("fpdx:user:locations:sell:girl", $partner->uid);
  400. Redis::geoAdd("fpdx:user:locations:sell:boy", $user->lng, $user->lat, $partner->uid);
  401. } else {
  402. Redis::zrem("fpdx:user:locations:sell:boy", $partner->uid);
  403. Redis::geoAdd("fpdx:user:locations:sell:girl", $user->lng, $user->lat, $partner->uid);
  404. }
  405. }
  406. // 修正上架状态
  407. // 上架=QQ或微信
  408. if (1 == $partner->is_sell && false == $this->isCanSell($partner)) {
  409. $partner->is_sell = 0;
  410. Redis::zrem("fpdx:user:locations:sell:boy", $partner->uid);
  411. Redis::zrem("fpdx:user:locations:sell:girl", $partner->uid);
  412. }
  413. // 修正首页feed流缓存
  414. if (-1 == $partner->is_sell && 1 == $partner->is_commit_check) {
  415. $partner->is_sell = 0;
  416. Redis::zrem("fpdx:user:locations:sell:boy", $partner->uid);
  417. Redis::zrem("fpdx:user:locations:sell:girl", $partner->uid);
  418. }
  419. // 修正审核状态
  420. if (
  421. 1 == $partner->is_commit_check
  422. && 0 != $partner->check_photo
  423. && 0 != $partner->photo_1_check
  424. && 0 != $partner->photo_2_check
  425. && 0 != $partner->photo_3_check
  426. && 0 != $partner->photo_4_check
  427. && 0 != $partner->voice_check
  428. ) {
  429. $partner->is_commit_check = 0;
  430. }
  431. $partner->save();
  432. if (!empty(array_intersect(array_keys($data), ['sex', 'school', 'is_sell']))) {
  433. AppearanceUserJob::dispatch($partner->uid)->onQueue('{appearance:user}');
  434. }
  435. if (!empty($partner->voice)) {
  436. $ts = new TaskService();
  437. $ts->task($partner->uid, 4);
  438. }
  439. try {
  440. if ($sell) {
  441. $this->sell($partner_id, 1);
  442. }
  443. } catch (\Exception $exception) {
  444. }
  445. }
  446. /**
  447. * 提交素材审核事件
  448. * @param $partner
  449. * @param $data
  450. * @throws AlertException
  451. */
  452. private function auditDiscribute($partner, $data)
  453. {
  454. // 照片
  455. if (array_key_exists('photo_src', $data) && $data['photo_src']) {
  456. event(new AuditMedia(
  457. $partner->uid,
  458. 'image',
  459. 'https://oss.pocketuniversity.cn' . $data['photo_src'],
  460. 'photo_src',
  461. "id={$partner->id}"
  462. ));
  463. }
  464. if (array_key_exists('photo_1', $data) && $data['photo_1']) {
  465. event(new AuditMedia(
  466. $partner->uid,
  467. 'image',
  468. 'https://oss.pocketuniversity.cn' . $data['photo_1'],
  469. 'photo_1',
  470. "id={$partner->id}"
  471. ));
  472. }
  473. if (array_key_exists('photo_2', $data) && $data['photo_2']) {
  474. event(new AuditMedia(
  475. $partner->uid,
  476. 'image',
  477. 'https://oss.pocketuniversity.cn' . $data['photo_2'],
  478. 'photo_2',
  479. "id={$partner->id}"
  480. ));
  481. }
  482. if (array_key_exists('photo_3', $data) && $data['photo_3']) {
  483. event(new AuditMedia(
  484. $partner->uid,
  485. 'image',
  486. 'https://oss.pocketuniversity.cn' . $data['photo_3'],
  487. 'photo_3',
  488. "id={$partner->id}"
  489. ));
  490. }
  491. if (array_key_exists('photo_4', $data) && $data['photo_4']) {
  492. event(new AuditMedia(
  493. $partner->uid,
  494. 'image',
  495. 'https://oss.pocketuniversity.cn' . $data['photo_4'],
  496. 'photo_4',
  497. "id={$partner->id}"
  498. ));
  499. }
  500. // 语音
  501. if (array_key_exists('voice', $data) && $data['voice']) {
  502. event(new AuditMedia($partner->uid, 'voice', $data['voice'], 'voice', "id={$partner->id}"));
  503. }
  504. }
  505. /**
  506. * 通过uid获取用户卡片信息
  507. * @param int $uid
  508. * @return PartnerModel
  509. */
  510. public function getUserSelf(int $uid)
  511. {
  512. /** @var PartnerModel $partner */
  513. $partner = PartnerModel::where('uid', $uid)->firstOrFail([
  514. 'id',
  515. 'uid',
  516. 'sex',
  517. 'age',
  518. 'star',
  519. 'height',
  520. 'school',
  521. 'weixin',
  522. 'qq',
  523. 'address',
  524. 'home',
  525. 'check_photo',
  526. 'photo_src',
  527. 'photo_1_check',
  528. 'photo_1',
  529. 'photo_2_check',
  530. 'photo_2',
  531. 'photo_3_check',
  532. 'photo_3',
  533. 'photo_4_check',
  534. 'photo_4',
  535. 'voice_check',
  536. 'voice',
  537. 'praises',
  538. 'introduce',
  539. 'expect',
  540. 'education',
  541. 'black_at',
  542. ]);
  543. $partner->black_at = $partner->black_at < time() ? 0 : $partner->black_at;
  544. $partner->setHidden([
  545. "check_photo",
  546. "photo_1_check",
  547. "photo_2_check",
  548. "photo_3_check",
  549. "photo_4_check",
  550. "voice_check",
  551. "check_contact",
  552. ]);
  553. if (1 != $partner->check_photo) {
  554. $partner->addHidden("photo_src");
  555. }
  556. if (1 != $partner->photo_1_check) {
  557. $partner->addHidden("photo_1");
  558. }
  559. if (1 != $partner->photo_2_check) {
  560. $partner->addHidden("photo_2");
  561. }
  562. if (1 != $partner->photo_3_check) {
  563. $partner->addHidden("photo_3");
  564. }
  565. if (1 != $partner->photo_4_check) {
  566. $partner->addHidden("photo_4");
  567. }
  568. if (1 != $partner->voice_check) {
  569. $partner->addHidden("voice");
  570. }
  571. try {
  572. /** @var UserModel $user */
  573. $user = UserModel::findOrfail($partner->uid, [
  574. 'uid',
  575. 'headimgurl',
  576. 'nickname',
  577. 'age',
  578. 'star',
  579. 'pair_voice',
  580. 'work_state',
  581. 'qq',
  582. 'weixin',
  583. 'be_vip_at',
  584. 'supvip_endat',
  585. 'introduce',
  586. 'expect',
  587. 'height',
  588. 'sex',
  589. 'school',
  590. 'address',
  591. 'home',
  592. 'logoff_at',
  593. 'tag_1',
  594. 'tag_2',
  595. 'tag_3',
  596. 'tag_4',
  597. 'claim_tag',
  598. 'identity_auth',
  599. 'wx_auth',
  600. ]);
  601. /** @var InviteConfigModel $userInviteConfig */
  602. $userInviteConfig = InviteConfigModel::find($partner->uid);
  603. if ($userInviteConfig) {
  604. $user->setAttribute("task_paint", $userInviteConfig->task_paint);
  605. $user->setAttribute("task_photo", $userInviteConfig->task_photo);
  606. $user->setAttribute("task_question", $userInviteConfig->task_question);
  607. $user->setAttribute("task_sing", $userInviteConfig->task_sing);
  608. $user->setAttribute("task_question_data", $userInviteConfig->task_question_data ? true : false);
  609. $user->setAttribute("task_sing_data", $userInviteConfig->task_sing_data ? true : false);
  610. }
  611. $partner->headimgurl = $user->headimgurl;
  612. $partner->nickname = $user->nickname;
  613. } catch (\Exception $e) {
  614. $user = null;
  615. $partner->headimgurl = null;
  616. $partner->nickname = null;
  617. }
  618. $partner->setAttribute("user", $user);
  619. return $partner;
  620. }
  621. /**
  622. * 获取卡片信息
  623. * @param int $partner_id
  624. * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
  625. */
  626. public function get(int $partner_id)
  627. {
  628. /** @var PartnerModel $partner */
  629. $partner = PartnerModel::findOrFail($partner_id);
  630. if (1 != $partner->check_photo) {
  631. $partner->addHidden("photo_src");
  632. }
  633. if (1 != $partner->photo_1_check) {
  634. $partner->addHidden("photo_1");
  635. }
  636. if (1 != $partner->photo_2_check) {
  637. $partner->addHidden("photo_2");
  638. }
  639. if (1 != $partner->photo_3_check) {
  640. $partner->addHidden("photo_3");
  641. }
  642. if (1 != $partner->photo_4_check) {
  643. $partner->addHidden("photo_4");
  644. }
  645. if (1 != $partner->voice_check) {
  646. $partner->addHidden("voice");
  647. }
  648. $partner->setHidden([
  649. "check_photo",
  650. "photo_1_check",
  651. "photo_2_check",
  652. "photo_3_check",
  653. "photo_4_check",
  654. "voice_check",
  655. "check_contact",
  656. ]);
  657. return $partner;
  658. }
  659. /**
  660. * 选择封面
  661. * @param int $partner_id
  662. * @param $field
  663. * @throws AlertException
  664. */
  665. public function selectCover(int $partner_id, $field)
  666. {
  667. if (!in_array($field, ['photo_1', 'photo_2', 'photo_3', 'photo_4'])) {
  668. throw new AlertException("未知的输入", 101);
  669. }
  670. $partner = PartnerModel::findOrFail($partner_id);
  671. $check_name = "{$field}_check";
  672. $tmp = $partner->photo_src;
  673. $tmp_check = $partner->check_photo;
  674. $partner->photo_src = $partner->{$field};
  675. $partner->check_photo = $partner->{$check_name};
  676. $partner->{$field} = $tmp;
  677. $partner->{$check_name} = $tmp_check;
  678. $partner->save();
  679. }
  680. /**
  681. * 点赞/无感 对象卡片
  682. * @param int $uid
  683. * @param int $partner_id
  684. * @param null $thumb 是否点赞
  685. * @return array
  686. * @throws AlertException
  687. */
  688. public function thumb(int $uid, int $partner_id, $thumb = null)
  689. {
  690. $res = array('eachlike' => false);
  691. $user = UserModel::findOrFail($uid);
  692. if ($user->partner_id == $partner_id) {
  693. // 操作的是自己的卡片
  694. return $res;
  695. }
  696. if (0 == $user->partner_id && !in_array($partner_id, [1, 2])) {
  697. // 操作用户是游客=>记录游客动作
  698. ThumbActionModel::createdOrDeleted($uid, $partner_id, $thumb);
  699. return $res;
  700. }
  701. $res = array('eachlike' => false);
  702. /** @var PartnerModel $partner */
  703. $partner = PartnerModel::findOrFail($partner_id);
  704. if (
  705. $praise = PraiseModel::where(array(['partner_id', $partner_id], ['uid', $uid]))->first()
  706. ) {
  707. if ((int)$praise->type === (int)$thumb) {
  708. // 重复操作
  709. return $res;
  710. }
  711. $praise->type = is_null($thumb) ? abs($praise->type - 1) : $thumb;
  712. if (0 == $praise->type && in_array($partner_id, [1, 2])) {
  713. throw new AlertException("这是你的专属小象,暂不支持取消心动哟~", 202);
  714. }
  715. if ($praise->type == 1) {
  716. $praiseModel = new PraiseModel();
  717. if ($praiseModel->isLike($partner->uid, $user->partner_id)) {
  718. $partnerUser = UserModel::where('partner_id', $partner->id)->first([
  719. 'uid',
  720. 'nickname',
  721. 'headimgurl',
  722. 'sex',
  723. 'weixin',
  724. 'qq',
  725. 'identity_auth',
  726. 'wx_auth',
  727. 'be_vip_at',
  728. 'supvip_endat',
  729. ]);
  730. $res['eachlike'] = true;
  731. $res['user'] = $partnerUser;
  732. }
  733. event(new ThumbUser($user, $partner));
  734. } else {
  735. event(new UnThumbUser($user, $partner));
  736. }
  737. } else {
  738. if (is_null($thumb) || 1 == $thumb) {
  739. $praiseModel = new PraiseModel();
  740. if ($praiseModel->isLike($partner->uid, $user->partner_id)) {
  741. $partnerUser = UserModel::where('partner_id', $partner->id)->first([
  742. 'uid',
  743. 'nickname',
  744. 'headimgurl',
  745. 'sex',
  746. 'weixin',
  747. 'qq',
  748. 'identity_auth',
  749. 'wx_auth',
  750. 'be_vip_at',
  751. 'supvip_endat',
  752. ]);
  753. $res['eachlike'] = true;
  754. $res['user'] = $partnerUser;
  755. }
  756. event(new ThumbUser($user, $partner));
  757. } else {
  758. return $res;
  759. }
  760. }
  761. return $res;
  762. }
  763. /**
  764. * 超级喜欢某人
  765. * @param int $uid
  766. * @param int $partner_id
  767. * @return bool
  768. */
  769. public function superLike(int $uid, int $partner_id)
  770. {
  771. $user = UserModel::findOrFail($uid);
  772. if ($user->supvip_endat < time()) {
  773. return false;
  774. }
  775. if (SuperLikeModel::where(array(['uid', $uid], ['partner_id', $partner_id]))->exists()) {
  776. return true;
  777. } else {
  778. PraiseModel::where(array(['uid', $uid], ['partner_id', $partner_id]))->update(['lock' => 100]);
  779. SuperLikeModel::create([
  780. 'created_at' => time(),
  781. 'updated_at' => time(),
  782. 'uid' => $uid,
  783. 'partner_id' => $partner_id,
  784. ]);
  785. return true;
  786. }
  787. }
  788. /**
  789. * 解锁喜欢我的卡片
  790. * @param int $uid
  791. * @param int $partner_id
  792. * @return bool
  793. * @throws AlertException
  794. */
  795. public function lockLikeme(int $uid, int $partner_id)
  796. {
  797. $partner = PartnerModel::findOrFail($partner_id);
  798. /** @var UserModel $user */
  799. $user = UserModel::findOrFail($uid);
  800. if ($user->supvip_endat < time()) {
  801. throw new AlertException("你不是超级会员");
  802. }
  803. if ($user->partner_id == 0) {
  804. throw new AlertException("你还没有生成交友卡片", 100);
  805. }
  806. $praise = PraiseModel::where([['uid', $partner->uid], ['partner_id', $user->partner_id]])->firstOrFail();
  807. if ($praise->lock >= 100) {
  808. throw new AlertException("卡片已经被解锁了", 102);
  809. }
  810. if (
  811. LocklikemeModel::where(array(
  812. ['uid', $uid],
  813. ['origin', 'supvip'],
  814. ['created_at', '>', mktime(0, 0, 0)],
  815. ))->count() >= 5
  816. ) {
  817. throw new AlertException("没有解锁次数了", 101);
  818. }
  819. DB::beginTransaction();
  820. try {
  821. $praise->lock = 100;
  822. $praise->save();
  823. LocklikemeModel::create([
  824. 'uid' => $uid,
  825. 'partner_id' => $partner_id,
  826. 'origin' => 'supvip',
  827. ]);
  828. DB::commit();
  829. return true;
  830. } catch (\Exception $exception) {
  831. DB::rollBack();
  832. throw $exception;
  833. }
  834. }
  835. /**
  836. * app解锁喜欢我的卡片
  837. * @param int $uid
  838. * @param int $partner_id
  839. * @return bool
  840. * @throws AlertException
  841. */
  842. public function appLockLikeMe(int $uid, int $partner_id)
  843. {
  844. $partner = PartnerModel::findOrFail($partner_id);
  845. $user = UserModel::findOrFail($uid);
  846. if ($user->partner_id == 0) {
  847. throw new AlertException("你还没有生成交友卡片", 100);
  848. }
  849. $praise = PraiseModel::where([['uid', $partner->uid], ['partner_id', $user->partner_id]])->firstOrFail();
  850. if ($praise->lock >= 100) {
  851. throw new AlertException("卡片已经被解锁了", 102);
  852. }
  853. if ($user->app_like_unlock_count == 0) {
  854. throw new AlertException("没有解锁次数了", 101);
  855. }
  856. DB::beginTransaction();
  857. try {
  858. $praise->lock = 100;
  859. $praise->save();
  860. $user->decrement('app_like_unlock_count', 1);
  861. LocklikemeModel::create([
  862. 'uid' => $uid,
  863. 'partner_id' => $partner_id,
  864. 'origin' => 'app_sign',
  865. ]);
  866. DB::commit();
  867. return true;
  868. } catch (\Exception $exception) {
  869. DB::rollBack();
  870. throw $exception;
  871. }
  872. }
  873. /**
  874. * 清理过期超级喜欢
  875. */
  876. public function clearSuperlike()
  877. {
  878. $supers = SuperLikeModel::whereBetween('created_at', [time() - 86400 * 7, time() - 86400 * 3])->get();
  879. foreach ($supers as $super) {
  880. Redis::zrem("fpdx:feed:superlike:{$super->uid}", $super->partner_id);
  881. }
  882. }
  883. }