123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace App\Http\Controllers\PartnerPay;
- use App\Http\Controllers\Controller;
- use App\Http\Controllers\Core\Auth;
- use App\Models\ConfessModel;
- use App\Models\User\UserModel;
- use App\Models\NoticeModel;
- use App\Models\PartnerModel;
- use Illuminate\Http\Request;
- use App\Models\OrderModel;
- use App\Models\Fpdx\MediaModel;
- class Confess extends Controller
- {
- /**
- * 计算表白费用
- * @param int $uid
- * @param int $partner_id
- * @param int $send_msg
- * @param int $receive_msg
- * @return int
- * @throws \ApiException
- */
- private function calcFee(int $uid, int $partner_id, int $send_msg = 1, int $receive_msg = 1)
- {
- $fee = 100;
- if (
- ConfessModel::where([
- ['uid', $uid],
- ['partner_id', $partner_id],
- ['receive_state', -1]
- ])->exists()
- ) {
- throw new \ApiException("你被对方拒绝了", 101);
- }
- if (
- ConfessModel::where([
- ['uid', $uid],
- ['state', 1],
- ['partner_id', $partner_id]
- ])->count() > 2
- ) {
- throw new \ApiException('一张卡片只能表白3次', 101);
- }
- if ($send_msg == 1) {
- $fee += 10;
- }
- if ($receive_msg == 1) {
- $fee += 10;
- }
- return $fee;
- }
- /**
- * 发起表白
- * @param Request $request
- * @return array
- * @throws \ApiException
- * @throws \Tymon\JWTAuth\Exceptions\JWTException
- */
- public function store(Request $request)
- {
- $this->validate($request, [
- 'partner_id' => 'required',
- 'nickname' => 'required',
- 'media_id' => 'required',
- 'content' => 'required',
- 'send_self' => 'required',
- 'send_msg' => 'required',
- 'receive_msg' => 'required'
- ]);
- $partner_id = $request->input('partner_id');
- $uid = Auth::auth();
- $fee = $this->calcFee($uid, $partner_id, $request->input('send_msg'), $request->input('receive_msg'));
- $confessModel = new ConfessModel();
- $confess = $confessModel->fill([
- 'nickname' => $request->input('nickname'),
- 'media_id' => $request->input('media_id'),
- 'content' => $request->input('content'),
- 'send_self' => $request->input('send_self'),
- 'send_msg' => $request->input('send_msg'),
- 'receive_msg' => $request->input('receive_msg'),
- 'state' => 0,
- 'create_at' => time()
- ]);
- if (!$confess->save()) {
- throw new \ApiException("数据库异常", 505);
- }
- if ($fee > 0) { # 下单
- $orderObj = new Order();
- $order_id = $orderObj->unifyByConfess($fee, $confess->id);
- }
- return array(
- 'code' => 200,
- 'message' => 'success',
- 'data' => [
- 'order_id' => $order_id,
- 'confess_id' => $confess->id
- ],
- );
- }
- /**
- * 表白支付结果处理
- * @param OrderModel $order
- * @return bool
- * @throws \Exception
- */
- public function payment(OrderModel $order)
- {
- $ticketController = new Ticket();
- \DB::beginTransaction();
- try {
- // 获取表白记录信息
- $confess = ConfessModel::find($order->type_id);
- // 让表白生效
- $confess->state = 1;
- $confess->save();
- // 获取被表白卡片信息
- $partner = PartnerModel::find($confess->partner_id);
- // 获取被表白用户信息
- $user = UserModel::find($partner->uid);
- $invite = new User();
- $invite->task($user->uid, 5);
- // 表白者-加入通知
- NoticeModel::create([
- 'uid' => $order->uid,
- 'title' => '你的表白已发送成功',
- 'content' => '你的表白已通过系统信息通知到对方,如有回应我们将第一时间通知你反馈结果',
- 'type' => 10,
- 'type_id' => $confess->id,
- ]);
- // 被表白用户-加入通知
- NoticeModel::create([
- 'uid' => $user->uid,
- 'title' => "你收到一条来自{$confess->nickname}的表白",
- 'content' => $confess->content,
- 'type' => 6,
- 'type_id' => $confess->id,
- ]);
- // 发送短信
- $media = MediaModel::mediaInfo($partner->media_id, 'msy');
- $params = array(
- 'username' => ", 系统中有人喜欢了你上架的室友,并发送了表白信息",
- 'place' => "微信公众号「{$media->public_name}」中回复\"{$media->keyword[0]}bbtx\"即",
- 'type' => '表白'
- );
- $params['username'] = ",系统中有人喜欢了你,并向你发了表白信息";
- \DB::commit();
- return true;
- } catch (\Exception $e) {
- \DB::rollBack();
- return false;
- }
- }
- }
|