hejianhao
2025-04-16 dab2d210ca06c1faa514c6388fbd5de1ab355360
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
<template>
    <el-dialog
    title="新增分类"
    :visible="value"
    width="30%"
    :show-close="false"
    :modal='false'
    :before-close="onCancel">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="权重" prop="weight">
        <el-input v-model="ruleForm.weight"></el-input>
      </el-form-item>
      <el-form-item label="图标" prop="icon">
        <el-upload
          class="avatar-uploader"
          :action="api"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="ruleForm.icon" :src="ruleForm.icon" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <div class="samll-text">图片格式仅限PNG、JPEG、JPG<br>图片大小10M以内,限传1张</div>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="onCancel('ruleForm')">取 消</el-button>
      <el-button type="primary" @click="onOk('ruleForm')" :loading="okLoading">确 定</el-button>
    </span>
  </el-dialog>
</template>
 
<script>
import vUpload from "com/upload/upload1";
export default {
  props: {
    value: {type: Boolean},
  },
  components:{vUpload},
 
  data() {
    return {
      ruleForm: {
        name:"" , //分类名称
        weight:"",
        icon:"",
      },
      okLoading: false,
      rules: {
        name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在10个字符', trigger: 'blur' }
        ],
        weight: [
          { required: true, message: '请输入权重', trigger: 'blur' },
        ],
        icon: [
          { required: true, message: '请上传图片', trigger: 'blur' },
        ],
       } 
    }
  },
 
  mounted() {
    this.api = this.api = this.$js.api.upload; //获取上传的路径
  },
 
  methods: {
    /** 上传图标 */
    handleAvatarSuccess(res, file) {
      console.log(file)
      this.ruleForm.icon = URL.createObjectURL(file.raw);
    },
 
    /** 限制用户上传的图片格式和大小*/
    beforeAvatarUpload(file) {
      var isJPG = false
      switch (file.type) {
      case "image/png":
          isJPG = true;
          break;
      case "image/jpeg":
          isJPG = true;
          break;
      case "image/jpg":
          isJPG = true;
          break;
      default:
          isJPG = false;
          break;
      }
      const isLt2M = file.size / 1024 / 1024 < 10;
 
      if (!isJPG) {
        demo.toast("上传头像图片只能是 PNG、JPEG、JPG 格式!");
      }
      if (!isLt2M) {
        demo.toast("上传头像图片大小不能超过 10MB!");
      }
      return isJPG && isLt2M;
    },
  
    /** 取消 */
    onCancel (formName) {
      this.$emit('change', false);
      this.$refs[formName].resetFields();
    },
 
    /** 确认 */
    onOk(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
      // let parmas = {
    
      // };
      
      // this.okLoading = true;
      // this.$api.post("dyn/type/edit", parmas, 
      //   e => {
      //     demo.toast("编辑成功");
      //     this.$emit('success');
      //     this.onCancel();
      //   },
      //   err=> {
      //     demo.toast(err.msg)
      //   }
      // )
      // this.okLoading = false;
    }
 
  }
 
}
</script>
 
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
  .samll-text {
    font-size: 12px;
    line-height: 20px;
    color: #8c939d;
  }
</style>