当前位置: 首页 > 工具软件 > Laravel-Phone > 使用案例 >

Laravel-小程序 买票之详情和购买

尉迟韬
2023-12-01
<?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 listShow(Request $request)
    {
        $page = $request->get('page');
        $data = Concert::with('hall')->paginate(6);

        return ['code'=>200,'msg'=>'ok','data'=>$data];
    }

    public function info(Request $request)
    {
        $id = $request->get('id');
        $data = Concert::with('hall')->where('id',$id)->first();
        return ['code'=>200,'msg'=>'ok','data'=>$data];
    }

    //查看票的剩余量
    public function  buy(Request $request)
    {

        $id = $request->get('id');// 演唱会列表id


        $sid = $request->get('sid');//厅id

        $data = $this->getTicketNum($id,$sid);

        if(!empty($data)){
            $num = count($data);
            return ['code'=>200,'msg'=>'ok','data'=>$num];
        }
        return ['code'=>40005,'msg'=>'没有票了','data'=>''];
    }


    //获取票的数量
    public function getTicketNum($id,$sid)
    {
        $res = Hall::with('seat')->where('sid',$sid)->get()->toArray();


        $arr = $res[0]['seat'];
        $data=[];
        foreach ($arr as $k=>$v){
            if($v['status']==1){
                array_push($data,$v['seat']);
            }
        }
        return $data;//返回数组
    }

    /*
     * 通过订单获取到座位
     * **/
    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 style="flex: none;">

<view>标题:{{data.title}}</view>
<view><image src="{{data.img}}"></image></view>
<view>内容:</view>
<view>{{data.text}}</view>
<view>场地:{{data.hall.hall}}</view>
<view>价格:{{data.price}}元</view>

<view class="shareBox">
    <button type="primary" open-type="contact">在线咨询</button>
  </view>

<text bindtap="map">地图</text>
<button bindtap="btn" data-id="{{data.id}}" data-sid="{{data.hall.sid}}" style="position: absolute; bottom: 40px; left: 100px;">购买</button>
</view>
// pages/info/info.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id
    this.info(id);

  },
  info(id){
    wx.request({
      url: 'http://www.study.com/api/info',
      data:{id},
      success:res=>{
          if(res.data.code==200){
            let data = res.data.data
            this.setData({
              data
            })
          }
      }
  })
  },
  btn(e){
    let phone = wx.getStorageSync('phone')
    let token = wx.getStorageSync('token')

    if(phone=="" || token == ""){
      wx.navigateTo({
        url: '/pages/indexs/indexs',
      })
      return
    }
    let id = e.currentTarget.dataset.id //演唱会id
    let sid = e.currentTarget.dataset.sid //厅id
    wx.request({
      url: 'http://www.study.com/api/buy',
      data:{id,sid},
      header:{token},
      success:res=>{
       if(res.data.code==200){
         let num = res.data.data
        wx.navigateTo({
          url: '/pages/buy/buy?id='+id+'&sid='+sid+'&num='+num,
        })
       }else{
         wx.showToast({
           title: res.data.msg,
         })
       }
      }
    })
  
  },
  map(){
    wx.chooseLocation({
      latitude: 0,
    })
  }
 


})

 类似资料: