AppVersionModal.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <template>
  2. <div ref="container">
  3. <a-modal
  4. title="发布新版本"
  5. :visible="visible"
  6. :confirmLoading="confirmLoading"
  7. :getContainer="() => $refs.container"
  8. :maskStyle="{ top: '93px', left: '154px' }"
  9. :wrapClassName="wrapClassNameInfo()"
  10. :mask="isDesktop()"
  11. :maskClosable="false"
  12. @cancel="handleCancel"
  13. :footer="null"
  14. width="500px"
  15. style="top: 5%"
  16. >
  17. <a-spin :spinning="confirmLoading">
  18. <a-form :form="form" class="form-content" id="appVersionModal">
  19. <a-form-item label="版本号" :labelCol="labelCol" :wrapperCol="wrapperCol">
  20. <a-input placeholder="请输入" allowClear v-decorator="['version', validatorRules.version]" />
  21. </a-form-item>
  22. <a-form-item label="上传文件" :labelCol="labelCol" :wrapperCol="wrapperCol">
  23. <div class="upload-container">
  24. <a-upload
  25. name="file"
  26. :data="uploadOpts.data"
  27. :showUploadList="true"
  28. :multiple="false"
  29. :headers="uploadOpts.headers"
  30. :action="uploadOpts.action"
  31. :method="uploadOpts.method"
  32. :accept="uploadOpts.accept"
  33. :fileList="fileList"
  34. @change="handleChange"
  35. :beforeUpload="beforeUpload"
  36. @reject="handleReject"
  37. >
  38. <div class="upload-content">
  39. <div class="upload-icon">
  40. <a-icon type="folder-open" style="color: #4e73f8; font-size: 32px;" />
  41. </div>
  42. <a-button type="primary" class="upload-button">上传文件</a-button>
  43. </div>
  44. </a-upload>
  45. </div>
  46. </a-form-item>
  47. <div class="form-actions">
  48. <a-button @click="handleCancel" class="cancel-button">取消</a-button>
  49. <a-button type="primary" @click="handleOk" :loading="confirmLoading" class="confirm-button">确定发布新版本</a-button>
  50. </div>
  51. </a-form>
  52. </a-spin>
  53. </a-modal>
  54. </div>
  55. </template>
  56. <script>
  57. import Vue from 'vue'
  58. import { ACCESS_TOKEN } from '@/store/mutation-types'
  59. import pick from 'lodash.pick'
  60. import { autoJumpNextInput } from '@/utils/util'
  61. import { mixinDevice } from '@/utils/mixin'
  62. import { postAction } from '@/api/manage'
  63. export default {
  64. name: 'AppVersionModal',
  65. mixins: [mixinDevice],
  66. data() {
  67. return {
  68. visible: false,
  69. confirmLoading: false,
  70. labelCol: { span: 4 },
  71. wrapperCol: { span: 20 },
  72. fileList: [],
  73. validatorRules: {
  74. version: {
  75. rules: [
  76. { required: true, message: '请输入版本号!' },
  77. { pattern: /^\d+\.\d+\.\d+$/, message: '版本号格式不正确!' }
  78. ]
  79. },
  80. url: {
  81. rules: [
  82. { required: true, message: '请上传.apk文件!' }
  83. ]
  84. }
  85. },
  86. form: this.$form.createForm(this),
  87. model: {},
  88. uploadOpts:{
  89. headers: {
  90. 'X-Access-Token': Vue.ls.get(ACCESS_TOKEN)
  91. },
  92. method: 'POST',
  93. action: window._CONFIG['domianURL'] + '/apkVersion/upload',
  94. accept: '.apk',
  95. data:{
  96. biz: 'bill'
  97. }
  98. },
  99. }
  100. },
  101. methods: {
  102. add() {
  103. this.edit({})
  104. },
  105. edit(record) {
  106. this.form.resetFields()
  107. this.model = Object.assign({}, record)
  108. this.fileList = []
  109. this.visible = true
  110. this.$nextTick(() => {
  111. this.form.setFieldsValue(
  112. pick(
  113. this.model,
  114. 'url',
  115. 'version'
  116. )
  117. )
  118. autoJumpNextInput('appVersionModal')
  119. })
  120. },
  121. beforeUpload(file) {
  122. // 检查文件类型
  123. const isAPK = file.type === 'application/vnd.android.package-archive' || file.name.endsWith('.apk')
  124. if (!isAPK) {
  125. this.$message.error('只能上传APK文件!')
  126. return false
  127. }
  128. // 阻止自动上传
  129. return true
  130. },
  131. handleChange(info) {
  132. this.fileList = [info.file]
  133. console.log('文件列表:', this.fileList)
  134. if (info.file.status === 'uploading') {
  135. console.log(info.file, info.fileList)
  136. this.confirmLoading = true
  137. }else if(info.file.status === 'removed'){
  138. this.fileList = []
  139. this.model.url = ''
  140. }else if (info.file.status === 'done') {
  141. this.confirmLoading = false
  142. console.log('model=====', this.model)
  143. if (info.file.response) {
  144. if (info.file.response.code === 200) {
  145. info.file.name = info.file.response.data
  146. this.model.url = info.file.response.data
  147. } else {
  148. this.$message.warning(info.file.response.data, 8)
  149. }
  150. } else {
  151. this.$message.error(`${info.file.name} ${info.file.response.data}.`)
  152. }
  153. } else if (info.file.status === 'error') {
  154. this.confirmLoading = false
  155. this.$message.error(`文件上传失败: ${info.file.msg} `)
  156. }
  157. },
  158. handleReject(file) {
  159. console.log('文件类型不正确:', file)
  160. this.$message.error('文件类型不正确,请上传.apk文件!')
  161. },
  162. handleOk() {
  163. this.form.validateFields((err, values) => {
  164. if (err) return
  165. console.log('values======',values)
  166. if (this.fileList.length === 0) {
  167. this.$message.error('请上传.apk类型文件!')
  168. return
  169. }
  170. const formData = {
  171. version: values.version,
  172. url: this.fileList[0].name // 实际应用中这里应该是上传后的URL
  173. }
  174. this.confirmLoading = true
  175. postAction('/apkVersion/add', formData).then(res => {
  176. if (res.code === 200) {
  177. this.$message.success(res.msg)
  178. this.visible = false
  179. this.$emit('ok', formData)
  180. } else {
  181. this.$message.error(res.data||'操作失败')
  182. }
  183. }).catch(err => {
  184. console.error(err)
  185. }).finally(() => {
  186. this.confirmLoading = false
  187. })
  188. })
  189. },
  190. handleCancel() {
  191. this.visible = false
  192. }
  193. }
  194. }
  195. </script>
  196. <style lang="less" scoped>
  197. .form-content {
  198. padding: 0 10px;
  199. }
  200. .upload-container {
  201. border: 1px dashed #d9d9d9;
  202. border-radius: 4px;
  203. padding: 24px;
  204. background-color: #fafafa;
  205. }
  206. .upload-content {
  207. display: flex;
  208. flex-direction: column;
  209. align-items: center;
  210. justify-content: center;
  211. padding: 10px 0;
  212. }
  213. .upload-icon {
  214. margin-bottom: 16px;
  215. }
  216. .upload-button {
  217. height: 32px;
  218. font-size: 14px;
  219. border-radius: 4px;
  220. background-color: #4e73f8;
  221. border-color: #4e73f8;
  222. }
  223. .reupload-button {
  224. background-color: #4e73f8;
  225. border-color: #4e73f8;
  226. }
  227. .form-actions {
  228. display: flex;
  229. justify-content: flex-end;
  230. margin-top: 24px;
  231. .cancel-button {
  232. margin-right: 12px;
  233. min-width: 80px;
  234. height: 32px;
  235. }
  236. .confirm-button {
  237. background-color: #4e73f8;
  238. border-color: #4e73f8;
  239. min-width: 120px;
  240. height: 32px;
  241. }
  242. }
  243. /deep/ .ant-upload.ant-upload-select {
  244. display: block;
  245. }
  246. /deep/ .ant-upload-list-item {
  247. margin-top: 8px;
  248. }
  249. /deep/ .ant-form-item-label {
  250. text-align: left;
  251. line-height: 32px;
  252. }
  253. /deep/ .ant-form-item {
  254. margin-bottom: 24px;
  255. }
  256. /deep/ .ant-modal-close-x {
  257. width: 46px;
  258. height: 46px;
  259. line-height: 46px;
  260. }
  261. /deep/ .ant-modal-body {
  262. padding: 16px 24px 24px;
  263. }
  264. /deep/ .ant-form-item-control {
  265. line-height: 32px;
  266. }
  267. /deep/ .ant-input {
  268. height: 32px;
  269. }
  270. </style>