fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
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
<template>
  <div>
    <el-dialog :visible.sync="dialogVisible" :title="title" :close-on-click-modal="false" :modal-append-to-body="false" v-if="dialogVisible" width="564px">
      <el-form size="mini" class="createProd" ref="form">
        <el-form-item label="商品名称:">
          <el-select
            v-model.trim="content"
            filterable
            remote
            placeholder="请输入商品名称"
            :remote-method="remoteMethod"
            :loading="vagueLoading"
            clearable
            @change="handChange"
            >
            <el-option
              v-for="item in options"
              :key="item.spuId"
              :label="item.spuName"
              :value="item.spuId">
               <span class="prodLeft">{{ item.spuName }}</span>
               <span class="prodRight">{{ item.spuNum }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer buttonPosition">
        <el-button size="mini" @click="submit" type="primary"  :loading="loading">确定</el-button>
        <el-button size="mini" @click="dialogVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
 
<script>
import integralProdApi from '@/api/member/integralProd'
export default {
  props: {
    title: {
      type: String,
      default: null
    },
    show: {
      type: Boolean,
      default: false
    },
    shopId: {
      type: String,
      default: null
    }
  },
  data () {
    return {
      dialogVisible: false,
      content: null,
      options: [],
      prodItem: null, // 选择的商品信息
      vagueLoading: false,
      loading: false
    }
  },
  watch: {
    /**
     * 监控外部显示变量变化
     * 传递到dialog组件
     */
    show: function (newShow, oldShow) {
      this.dialogVisible = newShow
    },
    /**
     * 监控内部显示属性变化
     * 传递到外部调用变量
     */
    dialogVisible: function (newDialogShow, oldDialogShow) {
      this.$emit('update:show', newDialogShow)
      if (!newDialogShow) {
        this.content = null
        this.options = []
      }
    }
  },
  methods: {
    // 确定
    async submit () {
      if (!this.shopId) {
        this.$message({
          message: '此用户没有店铺',
          type: 'warning'
        })
        return
      }
      if (!this.content) {
        this.$message({
          message: '请输入商品名称',
          type: 'warning'
        })
        return
      }
      try {
        if (this.prodItem) {
          this.loading = true
          const res = await integralProdApi.getProdInfo({ shopId: this.shopId, spuId: this.prodItem.spuId })
          if (res.data && res.code === '0') {
            this.loading = false
            this.$emit('hand-selected', res.data, this.prodItem)
            this.dialogVisible = false
          } else {
            this.loading = false
          }
        }
      } catch (error) {
        this.loading = false
      }
    },
    // 远程搜索
    async remoteMethod (queryString) {
      if (queryString) {
        this.vagueLoading = true
        try {
          const res = await integralProdApi.queryProdBySpuName({ spuName: queryString })
          if (res.data && res.code === '0') {
            this.options = res.data
            this.vagueLoading = false
          }
        } catch (error) {
          this.vagueLoading = false
        }
      } else {
        this.options = []
      }
    },
    handChange (val) {
      if (val) {
        this.prodItem = this.options.find(item => {
          return item.spuId === this.content
        })
      }
      this.options = []
    }
  }
}
</script>
 
<style lang="scss">
.createProd{
  .el-select{
    width: 400px;
  }
}
.prodLeft{
  float: left;
}
.prodRight{
  float: right;
  color: #8492a6;
  font-size: 13px;
  margin-left: 20px;
}
</style>