浏览代码

充值模板的改版

liuzejian 1 年之前
父节点
当前提交
492a447ded
共有 1 个文件被更改,包括 51 次插入13 次删除
  1. 51 13
      modules/Channel/Http/Controllers/PayTemplateController.php

+ 51 - 13
modules/Channel/Http/Controllers/PayTemplateController.php

@@ -4,7 +4,9 @@ namespace Modules\Channel\Http\Controllers;
 
 use Carbon\Carbon;
 use Catch\Base\CatchController;
+use Illuminate\Foundation\Validation\ValidatesRequests;
 use Illuminate\Http\Request;
+use Illuminate\Support\Facades\DB;
 use Modules\Channel\Exceptions\ChannelBusinessException;
 use Modules\Channel\Models\PayProduct;
 use Modules\Channel\Models\PayTemplate;
@@ -14,6 +16,8 @@ use Modules\Common\Errors\Errors;
 
 class PayTemplateController extends CatchController
 {
+    use ValidatesRequests;
+
     public function __construct(protected readonly PayTemplate $payTemplate,protected readonly PayTemplateItem $payTemplateItem,protected readonly PayProduct $payProduct)
     {
 
@@ -36,6 +40,9 @@ class PayTemplateController extends CatchController
         if(UserService::userHasRole($uid,'administrator')){
             $uid = 0;
         }
+        if($uid) {
+            $this->initPayTemplate($uid);
+        }
         $name = $request->get('name');
         $where = [[
             'uid','=',$uid
@@ -43,9 +50,36 @@ class PayTemplateController extends CatchController
         if($name){
             $where[] = ['name','like','%'.$name.'%'];
         }
-        return $this->payTemplate->orderBy('id','desc')->where($where)->paginate(20);
+        $result = $this->payTemplate->orderBy('id','desc')->where($where)->paginate($request->input('limit', 20));
+        foreach ($result as $item) {
+            $item->status_str = $item->status ? '默认模板': '非默认模板';
+        }
+
+        return $result;
     }
 
+    /**
+     * 如果优化是没有设置充值模板,那么就是用官方模板进行初始化
+     * @param $uid
+     */
+    private function initPayTemplate($uid) {
+        if(!$this->payTemplate->where('uid', $uid)->count()) {
+            $systemPayTemplates = $this->payTemplate->where('uid', 0)->get();
+            $insertData = [];
+            $now = date('Y-m-d H:i:s');
+            foreach ($systemPayTemplates as $systemPayTemplate) {
+                $insertData[] = [
+                    'uid' => $uid,
+                    'name' => $systemPayTemplate->name,
+                    'status' => $systemPayTemplate->status,
+                    'created_at' => $now,
+                    'updated_at' => $now,
+                    'type' => $systemPayTemplate->type,
+                ];
+            }
+            $this->payTemplate->insert($insertData);
+        }
+    }
 
     /**
      * 添加模板
@@ -55,21 +89,23 @@ class PayTemplateController extends CatchController
      */
     public function store(Request $request)
     {
+        $this->validate($request, [
+            'name' => 'required',
+            'options' => 'required',
+            'type' => 'required|in:1,2'
+        ]);
         $uid = $this->getLoginUser()->id;
         $name = $request->post('name');
+        // 0-非默认模板,1-默认模板
         $status = $request->post('status');
         $options = $request->post('options');
-        if(empty($name) || empty($options)){
-            ChannelBusinessException::throwError(Errors::PARAM_EMPTY);
-        }
+        // 1-首充模板,2-非首充模板
+        $type = $request->input('type');
+
         if(UserService::userHasRole($uid,'administrator')){
             $uid = 0;
         }
 
-        $exists = $this->payTemplate->where('uid',$uid)->where('name',$name)->count();
-        if($exists){
-            ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_EXISTS_ERROR);
-        }
         $option_list = json_decode($options,1);
         $data = [];
         $default_optioin = 0;
@@ -102,11 +138,14 @@ class PayTemplateController extends CatchController
 
         $default_optioin = $default_optioin>0?$default_optioin:$option_list[0]['price'];
 
-        if($status == 1){
-            $this->payTemplate->where('uid',$uid)->update(['status'=>0]);
+        if($status == 1 && 0 == $uid){
+            $this->payTemplate->where([
+                'uid' => $uid, 'type' => $type
+            ])->update(['status'=>0]);
         }
 
-        $pay_template_info = $this->payTemplate->create(['name'=>$name,'uid'=>$uid,'status'=>$status]);
+        $pay_template_info = $this->payTemplate->create(['name'=>$name,'uid'=>$uid,
+            'type' => $type, 'status' => $status]);
 
         foreach($option_list as $option){
             $type = $option['type'];
@@ -133,8 +172,7 @@ class PayTemplateController extends CatchController
      */
     public function show($id)
     {
-        //$uid = $this->getLoginUser()->id;
-        $uid= 1;
+        $uid = $this->getLoginUser()->id;
         if(UserService::userHasRole($uid,'administrator')){
             $uid = 0;
         }