Browse Source

Merge branch 'master' of http://39.98.194.76:3000/txct/wanyue_app

gorden 4 months ago
parent
commit
0071b8bfdd

+ 60 - 12
app/admin/controller/card/CardController.php

@@ -9,48 +9,96 @@
 
 namespace app\admin\controller\card;
 
+use app\admin\service\card\CardService;
+use app\admin\validate\card\CardValidate;
+use support\Request;
+use Tinywan\Jwt\JwtToken;
+
 class CardController
 {
 
     /**
-     * Notes: 卡列表
+     * Notes: 储值卡列表
      * User: ZhouBenXu
      * DateTime: 2024/6/28 上午10:42
      */
-    public function list(){
-
+    public function list(Request $request)
+    {
+        $params = $request->get();
+        $params['page'] = $params['page'] ?? 1;
+        $params['pageSize'] = $params['pageSize'] ?? 20;
+        $params['join_card_batch_user_id'] = JwtToken::getCurrentId();
+        // 验证器
+        $validate = new CardValidate();
+        if (!$validate->scene('list')->check($params)) {
+            return json_fail($validate->getError());
+        }
+        return CardService::getList($params);
     }
 
     /**
-     * Notes: 卡详情
+     * Notes: 储值卡详情
      * User: ZhouBenXu
      * DateTime: 2024/6/28 上午10:43
      */
-    public function info(){
-
+    public function info(Request $request)
+    {
+        $params = $request->get();
+        // 验证器
+        $validate = new CardValidate();
+        if (!$validate->scene('info')->check($params)) {
+            return json_fail($validate->getError());
+        }
+        return CardService::getInfo($params['card_id']);
     }
 
 
     /**
-     * Notes: 修改卡信息
+     * Notes: 修改储值卡信息
      * User: ZhouBenXu
      * DateTime: 2024/6/28 上午10:43
      */
-    public function save()
+    public function save(Request $request)
     {
-
-
+        $params = $request->post();
+        $params['join_card_batch_user_id'] = JwtToken::getCurrentId();
+        // 验证器
+        $validate = new CardValidate();
+        if (!$validate->scene('save')->check($params)) {
+            return json_fail($validate->getError());
+        }
+        // 校验有效期 0 永久 1增加 2 减少
+        return CardService::save($params);
     }
 
 
     /**
-     * Notes: 删除卡信息
+     * Notes: 删除储值卡信息
      * User: ZhouBenXu
      * DateTime: 2024/6/28 上午10:43
      */
-    public function delete()
+    public function delete(Request $request)
     {
+        // 验证器
+        $validate = new CardValidate();
+        if (!$validate->scene('delete')->check($request->post())) {
+            return json_fail($validate->getError());
+        }
+        $card_ids = $request->post('card_ids');
+        return CardService::delete($card_ids);
+    }
+
 
+    /**
+     * Notes: 储值卡导出
+     * User: ZhouBenXu
+     * DateTime: 2024/6/28 下午2:44
+     * @param Request $request
+     * @return \support\Response
+     */
+    public function exportCard(Request $request)
+    {
+        return CardService::exportCard($request);
     }
 
 

+ 25 - 4
app/admin/service/card/CardBatchService.php

@@ -92,11 +92,31 @@ class CardBatchService
             } elseif ($params['validtimes_status'] == 1) {
                 $model->card_batch_validtimes = bcadd($model->card_batch_validtimes, $params['card_batch_validtimes']); // 卡批次有效期 0=永久 天
             } elseif ($params['validtimes_status'] == 2) {
+                $end_date = (string)bcadd(strtotime($model->card_batch_addtimes), (bcsub($model->card_batch_validtimes, $params['card_batch_validtimes']) * 86400));
+                if ($end_date <= time()) {
+                    return json_fail('卡批次有效期必须大于当前时间~');
+                }
                 $model->card_batch_validtimes = bcsub($model->card_batch_validtimes, $params['card_batch_validtimes']); // 卡批次有效期 0=永久 天
             }
             if ($model->save()) {
+                 // 修改卡批次下面的卡信息
+                // CardService::saveCardByCardBatchId($params['card_batch_id'], $params);
                 // 修改卡批次下面的卡信息
-                CardService::saveCardByCardBatchId($params['card_batch_id'], $params);
+                $model = new Card();
+                $data['card_name'] = $params['card_batch_name'];
+
+                $card_status = array_values($model::CARD_STATUS);
+                if (in_array($params['card_batch_status'], $card_status)) {
+                    $data['card_status'] = $params['card_batch_status']; // 卡批次状态
+                }
+                if ($params['validtimes_status'] == 0) { // 卡批次有效期 0 永久天  1 增加 2减少
+                    $data['card_validtimes'] = 0;
+                } elseif ($params['validtimes_status'] == 1) {
+                    $data['card_validtimes'] = Db::raw('card_validtimes+' . $params['card_batch_validtimes'] * 86400);
+                } elseif ($params['validtimes_status'] == 2) {
+                    $data['card_validtimes'] = Db::raw('card_validtimes-' . $params['card_batch_validtimes'] * 86400);;
+                }
+                $model->where('join_card_batch_id', $params['card_batch_id'])->update($data);
             }
             Db::commit();
         } catch (\PDOException $e) {
@@ -151,7 +171,6 @@ class CardBatchService
     }
 
 
-
     /**
      * Notes: 卡批次列表
      * User: ZhouBenXu
@@ -236,6 +255,7 @@ class CardBatchService
             $card_status_total = Db::select($sql);
             $card_status_total = array_column($card_status_total, null, 'join_card_batch_id');
             foreach ($rows as $key => &$value) {
+                $value['card_batch_status_value'] = $value['card_batch_status'];
                 $value['card_batch_status'] = array_flip($cardBatchModel::CARD_BATCH_STATUS)[$value['card_batch_status']];
                 !empty($value['card_batch_json']) && $value['card_batch_json'] = json_decode($value['card_batch_json'], true);
                 !empty($value['card_batch_extend_json']) && $value['card_batch_extend_json'] = json_decode($value['card_batch_extend_json'], true);
@@ -262,13 +282,14 @@ class CardBatchService
      */
     public static function getInfo($card_batch_id)
     {
-        $cardBatch = CardBatch::where('card_batch_id', $card_batch_id)->first()->toArray();
+        $cardBatch = CardBatch::where('card_batch_id', $card_batch_id)->first() ?? [];
         if (!empty($cardBatch)) {
+            $cardBatch['card_batch_status_value'] = $cardBatch['card_batch_status'];
             $cardBatch['card_batch_status'] = array_flip(CardBatch::CARD_BATCH_STATUS)[$cardBatch['card_batch_status']];
             !empty($cardBatch['card_batch_json']) && $cardBatch['card_batch_json'] = json_decode($cardBatch['card_batch_json'], true);
             !empty($cardBatch['card_batch_extend_json']) && $cardBatch['card_batch_extend_json'] = json_decode($cardBatch['card_batch_extend_json'], true);
         }
-        return json_success('',$cardBatch);
+        return json_success('', $cardBatch);
     }
 
 }

