EditBanner.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <el-form ref="form" :model="form" label-width="120px">
  3. <el-form-item label="标题">
  4. <el-input v-model="form.title" size="mini"></el-input>
  5. </el-form-item>
  6. <el-form-item label="宣传图">
  7. <el-upload
  8. class="avatar-uploader"
  9. :action="uploadAction"
  10. :show-file-list="false"
  11. :on-success="onUploadSuccess"
  12. :before-upload="onBeforeUpload"
  13. >
  14. <img v-if="form.icon" :src="form.icon" class="avatar" />
  15. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  16. </el-upload>
  17. </el-form-item>
  18. <el-form-item label="跳转地址">
  19. <el-input v-model="form.url" size="mini"></el-input>
  20. </el-form-item>
  21. <el-form-item>
  22. <el-button type="primary" @click="save" size="mini">保存</el-button>
  23. <el-button @click="goBack" size="mini">取消</el-button>
  24. </el-form-item>
  25. </el-form>
  26. </template>
  27. <script>
  28. export default {
  29. data() {
  30. return {
  31. uploadAction: process.env.SERVER_PATH + "upload",
  32. form: {
  33. id: 0,
  34. title: "",
  35. icon: "",
  36. url: ""
  37. }
  38. };
  39. },
  40. methods: {
  41. save: function() {
  42. var that = this;
  43. this.$http
  44. .bannerSave(
  45. {
  46. id: this.form.id,
  47. title: this.form.title,
  48. icon: this.form.icon,
  49. url: this.form.url
  50. },
  51. this
  52. )
  53. .then(res => {
  54. if (res.code === 0) {
  55. this.$message({
  56. message: "操作成功",
  57. type: "success",
  58. duration: 1000,
  59. onClose: function() {
  60. that.goBack();
  61. }
  62. });
  63. }
  64. });
  65. },
  66. goBack: function() {
  67. this.$router.back();
  68. },
  69. onBeforeUpload: function(file) {
  70. const isJPG = file.type === "image/jpeg";
  71. const isPNG = file.type === "image/png";
  72. if (!isJPG && !isPNG) {
  73. this.$message.error("上传商品图标只能是 JPG/PNG 格式!");
  74. return false;
  75. } else {
  76. return true;
  77. }
  78. },
  79. onUploadSuccess: function(res, file) {
  80. if (res.code != 0) {
  81. this.$message.error(
  82. "上传图片失败 原因:" + res.msg + "(" + res.code + ")"
  83. );
  84. return;
  85. } else {
  86. this.form.icon = res.obj;
  87. }
  88. }
  89. },
  90. mounted: function() {
  91. var id = this.$route.query.id;
  92. if (id === 0) {
  93. return;
  94. }
  95. this.form.id = id;
  96. this.$http.bannerGet({ id: id }, this).then(res => {
  97. if (res.code === 0) {
  98. this.form.title = res.obj.title;
  99. this.form.icon = res.obj.icon;
  100. this.form.url = res.obj.url;
  101. }
  102. });
  103. }
  104. };
  105. </script>
  106. <style scoped>
  107. .avatar-uploader img {
  108. height: 100%;
  109. width: 100%;
  110. }
  111. .el-form-item {
  112. margin-bottom: 0px;
  113. }
  114. </style>