LockService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace App\Services\Share;
  3. use App\Exceptions\AlertException;
  4. use App\Exceptions\DBException;
  5. use App\Models\BuyInviteListModel;
  6. use App\Models\BuyInviteModel;
  7. use App\Models\PartnerModel;
  8. use App\Models\RecodeModel;
  9. use App\Models\User\UserModel;
  10. use App\Services\Service;
  11. use App\Services\User\PartnerService;
  12. use Illuminate\Http\Response;
  13. class LockService extends Service
  14. {
  15. /**
  16. * 正在解锁的卡片
  17. * @param int $uid
  18. * @return array
  19. */
  20. public function inglist(int $uid): array
  21. {
  22. $recodes = RecodeModel::where('uid', $uid)->get();
  23. $listmodel = new BuyInviteListModel();
  24. $datas = $listmodel->where([
  25. ['uid', $uid],
  26. ['expired_at', '>', time()],
  27. ])->whereNotIn('partner_id', $recodes->pluck('partner_id'))
  28. ->orderBy('id', 'desc')->get();
  29. $ps = new PartnerService();
  30. foreach ($datas as &$data) {
  31. $partner = $ps->get($data->partner_id);
  32. $data->photo = "https://oss.pocketuniversity.cn/media/2018-10-10/5bbd93dbc013b.png";
  33. isset($partner->photo_4) && $data->photo = "https://oss.pocketuniversity.cn{$partner->photo_4}";
  34. isset($partner->photo_3) && $data->photo = "https://oss.pocketuniversity.cn{$partner->photo_3}";
  35. isset($partner->photo_2) && $data->photo = "https://oss.pocketuniversity.cn{$partner->photo_2}";
  36. isset($partner->photo_1) && $data->photo = "https://oss.pocketuniversity.cn{$partner->photo_1}";
  37. isset($partner->photo_src) && $data->photo = "https://oss.pocketuniversity.cn{$partner->photo_src}";
  38. $data->type = 1;
  39. $data->title = '解锁分享';
  40. $data->desc = "成功后可解锁对方联系方式";
  41. $data->completeness = $listmodel->effectiveInvite($data->id);
  42. }
  43. return $datas->toArray();
  44. }
  45. /**
  46. * 成功列表
  47. * @param int $uid
  48. * @param array $pages
  49. * @return array
  50. */
  51. public function succlist(int $uid, array $pages): array
  52. {
  53. $recodes = RecodeModel::where('uid', $uid)->get();
  54. $listmodel = new BuyInviteListModel();
  55. $total = $listmodel->where('uid', $uid)->whereIn('partner_id', $recodes->pluck('partner_id'))->count();
  56. $datas = $listmodel->where('uid', $uid)->whereIn('partner_id', $recodes->pluck('partner_id'))
  57. ->orderBy('id', 'desc')->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
  58. $ps = new PartnerService();
  59. foreach ($datas as &$data) {
  60. $partner = $ps->get($data->partner_id);
  61. $data->photo = "https://oss.pocketuniversity.cn/media/2018-10-10/5bbd93dbc013b.png";
  62. isset($partner->photo_4) && $data->photo = "https://oss.pocketuniversity.cn{$partner->photo_4}";
  63. isset($partner->photo_3) && $data->photo = "https://oss.pocketuniversity.cn{$partner->photo_3}";
  64. isset($partner->photo_2) && $data->photo = "https://oss.pocketuniversity.cn{$partner->photo_2}";
  65. isset($partner->photo_1) && $data->photo = "https://oss.pocketuniversity.cn{$partner->photo_1}";
  66. isset($partner->photo_src) && $data->photo = "https://oss.pocketuniversity.cn{$partner->photo_src}";
  67. $data->type = 1;
  68. $data->title = '解锁分享';
  69. $data->desc = "成功后可解锁对方联系方式";
  70. }
  71. return [
  72. 'page' => $pages['page'],
  73. 'total' => $total,
  74. 'limit' => $pages['limit'],
  75. 'list' => $datas->toArray(),
  76. ];
  77. }
  78. /**
  79. * 失败列表
  80. * @param int $uid
  81. * @param array $pages
  82. * https://oss.pocketuniversity.cn/media/2018-10-10/5bbd7bedb5f9b.png
  83. * @return array
  84. */
  85. public function faillist(int $uid, array $pages): array
  86. {
  87. $recodes = RecodeModel::where('uid', $uid)->get();
  88. $listmodel = new BuyInviteListModel();
  89. $total = $listmodel->where([
  90. ['uid', $uid],
  91. ['expired_at', '<', time()],
  92. ])->whereNotIn('partner_id', $recodes->pluck('partner_id'))->count();
  93. $datas = $listmodel->where([
  94. ['uid', $uid],
  95. ['expired_at', '<', time()],
  96. ])->whereNotIn('partner_id', $recodes->pluck('partner_id'))->orderBy('id', 'desc')
  97. ->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
  98. $ps = new PartnerService();
  99. foreach ($datas as &$data) {
  100. $partner = $ps->get($data->partner_id);
  101. $data->photo = "https://oss.pocketuniversity.cn/media/2018-10-10/5bbd93dbc013b.png";
  102. isset($partner->photo_4) && $data->photo = "https://oss.pocketuniversity.cn{$partner->photo_4}";
  103. isset($partner->photo_3) && $data->photo = "https://oss.pocketuniversity.cn{$partner->photo_3}";
  104. isset($partner->photo_2) && $data->photo = "https://oss.pocketuniversity.cn{$partner->photo_2}";
  105. isset($partner->photo_1) && $data->photo = "https://oss.pocketuniversity.cn{$partner->photo_1}";
  106. isset($partner->photo_src) && $data->photo = "https://oss.pocketuniversity.cn{$partner->photo_src}";
  107. $data->type = 1;
  108. $data->title = '解锁分享';
  109. $data->desc = "成功后可解锁对方联系方式";
  110. }
  111. return [
  112. 'page' => $pages['page'],
  113. 'total' => $total,
  114. 'limit' => $pages['limit'],
  115. 'list' => $datas->toArray(),
  116. ];
  117. }
  118. /**
  119. * 核销解锁助力
  120. * @param int $uid
  121. * @param int $list_id
  122. * @return Response
  123. * @throws AlertException
  124. * @throws DBException
  125. * @throws \Throwable
  126. */
  127. public function check(int $uid, int $list_id): array
  128. {
  129. $invite = BuyInviteListModel::findOrFail($list_id);
  130. $rs = new RedpackService();
  131. try {
  132. $rs->check($uid, $invite->uid);
  133. } catch (\Exception $e) {
  134. }
  135. if ($invite->expired_at < time()) {
  136. throw new AlertException("链接失效", 105);
  137. }
  138. if (
  139. BuyInviteModel::where([
  140. ['invite_id', $list_id],
  141. ['invite_uid', $uid],
  142. ])->exists()
  143. ) {
  144. throw new AlertException("你已经帮ta助力过了哦", 102);
  145. }
  146. $inviteModel = new BuyInviteModel();
  147. if (
  148. $inviteModel->where([
  149. ['invite_uid', $uid],
  150. ['created_at', '>', mktime(0, 0, 0)],
  151. ])->count() > 2
  152. ) {
  153. throw new AlertException("你今日的助力机会用完啦,明天再帮ta助力吧", 103);
  154. }
  155. $model = new BuyInviteListModel();
  156. $process = $model->effectiveInvite($list_id);
  157. if ($process == 100) {
  158. throw new AlertException("该助力已结束,卡片成功解锁啦", 104);
  159. }
  160. $comment = array(
  161. '希望这世界上从此少一个单身狗',
  162. '希望你不在是一个人过',
  163. '祝脱单',
  164. '成了别忘了发红包',
  165. '等着吃你的喜糖',
  166. '直觉告诉我,你这次要脱单',
  167. '坐等吃狗粮',
  168. '我已经准备好,喝喜酒的红包',
  169. '你这波突然袭击,很skr',
  170. '单身的终点,浪漫的节点,幸福的起点',
  171. '这波操作很骚',
  172. '大吉大利,今晚脱单',
  173. '请接收我1w点的助力暴击',
  174. '行动才是脱单的纲领,你做到了',
  175. '我很介意你单身,so赶紧脱单',
  176. '明天,我希望可以给你发来脱单贺电',
  177. '我感受到爱情的航班在呼唤你',
  178. '这波操作很骚',
  179. );
  180. $rand = rand(0, 16);
  181. $fill = array(
  182. 'invite_id' => $list_id,
  183. 'invite_uid' => $uid,
  184. 'state' => 0,
  185. 'comment' => $comment[$rand],
  186. );
  187. $user = UserModel::findOrFail($uid);
  188. if ($user->sex == 2) {
  189. $sex_k = 0.8;
  190. } else {
  191. $sex_k = 0.5;
  192. }
  193. $r = sprintf('%.1f', rand(3, 10) / 10);
  194. $dis_score = sprintf('%d', (100 - $process) * $sex_k * $r);
  195. $dis_score = $dis_score < 1 ? 1 : $dis_score;
  196. $fill['dis_score'] = $dis_score;
  197. $list = new BuyInviteModel();
  198. $liModel = $list->fill($fill);
  199. $liModel->save();
  200. try {
  201. $lModel = new BuyInviteListModel();
  202. $lModel->autoUnlock($list_id);
  203. } catch (\Exception $e) {
  204. $liModel->delete();
  205. throw new DBException("解锁异常, 请将问题反馈给我们", 505);
  206. }
  207. $user = UserModel::findOrFail($invite->uid);
  208. return response([
  209. 'score' => $dis_score,
  210. 'user' => [
  211. 'nickname' => $user->nickname,
  212. ],
  213. ]);
  214. }
  215. /**
  216. * 创建卡片分享
  217. * @param int $uid
  218. * @param int $partner_id
  219. * @return array
  220. * @throws AlertException
  221. * @throws DBException
  222. */
  223. public function store(int $uid, int $partner_id)
  224. {
  225. $partner = PartnerModel::findOrFail($partner_id);
  226. if (1 != $partner->is_sell || 0 != $partner->is_hide) {
  227. throw new AlertException("卡片不存在", 404);
  228. }
  229. if (
  230. RecodeModel::where([
  231. ['uid', $uid],
  232. ['partner_id', $partner_id],
  233. ])->exists()
  234. ) {
  235. throw new AlertException("该卡片已经被解锁", 202);
  236. }
  237. $listmodel = new BuyInviteListModel();
  238. $list = $listmodel->where([
  239. ['uid', $uid],
  240. ['partner_id', $partner_id],
  241. ['expired_at', '>', time()],
  242. ])->first();
  243. if (collect($list)->isEmpty()) {
  244. $list = $listmodel->fill([
  245. 'uid' => $uid,
  246. 'partner_id' => $partner_id,
  247. 'expired_at' => time() + 86400,
  248. ]);
  249. if ($list->save()) {
  250. return $list->toArray();
  251. } else {
  252. throw new DBException("网络异常,请重新尝试或联系客服", 500);
  253. }
  254. } else {
  255. return $list->toArray();
  256. }
  257. }
  258. }