Procházet zdrojové kódy

储值卡删除 储值卡导出

ZhouBenXu před 4 měsíci
rodič
revize
e0964ddbde

+ 20 - 1
app/admin/controller/card/CardController.php

@@ -70,9 +70,28 @@ class CardController
      * 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);
     }
 
 

+ 105 - 0
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
 {
@@ -212,4 +213,108 @@ class CardService
         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("储值卡删除失败~");
+        }
+    }
+
+
 }

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

@@ -17,6 +17,7 @@ class CardValidate extends Validate
         'card_id' => 'require', // 储值卡id
         'page' => 'require', // 页码
         'pageSize' => 'require', // 分页条数
+        'card_ids' => 'require', // 储值卡id
     ];
 
     protected $message = [];

+ 2 - 0
route/admin.php

@@ -899,6 +899,8 @@ 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']);
         })->middleware([
             \app\middleware\AdminAuthCheck::class
         ]);