SharePairManager.php 13 KB

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