+ 248 - 3
app/admin/service/card/CardService.php

@@ -12,6 +12,7 @@ namespace app\admin\service\card;
 use support\Db;
 use support\exception\BusinessException;
 use app\model\Card;
+use support\Request;
 
 class CardService
 {
@@ -75,13 +76,14 @@ class CardService
         return json_success('success');
     }
 
+
     /**
      * Notes: 根据卡批次修改卡信息
      * User: ZhouBenXu
      * DateTime: 2024/6/27 下午5:03
      * @param $join_card_batch_id
      * @param $save
-     * @return \support\Response
+     * @return string|\support\Response|true
      */
     public static function saveCardByCardBatchId($join_card_batch_id, $save)
     {
@@ -100,10 +102,252 @@ class CardService
             } elseif ($save['validtimes_status'] == 1) {
                 $data['card_validtimes'] = Db::raw('card_validtimes+' . $save['card_batch_validtimes'] * 86400);
             } elseif ($save['validtimes_status'] == 2) {
-                $data['card_validtimes'] = Db::raw('card_validtimes-' . $save['card_batch_validtimes'] * 86400);
+                $data['card_validtimes'] = Db::raw('card_validtimes-' . $save['card_batch_validtimes'] * 86400);;
             }
             $model->where('join_card_batch_id', $join_card_batch_id)->update($data);
             Db::commit();
