EditGoodsDescribe.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <div>
  3. <el-upload :action="uploadAction" list-type="picture-card" :file-list="items" :on-success="onUploadSuccess" :before-upload="onBeforeUpload" :on-remove="onRemove">
  4. <i class="el-icon-plus"></i>
  5. </el-upload>
  6. <div>
  7. <el-button type="primary" @click="save">保存</el-button>
  8. <el-button @click="goBack">取消</el-button>
  9. </div>
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. data() {
  15. return {
  16. uploadAction: process.env.SERVER_PATH + 'upload',
  17. items: [],
  18. gid: 0
  19. }
  20. },
  21. methods: {
  22. onBeforeUpload: function(file) {
  23. const isJPG = file.type === 'image/jpeg'
  24. const isPNG = file.type === 'image/png'
  25. if (!isJPG && !isPNG) {
  26. this.$message.error('上传商品图标只能是 JPG/PNG 格式!')
  27. return false
  28. } else {
  29. return true
  30. }
  31. },
  32. onUploadSuccess: function(res, file) {
  33. if (res.code != 0) {
  34. this.$message.error(
  35. '上传图片失败 原因:' + res.msg + '(' + res.code + ')'
  36. )
  37. } else {
  38. this.items.push({ name: '', url: res.obj })
  39. }
  40. },
  41. onRemove: function(file, fileList){
  42. this.items = fileList;
  43. },
  44. save: function() {
  45. var items = []
  46. this.items.forEach(it => {
  47. items.push({ title: it.name, content: it.url })
  48. })
  49. var that = this
  50. this.$http
  51. .saveGoodDescribe({ gid: this.gid, items: items }, this)
  52. .then(res => {
  53. if (res.code === 0) {
  54. this.$message({
  55. message: '操作成功',
  56. type: 'success',
  57. duration: 1000,
  58. onClose: function() {
  59. that.goBack()
  60. }
  61. })
  62. }
  63. })
  64. },
  65. goBack: function(){
  66. this.$router.back();
  67. }
  68. },
  69. mounted: function() {
  70. let gid = this.$route.query.gid
  71. this.gid = gid
  72. this.$http.getGoodDescribe({ gid: gid }, this).then(res => {
  73. if (res.code === 0) {
  74. var items = []
  75. res.obj.forEach(item => {
  76. items.push({ id: item.id, name: item.title, url: item.content })
  77. })
  78. this.items = items
  79. }
  80. })
  81. }
  82. }
  83. </script>