PairService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace App\Services\Share;
  3. use App\Exceptions\AlertException;
  4. use App\Models\Fpdx\ActivityModel;
  5. use App\Models\Fpdx\InviteModel;
  6. use App\Models\Fpdx\PairModel;
  7. use App\Models\User\AuthKey;
  8. use App\Models\User\OpenidUserModel;
  9. use App\Models\User\UserModel;
  10. use App\Services\Pair\NoticeService;
  11. use App\Services\Pay\OrderService;
  12. use App\Services\Service;
  13. use Illuminate\Database\Eloquent\ModelNotFoundException;
  14. class PairService extends Service
  15. {
  16. /**
  17. * 获取邀请信息
  18. * @param int $list_id
  19. * @return array
  20. */
  21. public function get(int $list_id)
  22. {
  23. $score_count = InviteModel::where(array(['pair_id', $list_id], ['state', 0], ['invite_uid', '!=', 1]))->count();
  24. $pay_count = InviteModel::where(array(['pair_id', $list_id], ['state', 1]))->count();
  25. $invalid_count = InviteModel::where(array(['pair_id', $list_id], ['state', 2]))->count();
  26. $invites = InviteModel::where('pair_id', $list_id)->get();
  27. $auths = AuthKey::whereIn('auth_key', $invites->pluck('invite_uid')->toArray())->get();
  28. $users = UserModel::whereIn('uid', $auths->pluck('uid')->toArray())->get([
  29. 'uid',
  30. 'sex',
  31. 'headimgurl',
  32. 'nickname',
  33. ]);
  34. foreach ($invites as &$invite) {
  35. if (1 == $invite->invite_uid) {
  36. $invite->invite_user = [
  37. 'sex' => 0,
  38. 'headimgurl' => "https://oss.pocketuniversity.cn/media/2019-07-01/5d19dbb0260a7.jpg",
  39. 'nickname' => "公众号助力",
  40. ];
  41. } else {
  42. $auth = $auths->where('auth_key', $invite->invite_uid)->first();
  43. if (is_null($auth)) {
  44. $openid = collect(OpenidUserModel::where("openid", $invite->invite_uid)->first())->toArray();
  45. if (empty($openid)) {
  46. $invite->invite_user = [];
  47. } else {
  48. $invite->invite_user = [
  49. 'sex' => $openid['sex'],
  50. 'headimgurl' => null,
  51. 'nickname' => null,
  52. ];
  53. }
  54. } else {
  55. $invite->invite_user = $users->where('uid', $auth->uid)->first();
  56. }
  57. }
  58. }
  59. return array(
  60. 'score_count' => $score_count,
  61. 'pay_count' => $pay_count,
  62. 'invalid_count' => $invalid_count,
  63. 'list' => $invites,
  64. );
  65. }
  66. /**
  67. * 正在进行的分享列表
  68. * @param int $uid
  69. * @return array
  70. */
  71. public function inglist(int $uid): array
  72. {
  73. $ings = ActivityModel::getActivitys();
  74. $data = PairModel::where([
  75. ['stage_id', $ings['next']],
  76. ['uid', $uid],
  77. ])->first();
  78. if (collect($data)->isEmpty()) {
  79. return array();
  80. } else {
  81. $activity = ActivityModel::find($ings['next']);
  82. $tmp = [
  83. 'id' => $data->id,
  84. 'expired_at' => $activity->signend_time,
  85. ];
  86. if ($data->order_id > 0) {
  87. $tmp['type'] = 5;
  88. $tmp['photo'] = 'https://oss.pocketuniversity.cn/media/2018-10-10/5bbd7c7ac8c90.png';
  89. $tmp['title'] = '报名费退款分享';
  90. $tmp['desc'] = "分享成功后可退还你的报名费";
  91. $tmp['completeness'] = sprintf('%d', (9.9 - $data->pay) / 9.9 * 100);
  92. } else {
  93. $tmp['type'] = 2;
  94. $tmp['photo'] = 'https://oss.pocketuniversity.cn/media/2018-10-10/5bbd7c43125ec.png';
  95. $tmp['title'] = '成功率提升分享';
  96. $tmp['desc'] = "成功后可提升活动匹配优先级";
  97. $tmp['completeness'] = $data->score + $data->add_score;
  98. }
  99. $res[] = $tmp;
  100. return $res;
  101. }
  102. }
  103. /**
  104. * 成功率列表
  105. * @param int $uid
  106. * @param array $pages
  107. * @return array
  108. */
  109. public function scorelist(int $uid, array $pages): array
  110. {
  111. $ings = ActivityModel::getActivitys();
  112. $total = PairModel::where([
  113. ['stage_id', '<', $ings['next']],
  114. ['uid', $uid],
  115. ['order_id', 0],
  116. ])->count();
  117. $datas = PairModel::where([
  118. ['stage_id', '<', $ings['next']],
  119. ['uid', $uid],
  120. ['score', 90],
  121. ['order_id', 0],
  122. ])->orderBy('id', 'desc')->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
  123. $res = array();
  124. foreach ($datas as $data) {
  125. $res[] = array(
  126. 'id' => $data->id,
  127. 'expired_at' => time() - 86400,
  128. 'photo' => 'https://oss.pocketuniversity.cn/media/2018-10-10/5bbd7c43125ec.png',
  129. 'title' => '成功率提升分享',
  130. 'desc' => '成功后可提升活动匹配优先级',
  131. 'completeness' => $data->score + $data->add_score,
  132. 'type' => 2,
  133. );
  134. }
  135. return [
  136. 'total' => $total,
  137. 'page' => $pages['page'],
  138. 'limit' => $pages['limit'],
  139. 'list' => $res,
  140. ];
  141. }
  142. /**
  143. * 退费列表
  144. * @param int $uid
  145. * @param array $pages
  146. * @return array
  147. */
  148. public function refundlist(int $uid, array $pages): array
  149. {
  150. $ings = ActivityModel::getActivitys();
  151. $total = PairModel::where([
  152. ['stage_id', '<', $ings['next']],
  153. ['uid', $uid],
  154. ['score', 90],
  155. ['order_id', '>', 0],
  156. ])->count();
  157. $datas = PairModel::where([
  158. ['stage_id', '<', $ings['next']],
  159. ['uid', $uid],
  160. ['score', 90],
  161. ['order_id', '>', 0],
  162. ])->orderBy('id', 'desc')->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
  163. $res = array();
  164. foreach ($datas as $data) {
  165. $res[] = array(
  166. 'id' => $data->id,
  167. 'expired_at' => time() - 86400,
  168. 'photo' => 'https://oss.pocketuniversity.cn/media/2018-10-10/5bbd7c7ac8c90.png',
  169. 'title' => '报名费退款分享',
  170. 'desc' => '分享成功后可退还你的报名费',
  171. 'completeness' => sprintf('%d', (9.9 - $data->pay) / 9.9 * 100),
  172. 'type' => 5,
  173. );
  174. }
  175. return [
  176. 'total' => $total,
  177. 'page' => $pages['page'],
  178. 'limit' => $pages['limit'],
  179. 'list' => $res,
  180. ];
  181. }
  182. /**
  183. * 会员助力
  184. * @param int $uid
  185. * @param int $invite_id
  186. * @return array
  187. * @throws AlertException
  188. * @throws \Throwable
  189. */
  190. public function vipCheck(int $uid, int $invite_id)
  191. {
  192. $user = UserModel::findOrFail($uid);
  193. if ($user->be_vip_at == 0) {
  194. throw new AlertException("你还不是会员", 101);
  195. }
  196. $pair = PairModel::findOrFail($invite_id);
  197. if ($uid != $pair->uid) {
  198. throw new AlertException("只能为助力自己", 102);
  199. }
  200. $data = $this->check(1, $pair->id);
  201. return $data;
  202. }
  203. /**
  204. * 邀请助力
  205. * @param string $openid
  206. * @param int $pair_id
  207. * @return array
  208. * @throws AlertException
  209. * @throws \Throwable
  210. */
  211. public function check(string $openid, int $pair_id): array
  212. {
  213. try {
  214. /** @var PairModel $pair */
  215. $pair = PairModel::findOrFail($pair_id);
  216. } catch (ModelNotFoundException $exception) {
  217. throw new AlertException("错误的分享id", 401);
  218. }
  219. $authkey = AuthKey::where('uid', $pair->uid)->get()->pluck('auth_key')->toArray();
  220. if (in_array($openid, $authkey)) {
  221. throw new AlertException("不能给自己助力", 101);
  222. }
  223. $activitys = ActivityModel::getActivitys();
  224. if ($pair->stage_id != $activitys['next']) {
  225. throw new AlertException('啊偶,该助力申请过期啦。', 103);
  226. }
  227. if (
  228. InviteModel::where([
  229. ['pair_id', $pair->id],
  230. ['invite_uid', $openid],
  231. ])->exists()
  232. ) {
  233. if ($openid == 1) {
  234. throw new AlertException('你已领取会员专属成功率,用其他方式提升吧。', 104);
  235. }
  236. throw new AlertException('你已经帮ta助力过了哦', 104);
  237. }
  238. if (
  239. InviteModel::where([
  240. ['invite_uid', $openid],
  241. ['create_at', '>', mktime(0, 0, 0)],
  242. ])->count() > 2 && $openid != 1
  243. ) {
  244. throw new AlertException('你今日的助力机会用完啦,明天再帮ta助力吧', 105);
  245. }
  246. $sex_k = 0.5;
  247. $comment = array(
  248. '希望这世界上从此少一个单身狗',
  249. '希望你不在是一个人过',
  250. '祝脱单',
  251. '成了别忘了发红包',
  252. '等着吃你的喜糖',
  253. '直觉告诉我,你这次要脱单',
  254. '坐等吃狗粮',
  255. '我已经准备好,喝喜酒的红包',
  256. '你这波突然袭击,很skr',
  257. '单身的终点,浪漫的节点,幸福的起点',
  258. '这波操作很骚',
  259. '大吉大利,今晚脱单',
  260. '请接收我1w点的助力暴击',
  261. '行动才是脱单的纲领,你做到了',
  262. '我很介意你单身,so赶紧脱单',
  263. '明天,我希望可以给你发来脱单贺电',
  264. '我感受到爱情的航班在呼唤你',
  265. '这波操作很骚',
  266. );
  267. $data = array(
  268. 'uid' => $pair->uid,
  269. 'pair_id' => $pair->id,
  270. 'stage_id' => $pair->stage_id,
  271. 'invite_uid' => $openid,
  272. 'comment' => $comment[rand(0, 16)],
  273. 'create_at' => time(),
  274. );
  275. if ($pair->score < 90) {
  276. // 增加score
  277. $data['state'] = 0;
  278. $r = sprintf('%.1f', rand(3, 10) / 10);
  279. $rand = sprintf('%d', (90 - $pair->score) * $sex_k * $r);
  280. if ($openid == 1) {
  281. $rand = rand(10, 20);
  282. }
  283. $rand = intval($rand) < 1 ? 1 : intval($rand);
  284. $dis_score = ($pair->score + $rand) > 90 ? 90 - $pair->score : $rand;
  285. $data['dis_score'] = $dis_score;
  286. PairModel::where('id', $pair_id)->increment('score', $dis_score);
  287. if ($pair->state < 100) {
  288. PairModel::where('id', $pair_id)->where('state', '<', 100)->increment('state', 100);
  289. try {
  290. $noticeService = new NoticeService();
  291. $noticeService->inviteToSuccess($pair->uid);
  292. } catch (\Exception $e) {
  293. }
  294. } else {
  295. try {
  296. // 邀请好友助力通知
  297. $noticeService = new NoticeService();
  298. $noticeService->inviteToScore($pair->uid, $pair->id);
  299. // 帮助好友助力通知
  300. // $noticeService->HelpToScore($openid, $pair->id);
  301. } catch (\Exception $e) {
  302. }
  303. }
  304. } elseif ($pair->pay > 0 && $openid != 1) {
  305. // 退款
  306. $data['state'] = 1;
  307. $r = sprintf('%.1f', rand(3, 10) / 10);
  308. $rand = 1;
  309. $dis_score = ($pair->pay - $rand) < 0 ? $pair->pay : $rand;
  310. $data['dis_score'] = $dis_score;
  311. if ($dis_score > 0) {
  312. try {
  313. $orderObj = new OrderService();
  314. $orderObj->refund($pair->order_id, $dis_score * 100, "72小时助力退款", true);
  315. PairModel::where('id', $pair->id)->decrement('pay', $dis_score);
  316. try {
  317. $noticeService = new NoticeService();
  318. $noticeService->inviteToRefund($pair->uid, $pair->id, $dis_score);
  319. } catch (\Exception $e) {
  320. }
  321. } catch (\Exception $exception) {
  322. $data['state'] = 2;
  323. $data['dis_score'] = 0;
  324. }
  325. } else {
  326. throw new AlertException('如有疑问,可以联系客服,请将问题反馈给我们', 505);
  327. }
  328. } else {
  329. // 无效的邀请
  330. $data['state'] = 2;
  331. $data['dis_score'] = 0;
  332. }
  333. $inviteModel = new InviteModel();
  334. $invite = $inviteModel->fill($data);
  335. if ($invite->save()) {
  336. $pairUser = UserModel::find($pair->uid);
  337. return array(
  338. 'state' => $data['state'],
  339. 'dis_score' => $data['dis_score'],
  340. 'comment' => $data['comment'],
  341. 'user' => [
  342. 'nickname' => $pairUser->nickname,
  343. ],
  344. );
  345. } else {
  346. throw new AlertException('如有疑问,可以联系客服,请将问题反馈给我们', 505);
  347. }
  348. }
  349. }