InvitationController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace App\Http\Controllers\Deed;
  3. use App\Exceptions\AlertException;
  4. use App\Http\Controllers\Controller;
  5. use App\Http\Controllers\Core\Auth;
  6. use App\Models\Invite\QuestionTemplateModel;
  7. use App\Models\User\InviteConfigModel;
  8. use App\Models\User\LikeInviteQuestionModel;
  9. use App\Models\User\UserModel;
  10. use App\Services\Deed\InvitationService;
  11. use Illuminate\Http\Request;
  12. class InvitationController extends Controller
  13. {
  14. /**
  15. * 发起心动邀请
  16. * @param Request $request
  17. * @return array
  18. * @throws AlertException
  19. */
  20. public function create(Request $request)
  21. {
  22. $uid = Auth::auth();
  23. $this->validate($request, [
  24. 'invite_uid' => 'required|integer',
  25. 'question_type' => 'in:0,1,2,3,4',
  26. 'question_answer' => '',
  27. 'question_id' => 'integer',
  28. 'say_hello' => '',
  29. 'kk' => '',
  30. 'paint_id' => ''
  31. ]);
  32. $invite_uid = $request->post('invite_uid');
  33. $question_type = $request->post('question_type', 0);
  34. $question_id = $request->post('question_id', 0);
  35. $question_answer = $request->post('question_answer');
  36. $kk = $request->post('kk');
  37. $paint_id = $request->post('paint_id', 0);
  38. $request_content = $request->post('request_content');
  39. $question = array(
  40. 'question_id' => $question_id,
  41. 'question_answer' => $question_answer
  42. );
  43. $say_hello = $request->post("say_hello", null);
  44. $is = new InvitationService();
  45. $invite_id = $is->create(
  46. $uid,
  47. $invite_uid,
  48. $question_type,
  49. $question,
  50. $say_hello,
  51. $paint_id,
  52. $request_content,
  53. $kk
  54. );
  55. return response([
  56. 'code' => 200,
  57. 'message' => 'success',
  58. 'data' => [
  59. 'invitate_id' => $invite_id
  60. ]
  61. ]);
  62. }
  63. /**
  64. * 通过id获取邀请信息
  65. * @param int $id
  66. * @return array
  67. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  68. */
  69. public function getbyid(int $id)
  70. {
  71. $uid = Auth::auth();
  72. $is = new InvitationService();
  73. $data = $is->getById($uid, $id);
  74. return array(
  75. 'code' => 200,
  76. 'message' => 'success',
  77. 'data' => $data
  78. );
  79. }
  80. /**
  81. * 查看收到的待处理邀请列表
  82. * @param Request $request
  83. * @return array
  84. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  85. */
  86. public function receivesByUnHandle(Request $request)
  87. {
  88. $uid = Auth::auth();
  89. $pages = array(
  90. 'page' => $request->get('page') ?? 1,
  91. 'limit' => 100
  92. );
  93. $is = new InvitationService();
  94. $data = $is->receivesByUnhandle($uid, $pages);
  95. return array(
  96. 'code' => 200,
  97. 'message' => 'success',
  98. 'data' => $data
  99. );
  100. }
  101. /**
  102. * 查看待处理邀请列表
  103. * @param Request $request
  104. * @return array
  105. * @throws \Exception
  106. */
  107. public function unhandle(Request $request)
  108. {
  109. $uid = Auth::auth();
  110. $pages = array(
  111. 'page' => $request->get('page') ?? 1,
  112. 'limit' => 100
  113. );
  114. $is = new InvitationService();
  115. $data = $is->unhandle($uid, $pages);
  116. return array(
  117. 'code' => 200,
  118. 'message' => 'success',
  119. 'data' => $data
  120. );
  121. }
  122. /**
  123. * 为心动考验支付小花
  124. * @param Request $request
  125. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  126. * @throws AlertException
  127. */
  128. public function pay(Request $request)
  129. {
  130. $this->validate($request, ['invite_uid' => 'required', 'type' => 'required']);
  131. $type = $request->post('type', 1);
  132. $invite_uid = $request->post('invite_uid', 0);
  133. $uid = Auth::auth();
  134. $is = new InvitationService();
  135. $is->pay($uid, $invite_uid, $type);
  136. return response([
  137. 'code' => 200,
  138. 'message' => 'success'
  139. ]);
  140. }
  141. /**
  142. * 查看收到的邀请列表
  143. * @param Request $request
  144. * @return array
  145. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  146. */
  147. public function receives(Request $request)
  148. {
  149. $uid = Auth::auth();
  150. $pages = array(
  151. 'page' => $request->get('page') ?? 1,
  152. 'limit' => 10
  153. );
  154. $onlyreceive = $request->get('onlyreceive') ?? false;
  155. $is = new InvitationService();
  156. $data = $is->receives($uid, $pages, $onlyreceive);
  157. return array(
  158. 'code' => 200,
  159. 'message' => 'success',
  160. 'data' => $data
  161. );
  162. }
  163. /**
  164. * 查看发出的邀请列表
  165. * @param Request $request
  166. * @return array
  167. */
  168. public function sends(Request $request)
  169. {
  170. $uid = Auth::auth();
  171. $pages = array(
  172. 'page' => $request->get('page') ?? 1,
  173. 'limit' => 20
  174. );
  175. $onlyreceive = $request->get('onlyreceive') ?? false;
  176. $is = new InvitationService();
  177. $data = $is->sends($uid, $pages, $onlyreceive);
  178. return array(
  179. 'code' => 200,
  180. 'message' => 'success',
  181. 'data' => $data
  182. );
  183. }
  184. /**
  185. * 查看邀请列表
  186. * @param Request $request
  187. * @return array
  188. */
  189. public function list(Request $request)
  190. {
  191. $uid = Auth::auth();
  192. $pages = array(
  193. 'page' => $request->get('page') ?? 1,
  194. 'limit' => 20
  195. );
  196. $is = new InvitationService();
  197. $data = $is->list($uid, $pages);
  198. return array(
  199. 'code' => 200,
  200. 'message' => 'success',
  201. 'data' => $data
  202. );
  203. }
  204. /**
  205. * 心动邀请同意
  206. * @param int $invite_id
  207. * @param Request $request
  208. * @return array
  209. * @throws AlertException
  210. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  211. */
  212. public function fixstate(int $invite_id, Request $request)
  213. {
  214. $this->validate($request, [
  215. 'state' => 'required|in:-1,1',
  216. ]);
  217. $state = $request->post('state');
  218. $uid = Auth::auth();
  219. $is = new InvitationService();
  220. $is->fixstate($invite_id, $uid, $state);
  221. return [
  222. 'code' => 200,
  223. 'message' => 'success'
  224. ];
  225. }
  226. /**
  227. * 查阅心动邀请
  228. * @param int $invite_id
  229. * @return array
  230. * @throws AlertException
  231. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  232. */
  233. public function fixread(int $invite_id)
  234. {
  235. $uid = Auth::auth();
  236. $is = new InvitationService();
  237. $is->fixread($invite_id, $uid);
  238. return array(
  239. 'code' => 200,
  240. 'message' => 'success'
  241. );
  242. }
  243. /**
  244. * 获取某人设置的心动问题和
  245. * @param Request $request
  246. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  247. */
  248. public function questions(Request $request)
  249. {
  250. $type = $request->get('type', 'text');
  251. $uid = $request->get('uid');
  252. $sex = UserModel::where('uid', $uid)->value('sex') ?: 2;
  253. $config = InviteConfigModel::select('task_question_data', 'task_sing_data')->where('uid', $uid)->first();
  254. $template_ids = [];
  255. if ($config) {
  256. switch ($type) {
  257. case "text":
  258. $template_ids = collect($config->task_question_data)->pluck('id')->toArray();
  259. break;
  260. case 'music':
  261. $template_ids = collect($config->task_sing_data)->pluck('id')->toArray();
  262. break;
  263. }
  264. }
  265. if (!$template_ids) {
  266. $template_ids = array_random(QuestionTemplateModel::where(['type' => $type])->where(
  267. 'select_count',
  268. '>',
  269. 0
  270. )->pluck('id')->toArray(), 20);
  271. }
  272. $templates = QuestionTemplateModel::whereIn('id', $template_ids)
  273. ->where('type', $type)
  274. ->where('select_count', '>', 0)
  275. ->get();
  276. foreach ($templates as $template) {
  277. $voice = LikeInviteQuestionModel::where([
  278. 'template_id' => $template->id,
  279. 'select' => 1,
  280. 'sex' => $sex
  281. ])->inRandomOrder()->first();
  282. unset($voice->question);
  283. $template->voice = $voice;
  284. }
  285. return response([
  286. 'code' => 200,
  287. 'message' => 'OK',
  288. 'data' => $templates,
  289. ]);
  290. }
  291. }