+        } catch (\PDOException $e) {
+            Db::rollBack();
+            dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
+            return false;
+        } catch (BusinessException $e) {
+            Db::rollBack();
+            dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
+            return $e->getMessage();
+        } catch (\Exception $e) {
+            Db::rollBack();
+            dump($e->getTrace());
+            return false;
+        }
+        _syslog("修改储值卡", "卡批次名称【" . $save['card_batch_name'] . "】");
+        return true;
+    }
+
+
+    /**
+     * Notes: 储值卡列表
+     * User: ZhouBenXu
+     * DateTime: 2024/6/28 上午11:36
+     * @param $params
+     * @return \support\Response
+     */
+    public static function getList($params)
+    {
+
+        $cardModel = new Card();
+        $page = $params['page'];
+        $page_size = $params['pageSize'];
+        $join_card_batch_id = $params['join_card_batch_id'] ?? "";
+        $join_card_category_id = $params['join_card_category_id'] ?? "";
+        $join_card_member_id = $params['join_card_member_id'] ?? "";
+        $card_status = !empty($params['card_status']) ? Card::CARD_STATUS[$params['card_status']] : "";
+        $card_category = $params['card_category'] ?? "";
+        $card_name = $params['card_name'] ?? "";
+
+        $rows = $cardModel::select('*')
+            ->when(!empty($card_name) && $card_name != '', function ($query) use ($card_name) {
+                $query->where('card_name', 'like', '%' . $card_name . '%');
+            })->when(!empty($card_status), function ($query) use ($card_status) {
+                $query->where('card_status', $card_status);
+            })->when(!empty($join_card_batch_id), function ($query) use ($join_card_batch_id) {
+                $query->where('join_card_batch_id', $join_card_batch_id);
+            })->when(!empty($join_card_category_id), function ($query) use ($join_card_category_id) {
+                $query->where('join_card_category_id', $join_card_category_id);
+            })->when(!empty($join_card_member_id), function ($query) use ($join_card_member_id) {
+                $query->where('join_card_member_id', $join_card_member_id);
+            })->when(!empty($card_category), function ($query) use ($card_category) {
+                $query->where('card_category', $card_category);
+            })
+            ->orderBy('card_id', 'DESC')
+            ->forPage($page, $page_size)
+            ->get()
+            ->toArray();
+
+        $total = $cardModel::select('card_id')
+            ->when(!empty($card_name) && $card_name != '', function ($query) use ($card_name) {
+                $query->where('card_name', 'like', '%' . $card_name . '%');
+            })->when(!empty($card_status), function ($query) use ($card_status) {
+                $query->where('card_status', $card_status);
+            })->when(!empty($join_card_batch_id), function ($query) use ($join_card_batch_id) {
+                $query->where('join_card_batch_id', $join_card_batch_id);
+            })->when(!empty($join_card_category_id), function ($query) use ($join_card_category_id) {
+                $query->where('join_card_category_id', $join_card_category_id);
+            })->when(!empty($join_card_member_id), function ($query) use ($join_card_member_id) {
+                $query->where('join_card_member_id', $join_card_member_id);
+            })->when(!empty($card_category), function ($query) use ($card_category) {
+                $query->where('card_category', $card_category);
+            })
+            ->count();
+        if (!empty($rows)) {
+            foreach ($rows as $key => &$value) {
+                $value['card_status_value'] = $value['card_status'];
+                $value['card_status'] = array_flip(Card::CARD_STATUS)[$value['card_status']];
+                !empty($value['card_json']) && $value['card_json'] = json_decode($value['card_json'], true);
+                !empty($value['card_assign_json']) && $value['card_assign_json'] = json_decode($value['card_assign_json'], true);
+                !empty($value['card_process_json']) && $value['card_process_json'] = json_decode($value['card_process_json'], true);
+                !empty($value['card_extend_json']) && $value['card_extend_json'] = json_decode($value['card_extend_json'], true);
+            }
+
+        }
+        return json_success('', compact('rows', 'page', 'page_size', 'total'));
+    }
+
+
+    /**
+     * Notes: 储值卡详情
+     * User: ZhouBenXu
+     * DateTime: 2024/6/28 上午11:41
+     * @param $card_id
+     * @return \support\Response
+     */
+    public static function getInfo($card_id)
+    {
+        $cardInfo = Card::where('card_id', $card_id)->first() ?? [];
+        if (!empty($cardInfo)) {
+            $cardInfo['card_status_value'] = $cardInfo['card_status'];
+            $cardInfo['card_status'] = array_flip(Card::CARD_STATUS)[$cardInfo['card_status']];
+            !empty($cardInfo['card_json']) && $cardInfo['card_json'] = json_decode($cardInfo['card_json'], true);
+            !empty($cardInfo['card_assign_json']) && $cardInfo['card_assign_json'] = json_decode($cardInfo['card_assign_json'], true);
+            !empty($cardInfo['card_process_json']) && $cardInfo['card_process_json'] = json_decode($cardInfo['card_process_json'], true);
+            !empty($cardInfo['card_extend_json']) && $cardInfo['card_extend_json'] = json_decode($cardInfo['card_extend_json'], true);
+        }
+        return json_success('', $cardInfo);
+    }
+
+    /**
+     * Notes: 储值卡导出数据
+     * User: ZhouBenXu
+     * DateTime: 2024/6/28 下午2:44
+     * @param Request $request
+     * @return \support\Response
+     */
+    public static function exportCard(Request $request)
+    {
+        $card_ids = $request->get('card_id', '');
+        $cardModel = new Card();
+        $list = $cardModel::select('*')->whereIn('card_id', $card_ids)->get()->toArray();
+        $exportData = [];
+        // 增加签名
+        if (!empty($list)) {
+            foreach ($list as $key => $value) {
+                $info['card_name'] = $value['card_name'];
+                $info['card_pass'] = $value['card_pass'];
+                $info['card_prefix'] = $value['card_prefix'];
+                $info['card_suffix'] = $value['card_suffix'];
+                $info['card_sort'] = $value['card_sort'];
+                $info['card_validtimes'] = $value['card_validtimes'];
+                // 生成签名
+                $card_no = $value['card_prefix'] . $value['card_sort'] . $value['card_suffix'];
+                $card_sign = self::generateSignature($info, $info['card_pass']);
+                $card_extend_json['action'] = "bind-card";
+                $card_extend_json['data'] = ["card_id" => $card_no, "card_sign" => $card_sign];
+                $info['card_no'] = $card_no;
+                $info['card_extend_json'] = $card_extend_json;
+                $exportData[] = $info;
+            }
+        }
+        return json_success('', $exportData);
+    }
+
+    /**
+     * Notes: 生成签名
+     * User: ZhouBenXu
+     * DateTime: 2024/6/28 下午2:32
+     * @param $data
+     * @param $secret
+     * @return string
+     */
+    public static function generateSignature($data, $secret)
+    {
+        // 将数组转换为字符串,并按照字母排序
+        $dataString = http_build_query($data, '', '&', PHP_QUERY_RFC3986);
+        // 使用HMAC-SHA256算法生成签名
+        $signature = hash_hmac('sha256', $dataString, $secret);
+        return $signature;
+    }
+
+
+    /**
+     * Notes: 验证签名
+     * User: ZhouBenXu
+     * DateTime: 2024/6/28 下午2:33
+     * @param $data
+     * @param $providedSignature
+     * @param $secret
+     * @return bool
+     */
+    public static function verifySignature($data, $providedSignature, $secret)
+    {
+        // 生成数据的签名
+        $generatedSignature = self::generateSignature($data, $secret);
+        // 比较生成的签名与提供的签名是否一致
+        return hash_equals($generatedSignature, $providedSignature);
+    }
+
+
+    /**
+     * Notes: 储值卡删除
+     * User: ZhouBenXu
+     * DateTime: 2024/6/28 下午2:58
+     * @param $card_ids
+     * @return \support\Response
+     */
+    public static function delete($card_ids)
+    {
+        if (!is_array($card_ids)) {
+            $card_ids = [$card_ids];
+        }
+        $card = Card::whereIn('card_id', $card_ids)->get()->toArray();
+        if (!$card) {
+            return json_fail("数据错误~");
+        }
+        // 是否有激活的卡片
+        if (Card::whereIn('card_id', $card_ids)->where('card_status', 'ACTIVED')->exists()) {
+            return json_fail('存在已激活卡片不可以删除');
+        }
+        Db::beginTransaction();
+        try {
+            Card::whereIn('card_id', $card_ids)->delete();
+            Db::commit();
+//            _syslog("删除储值卡", "删除的储值卡ID【" . implode(',', $card_ids) . '】');
+            return json_success("储值卡删除成功");
+        } catch (\Exception $e) {
+            Db::rollBack();
+            return json_fail("储值卡删除失败~");
+        }
+    }
+
+
+    /**
+     * Notes: 储值卡修改
+     * User: ZhouBenXu
+     * DateTime: 2024/6/28 下午3:17
+     * @param $params
+     * @return \support\Response
+     */
+    public static function save($params)
+    {
+        Db::beginTransaction();
+        try {
+            $cardBatchModel = new Card();
+            $model = $cardBatchModel::find($params['card_id']);
+            $model->card_name = $params['card_name'];
+            $model->join_card_member_id = $params['join_card_member_id'];
+            $params['card_status'] && $params['card_status'] = $model->card_status = Card::CARD_STATUS[$params['card_status']]; // 储值卡状态
+            if ($params['validtimes_status'] == 0) { // 卡批次有效期 0 永久天  1 增加 2减少
+                $model->card_validtimes = 0;
+            } elseif ($params['validtimes_status'] == 1) {
+                $model->card_validtimes = baadd($model->card_validtimes + $params['card_batch_validtimes'] * 86400);
+            } elseif ($params['validtimes_status'] == 2) {
+                $card_validtimes = bcsub($model->card_validtimes - $params['card_batch_validtimes'] * 86400);
+                // 修改后的有效期不允许小于当前时间
+                if (!$card_validtimes <= time()) {
+                    return json_fail('储值卡有效期必须大于当前时间~');
+                }
+                $model->card_validtimes = $card_validtimes;
+            }
+            $model->save();
+            Db::commit();
         } catch (\PDOException $e) {
             Db::rollBack();
             dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
@@ -117,8 +361,9 @@ class CardService
             dump($e->getTrace());
             return json_fail('数据写入失败~');
         }
-        _syslog("修改储值卡", "卡批次名称【" . $save['card_batch_name'] . "】");
+//        _syslog("修改储值卡", "储值卡id【" . $params['card_id'] . "】");
         return json_success('success');
     }
 
