Quellcode durchsuchen

抽獎功能完成(缺動畫)

maa3606 vor 2 Jahren
Ursprung
Commit
87b553027b

+ 7 - 2
app/Http/Controllers/CheckInController.php

@@ -19,15 +19,20 @@ class CheckInController extends Controller
         //
     }
 
+    public function index_by_activity(UpdateCheckInRequest $request)
+    {
+        return CheckIn::where('activity_id', $request->activityId)->get();
+    }
+
     public function draw_user_when_check_in(StoreCheckInRequest $request)
     {
         return CheckIn::where('is_checked_in', 'true')->get()->random($request->number);
     }
 
-    public function draw_user_with_place(StoreCheckInRequest $request)
+    public function draw_user_by_region(StoreCheckInRequest $request)
     {
         return CheckIn::where('is_checked_in', 'true')
-            ->whereTime('check_ins.updated_at', '<=', '2023-02-08 08:23:42')
+            ->whereTime('check_ins.updated_at', '<=', $request->deadline)
             ->whereIn('region', $request->region)
             ->get()
             ->random($request->number);

+ 5 - 0
app/Http/Controllers/PrizeController.php

@@ -18,6 +18,11 @@ class PrizeController extends Controller
         return Prize::all();
     }
 
+    public function index_by_activity(UpdatePrizeRequest $request)
+    {
+        return Prize::where('activity_id', $request->activityId)->get();
+    }
+
     public function drawable_prizes()
     {
         return Prize::where('count', '>', 0)->get();

+ 11 - 1
app/Http/Controllers/RecipientsController.php

@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
 use App\Models\Recipients;
 use App\Http\Requests\StoreRecipientsRequest;
 use App\Http\Requests\UpdateRecipientsRequest;
+use App\Models\Prize;
 
 class RecipientsController extends Controller
 {
@@ -17,7 +18,16 @@ class RecipientsController extends Controller
     {
         return Recipients::all();
     }
-    
+
+    public function index_by_activity(UpdateRecipientsRequest $request)
+    {
+        return Recipients::join('prizes', 'recipients.prize_id', '=', 'prizes.id')
+            ->select('user_id', 'prizes.activity_id', 'prize_id', 'recipients.created_at')
+            ->where('prizes.activity_id', $request->activityId)
+            ->orderBy('recipients.created_at', 'DESC')
+            ->get();
+    }
+
     /**
      * Show the form for creating a new resource.
      *

+ 56 - 29
resources/js/src/views/Draw.vue

@@ -2,10 +2,9 @@
   <div>
     <b-row class="prize-list-wrapper">
       <b-col md="6">
-        <b-card title="獎項列表">
-          <b-card-text>所有獎項,點擊獎項以抽取</b-card-text>
+        <b-card title="獎項列表" style="height:75vh;">
           <b-table responsive hover :items="getPrizeList(prizeList)" bordered :fields="fields" @row-clicked="click"
-            v-b-modal.modal-draw-error>
+            v-b-modal.modal-center style="max-height: 65vh; overflow: auto;">
             <template #cell(ICON)="data" class="text-center">
               <div class="text-center">
                 <feather-icon :icon="data.value" />
@@ -14,25 +13,28 @@
           </b-table>
         </b-card>
       </b-col>
-      <b-modal id="modal-center" centered title="抽獎" cancel-variant="outline-secondary" cancel-title="Close"
-        ok-title="Accept" @ok="test">
-        <label>獎品:</label>
-        <b-form-input v-model="prize" :disabled=true />
-        <br>
-        <label>地區: </label>
-        <v-select multiple v-model="placeSelected" :options="placeOption" />
-        <br>
-        <label>數量: </label>
-        <b-form-spinbutton v-model="drawNumber" min="1" :max=drawMax />
-      </b-modal>
 
-      <b-modal id="modal-draw-error" centered title="抽獎" ok-title="Close">
-        <label>獎品數量以抽完</label>
+      <b-modal id="modal-center" centered title="抽獎" cancel-variant="outline-secondary" cancel-title="Close"
+        :ok-only="draw_zero" ok-title="Accept" @ok="draw">
+        <div v-if="draw_zero">
+          <label>獎品數量以抽完</label>
+        </div>
+        <div v-else>
+          <label>獎品:</label>
+          <b-form-input v-model="prize" :disabled=true />
+          <br>
+          <label>地區: </label>
+          <v-select multiple v-model="region" :options="regionOption" />
+          <br>
+          <label>數量: </label>
+          <b-form-spinbutton v-model="drawNumber" min="1" :max=drawMax />
+        </div>
       </b-modal>
 
       <b-col md="6">
-        <b-card title="抽獎紀錄">
-          <b-table responsive :items="getRecipientsList(recipientsList)" bordered>
+        <b-card title="抽獎紀錄" style="height:75vh;">
+          <b-table responsive :items="getRecipientsList(recipientsList)" bordered
+            style="max-height: 65vh; overflow: auto;">
             <template #cell(ICON)="data" class="text-center">
               <div class="text-center">
                 <feather-icon :icon="data.value" />
@@ -68,13 +70,15 @@ export default {
   },
   data() {
     return {
+      activity: [],
       userList: [],
+      userIdList: [],
       prizeList: [],
       recipientsList: [],
 
       fields: ['獎項', '數量'],
-      placeOption: ['北區', '中區', '南區', '來賓'],
-      placeSelected: ['北區', '中區', '南區', '來賓'],
+      regionOption: ['北區', '中區', '南區', '來賓'],
+      region: ['北區', '中區', '南區', '來賓'],
       drawMax: 1,
       drawNumber: 1,
 
@@ -84,18 +88,34 @@ export default {
     }
   },
   created() {
-    useJwt.postData('/api/user/index').then(res => {
+    this.activity = this.$route.params.activity;
+    console.log(this.activity);
+
+    useJwt.postData('/api/check_in/index_by_activity', { activityId: this.activity.id }).then(res => {
       this.userList = res.data;
     });
 
-    useJwt.postData('/api/prize/index').then(res => {
+    useJwt.postData('/api/user/index').then(res => {
+      this.userIdList = res.data;
+    });
+
+    useJwt.postData('/api/prize/index_by_activity', { activityId: this.activity.id }).then(res => {
       this.prizeList = res.data;
     });
 
-    useJwt.postData('/api/recipients/index').then(res => {
+    useJwt.postData('/api/recipients/index_by_activity', { activityId: this.activity.id }).then(res => {
       this.recipientsList = res.data;
     });
   },
+  computed: {
+    draw_zero() {
+      if (this.drawMax == 0) {
+        return true;
+      } else {
+        return false;
+      }
+    }
+  },
   methods: {
     getPrizeList(prizeList) {
       var output = [];
@@ -116,7 +136,8 @@ export default {
     getRecipientsList(recipientsList) {
       var output = [];
       recipientsList.forEach(element => {
-        output.push({ '獎項': this.getPrizeName(element.prize_id), '獲獎人': this.userList[element.user_id], '時間': element.created_at });
+        console.log(element.created_at);
+        output.push({ '獎項': this.getPrizeName(element.prize_id), '獲獎人': this.userIdList[element.user_id], '時間': this.dateFormat(element.created_at) });
       });
       return output;
     },
@@ -127,7 +148,7 @@ export default {
       this.drawNumber = 1;
     },
     draw() {
-      useJwt.postData('/api/check_in/drawP', { number: this.drawNumber, region: this.placeSelected }).then(res => {
+      useJwt.postData('/api/check_in/draw_by_region', { deadline: this.activity.deadline, region: this.region, number: this.drawNumber }).then(res => {
         res.data.forEach(element => {
           console.log(element);
           useJwt.postData('/api/prize/draw', { prizeId: this.prizeId }).then(p => {
@@ -144,15 +165,21 @@ export default {
       });
     },
     updateDataset() {
-      useJwt.postData('/api/prize/index').then(res => {
+      useJwt.postData('/api/prize/index_by_activity', { activityId: this.activity.id }).then(res => {
         this.prizeList = res.data;
       });
-      useJwt.postData('/api/recipients/index').then(res => {
+      useJwt.postData('/api/recipients/index_by_activity', { activityId: this.activity.id }).then(res => {
         this.recipientsList = res.data;
       });
     },
-    test() {
-      console.log(this.placeSelected);
+    dateFormat(date) {
+      var d = new Date(date);
+      return d.getFullYear()
+        + '/' + (d.getMonth() + 1).toString().padStart(2, '0')
+        + '/' + d.getDay().toString().padStart(2, '0')
+        + '/' + d.getHours().toString().padStart(2, '0')
+        + ':' + d.getMinutes().toString().padStart(2, '0')
+        + ':' + d.getSeconds().toString().padStart(2, '0');
     },
   },
 }

+ 4 - 1
routes/api.php

@@ -99,7 +99,7 @@ Route::middleware(['auth:sanctum', 'abilities:Admin'])->group(function () {
     Route::post('/check_in/update', [CheckInController::class, 'update']);
     Route::post('/check_in/check_out', [CheckInController::class, 'check_out']);
     Route::post('/check_in/draw', [CheckInController::class, 'draw_user_when_check_in']);
-    Route::post('/check_in/drawP', [CheckInController::class, 'draw_user_with_place']);
+    Route::post('/check_in/draw_by_region', [CheckInController::class, 'draw_user_by_region']);
 
     // Prize
     Route::post('/prize/draw', [PrizeController::class, 'draw']);
@@ -109,6 +109,9 @@ Route::middleware(['auth:sanctum', 'abilities:Admin'])->group(function () {
 });
 Route::post('/user/index', [UserController::class, 'index']);
 Route::post('/prize/index', [PrizeController::class, 'index']);
+Route::post('/check_in/index_by_activity', [CheckInController::class, 'index_by_activity']);
+Route::post('/prize/index_by_activity', [PrizeController::class, 'index_by_activity']);
 Route::post('/recipients/index', [RecipientsController::class, 'index']);
+Route::post('/recipients/index_by_activity', [RecipientsController::class, 'index_by_activity']);
 
 Route::post('/prize/drawable_prizes', [PrizeController::class, 'drawable_prizes']);