Browse Source

设置规格

afa 5 months ago
parent
commit
517bb7372c

+ 8 - 12
app/admin/controller/shop/CustomerSpec.php

@@ -11,6 +11,7 @@ use app\common\model\Customer;
 use app\common\model\StockConfig;
 use think\facade\Db;
 use app\common\model\ProductConfig;
+use app\common\service\SpecService;
 use app\common\model\CustomerSpec as CustomerSpecModel;
 
 #[Group("shop/customer_spec")]
@@ -52,16 +53,14 @@ class CustomerSpec extends Backend
         $type_box    =$this->request->post('type_box');
         $type_list   =$this->request->post('type_list');
         if(empty($customer_id) || empty($type_box) || empty($type_list)) return resp_json(0,'请填写完整信息');
-        $specsList = [];
-        foreach ($type_box as $item) {
-           if(empty($item['price'])) return resp_json(0, '请填写发货价格');
-           $specsList[] = ['type_id'=> $item['type_id'], 'type_name'=> $item['type_name'],'specs_id'=>$item['value'],'specs_name'=>$item['name'], 'price'=> $item['price']];
-        }
+        //判断是否有发货价格
+        if(SpecService::getIsZeroSpecsPrice($type_box) == false) return resp_json(0, '请填写发货价格');
+
         if($this->model->where('customer_id', $customer_id)->count()>0) return resp_json(0,'客户信息已存在');
