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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<template>
  <div class="user">
    <div class="common-form">编辑转盘</div>
    <div class="product-content">
      <el-form ref="form" :model="form" :rules="formRules" label-width="150px">
        <el-form-item label="每日次数" prop="times" :rules="[{required: true,message: ' '}]">
          <el-input type="text" v-model="form.times" placeholder="请输入每日次数" class="max-w460"></el-input>
        </el-form-item>
        <el-form-item label="消耗积分" prop="points" :rules="[{required: true,message: ' '}]">
          <el-input type="text" v-model="form.points" placeholder="请输入消耗积分" class="max-w460"></el-input>
        </el-form-item>
        <el-form-item label="活动状态" prop="status" :rules="[{required: true,message: ' '}]">
          <el-switch v-model="form.status" active-color="#13ce66" inactive-color="#999999" :active-value="1"
            :inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item label="活动规则" prop="content" :rules="[{required: true,message: ' '}]">
          <el-input type="textarea" :rows="3" v-model="form.content" placeholder="请输入活动规则" class="max-w460"></el-input>
        </el-form-item>
        <el-form-item label="背景图" prop="file_path">
          <el-row>
            <el-button type="primary" @click="openUpload" size="mini">选择图片</el-button>
            <div v-if="form.file_path!=''" class="img">
              <img :src="form.file_path" width="100" height="100" />
            </div>
          </el-row>
        </el-form-item>
        <div class="common-form">礼品设置</div>
        <el-form-item label="" v-if="tableData.length<20">
          <el-button type="primary" @click="addCoupon()">添加优惠券</el-button>
          <el-button type="primary" @click="addPoint()">添加积分</el-button>
          <el-button type="primary" @click="addNothing()">添加空项</el-button>
        </el-form-item>
        <el-form-item>
          <el-table :data="tableData" highlight-current-row @current-change="handleCurrentChange"
            ref="singleTable">
            <el-table-column prop="lottery_id" label="序号" type="index">
            </el-table-column>
            <el-table-column prop="name" label="名称" :rules="[{required: true,message: ' '}]">
              <template slot-scope="scope">
                <el-input type="text" v-if="scope.row.type==0" v-model="scope.row.name" placeholder="请输入">
                </el-input>
                <span v-else>{{scope.row.name}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="image" label="图片" width="180">
              <template slot-scope="scope">
                <img v-img-url="hasImages(scope.row.image)" alt="" width="50px" @click="changeImg(scope.row)" />
              </template>
            </el-table-column>
            <el-table-column prop="type" label="类型">
              <template slot-scope="scope">
                <span v-if="scope.row.type==0">无礼品</span>
                <span v-if="scope.row.type==1">优惠券</span>
                <span v-if="scope.row.type==2">积分</span>
                <span v-if="scope.row.type==3">商品</span>
              </template>
            </el-table-column>
            <el-table-column prop="stock" label="活动库存" :rules="[{required: true,message: ' '}]">
              <template slot-scope="scope">
                <el-input type="number" v-model="scope.row.stock" placeholder="">
                </el-input>
              </template>
            </el-table-column>
            <el-table-column prop="draw_num" label="已送出">
            </el-table-column>
            <el-table-column prop="is_default" label="是否默认" :rules="[{required: true,message: ' '}]">
              <template slot-scope="scope">
                <el-switch @change="changeSwitch(scope.row)" v-model="scope.row.is_default==1" active-color="#13ce66"
                  inactive-color="#999999">
                </el-switch>
              </template>
            </el-table-column>
            <el-table-column prop="address" label="操作">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click='delcoupon(scope.row)'> 删除 </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
      <!--提交 by yj-->
      <div class="common-button-wrapper">
        <el-button type="info" @click="gotoBack">返回</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="save_loading">提交</el-button>
      </div>
    </div>
    <el-dialog title="添加积分" :visible.sync="isPoint">
      <el-input v-model="point" autocomplete="off"></el-input>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closePointFunc(false)">取 消</el-button>
        <el-button type="primary" @click="closePointFunc(true)">确 定</el-button>
      </div>
    </el-dialog>
    <!--上传图片组件-->
    <Upload v-if="isupload" :isupload="isupload" :type="type" @returnImgs="returnImgsFunc">上传图片</Upload>
    <!--选择优惠券-->
    <GetCoupon v-if="open_add" :open_add="open_add" @closeDialog="closeProductDialogFunc($event)">选择优惠券弹出层</GetCoupon>
    <!--选择商品-->
    <Product :isproduct="isproduct" :excludeIds="exclude_ids" :islist="false" @closeDialog="closeProductFunc"></Product>
  </div>
</template>
<script>
  import UserApi from '@/api/user.js';
  import LotteryApi from '@/api/lottery.js';
  import GetCoupon from '@/components/coupon/GetCoupon';
  import Product from '@/components/product/Product.vue';
  import Upload from '@/components/file/Upload';
  import {
    formatModel
  } from '@/utils/base.js';
  export default {
    components: {
      /*选择优惠券件*/
      GetCoupon,
      Product,
      Upload
    },
    data() {
      return {
        form: {
          prize: [],
          points: 0,
          is_times: 0,
          coupon_num: 1,
          times: '',
          name: '',
          lottery_id: 0,
          image_id: '',
          content: '',
          file_path: '',
          status: 0
        },
        file_path: '',
        tableData: [],
        loading: false,
        open_add: false,
        /*是否打开选择商品*/
        isproduct: false,
        exclude_ids: [],
        spec_type: 10,
        /*是否打开图片选择*/
        isupload: false,
        formRules: {
          image_id: [{
            required: true,
            message: '请上传背景图',
            trigger: 'blur'
          }],
        },
        /*左边长度*/
        formLabelWidth: '120px',
        isPoint: false,
        point: '',
        isNothing: false,
        isLottery: -1,
        prize_ids: [],
        save_loading:false
      };
    },
    created() {
      /*获取数据*/
      this.getData();
    },
    methods: {
      hasImages(e) {
        if (e) {
          return e;
        } else {
          return '';
        }
      },
      /*添加优惠券*/
      addCoupon() {
 
        this.open_add = true;
      },
      /*关闭优惠券*/
      closeProductDialogFunc(e) {
        let self = this;
        self.open_add = e.openDialog;
        if (e.type == 'success') {
          let params = {
            prize_id:0,
            award_id: e.params.coupon_id,
            name: e.params.name,
            stock: 1,
            type: 1,
            image: '',
            points:0,
            is_default: 0
          }
          self.tableData.push(params);
        }
      },
 
      /*添加商品*/
      addProduct() {
 
        this.isproduct = true;
 
      },
      /*关闭商品*/
      closeProductFunc(e) {
        let self = this;
        self.isproduct = e.openDialog;
        if (e.type == 'success') {
          let params = {
            prize_id:0,
            award_id: e.params.product_id,
            name: e.params.product_name,
            stock: 1,
            type: 3,
            image: e.params.product_image,
             points:0,
            is_default: 0
          }
          self.tableData.push(params);
        }
      },
      addPoint() {
        this.isPoint = true;
      },
      closePointFunc(e) {
        let self = this;
        self.isPoint = e.openDialog;
        let points = this.point;
        if (e) {
          let params = {
            prize_id:0,
            award_id: 0,
            name: '积分',
            points: 0,
            stock: 1,
            type: 2,
            image: '',
            is_default: 0
          }
          params.points = points;
          params.name = points + '积分';
          self.tableData.push(params);
        }
      },
      addNothing() {
        let self = this;
        let params = {
          prize_id:0,
          award_id: 0,
          name: '祝你好运',
          stock: 1,
          type: 0,
          image: '',
          points:0,
          is_default: 0
        }
        self.tableData.push(params);
      },
      /*获取数据*/
      getData() {
        let self = this;
        LotteryApi.getData(true).then(data => {
          self.form = formatModel(self.form, data.data.data);
          if (self.form.prize != null) {
            self.tableData = self.form.prize;
            self.prize_ids=[];
          }
        }).catch(error => {});
      },
      delProduct(item) {
        let self = this;
        let n = self.prodcutData.indexOf(item);
        self.prodcutData.splice(n, 1);
        self.form.product.splice(n, 1);
      },
      delcoupon(item) {
        let self = this;
        let n = self.tableData.indexOf(item);
        if (item.prize_id) {
          self.prize_ids = [...self.prize_ids,item.prize_id];
        }
        self.tableData.splice(n, 1);
      },
      /*提交表单*/
      onSubmit() {
        let self = this;
        let form = self.form;
        if (self.tableData.length > 20) {
          self.$message.error('最多能设置20项礼品信息');
          return
        }
        let flag = false;
        self.tableData.forEach((item,index)=>{
          if(item.is_default==1){
            flag = true;
            return
          }
        })
        if(!flag){
          self.$message.error('请设置默认项');
           return
        }
        form.prize = self.tableData;
        if(self.prize_ids.length>0){
          form.prize_ids=self.prize_ids;
        }else{
          form.prize_ids='';
        }
        self.$refs.form.validate((valid) => {
          if (valid) {
            self.save_loading = true;
            LotteryApi.EditLottery(form, true).then(data => {
                if (data.code == 1) {
                  self.$message({
                    message: data.msg,
                    type: 'success'
                  });
                  
                  /*获取数据 by yj*/
                  this.getData();
                  setTimeout(function(){
                          self.save_loading = false; 
                  },3000)
                  
                 // self.$router.push('/plus/lottery/index');
                } else {
                  self.save_loading = false;
                }
              })
              .catch(error => {
                self.save_loading = false;
              });
          }
        });
 
      },
      changeImg(e) {
        this.isLottery = this.tableData.indexOf(e);
        this.isupload = true;
      },
      /*上传*/
      openUpload(e) {
        this.type = e;
        this.isupload = true;
      },
      /*获取图片*/
      returnImgsFunc(e) {
 
        if (e != null && e.length > 0) {
          if (this.isLottery != -1) {
            this.$set(this.tableData[this.isLottery], 'image', e[0].file_path)
          } else {
            this.form.file_path = e[0].file_path;
            this.form.image_id = e[0].file_id;
          }
 
        }
        this.isLottery = -1;
        this.isupload = false;
      },
      /*返回上一页面*/
      gotoBack() {
        this.$router.back(-1);
      },
      setCurrent(row) {
        this.$refs.singleTable.setCurrentRow(row);
      },
      handleCurrentChange(val) {
        this.currentRow = val;
      },
      changeSwitch(row) {
        this.tableData.forEach((item, index) => {
          item.is_default = 0;
        })
        row.is_default = 1
      }
    }
  };
</script>