quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<template>
  <!--
          作者:luoyiming
          时间:2020-06-04
          描述:插件中心-商品推荐
      -->
  <div class="product-add">
    <el-form size="small" ref="form" :model="form" label-width="100px">
      <div class="common-form">商品推荐</div>
      <el-form-item label="商品推荐">
        <el-radio-group v-model="form.is_recommend">
          <el-radio :label="0">关</el-radio>
          <el-radio :label="1">开</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="模块名称" :rules="[{ required: true, message: ' ' }]" prop="name">
        <el-input v-model="form.name" placeholder="请输入模块名称" class="max-w460" :disabled="form.is_recommend == 0 ? true : false"></el-input>
      </el-form-item>
      <el-form-item label="展示位置">
        <el-checkbox-group v-model="form.location">
          <el-checkbox v-for="(item, index) in all_type" :label="item.value" :key="index" :disabled="form.is_recommend == 0 ? true : false">{{ item.name }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="推荐商品">
        <el-radio-group v-model="form.choice">
          <el-radio :label="0" :disabled="form.is_recommend == 0 ? true : false">按条件选择</el-radio>
          <el-radio :label="1" :disabled="form.is_recommend == 0 ? true : false">自定义选择</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="" v-if="form.choice == 0">
        <el-select v-model="type" placeholder="" :disabled="form.is_recommend == 0 ? true : false">
          <el-option v-for="(item, index) in showType" :label="item.name" :value="item.value" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
 
      <el-form-item label="" v-if="form.choice == 0">
        <el-select v-model="num" placeholder="" :disabled="form.is_recommend == 0 ? true : false">
          <el-option v-for="(item, index) in showNum" :label="item.name" :value="item.value" :key="index"></el-option>
        </el-select>
      </el-form-item>
 
      <el-form-item size="small" label="" v-if="form.choice == 1">
        <div class="common-level-rail"><el-button size="small" icon="el-icon-plus" @click="addClick">添加商品</el-button></div>
        <div class="pb50">
          <el-table size="small" :data="tableData" border>
            <el-table-column label="图片" width="60">
              <template slot-scope="scope">
                <div class="product-info">
                  <div class="pic"><img width="30px" height="30px" v-img-url="scope.row.product_image" /></div>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="product_name" label="商品"></el-table-column>
            <el-table-column label="排序" width="100">
              <template slot-scope="scope">
                <el-input v-model="scope.row.sort" type="number" :disabled="form.is_recommend == 0 ? true : false"></el-input>
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="90">
              <template slot-scope="scope">
                <el-button @click="deleteClick(scope.row)" type="text" size="small" :disabled="form.is_recommend == 0 ? true : false">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-form-item>
      <!--提交-->
      <div class="common-button-wrapper"><el-button type="primary" size="small" @click="onSubmit" :loading="loading">提交</el-button></div>
    </el-form>
    <!--选择商品-->
    <Product :isproduct="isproduct" @closeDialog="closeProductDialogFunc($event)">产品列表弹出层</Product>
  </div>
</template>
 
<script>
import RecommendApi from '@/api/recommend.js';
import Product from '@/components/product/Product';
export default {
  components: {
    /*商品选择*/
    Product
  },
  data() {
    return {
      form: {
        is_recommend: 0,
        choice: 0,
        location: [],
        name: ''
      },
      all_type: [
        {
          name: '购物车',
          value: 10
        },
        {
          name: '会员中心',
          value: 20
        },
        {
          name: '支付完成页',
          value: 30
        }
      ],
      showType: [
        {
          name: '根据销量降序展示',
          value: 10
        },
        {
          name: '根据添加商品时间降序展示',
          value: 20
        },
        {
          name: '根据人气降序展示',
          value: 30
        }
      ],
      showNum: [
        {
          name: '显示当前20个',
          value: 20
        },
        {
          name: '显示当前30个',
          value: 30
        },
        {
          name: '显示当前50个',
          value: 50
        }
      ],
      type: 10,
      num: 20,
      tableData: [],
      product_arr: [],
      isproduct: false,
      Data: [],
      loading: false
    };
  },
  created() {
    /*获取数据*/
    this.getData();
  },
  methods: {
    /*获取当前数据*/
    getData() {
      let self = this;
      let Params = {};
      RecommendApi.getData(Params, true)
        .then(data => {
          self.loading = false;
          self.form = data.data.vars.values;
          // 转成整数,兼容组件
          for(let i=0;i<self.form.location.length;i++){
            self.$set(self.form.location, i, parseInt(self.form.location[i]));
          }
          if (self.form.choice == 1) {
            self.product_arr = data.data.product_arr;
            self.tableData = self.form.product;
          } else {
            self.type = parseInt(self.form.type);
            self.num = parseInt(self.form.num);
          }
          self.form.is_recommend = parseInt(self.form.is_recommend);
          self.form.choice = parseInt(self.form.choice);
        })
        .catch(error => {
          self.loading = false;
        });
    },
 
    /**
     * 保存数据
     */
    onSubmit() {
      let self = this;
      let params = self.form;
 
      if (params.choice == 1) {
        params.product = self.tableData;
      } else {
        params.type = self.type;
        params.num = self.num;
      }
      if (!self.checkData(params)) {
        return false;
      }
      self.loading = true;
      RecommendApi.saveData(params, true)
        .then(data => {
          self.loading = false;
          if (data.code == 1) {
            self.$message({
              message: '恭喜你,保存成功',
              type: 'success'
            });
            self.getData();
          } else {
            self.loading = false;
          }
        })
        .catch(error => {
          self.loading = false;
        });
    },
    /*关闭弹窗*/
    closeProductDialogFunc(e) {
      this.isproduct = e.openDialog;
      if (e.type == 'success') {
        if (this.product_arr.indexOf(e.params.product_id) > -1) {
          this.$message({
            message: '你已选中该产品',
            type: 'error'
          });
          return false;
        }
        let param = {};
        param.product_id = e.params.product_id;
        param.product_name = e.params.product_name;
        param.product_image = e.params.product_image;
        param.sort = 1;
        this.tableData.push(param);
        this.product_arr.push(e.params.product_id);
      }
    },
    /*添加商品*/
    addClick() {
      this.isproduct = true;
    },
    /*删除商品*/
    deleteClick(e) {
      let index = this.product_arr.indexOf(e.product_id);
      this.product_arr.splice(index, 1);
      this.tableData.splice(index, 1);
    },
    /*数据验证*/
    checkData(param) {
      if (!param.name) {
        return false;
      }
      if (param.location.length == 0) {
        this.$message({
          message: '请选择展示位置',
          type: 'error'
        });
        return false;
      }
      if (param.choice == 1 && param.product.length == 0) {
        this.$message({
          message: '请添加商品',
          type: 'error'
        });
        return false;
      }
      return true;
    }
  }
};
</script>
 
<style></style>