<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Models\ych\Concert;
use App\Models\ych\Hall;
use App\Models\ych\Seat;
use App\Models\ych\Ticket;
use App\Models\ych\User;
use App\Service\SendCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class ConcertController extends Controller
{
public function order(Request $request)
{
//生成订单了,根据座位和时间戳的拼接生成唯一的票号。
$id = $request->get('id');// ych列表id
$uid = $request['uid'];
$sid = $request->get('sid');//厅id
$data = $this->getTicketNum($id,$sid);
$hall = Hall::where('id',$sid)->first();//大厅
$seat = array_rand($data,1);//随机取出一个
$seat = $data[$seat];
DB::beginTransaction();
try {
Seat::where(['id'=>$sid,'seat'=>$seat])->update(['status'=>2]);//支付中的状态。
$ticket = time().$id.$hall->hall.$seat;//生成唯一的票子号
$one = Ticket::create(['ticket'=>$ticket,'cid'=>$id,'status'=>4,'uid'=>$uid]);
//redis中存储订单的过期时间。如果时间到期了订单还未支付改变状态。就原路返回。
DB::commit();
}catch (\Exception $e){
DB::rollBack();
return ['code'=>44444,'msg'=>$e->getMessage()];
}
$conceet = Concert::where('id',$id)->first();//电影的详细信息
$res['one'] = $one;
$res['conceet'] = $conceet;
$res['ticket'] = $ticket;;
return ['code'=>'200','msg'=>'生成订单','res'=>$res];
}
//我的订单
public function myOrder(Request $request)
{
$phone = $request->get('phone');
$admin = User::where('phone',$phone)->first();
$res = Ticket::where('uid',$admin->id)->get()->toArray();
foreach ($res as $k=>$v){
$res[$k]['nickname']=$admin->username;
$res[$k]['phone']=$admin->phone;
}
return ['code'=>'200','msg'=>'订单','res'=>$res];
}
public function pay(Request $request)
{
$id = $request['uid'];
// dd($id);
$tid= $request->get('tid');
$ticket = Ticket::where('id',$tid)->first();//根据cid在查询一边
$money = Ticket::getMoney($ticket->cid);
$user = User::where('id',$id)->first();
if($user->money < $money->price){
return ['code'=>41000,'msg'=>'您的余额不足','data'=>''];
}
DB::beginTransaction();
try {
$payAfterMoney = $user->money-$money->price;
User::where('id',$id)->update(['money'=>$payAfterMoney]);
// Ticket::where('id',$tid)->create(['status'=>2,'uid'=>$id]);
$this->setSeatStatus($ticket,3);
$ticket->update(['status'=>2]);
DB::commit();
$data['phone'] = $user->phone;
$data['ticket'] = $ticket->ticket;
// $res = json_encode($data);
ProcessPodcast::dispatch($data);
}catch (\Exception $e){
DB::rollBack();
return ['code'=>44444,'msg'=>$e->getMessage()];
}
return response()->json(['code'=>200,'msg'=>'支付成功','data'=>''],200);
}
/*
* 通过订单获取到座位
* **/
public function setSeatStatus($ticket,$status)
{
$hall = mb_substr($ticket->ticket,11,2);//大厅
$seat = mb_substr($ticket->ticket,-2,2);//座位号
$sid = Hall::where('hall',$hall)->first();
Seat::where(['id'=>$sid->sid,'seat'=>$seat])->update(['status'=>$status]);
}
}
<view>
<view>
<view>标题:{{data.conceet.title}}</view>
<view>价格:{{data.conceet.price}}</view>
</view>
<view> 订单号:{{data['ticket']}} </view>
<button bindtap="pay" data-id="{{data.one.id}}">支付</button>
</view>
// pages/buy/buy.js
Page({
/**
* 页面的初始数据
*/
data: {
data:[]
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
let id = options.id
let sid = options.sid //厅id
this.order(id,sid)
},
order(id,sid){
let token = wx.getStorageSync('token')
wx.request({
url: 'http://www.study.com/api/order',
data:{id,sid},
header:{
token
},
success:res=>{
if(res.data.code==200){
let data = res.data.res
console.log(data);
this.setData({
data
})
}
}
})
},
pay(e){
let tid = e.currentTarget.dataset.id
let token = wx.getStorageSync('token')
wx.request({
url: 'http://www.study.com/api/pay',
data:{tid},
header:{token},
success:res=>{
if(res.data.code==200){
wx.showToast({
title: res.data.msg,
icon: 'success',
duration: 2000,
success:function(){
setTimeout(function () {
//要延时执行的代码
wx.navigateTo({
url: '/pages/myOrder/myOrder'
})
}, 2000) //延迟时间
}
})
}
}
})
}
})