index.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <div class="block">
  3. <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px" class="demo-ruleForm">
  4. <el-form-item label="藏品名称:" prop="name" style="width: 550px">
  5. <el-input v-model="ruleForm.name" maxlength="20" show-word-limit></el-input>
  6. </el-form-item>
  7. <!-- <el-form-item label="藏品图片 :" prop="avatar">
  8. <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/" :show-file-list="false"
  9. :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
  10. <img v-if="ruleForm.imageUrl" :src="ruleForm.imageUrl" class="avatar" />
  11. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  12. </el-upload>
  13. </el-form-item> -->
  14. <el-form-item label="藏品图片:" prop="avatar">
  15. <el-upload class="avatar-uploader" action="http://localhost:80/dev-api/files/send" accept="image/*"
  16. :show-file-list="false" :on-success="handleAvatarSuccess">
  17. <img v-if="ruleForm.avatar" :src="ruleForm.avatar" class="avatar" />
  18. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  19. </el-upload>
  20. </el-form-item>
  21. <el-form-item label="藏品价格:" prop="price" style="width: 550px">
  22. <el-input v-model="ruleForm.price" show-word-limit>
  23. <template slot="append">¥</template>
  24. </el-input>
  25. </el-form-item>
  26. <el-form-item label="藏品数量:" prop="number" style="width: 550px">
  27. <el-input v-model="ruleForm.number" show-word-limit>
  28. <template slot="append">份</template>
  29. </el-input>
  30. </el-form-item>
  31. <el-form-item label="售卖时间:" prop="date" style="width: 550px">
  32. <el-date-picker v-model="ruleForm.date" type="datetimerange" start-placeholder="开始日期" end-placeholder="结束日期"
  33. :default-time="['12:00:00']">
  34. </el-date-picker>
  35. </el-form-item>
  36. <el-form-item label="作品故事" prop="desc" style="width:550px;">
  37. <el-input type="textarea" v-model="ruleForm.desc" class="inputStore" :rows="15"></el-input>
  38. </el-form-item>
  39. <el-button type="primary" style="margin-left: 200px" @click="submitForm('ruleForm')">提交</el-button>
  40. <el-button @click="resetForm('ruleForm')">重置</el-button>
  41. </el-form>
  42. <el-backtop :bottom="60">
  43. <div style="{
  44. height: 100%;
  45. width: 100%;
  46. background-color: #f2f5f6;
  47. box-shadow: 0 0 6px rgba(0,0,0, .12);
  48. text-align: center;
  49. line-height: 40px;
  50. color: #1989fa;
  51. }">
  52. top
  53. </div>
  54. </el-backtop>
  55. </div>
  56. </template>
  57. <script>
  58. import { addCollections } from '../../../api/collection/collections'
  59. export default {
  60. data() {
  61. return {
  62. ruleForm: {
  63. name: "",
  64. // 时间
  65. date: "",
  66. price: "",
  67. number: "",
  68. // 头像
  69. avatar: "",
  70. desc: ""
  71. },
  72. //表单验证
  73. rules: {
  74. name: [
  75. { required: true, message: "请输入藏品名称", trigger: "blur" },
  76. {
  77. min: 2,
  78. max: 20,
  79. message: "长度在 2 到 20个字符",
  80. trigger: "blur",
  81. },
  82. ],
  83. desc: [
  84. { required: true, message: "请输入作品故事", trigger: "blur" },
  85. {
  86. min: 20,
  87. max: 200,
  88. message: "长度在 20 到 200个字符",
  89. trigger: "blur",
  90. },
  91. ],
  92. price: [
  93. { required: true, message: "请输入藏品价格", trigger: "blur" },
  94. {
  95. max: 20,
  96. message: "长度在 2 到 20个字符",
  97. trigger: "blur",
  98. },
  99. ],
  100. number: [
  101. { required: true, message: "请输入藏品数量", trigger: "blur" },
  102. {
  103. max: 20,
  104. message: "长度在 2 到 20个字符",
  105. trigger: "blur",
  106. },
  107. ],
  108. date: [
  109. {
  110. required: true,
  111. message: "请按照选择时间",
  112. trigger: "change",
  113. },
  114. ],
  115. },
  116. };
  117. },
  118. methods: {
  119. // 提交表单
  120. submitForm(formName) {
  121. this.$refs[formName].validate((valid) => {
  122. if (valid) {
  123. addCollections(this.ruleForm).then((res) => {
  124. this.$message({
  125. message: '添加成功',
  126. type: 'success'
  127. })
  128. console.log(res);
  129. this.$refs[formName].resetFields();
  130. this.$router.push({ name: "collections" });
  131. }).catch((e) => {
  132. console.log(e);
  133. })
  134. } else {
  135. console.log("error submit!!");
  136. return false;
  137. }
  138. });
  139. },
  140. // 自定义上传头像 覆盖默认上传
  141. handleUpload({ file }) {
  142. const formData = new FormData();
  143. formData.append("file", file);
  144. // 调用上传头像接口
  145. uploadAvatar(formData).then((res) => {
  146. if (res.code === 200) {
  147. // 如果返回状态码为200 将返回的头像地址赋值给表单头像字段
  148. this.ruleForm.avatar = res.data;
  149. }
  150. });
  151. },
  152. // 上传头像成功
  153. handleAvatarSuccess(res, file) {
  154. this.$message({
  155. type: 'success',
  156. message: '上传成功!'
  157. });
  158. this.ruleForm.avatar = URL.createObjectURL(file.raw);
  159. },
  160. // 重置表单
  161. resetForm(formName) {
  162. this.$refs[formName].resetFields();
  163. },
  164. },
  165. };
  166. </script>
  167. <style lang="scss" scoped>
  168. .avatar-uploader .el-upload {
  169. border: 1px dashed #d9d9d9;
  170. border-radius: 6px;
  171. cursor: pointer;
  172. position: relative;
  173. overflow: hidden;
  174. }
  175. .avatar-uploader .el-upload:hover {
  176. border-color: #409eff;
  177. }
  178. .avatar-uploader-icon {
  179. font-size: 28px;
  180. color: #8c939d;
  181. width: 400px;
  182. height: 300px;
  183. line-height: 300px;
  184. text-align: center;
  185. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)
  186. }
  187. .avatar {
  188. width: 300px;
  189. height: 300px;
  190. display: block;
  191. }
  192. .block {
  193. margin: 20px 100px;
  194. padding: 60px;
  195. border: 1px solid #ebebeb;
  196. border-radius: 5px;
  197. }
  198. </style>