当前位置: 首页 > 工具软件 > 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 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 refund(Request $request)
    {
//        dd($request->all());
        $id= $request->get('id');

        $ticket = Ticket::where(['id'=>$id])->first();
//        dd($ticket);
        //通过cid找到钱
        $concertMoney = Concert::where('id',$ticket->cid)->first();//票钱
//        dd($concertMoney);
        $user = User::where('id',$ticket->uid)->first();
        DB::beginTransaction();
        try {
            $refund = $user->money + $concertMoney->price;
            User::where('id',$ticket->uid)->update(['money'=>$refund]);
            Ticket::where(['id'=>$id])->update(['status'=>3]);//订单退票
            $this->setSeatStatus($ticket,4);
        }catch (\Exception $e){
            DB::rollBack();
            return ['code'=>44444,'msg'=>$e->getMessage()];
        }
        return ['code'=>200,'msg'=>'退款成功','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]);
    }

}

<button bindtap="order">我的订单</button>



// pages/mine/mine.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  order(){
   wx.navigateTo({
     url: '/pages/myOrder/myOrder',
   })
  },
 
  
})

<view>
  <label>我的订单</label>
  <view wx:for="{{data}}" wx:key="id">
    <view style="height: 150px; margin-top: 10px;">
      <view>姓名:{{item.nickname}}</view>
      <view>电话:{{item.phone}}</view>
      <view>订单号:{{item.ticket}}</view>
      <view wx:if="{{item.status==4}}">
        <view>订单状态:待支付</view>
      </view>

      <view wx:if="{{item.status==2}}">
        <view bindtap="refund" data-id="{{item.id}}"> 订单状态:已支付</view>
      </view>

      <view wx:if="{{item.status==3}}">
        <view bindtap="refund" data-id="{{item.id}}"> 订单状态:已退款</view>
      </view>
      <view>购买时间{{item.created_at}}</view>

    </view>
  </view>
</view>
// pages/myOrder/myOrder.js
Page({

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.myorder()
  },
  myorder(){
    let phone = wx.getStorageSync('phone')
    let token = wx.getStorageSync('token')
    wx.request({
      url: 'http://www.study.com/api/myOrder',
      data:{phone},
      header:{
        token
      },
      success:res=>{
          if(res.data.code==200){
            let data = res.data.res
            this.setData({
              data
            })
          }
      }
  })
  },
  refund(e){
   let id = e.currentTarget.dataset.id
   let token = wx.getStorageSync('token')
   var that =this
   wx.request({
    url: 'http://www.study.com/api/refund',
    data:{id},
    header:{token},
      success:res=>{
        if(res.data.code==200){
          wx.showToast({
            title: res.data.msg,
          })
        }
        that.onLoad()
      }
  })
   
}
  


})

 类似资料: