UserService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. namespace App\Http\Services\V1_1;
  3. use App\Generated\V1_1\DTOs\UidImAccountDTO;
  4. use App\Generated\V1_1\Messages\User\UidTranslationImAccountMessage;
  5. use App\Generated\V1_1\DTOs\ProfileUserDTO;
  6. use App\Generated\V1_1\Messages\User\RefreshMessage;
  7. use App\Generated\V1_1\DTOs\BannerDTO;
  8. use App\Generated\V1_1\DTOs\BlackDTO;
  9. use App\Generated\V1_1\DTOs\FpdxProfileDTO;
  10. use App\Generated\V1_1\DTOs\SupvipDTO;
  11. use App\Generated\V1_1\Messages\User\ProfileMessage;
  12. use App\Jobs\MockThumbJob;
  13. use App\Managers\UserManager;
  14. use App\Models\BlackListModel;
  15. use App\Models\Interlocution\AnswerModel;
  16. use App\Models\Interlocution\QuestionModel;
  17. use App\Models\Log\AppSignModel;
  18. use App\Models\PartnerModel;
  19. use App\Models\TagModel;
  20. use App\Models\User\InviteConfigModel;
  21. use App\Models\User\LocklikemeModel;
  22. use App\Models\User\Openid;
  23. use App\Models\User\UserModel;
  24. use App\Models\User\UserSysTagModel;
  25. use App\Utils\ImUtil;
  26. use Config;
  27. use App\Http\Controllers\Core\Auth;
  28. use Illuminate\Support\Carbon;
  29. use Illuminate\Support\Facades\Redis;
  30. use App\Generated\V1_1\Messages\User\ToggleTagMessage;
  31. use App\Generated\V1_1\Messages\User\UpdateProfileMessage;
  32. use App\Exceptions\AlertException;
  33. use App\Jobs\StoreSelfPartnerJob;
  34. use App\Models\NoticeModel;
  35. use App\Services\Goodnight\UserService as GoodNightUserService;
  36. use App\Services\User\ProfileService;
  37. use Exception;
  38. use App\Generated\V1_1\Messages\User\StoreSelfPartnerMessage;
  39. use App\Generated\V1_1\Messages\User\GetFpdxProfileMessage;
  40. use App\Generated\V1_1\Messages\User\CreateAnswerMessage;
  41. use App\Generated\V1_1\Messages\User\GetQuestionInfoMessage;
  42. use App\Generated\V1_1\Messages\User\GetQuestionAnswerListMessage;
  43. use App\Generated\V1_1\Messages\User\GetQuestionListMessage;
  44. use App\Generated\V1_1\Messages\User\DeletePhotosMessage;
  45. use App\Generated\V1_1\Messages\User\SelectCoverMessage;
  46. class UserService
  47. {
  48. protected $userManager;
  49. public function __construct(UserManager $userManager)
  50. {
  51. $this->userManager = $userManager;
  52. }
  53. public function refresh(RefreshMessage $message)
  54. {
  55. $uid = Auth::auth();
  56. $imUtil = new ImUtil();
  57. $user = UserModel::findOrFail($uid);
  58. if ($user->im_account) {
  59. $imSig = $imUtil->registerUser($user->im_account, $user->nickname, $user->headimgurl, $user->sex, 0);
  60. $user->im_sig = $imSig;
  61. } else {
  62. $user->im_account = md5(config('app.env') . $user->uid);
  63. $imSig = $imUtil->registerUser($user->im_account, $user->nickname, $user->headimgurl, $user->sex, 0);
  64. $user->im_sig = $imSig;
  65. }
  66. $user->save();
  67. $message->setResponse($user->uid, $user->im_account, $user->im_sig);
  68. }
  69. public function profile(ProfileMessage $message)
  70. {
  71. $model = $message->getModel() ?: 'fpdx';
  72. $uid = Auth::auth();
  73. $user = $this->userManager->profile($uid, $model);
  74. $message->setResponse(ProfileUserDTO::initFromEloquent($user));
  75. }
  76. public function getFpdxProfile(GetFpdxProfileMessage $message)
  77. {
  78. $uid = Auth::auth();
  79. $userModel = new UserModel();
  80. $userModel->initUser($uid);
  81. $user = UserModel::findOrFail($uid);
  82. /** @var UserModel $user */
  83. $user->login_at = time();
  84. $platform = Config::get('platform');
  85. if (in_array($platform, ['ios', 'android'])) {
  86. $user->login_app_platform = $platform;
  87. }
  88. $user->save();
  89. $inviteConf = InviteConfigModel::find($uid);
  90. $user->task_photo = $inviteConf->task_photo ?? false;
  91. $user->task_question = $inviteConf->task_question ?? false;
  92. $user->task_sing = $inviteConf->task_sing ?? false;
  93. $user->task_question_data = (bool) $inviteConf->task_question_data;
  94. // 普通会员
  95. // 超级VIP相关
  96. $supvip = ['supvip_endat' => 0];
  97. if ($user->supvip_endat > time()) {
  98. // 累计开通supvip天数
  99. $systag = UserSysTagModel::where('uid', $uid)->first();
  100. $supvip['supvip_days'] = $systag ? $systag->supvip_days : 0;
  101. // 每天心动我的解锁次数
  102. $supvip['locklikeme_cnt'] = 5 - LocklikemeModel::where([['uid', $user->uid], ['created_at', '>', mktime(0, 0, 0)], ['origin', 'supvip']])->count();
  103. // 到期时间
  104. $supvip['supvip_endat'] = $user->supvip_endat;
  105. }
  106. // 标签处理
  107. $tagModel = new TagModel();
  108. $tags = $tagModel->getAllTagByUser($user);
  109. // 是否关注fpdx
  110. $bindFpdxfwh = (int) Openid::where([['uid', $uid], ['subscribe', 1], ['public_id', 'gh_b598cb7474d8']])->exists();
  111. // 卡片和解锁卡统计
  112. /** @var PartnerModel $partner */
  113. $partner = PartnerModel::findOrFail($user->partner_id, ['praises', 'charm']);
  114. $banner = [
  115. 'is_praises' => $partner->praises ?? 0,
  116. 'charm' => $partner->charm ?? 0,
  117. 'count' => PartnerModel::where([['uid', $uid], ['is_hide', 0]])->count(),
  118. // 黑名单
  119. 'black' => BlackListModel::where('uid', $uid)->where('end_at', '>', time())->whereIn('type', ['msy', 'all'])->first()->end_at ?? 0,
  120. ];
  121. $lockCard = 0;
  122. // 黑名单
  123. $black = $user->black();
  124. // App签到
  125. $appSignAt = AppSignModel::where('uid', $uid)->where('date', date('Y-m-d'))->first() ? mktime(0, 0, 0) + 86400 : mktime(0, 0, 0);
  126. MockThumbJob::dispatch($uid)->delay(Carbon::now()->addHours(1));
  127. $course = Redis::getbit("app:course", $uid);
  128. $message->setResponse(FpdxProfileDTO::initFromEloquent($user), SupvipDTO::initFromEloquent($supvip), $bindFpdxfwh, $lockCard, BannerDTO::initFromEloquent($banner), BlackDTO::initFromEloquent($black), $appSignAt, $course, $tags);
  129. }
  130. public function toggleTag(ToggleTagMessage $message)
  131. {
  132. $tagId = $message->getTagId();
  133. $group = $message->getGroup();
  134. $uid = Auth::auth();
  135. $ps = new ProfileService();
  136. $ps->toggleTag($uid, $tagId, $group);
  137. }
  138. /**
  139. * @param UpdateProfileMessage $message
  140. * @throws AlertException
  141. * @throws \AlicFeng\IdentityCard\Exception\BirthdayException
  142. */
  143. public function updateProfile(UpdateProfileMessage $message)
  144. {
  145. $request = $message->getRequest();
  146. $uid = Auth::auth();
  147. /** @var UserModel $user */
  148. $user = UserModel::findOrFail($uid);
  149. // 更新基础数据
  150. $basedata = $request->only(['nickname', 'headimgurl', 'sex']);
  151. if (!empty($basedata)) {
  152. $pService = new ProfileService();
  153. $pService->updateUser($user, $basedata);
  154. }
  155. $app = $request->get('model') ?? "fpdx";
  156. switch ($app) {
  157. case 'goodnight':
  158. # 互道晚安
  159. $data = $request->only(['cover', 'like_sex', 'like_tone_1', 'like_tone_2', 'like_tone_3', 'voice']);
  160. if (!empty($data)) {
  161. $guService = new GoodNightUserService();
  162. $guService->updateUser($user, $data);
  163. }
  164. break;
  165. case 'fpdx':
  166. # 72小时恋爱体验
  167. default:
  168. # 基础信息
  169. $pService = new ProfileService();
  170. $data = $request->except(array('gold_flower', 'red_flower', 'task_type', 'wx_auth', 'identity_auth', 'wxkf', 'login_at', 'created_at', 'phone', 'task_photo', 'task_question', 'task_sing'));
  171. $pService->updateUser($user, $data);
  172. break;
  173. }
  174. $message->setResponse($this->userManager->profile($uid, $app));
  175. }
  176. /**
  177. * @param StoreSelfPartnerMessage $message
  178. * @throws Exception
  179. */
  180. public function storeSelfPartner(StoreSelfPartnerMessage $message)
  181. {
  182. $uid = Auth::auth();
  183. try {
  184. $user = UserModel::findOrFail($uid);
  185. if ($user->partner_id > 0) {
  186. $message->setResponse($user);
  187. }
  188. $partnerModel = new PartnerModel();
  189. $data = $user->toArray();
  190. $data['media_id'] = $message->getMediaId() ?: '';
  191. $data['secret'] = bin2hex(openssl_random_pseudo_bytes(4));
  192. $partner = $partnerModel->fill($data);
  193. $partner->created_at = time();
  194. if (!empty($partner->qq) || !empty($partner->weixin)) {
  195. $partner->is_sell = 1;
  196. }
  197. if (!empty($partner->photo_src)) {
  198. $partner->check_photo = 0;
  199. $partner->is_commit_check = 1;
  200. }
  201. if (!empty($partner->voice)) {
  202. $partner->voice_check = 0;
  203. $partner->is_commit_check = 1;
  204. }
  205. if (!empty($partner->photo_1)) {
  206. $partner->photo_1_check = 0;
  207. $partner->is_commit_check = 1;
  208. }
  209. if (!empty($partner->photo_2)) {
  210. $partner->photo_2_check = 0;
  211. $partner->is_commit_check = 1;
  212. }
  213. if (!empty($partner->photo_3)) {
  214. $partner->photo_3_check = 0;
  215. $partner->is_commit_check = 1;
  216. }
  217. if (!empty($partner->photo_4)) {
  218. $partner->photo_4_check = 0;
  219. $partner->is_commit_check = 1;
  220. }
  221. if (Redis::exists("storeselfpartner:{$uid}")) {
  222. throw new AlertException("请求频繁", 104);
  223. }
  224. Redis::setex("storeselfpartner:{$uid}", 5, true);
  225. if ($partner->save()) {
  226. $user->partner_id = $partner->id;
  227. $user->save();
  228. // 系统通知
  229. $sex = 1 == $partner->sex ? "小姐姐" : "小哥哥";
  230. NoticeModel::create(['uid' => $uid, 'title' => '交友卡片上传完成', 'content' => "你的交友卡片已经完成啦!现在可以去交友大厅邀请你心动的{$sex}啦~ ", 'type' => 5, 'type_id' => 0, 'create_at' => time(), 'update_at' => time(), 'tab_content' => '进入大厅', 'tab_url' => '/pages/index/index']);
  231. }
  232. Redis::del(["storeselfpartner:{$uid}"]);
  233. StoreSelfPartnerJob::dispatch($uid);
  234. } catch (Exception $e) {
  235. if (Redis::exists("storeselfpartner:{$uid}")) {
  236. Redis::del(["storeselfpartner:{$uid}"]);
  237. }
  238. throw $e;
  239. }
  240. $message->setResponse($user);
  241. }
  242. /**
  243. * 获取所有问题列表
  244. * @param GetQuestionListMessage $message
  245. */
  246. public function getQuestionList(GetQuestionListMessage $message)
  247. {
  248. $questions = QuestionModel::get();
  249. $message->setResponse($questions);
  250. }
  251. /**
  252. * 获取问题详情
  253. * @param GetQuestionInfoMessage $message
  254. */
  255. public function getQuestionInfo(GetQuestionInfoMessage $message)
  256. {
  257. $uid = Auth::auth();
  258. $questionId = $message->getQuestionId();
  259. $question = QuestionModel::find($questionId);
  260. $answer = AnswerModel::where('uid', $uid)->where('question_id', $questionId)->first();
  261. $question->answer = $answer;
  262. $message->setResponse($question);
  263. }
  264. /**
  265. * 获取我回答的问题
  266. * @param GetQuestionAnswerListMessage $message
  267. */
  268. public function getQuestionAnswerList(GetQuestionAnswerListMessage $message)
  269. {
  270. if (!($uid = $message->getUid())) {
  271. $uid = Auth::auth();
  272. }
  273. $questionIdArray = AnswerModel::where('uid', $uid)->pluck('question_id');
  274. $questions = QuestionModel::whereIn('id', $questionIdArray)->get();
  275. foreach ($questions as $question) {
  276. $question->answer = AnswerModel::where('uid', $uid)->where('question_id', $question->id)->first();
  277. }
  278. $message->setResponse($questions);
  279. }
  280. public function createAnswer(CreateAnswerMessage $message)
  281. {
  282. $questionId = $message->getQuestionId();
  283. $content = $message->getContent();
  284. $uid = Auth::auth();
  285. $question = QuestionModel::find($questionId);
  286. $answer = AnswerModel::updateOrCreate(['uid' => $uid, 'question_id' => $questionId], ['content' => $content]);
  287. $question->answer = $answer;
  288. $message->setResponse($question);
  289. }
  290. /**
  291. * @param DeletePhotosMessage $message
  292. * @throws Exception
  293. */
  294. public function deletePhotos(DeletePhotosMessage $message)
  295. {
  296. $field = $message->getField();
  297. $uid = Auth::auth();
  298. $pfs = new ProfileService();
  299. $pfs->deletePhoto($uid, $field);
  300. }
  301. /**
  302. * @param SelectCoverMessage $message
  303. * @throws AlertException
  304. */
  305. public function selectCover(SelectCoverMessage $message)
  306. {
  307. $field = $message->getField();
  308. $uid = Auth::auth();
  309. $user = UserModel::findOrFail($uid);
  310. /** @var UserModel $user */
  311. if (empty($user->{$field})) {
  312. throw new AlertException("参数错误", 101);
  313. }
  314. $identity_auth = $user->identity_auth;
  315. if (!empty($identity_auth)) {
  316. $identity_auths = explode(",", $identity_auth);
  317. array_walk($identity_auths, function (&$value) use ($field) {
  318. if ($value == "photo_src") {
  319. $value = $field;
  320. } elseif ($value == $field) {
  321. $value = "photo_src";
  322. }
  323. });
  324. $identity_auth = implode(",", $identity_auths);
  325. $user->identity_auth = $identity_auth;
  326. }
  327. $tmp = $user->photo_src;
  328. $user->photo_src = $user->{$field};
  329. $user->{$field} = $tmp;
  330. $user->save();
  331. $ps = new \App\Services\User\PartnerService();
  332. $ps->selectCover($user->partner_id, $field);
  333. }
  334. public function uidTranslationImAccount(UidTranslationImAccountMessage $message)
  335. {
  336. $uid = $message->getReqUid() ?? 0;
  337. $imAccount = $message->getReqIMAccount() ?? null;
  338. if ($uid) {
  339. $user = UserModel::whereUid($uid)->first();
  340. } else {
  341. $user = UserModel::where('im_account', $imAccount)->first();
  342. }
  343. if ($user) {
  344. $message->setResponse($user->uid, $user->im_account);
  345. } else {
  346. throw new AlertException("参数错误");
  347. }
  348. }
  349. }