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
<template>
  <div class="version inner-bg-style">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px"
             class="demo-ruleForm">
      <el-form-item label="版本类型:" prop="appType">
        <div>
          <el-select v-model="ruleForm.appType" class="custom-input-style" placeholder="请选择版本类型">
            <el-option label="Android" value="Android"></el-option>
            <el-option label="iOS" value="iOS"></el-option>
          </el-select>
        </div>
      </el-form-item>
      <el-form-item label="版本上传" prop="androidAPK" v-if="ruleForm.appType === 'Android'">
        <el-upload class="avatar-uploader" drag :action="action" :headers="headers" :data="{
              path: 'apk'
            }" accept=".apk" :before-upload="beforeAvatarUpload" :on-remove="handleRemove"
                   :on-success="handleAvatarSuccess">
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">只能上传apk文件</div>
        </el-upload>
      </el-form-item>
      <el-form-item label="版本号:" prop="version">
        <el-input v-model="ruleForm.version" class="custom-input-style" placeholder="请输入版本号">
        </el-input>
      </el-form-item>
      <el-form-item label="发布内容:" prop="content">
        <el-input type="textarea" class="custom-input-style" v-model="ruleForm.content"
                  :maxlength="500" show-word-limit placeholder="请输入发布内容"></el-input>
      </el-form-item>
      <el-form-item class="text-center" label-width="0">
        <el-button size="mini" type="primary" :loading="loading" @click="submitForm('ruleForm')">保存
        </el-button>
        <el-button size="mini" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { saveMobileVersion } from '../../api/system'
import Cookie from 'js-cookie'
// 跨域认证信息 header 名
const xsrfHeaderName = 'Authorization'
export default {
  name: 'version',
  data() {
    var validateVersion = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入版本号'))
      } else {
        const reg = /^([1-9]\d|[1-9])(\.([1-9]\d|\d)){2}$/
        if (reg.test(value)) {
          callback()
        } else {
          callback(new Error('请输入正确的版本号'))
        }
      }
    }
    return {
      loading: false,
      action: `${process.env.VUE_APP_CURRENTMODE === 'development' ? '/api/' : process.env.VUE_APP_API_BASE_URL}lbcloud-file/file/upload/file`,
      headers: {
        Authorization: 'Bearer ' + Cookie.get(xsrfHeaderName)
      },
      ruleForm: {
        appType: '',
        androidAPK: '',
        content: '',
        version: ''
      },
      rules: {
        appType: [{ required: true, message: '请选择版本类型', trigger: 'change' }],
        androidAPK: [{ required: true, message: '请上传APK', trigger: 'change' }],
        version: [{ required: true, validator: validateVersion, trigger: 'change' }],
        content: [{ required: true, message: '请输入发布内容', trigger: 'change' }]
      }
    }
  },
  methods: {
    cancel() {
      this.$router.push('/dash/version')
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this._saveMobileVersion({
            appType: this.ruleForm.appType,
            apkUrl: this.ruleForm.androidAPK,
            content: this.ruleForm.content,
            version: this.ruleForm.version,
            status: '0'
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async _saveMobileVersion(params) {
      if (this.loading) {
        return
      }
      this.loading = true
      const res = await saveMobileVersion(params)
      if (res.code === '0') {
        this.$message({
          message: '保存成功',
          type: 'success'
        })
        this.cancel()
      }
      this.loading = false
    },
    handleRemove() {
      this.ruleForm.androidAPK = ''
    },
    handleAvatarSuccess(res, file) {
      console.log(res.code)
      if (res.code === '200') {
        // this.ruleForm.androidAPK = URL.createObjectURL(file.raw)
        this.ruleForm.androidAPK = res.data.like
        this.$message({
          message: '上传成功',
          type: 'success'
        })
      } else {
        this.$message({
          message: '上传失败',
          type: 'error'
        })
      }
    },
    beforeAvatarUpload(file) {
      var fileSize = file.size / 1024 / 1024 < 300
      if (!fileSize) {
        this.$message.error('上传失败!上传文件大小不能超过 300MB!请重新上传')
        return false
      }
      var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
      if (testmsg !== 'apk') {
        this.$message({
          message: '上传失败!仅支持APK格式,请重新上传',
          type: 'error'
        })
        return false
      }
      return true
    }
  }
}
</script>
<style scoped>
.inner-bg-style {
  padding-top: 20px;
}
</style>