Confess.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Core\Auth;
  4. use App\Models\ConfessModel;
  5. use App\Models\User\UserModel;
  6. use App\Models\NoticeModel;
  7. use App\Models\PartnerModel;
  8. use App\Services\User\TaskService;
  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. * @deprecated 已弃用
  23. */
  24. private function calcFee(int $uid, int $partner_id, int $send_msg = 1, int $receive_msg = 1)
  25. {
  26. $fee = 100;
  27. if (
  28. ConfessModel::where([
  29. ['uid', $uid],
  30. ['partner_id', $partner_id],
  31. ['receive_state', -1]
  32. ])->exists()
  33. ) {
  34. throw new \ApiException("你被对方拒绝了", 101);
  35. }
  36. if (
  37. ConfessModel::where([
  38. ['uid', $uid],
  39. ['state', 1],
  40. ['partner_id', $partner_id]
  41. ])->count() > 2
  42. ) {
  43. throw new \ApiException('一张卡片只能表白3次', 101);
  44. }
  45. if ($send_msg == 1) {
  46. $fee += 10;
  47. }
  48. if ($receive_msg == 1) {
  49. $fee += 10;
  50. }
  51. return $fee;
  52. }
  53. /**
  54. * 发起表白
  55. * @param Request $request
  56. * @return array
  57. * @throws \ApiException
  58. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  59. * @deprecated
  60. */
  61. public function store(Request $request)
  62. {
  63. $this->validate($request, [
  64. 'partner_id' => 'required',
  65. 'nickname' => 'required',
  66. 'media_id' => 'required',
  67. 'content' => 'required',
  68. 'send_self' => 'required',
  69. 'send_msg' => 'required',
  70. 'receive_msg' => 'required'
  71. ]);
  72. $partner_id = $request->input('partner_id');
  73. $uid = Auth::auth();
  74. $fee = $this->calcFee($uid, $partner_id, $request->input('send_msg'), $request->input('receive_msg'));
  75. $confessModel = new ConfessModel();
  76. $confess = $confessModel->fill([
  77. 'nickname' => $request->input('nickname'),
  78. 'media_id' => $request->input('media_id'),
  79. 'content' => $request->input('content'),
  80. 'send_self' => $request->input('send_self'),
  81. 'send_msg' => $request->input('send_msg'),
  82. 'receive_msg' => $request->input('receive_msg'),
  83. 'state' => 0,
  84. 'create_at' => time()
  85. ]);
  86. if (!$confess->save()) {
  87. throw new \ApiException("数据库异常", 505);
  88. }
  89. if ($fee > 0) { # 下单
  90. $orderObj = new Order();
  91. $order_id = $orderObj->unifyByConfess($fee, $confess->id, $request);
  92. }
  93. return array(
  94. 'code' => 200,
  95. 'message' => 'success',
  96. 'data' => [
  97. 'order_id' => $order_id,
  98. 'confess_id' => $confess->id
  99. ],
  100. );
  101. }
  102. /**
  103. * 表白支付结果处理
  104. * @param OrderModel $order
  105. * @return bool
  106. * @throws \Exception
  107. * @deprecated 已弃用
  108. */
  109. public function payment(OrderModel $order)
  110. {
  111. \DB::beginTransaction();
  112. try {
  113. // 获取表白记录信息
  114. $confess = ConfessModel::find($order->type_id);
  115. // 让表白生效
  116. $confess->state = 1;
  117. $confess->save();
  118. // 获取被表白卡片信息
  119. $partner = PartnerModel::find($confess->partner_id);
  120. // 获取被表白用户信息
  121. $user = UserModel::find($partner->uid);
  122. $ts = new TaskService();
  123. $ts->task($user->uid, 5);
  124. \DB::commit();
  125. return true;
  126. } catch (\Exception $e) {
  127. \DB::rollBack();
  128. return false;
  129. }
  130. }
  131. }