+
 }

+ 33 - 0
app/admin/validate/card/CardValidate.php

@@ -0,0 +1,33 @@
+<?php
+/**
+ * CardBatchValidate.php
+ * User: ZhouBenXu
+ * Date: 2024/6/27
+ * Time: 上午9:54
+ * Notes:
+ */
+
+namespace app\admin\validate\card;
+
+use support\Validate;
+
+class CardValidate extends Validate
+{
+    protected $rule = [
+        'card_id' => 'require', // 储值卡id
+        'page' => 'require', // 页码
+        'pageSize' => 'require', // 分页条数
+        'card_ids' => 'require', // 储值卡id
+    ];
+
+    protected $message = [];
+
+    protected $scene = [
+        'save' => ['join_card_batch_user_id','card_batch_id','validtimes_status','card_batch_name'],
+        'delete' => ['card_ids'],
+        'list' => ['page','pageSize'],
+        'info' => ['card_id'],
+    ];
+
+
+}

+ 4 - 0
route/admin.php

@@ -898,6 +898,10 @@ Route::group('/admin', function () {
         /* 卡管理 */
         Route::group('/card', function () {
             Route::get('/list', [\app\admin\controller\card\CardController::class, 'list']);
+            Route::get('/info', [\app\admin\controller\card\CardController::class, 'info']);
+            Route::get('/exportCard', [\app\admin\controller\card\CardController::class, 'exportCard']);
+            Route::delete('/delete', [\app\admin\controller\card\CardController::class, 'delete']);
+            Route::post('/save', [\app\admin\controller\card\CardController::class, 'save']);
         })->middleware([
             \app\middleware\AdminAuthCheck::class
         ]);