Confess.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Http\Controllers\PartnerPay;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Controllers\Core\Auth;
  5. use App\Models\ConfessModel;
  6. use App\Models\User\UserModel;
  7. use App\Models\NoticeModel;
  8. use App\Models\PartnerModel;
  9. use Illuminate\Http\Request;
  10. use App\Models\OrderModel;
  11. use App\Models\Fpdx\MediaModel;
  12. class Confess extends Controller
  13. {
  14. /**
  15. * 计算表白费用
  16. * @param int $uid
  17. * @param int $partner_id
  18. * @param int $send_msg
  19. * @param int $receive_msg
  20. * @return int
  21. * @throws \ApiException
  22. */
  23. private function calcFee(int $uid, int $partner_id, int $send_msg = 1, int $receive_msg = 1)
  24. {
  25. $fee = 100;
  26. if (
  27. ConfessModel::where([
  28. ['uid', $uid],
  29. ['partner_id', $partner_id],
  30. ['receive_state', -1]
  31. ])->exists()
  32. ) {
  33. throw new \ApiException("你被对方拒绝了", 101);
  34. }
  35. if (
  36. ConfessModel::where([
  37. ['uid', $uid],
  38. ['state', 1],
  39. ['partner_id', $partner_id]
  40. ])->count() > 2
  41. ) {
  42. throw new \ApiException('一张卡片只能表白3次', 101);
  43. }
  44. if ($send_msg == 1) {
  45. $fee += 10;
  46. }
  47. if ($receive_msg == 1) {
  48. $fee += 10;
  49. }
  50. return $fee;
  51. }
  52. /**
  53. * 发起表白
  54. * @param Request $request
  55. * @return array
  56. * @throws \ApiException
  57. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  58. */
  59. public function store(Request $request)
  60. {
  61. $this->validate($request, [
  62. 'partner_id' => 'required',
  63. 'nickname' => 'required',
  64. 'media_id' => 'required',
  65. 'content' => 'required',
  66. 'send_self' => 'required',
  67. 'send_msg' => 'required',
  68. 'receive_msg' => 'required'
  69. ]);
  70. $partner_id = $request->input('partner_id');
  71. $uid = Auth::auth();
  72. $fee = $this->calcFee($uid, $partner_id, $request->input('send_msg'), $request->input('receive_msg'));
  73. $confessModel = new ConfessModel();
  74. $confess = $confessModel->fill([
  75. 'nickname' => $request->input('nickname'),
  76. 'media_id' => $request->input('media_id'),
  77. 'content' => $request->input('content'),
  78. 'send_self' => $request->input('send_self'),
  79. 'send_msg' => $request->input('send_msg'),
  80. 'receive_msg' => $request->input('receive_msg'),
  81. 'state' => 0,
  82. 'create_at' => time()
  83. ]);
  84. if (!$confess->save()) {
  85. throw new \ApiException("数据库异常", 505);
  86. }
  87. if ($fee > 0) { # 下单
  88. $orderObj = new Order();
  89. $order_id = $orderObj->unifyByConfess($fee, $confess->id);
  90. }
  91. return array(
  92. 'code' => 200,
  93. 'message' => 'success',
  94. 'data' => [
  95. 'order_id' => $order_id,
  96. 'confess_id' => $confess->id
  97. ],
  98. );
  99. }
  100. /**
  101. * 表白支付结果处理
  102. * @param OrderModel $order
  103. * @return bool
  104. * @throws \Exception
  105. */
  106. public function payment(OrderModel $order)
  107. {
  108. $ticketController = new Ticket();
  109. \DB::beginTransaction();
  110. try {
  111. // 获取表白记录信息
  112. $confess = ConfessModel::find($order->type_id);
  113. // 让表白生效
  114. $confess->state = 1;
  115. $confess->save();
  116. // 获取被表白卡片信息
  117. $partner = PartnerModel::find($confess->partner_id);
  118. // 获取被表白用户信息
  119. $user = UserModel::find($partner->uid);
  120. $invite = new User();
  121. $invite->task($user->uid, 5);
  122. // 表白者-加入通知
  123. NoticeModel::create([
  124. 'uid' => $order->uid,
  125. 'title' => '你的表白已发送成功',
  126. 'content' => '你的表白已通过系统信息通知到对方,如有回应我们将第一时间通知你反馈结果',
  127. 'type' => 10,
  128. 'type_id' => $confess->id,
  129. ]);
  130. // 被表白用户-加入通知
  131. NoticeModel::create([
  132. 'uid' => $user->uid,
  133. 'title' => "你收到一条来自{$confess->nickname}的表白",
  134. 'content' => $confess->content,
  135. 'type' => 6,
  136. 'type_id' => $confess->id,
  137. ]);
  138. // 发送短信
  139. $media = MediaModel::mediaInfo($partner->media_id, 'msy');
  140. $params = array(
  141. 'username' => ", 系统中有人喜欢了你上架的室友,并发送了表白信息",
  142. 'place' => "微信公众号「{$media->public_name}」中回复\"{$media->keyword[0]}bbtx\"即",
  143. 'type' => '表白'
  144. );
  145. $params['username'] = ",系统中有人喜欢了你,并向你发了表白信息";
  146. \DB::commit();
  147. return true;
  148. } catch (\Exception $e) {
  149. \DB::rollBack();
  150. return false;
  151. }
  152. }
  153. }