quanwei
2025-10-31 7a27a1d4d0038abe2115adb1753f897f56d66323
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
<template>
  <!--
      作者:系统
      时间:2025-10-28
      描述:名片等级管理
  -->
  <div class="common-seach-wrap">
    <div class="search-left">
      <el-button type="primary" @click="addGrade" v-auth="'/plus/business/grade/add'">添加等级</el-button>
    </div>
    
    <div class="table-container">
      <el-table v-loading="loading" :data="listData" border>
        <el-table-column prop="grade_id" label="等级ID" width="80" align="center"></el-table-column>
        <el-table-column prop="name" label="等级名称" align="center"></el-table-column>
        <el-table-column prop="price" label="查看联系方式价格" align="center">
          <template slot-scope="scope">
            {{ scope.row.price>0 ? '¥' + scope.row.price : '免费' }}
          </template>
        </el-table-column>
        <el-table-column prop="weight" label="权重" align="center">
           <!--<template slot-scope="scope">
            <el-input-number 
              v-model="scope.row.weight" 
              :min="0" 
              size="mini" 
              @change="handleWeightChange(scope.row)"
              v-auth="'/plus/business/grade/edit'"
            ></el-input-number>
          </template>-->
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" width="180" align="center">
        </el-table-column>
        <el-table-column prop="update_time" label="更新时间" width="180" align="center">
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
         <template slot-scope="scope">
            <el-button 
              type="text" 
              @click="editGrade(scope.row)"
              v-auth="'/plus/business/grade/edit'"
            >编辑</el-button>
            <el-button 
              type="text" 
              @click="deleteGrade(scope.row)"
              v-auth="'/plus/business/grade/delete'"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    <!-- 添加/编辑等级对话框 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="500px"
      @close="handleClose"
    >
      <el-form :model="formData" :rules="rules" ref="formData" label-width="150px">
        <el-form-item label="等级名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入等级名称"></el-input>
        </el-form-item>
        <el-form-item label="查看联系方式价格" prop="price">
          <el-input 
            v-model.number="formData.price" 
            placeholder="请输入价格,0表示免费"
            type="number"
            :min="0"
            :step="0.01"
          ></el-input>
        </el-form-item>
        <el-form-item label="权重" prop="weight">
          <el-input-number 
            v-model="formData.weight" 
            :min="0" 
            placeholder="权重越小,排序越靠前"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
 
<script>
import BusinessApi from '@/api/business.js';
 
export default {
  data() {
    return {
      loading: false,
      listData: [],
      dialogVisible: false,
      dialogTitle: '',
      formData: {
        grade_id: '',
        name: '',
        price: 0.00,
        weight: 100
      },
      rules: {
        name: [
          { required: true, message: '请输入等级名称', trigger: 'blur' },
          { min: 1, max: 20, message: '等级名称长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        price: [
          { type: 'number', min: 0, message: '价格不能小于0', trigger: 'blur' }
        ],
        weight: [
          { type: 'number', required: true, message: '请输入权重', trigger: 'blur' },
          { type: 'number', min: 0, message: '权重不能小于0', trigger: 'blur' }
        ]
      }
    };
  },
  mounted() {
    this.loadData();
  },
  methods: {
    // 加载数据
    loadData() {
      this.loading = true;
      BusinessApi.gradeList().then(res => {
        this.loading = false;
        this.listData = res.data.list || [];
        console.log(this.listData);
        
      }).catch(() => {
        this.loading = false;
        this.$message.error('获取数据失败');
      });
    },
    
    // 添加等级
    addGrade() {
      this.dialogTitle = '添加等级';
      this.formData = {
        grade_id: '',
        name: '',
        price: 0.00,
        weight: 100
      };
      this.dialogVisible = true;
    },
    
    // 编辑等级
    editGrade(row) {
      this.dialogTitle = '编辑等级';
      this.formData = {
        grade_id: row.grade_id,
        name: row.name,
        price: parseFloat(row.price) || 0.00,
        weight: parseInt(row.weight) || 100
      };
      this.dialogVisible = true;
    },
    
    // 关闭对话框
    handleClose() {
      this.$refs.formData.resetFields();
    },
    
    // 提交表单
    submitForm() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          const params = { ...this.formData };
          
          if (params.grade_id) {
            // 编辑
            BusinessApi.gradeEdit(params).then(res => {
              if (res.code === 1) {
                this.$message.success('编辑成功');
                this.dialogVisible = false;
                this.loadData();
              } else {
                this.$message.error(res.msg || '编辑失败');
              }
            }).catch(() => {
              this.$message.error('编辑失败');
            });
          } else {
            // 添加
            BusinessApi.gradeAdd(params).then(res => {
              if (res.code === 1) {
                this.$message.success('添加成功');
                this.dialogVisible = false;
                this.loadData();
              } else {
                this.$message.error(res.msg || '添加失败');
              }
            }).catch(() => {
              this.$message.error('添加失败');
            });
          }
        }
      });
    },
    
    // 删除等级
    deleteGrade(row) {
      this.$confirm('确定要删除这个等级吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        BusinessApi.gradeDelete({ grade_id: row.grade_id }).then(res => {
          if (res.code === 1) {
            this.$message.success('删除成功');
            this.loadData();
          } else {
            this.$message.error(res.msg || '删除失败');
          }
        }).catch(() => {
          this.$message.error('删除失败');
        });
      }).catch(() => {});
    },
    
    // 处理权重变化
    handleWeightChange(row) {
      const params = {
        grade_id: row.grade_id,
        weight: row.weight,
        name: row.name,
        price: row.price
      };
      
      BusinessApi.gradeEdit(params).then(res => { 
        if (res.code !== 1) {
          this.$message.error(res.msg || '更新权重失败');
          this.loadData(); // 重新加载数据
        }
      }).catch(() => {
        this.$message.error('更新权重失败');
        this.loadData();
      });
    },
    
    // 格式化日期
    formatDate(timestamp) {
      if (!timestamp) return '';
      const date = new Date(timestamp * 1000);
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, '0');
      const day = date.getDate().toString().padStart(2, '0');
      const hours = date.getHours().toString().padStart(2, '0');
      const minutes = date.getMinutes().toString().padStart(2, '0');
      const seconds = date.getSeconds().toString().padStart(2, '0');
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
  }
};
</script>
 
<style scoped>
.common-seach-wrap {
  padding: 20px;
  background-color: #fff;
  min-height: calc(100vh - 60px);
}
 
.search-left {
  margin-bottom: 20px;
}
 
.table-container {
  margin-top: 10px;
}
 
.dialog-footer {
  text-align: right;
}
</style>