UserService.php 13 KB

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