+
         $result = false;
         Db::startTrans();
         try {
-
             $result = $this->model->save([
                 'customer_id'=> $customer_id,
                 'variety'    => json_encode($type_list, JSON_UNESCAPED_UNICODE),
@@ -104,12 +103,9 @@ class CustomerSpec extends Backend
         }
         $params = $this->request->post("");
         if(empty($params['ids']) || empty($params['type_box']) || empty($params['type_list'])) return resp_json(0,'请填写完整信息');
-        $specsList = [];
-        foreach ($params['type_box'] as $item) {
-           if(empty($item['price'])) return resp_json(0, '请填写发货价格');
-           $specsList[] = ['type_id'=> $item['type_id'], 'type_name'=> $item['type_name'],'specs_id'=>$item['value'],'specs_name'=>$item['name'], 'price'=> $item['price']];
-        }
-       
+        //判断是否有发货价格
+        if(SpecService::getIsZeroSpecsPrice($params['type_box']) == false) return resp_json(0, '请填写发货价格');
+      
         $result = false;
         Db::startTrans();
         try {

+ 15 - 26
app/admin/controller/shop/ShopList.php

@@ -10,8 +10,8 @@ use app\admin\traits\Actions;
 use app\common\model\Customer;
 use app\common\model\User;
 use app\common\model\StockConfig;
-use app\common\model\ProductConfig;
-use think\Response;
+use app\common\model\CustomerSpec;
+use app\common\service\SpecService;
 use app\common\model\ShopList as ShopListModel;
 
 #[Group("shop/shop_list")]
@@ -40,25 +40,20 @@ class ShopList extends Backend
     {
         if($this->request->isAjax()){
             $ids = $this->request->post('ids/d', 0);
-            $field_name= $this->request->post('field_name');
-            $type_id = $this->request->post('type_id');
-            $specs   = $this->request->post('type_box');
-            $specsList = [];
-          
-            foreach ($specs as $item) {
-               if(empty($item['price'])) return resp_json(0, '请填写发货价格');
-               $specsList[] = ['name'=>$item['name'], 'price'=> $item['price']];
-            }
-            $row = $this->model::find($ids);
-            $row->type_spec = json_encode(['field_name'=>['type_id'=>$type_id, 'field_name'=>$field_name], 'specs'=> $specsList], JSON_UNESCAPED_UNICODE);
-
-            $row->save();
+            $params = $this->request->post('');
+            if(empty($params['variety']) || empty($ids) || empty($params['type_box'])) return resp_json(0, '参数错误');
+         
+            $this->model->where('id', $ids)->save([
+                'variety'    => json_encode($params['variety'], JSON_UNESCAPED_UNICODE),
+                'type_spec'  => json_encode($params['type_box'], JSON_UNESCAPED_UNICODE),
+            ]);
             return resp_json(200,'操作成功');
-
         }else{
           
             $ids = $this->request->get('ids/d', 0);
             $rows = $this->model::find($ids);
+            $rows->variety = $rows->variety? json_decode($rows->variety, true) : [];
+            $rows->type_spec = $rows->type_spec? json_decode($rows->type_spec, true) : [];
             $this->assign('row', $rows);
             return $this->fetch();
         }
@@ -69,22 +64,16 @@ class ShopList extends Backend
     public function get_box()
     {
         if($this->request->isAjax()){
-         
+            $result = [];
             $customer_id = $this->request->get('customer_id/d', 0);
             $type_id = $this->request->get('type_id/d', 0);
             if(empty($type_id) || empty($customer_id)) return resp_json(0, '参数错误');
-            $rows = $this->model::where('customer_id', $customer_id)->order('sort desc')->find();
+            $rows = CustomerSpec::where('customer_id', $customer_id)->order('sort desc')->find();
             if($rows->specs){
-
                 $specs = json_decode($rows->specs, true);
-
-                dump($specs);die;
-                foreach ($specs['specs'] as $item) {
-
-                }
+                $result = SpecService::getSpecsList($type_id, $specs);
             }
-
-            return json($rows, 200);
+            return json($result, 200);
         }
     }
  

+ 1 - 0
app/admin/view/goods/stock_config/add.html

@@ -16,6 +16,7 @@
                 {field: 'type_id', title: __('类型'), edit: 'radio',rules:"required",searchList: {'variety_name': __('品种'), 'packing_box': __('包装箱'), 'material': __('耗材')}},
                 {field: 'title',title: __('标题'),edit:'text',rules:'required'},
                 {field: 'field_name',title: __('库存唯一属性'),edit:'text',rules:'required'},
+                {field: 'unit',title: __('单位'),edit:'text',rules:'required'},
                 {field: 'sort',title: __('排序'),edit:'text'},
                 {field: 'status', title: __('状态'), edit:'switch',searchList: {1: __('正常'),2: __('隐藏')}},
             ]

+ 1 - 0
app/admin/view/goods/stock_config/index.html

@@ -37,6 +37,7 @@
                 {field: 'type_id',title: __('类型'),operate:'=',searchList: {'variety_name': __('品种'), 'packing_box': __('包装箱'), 'material': __('耗材')}},
                 {field: 'title',title: __('标题'),operate: '='},
                 {field: 'field_name', title: __('唯一标识'),operate:'LIKE'},
+                {field: 'unit', title: __('单位'),operate: false},
                 {field: 'status', title: __('状态'),width:120,searchList: {1: __('正常'),2: __('停用')},formatter:Yunqi.formatter.switch},
                 {field: 'sort', title: __('排序'),operate: false},
                 {field: 'createtime', title: __('创建时间'), width:160,formatter: Yunqi.formatter.datetime,operate:false,sortable: true},

+ 23 - 8
app/admin/view/shop/shop_list/specs.html

@@ -35,7 +35,7 @@
 </template>
 <script>
     import form from "@components/Form.js";
-    var typeList = [], boxList = [], field_name = '';
+    var typeList = [], boxList = [];
     export default {
         components: { 'YunForm': form },
         data() {
@@ -51,19 +51,25 @@
         },
         methods: {
             changeType: function (data, row) {
-        
+                
                 //获取规格
                 let obj = JSON.parse(row.target.value);
+                let idx = boxList.findIndex(item => item.value == obj.value);
+                if (idx != -1) {
+                    boxList.splice(idx, 1)
+                }else{
+                    boxList.push({ label: obj.label, value: obj.value });
+                }
                 Yunqi.ajax.get('shop/shop_list/get_box', {customer_id:Yunqi.data.row.customer_id, type_id: obj.value }, false, false, true).then(res => {
-
                     //编辑数据
+                    console.log(res, "========")
                     if (res.length > 0) {
                           for (var key in Object(res)) {
-                              let index = this.tableData.findIndex(item => item.value == res[key]['id']);
+                              let index = this.tableData.findIndex(item => item.value == res[key]['value']);
                               if (index != -1 ) {
                                   this.tableData.splice(index, 1)
                               }else {
-                                this.tableData.push({ name: res[key]['title'], value: res[key]['id'], price: res[key]['price'], type_id:obj.value, type_name: obj.label });
+                                this.tableData.push({ name: res[key]['name'], value: res[key]['value'], price: res[key]['price'], type_id:obj.value, type_name: obj.label });
                               }
                           }
                     }
@@ -71,8 +77,11 @@
             },
             onSubmit() {
                 //获取规格
-                if (this.tableData == "") return
-                Yunqi.ajax.post('shop/shop_list/specs', { ids: Yunqi.data.rows.id, type_id: JSON.parse(this.form.nameValue).value, field_name: field_name, type_box: this.tableData }, false, false, true).then(res => {
+                console.log(this.tableData, "==========111")
+                console.log(boxList, "==========22")
+
+                if (this.tableData == "" || boxList.length == 0) return
+                Yunqi.ajax.post('shop/shop_list/specs', { ids: Yunqi.data.row.id,  variety: boxList, type_box: this.tableData }, false, false, true).then(res => {
                     if (res.code == 200) {
                         this.$message.success(__('设置成功'));
                         Yunqi.api.closelayer(Yunqi.app.window.id, true);
@@ -89,12 +98,18 @@
         },
         onLoad: function () {
 
-         
+            for (let index = 0; index < this.form.nameValue.length; index++) {
+                this.form.nameValue[index] = JSON.stringify(this.form.nameValue[index]);
+            }
             //全部品种
             let type = Yunqi.data.fieldList;
             for (var key in Object(type)) {
                 typeList.push({ label: type[key], value: key });
             }
+            //品种
+            for (var key in Object(Yunqi.data.row.variety)) {
+                boxList[key] = JSON.parse(Yunqi.data.row.variety[key]);
+            }
         },
     }
 </script>

+ 46 - 0
app/common/service/SpecService.php

@@ -0,0 +1,46 @@
+<?php
+declare(strict_types=1);
+namespace app\common\service;
+
+use app\common\model\Msg;
+
+/**
+ * 消息服务
+ */
+class SpecService extends BaseService{
+
+   
+      protected function init()
+      {
+  
+      }
+
+      //判断价格为空
+      public static function getIsZeroSpecsPrice(array $data): bool
+      {     
+            $result = false;
+            foreach ($data as $item) {
+                  if(empty($item['price'])) return false;
+            }
+            return $result;
+      }
+
+
+
+      //获取规格列表
+      public static function getSpecsList(int $type_id, array $data): array
+      {     
+            $result = [];
+            foreach ($data as $item) {
+              
+                  if($item['type_id'] == $type_id){
+                        $result[] = $item;
+                  }
+            }
+            return $result;
+      }
+       
+
+
+   
+}