GteamService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace App\Services\Share;
  3. use App\Exceptions\AlertException;
  4. use App\Exceptions\ApiException;
  5. use App\Models\Gteam\ActivityModel;
  6. use App\Models\Gteam\EnterInviteModel;
  7. use App\Models\Gteam\EnterModel;
  8. use App\Models\User\UserModel;
  9. use App\Services\Service;
  10. class GteamService extends Service
  11. {
  12. /**
  13. * 正在进行的分享列表
  14. * @param int $uid
  15. * @return array
  16. */
  17. public function inglist(int $uid): array
  18. {
  19. $activitys = ActivityModel::where('match_end_at', '>', time())->get();
  20. $res = array();
  21. foreach ($activitys as $activity) {
  22. $data = EnterModel::where([
  23. ['activity_id', $activity->id],
  24. ['uid', $uid],
  25. ])->first();
  26. if (collect($data)->isEmpty()) {
  27. continue;
  28. } else {
  29. $tmp = [
  30. 'id' => $data->id,
  31. 'expired_at' => $activity->match_end_at,
  32. ];
  33. $tmp['type'] = 6;
  34. $tmp['photo'] = 'https://oss.pocketuniversity.cn/media/2018-10-10/5bbd7c43125ec.png';
  35. $tmp['title'] = '成功率提升分享';
  36. $tmp['desc'] = "成功后可提升活动匹配优先级";
  37. $tmp['completeness'] = $data->score;
  38. $res[] = $tmp;
  39. }
  40. }
  41. return $res;
  42. }
  43. /**
  44. * 成功率列表
  45. * @param int $uid
  46. * @param array $pages
  47. * @return array
  48. */
  49. public function succlist(int $uid, array $pages): array
  50. {
  51. $activity = ActivityModel::where('match_end_at', '<', time())->orderBy('id', 'desc')->first();
  52. $total = EnterModel::where([
  53. ['activity_id', '<=', $activity->id],
  54. ['uid', $uid],
  55. ])->count();
  56. $datas = EnterModel::where([
  57. ['activity_id', '<=', $activity->id],
  58. ['uid', $uid],
  59. ['score', 90],
  60. ])->orderBy('id', 'desc')->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
  61. $res = array();
  62. foreach ($datas as $data) {
  63. $res[] = array(
  64. 'id' => $data->id,
  65. 'expired_at' => time() - 86400,
  66. 'photo' => 'https://oss.pocketuniversity.cn/media/2018-10-10/5bbd7c43125ec.png',
  67. 'title' => '成功率提升分享',
  68. 'desc' => '成功后可提升活动匹配优先级',
  69. 'completeness' => $data->score + $data->add_score,
  70. 'type' => 6,
  71. );
  72. }
  73. return [
  74. 'total' => $total,
  75. 'page' => $pages['page'],
  76. 'limit' => $pages['limit'],
  77. 'list' => $res,
  78. ];
  79. }
  80. /**
  81. * 获取最近的报名气泡
  82. * @param int $activity_id
  83. * @return array
  84. */
  85. public function lastInvite(int $activity_id): array
  86. {
  87. $invites = EnterInviteModel::where([
  88. ['activity_id', $activity_id],
  89. ['state', '!=', 2],
  90. ])->take(30)->groupBy('enter_id')->get(['uid', 'enter_id']);
  91. foreach ($invites as $k => &$invite) {
  92. try {
  93. $invite->user = UserModel::findOrFail(
  94. $invite->uid,
  95. ['uid', 'headimgurl', 'nickname', 'sex', 'address', 'home']
  96. );
  97. $invite->invite = array(
  98. 'count' => EnterInviteModel::where([
  99. ['state', 0],
  100. ['enter_id', $invite->enter_id],
  101. ])->count(),
  102. 'dis_score' => EnterInviteModel::where([
  103. ['state', 0],
  104. ['enter_id', $invite->enter_id],
  105. ])->sum('dis_score'),
  106. );
  107. } catch (\Exception $e) {
  108. unset($invites->$k);
  109. continue;
  110. }
  111. }
  112. return $invites->toArray();
  113. }
  114. /**
  115. * 获取某个邀请的助力历史
  116. * @param int $invite_id
  117. * @return array
  118. */
  119. public function history(int $invite_id): array
  120. {
  121. $invites = EnterInviteModel::where('enter_id', $invite_id)->get();
  122. foreach ($invites as $k => &$invite) {
  123. try {
  124. $invite->user = UserModel::findOrFail($invite->invite_uid, ['uid', 'headimgurl', 'nickname', 'sex']);
  125. } catch (\Exception $e) {
  126. unset($invites->$k);
  127. continue;
  128. }
  129. }
  130. return $invites->toArray();
  131. }
  132. /**
  133. * 公众号助力
  134. * @param int $uid
  135. * @param int $invite_id
  136. * @return array
  137. * @throws AlertException
  138. */
  139. public function vipCheck(int $uid, int $invite_id): array
  140. {
  141. $user = UserModel::findOrFail($uid);
  142. if ($user->be_vip_at == 0) {
  143. throw new AlertException("你还不是会员", 101);
  144. }
  145. $enter = EnterModel::findOrFail($invite_id);
  146. if ($enter->uid != $uid) {
  147. throw new AlertException("只能助力自己", 101);
  148. }
  149. $data = $this->check(1, $enter->id);
  150. return $data;
  151. }
  152. /**
  153. * 邀请助力
  154. * @param int $uid
  155. * @param int $enter_id
  156. * @return array
  157. * @throws AlertException
  158. */
  159. public function check(int $uid, int $enter_id): array
  160. {
  161. $enter = EnterModel::findOrFail($enter_id);
  162. $activity = ActivityModel::findOrFail($enter->activity_id);
  163. if (time() > $activity->match_end_at) {
  164. throw new AlertException('啊偶,该助力申请过期啦。', 103);
  165. }
  166. if (
  167. EnterInviteModel::where([
  168. ['enter_id', $enter->id],
  169. ['invite_uid', $uid],
  170. ])->exists()
  171. ) {
  172. if ($uid == 1) {
  173. throw new AlertException('你已领取会员专属成功率,用其他方式提升吧。', 104);
  174. }
  175. throw new AlertException('你已经帮ta助力过了哦', 104);
  176. }
  177. if (
  178. EnterInviteModel::where([
  179. ['invite_uid', $uid],
  180. ['created_at', '>', mktime(0, 0, 0)],
  181. ])->count() > 2 && $uid != 1
  182. ) {
  183. throw new AlertException('你今日的助力机会用完啦,明天再帮ta助力吧', 105);
  184. }
  185. $sex_k = 0.5;
  186. $comment = array(
  187. '希望这世界上从此少一个单身狗',
  188. '希望你不在是一个人过',
  189. '祝脱单',
  190. '成了别忘了发红包',
  191. '等着吃你的喜糖',
  192. '直觉告诉我,你这次要脱单',
  193. '坐等吃狗粮',
  194. '我已经准备好,喝喜酒的红包',
  195. '你这波突然袭击,很skr',
  196. '单身的终点,浪漫的节点,幸福的起点',
  197. '这波操作很骚',
  198. '大吉大利,今晚脱单',
  199. '请接收我1w点的助力暴击',
  200. '行动才是脱单的纲领,你做到了',
  201. '我很介意你单身,so赶紧脱单',
  202. '明天,我希望可以给你发来脱单贺电',
  203. '我感受到爱情的航班在呼唤你',
  204. '这波操作很骚',
  205. );
  206. $data = array(
  207. 'uid' => $enter->uid,
  208. 'enter_id' => $enter->id,
  209. 'activity_id' => $enter->activity_id,
  210. 'invite_uid' => $uid,
  211. 'comment' => $comment[rand(0, 16)],
  212. 'created_at' => time(),
  213. );
  214. if ($enter->score < 90) {
  215. // 增加score
  216. $data['state'] = 0;
  217. $r = sprintf('%.1f', rand(3, 10) / 10);
  218. $rand = sprintf('%d', (90 - $enter->score) * $sex_k * $r);
  219. if ($uid == $enter->uid) {
  220. $rand = rand(5, 10);
  221. }
  222. if ($uid == 1) {
  223. $rand = rand(10, 20);
  224. }
  225. $rand = intval($rand) < 1 ? 1 : intval($rand);
  226. $dis_score = ($enter->score + $rand) > 90 ? 90 - $enter->score : $rand;
  227. $data['dis_score'] = $dis_score;
  228. EnterModel::where('id', $enter_id)->increment('score', $dis_score);
  229. // 通知
  230. try {
  231. $ns = new \App\Services\Gteam\NoticeService();
  232. $ns->inviteToScore($enter->uid, $uid, $enter->id);
  233. $ns->helpToScore($uid, $enter->uid, $enter->id);
  234. } catch (\Exception $e) {
  235. app('sentry')->captureException($e);
  236. }
  237. } else {
  238. // 无效的邀请
  239. $data['state'] = 2;
  240. $data['dis_score'] = 0;
  241. }
  242. $inviteModel = new EnterInviteModel();
  243. $invite = $inviteModel->fill($data);
  244. if ($invite->save()) {
  245. $enterUser = UserModel::find($enter->uid);
  246. return array(
  247. 'state' => $data['state'],
  248. 'dis_score' => $data['dis_score'],
  249. 'comment' => $data['comment'],
  250. 'user' => [
  251. 'nickname' => $enterUser->nickname,
  252. 'headimgurl' => $enterUser->headimgurl,
  253. ],
  254. );
  255. } else {
  256. throw new AlertException('如有疑问,可以联系客服,请将问题反馈给我们', 505);
  257. }
  258. }
  259. /**
  260. * 公众号助力
  261. * @param string $openid
  262. * @return int
  263. * @throws AlertException
  264. * @throws ApiException
  265. */
  266. public function gzhzl(string $openid): int
  267. {
  268. $resp = \DB::table('kddx_user_openid')->where("openid", $openid)->first();
  269. if (collect($resp)->isEmpty()) {
  270. throw new ApiException("未绑定uid,点此回到活动首页");
  271. }
  272. $activity = ActivityModel::whereBetween(
  273. 'match_end_at',
  274. [mktime(0, 0, 0), mktime(0, 0, 0) + 86400]
  275. )->firstOrFail();
  276. $enter = EnterModel::where([
  277. ['activity_id', $activity->id],
  278. ['uid', $resp->uid],
  279. ])->first();
  280. if (collect($enter)->isEmpty()) {
  281. throw new ApiException("未找到本期报名记录,点此回到活动首页");
  282. }
  283. if ($enter->score >= 90) {
  284. throw new ApiException("本期助力已经达上限,点此回到活动首页");
  285. }
  286. $data = $this->check(1, $enter->id);
  287. return $enter->id;
  288. }
  289. }