create.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <template>
  2. <div class="create">
  3. <div class="breadcrumb">
  4. <ul>
  5. <li v-for="({ name, path }, i) in breadcrumbList" :key="name">
  6. <span v-if="i !== 0" class="separator">></span>
  7. <span :class="['breadcrumb-name', { pointer: path }]" @click="path ? $router.push(path) : ''">
  8. {{ name }}
  9. </span>
  10. </li>
  11. </ul>
  12. </div>
  13. <div class="basic-info">
  14. <div class="basic-info-title">基本信息</div>
  15. <el-form ref="form" :model="form" :rules="formRules" label-width="80px">
  16. <el-form-item label="封面" prop="picture_url">
  17. <el-upload
  18. class="cover-uploader"
  19. action="none"
  20. :show-file-list="false"
  21. :http-request="uploadCover"
  22. :before-upload="beforeCoverUpload"
  23. >
  24. <img v-if="form.picture_url" :src="form.picture_url" class="cover" />
  25. <div v-else class="cover-uploader-icon">
  26. <i class="el-icon-plus"></i>
  27. <span>点击上传封面</span>
  28. </div>
  29. </el-upload>
  30. <div class="tips">支持jpg、png格式图片,图片尺寸必须为 456 x 640px,大小不超过5mb。</div>
  31. </el-form-item>
  32. <el-form-item label="书籍名称" prop="name">
  33. <el-input v-model="form.name" placeholder="请输入内容" />
  34. </el-form-item>
  35. <el-form-item label="作者" prop="author">
  36. <el-input v-model="form.author" placeholder="请输入内容" />
  37. </el-form-item>
  38. <el-form-item label="现价" prop="price">
  39. <el-input v-model="form.price" placeholder="请输入内容">
  40. <template slot="append">元</template>
  41. </el-input>
  42. </el-form-item>
  43. <el-form-item label="原价" prop="price_old">
  44. <el-input v-model="form.price_old" placeholder="请输入内容">
  45. <template slot="append">元</template>
  46. </el-input>
  47. </el-form-item>
  48. <el-form-item label="标签" prop="label_name_list">
  49. <el-select
  50. v-model="form.label_name_list"
  51. multiple
  52. filterable
  53. allow-create
  54. default-first-option
  55. placeholder="请输入内容"
  56. />
  57. </el-form-item>
  58. <el-form-item label="分类" prop="type_id">
  59. <el-select v-model="form.type_id" placeholder="选择分类">
  60. <el-option v-for="{ id: typeId, name } in type_list" :key="typeId" :label="name" :value="typeId" />
  61. </el-select>
  62. </el-form-item>
  63. <el-form-item label="简介" prop="description">
  64. <el-input
  65. v-model="form.description"
  66. type="textarea"
  67. :autosize="{ minRows: 12 }"
  68. :maxlength="500"
  69. show-word-limit
  70. placeholder="请输入更多内容"
  71. />
  72. </el-form-item>
  73. <el-form-item>
  74. <el-button type="primary" @click="addBook">确定</el-button>
  75. <el-button @click="$router.push('/')">取消</el-button>
  76. </el-form-item>
  77. </el-form>
  78. </div>
  79. </div>
  80. </template>
  81. <script>
  82. import { fileUpload } from '@/api/app';
  83. import { GetBook, UpdateBook, AddBook, GetBookTypeList } from '@/api/book';
  84. export default {
  85. name: 'CreateBookPage',
  86. data() {
  87. return {
  88. book_id: this.$route.query.book_id,
  89. form: {
  90. picture_url: '',
  91. picture_id: '',
  92. name: '',
  93. author: '',
  94. price: '',
  95. price_old: '',
  96. label_name_list: '',
  97. type_id: '',
  98. description: '',
  99. },
  100. formRules: {
  101. picture_url: [{ required: true, message: '请上传封面', trigger: 'blur' }],
  102. name: [{ required: true, message: '请输入书籍名称', trigger: 'blur' }],
  103. author: [{ required: true, message: '请输入作者', trigger: 'blur' }],
  104. price: [{ required: true, message: '请输入现价', trigger: 'blur' }],
  105. type_id: [{ required: true, message: '请选择分类', trigger: 'blur' }],
  106. description: [{ required: true, message: '请输入简介', trigger: 'blur' }],
  107. },
  108. type_list: [],
  109. breadcrumbList: [
  110. { name: '教材管理', path: '/home' },
  111. { name: '新建教材', path: '' },
  112. { name: '基本信息', path: '' },
  113. ],
  114. };
  115. },
  116. created() {
  117. if (this.book_id) {
  118. this.getBookDetail();
  119. }
  120. GetBookTypeList().then(({ type_list }) => {
  121. this.type_list = type_list;
  122. });
  123. },
  124. methods: {
  125. uploadCover(file) {
  126. fileUpload('Mid', file, { isGlobalprogress: true }).then(({ file_info_list }) => {
  127. if (file_info_list.length > 0) {
  128. const { file_url, file_id } = file_info_list[0];
  129. this.form.picture_url = file_url;
  130. this.form.picture_id = file_id;
  131. }
  132. });
  133. },
  134. beforeCoverUpload(file) {
  135. return new Promise((resolve, reject) => {
  136. // 图片尺寸必须为 456 x 640px
  137. const img = new Image();
  138. img.src = window.URL.createObjectURL(file);
  139. let isCorrectSize = true; // 是否为正确尺寸
  140. img.onload = () => {
  141. const { width, height } = img;
  142. if (width !== 456 || height !== 640) {
  143. isCorrectSize = false;
  144. this.$message.error('图片尺寸必须为 456 x 640px!');
  145. reject();
  146. }
  147. const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
  148. if (!isJPG) {
  149. this.$message.error('上传封面图片只能是 JPG 或 PNG 格式!');
  150. reject();
  151. }
  152. const isLt5M = file.size / 1024 / 1024 < 5;
  153. if (!isLt5M) {
  154. this.$message.error('上传封面图片大小不能超过 5MB!');
  155. reject();
  156. }
  157. if (isCorrectSize && isJPG && isLt5M) {
  158. resolve();
  159. } else {
  160. reject();
  161. }
  162. };
  163. });
  164. },
  165. addBook() {
  166. this.$refs.form.validate((valid) => {
  167. if (valid) {
  168. if (this.book_id) {
  169. this.updateBook();
  170. } else {
  171. AddBook({ ...this.form, sys_version_type: 1 }).then(({ id }) => {
  172. this.$router.push(`/book/setting/${id}`);
  173. });
  174. }
  175. } else {
  176. return false;
  177. }
  178. });
  179. },
  180. getBookDetail() {
  181. GetBook({ id: this.book_id }).then(
  182. ({ name, picture_id, picture_url, author, type_id, price, price_old, label_name_list, description }) => {
  183. this.form = {
  184. name,
  185. picture_id,
  186. picture_url,
  187. author,
  188. type_id,
  189. price,
  190. price_old,
  191. label_name_list,
  192. description,
  193. };
  194. },
  195. );
  196. },
  197. updateBook() {
  198. UpdateBook({ id: this.book_id, ...this.form }).then(() => {
  199. this.$router.push(`/book/setting/${this.book_id}`);
  200. });
  201. },
  202. },
  203. };
  204. </script>
  205. <style lang="scss" scoped>
  206. .create {
  207. padding: 8px 24px;
  208. .breadcrumb {
  209. display: flex;
  210. align-items: center;
  211. > ul {
  212. display: flex;
  213. li {
  214. font-size: 14px;
  215. font-weight: 400;
  216. color: $font-light-color;
  217. .separator {
  218. margin: 0 8px;
  219. color: #c9cdd4;
  220. }
  221. &:nth-last-child(1) {
  222. font-weight: bold;
  223. color: $font-color;
  224. }
  225. }
  226. }
  227. }
  228. .basic-info {
  229. width: 1200px;
  230. padding: 24px;
  231. margin: 24px auto 16px;
  232. background-color: #fff;
  233. border-radius: 4px;
  234. &-title {
  235. margin-bottom: 24px;
  236. font-size: 18px;
  237. font-weight: bold;
  238. }
  239. .el-form {
  240. .el-input,
  241. .el-textarea,
  242. .el-select {
  243. width: 400px;
  244. :deep .el-input__inner,
  245. :deep .el-textarea__inner {
  246. background-color: #fff;
  247. border-color: #dcdcdc;
  248. }
  249. }
  250. .cover-uploader {
  251. :deep img {
  252. max-width: 228px;
  253. max-height: 320px;
  254. }
  255. :deep .el-upload {
  256. position: relative;
  257. overflow: hidden;
  258. cursor: pointer;
  259. border: 1px dashed #d9d9d9;
  260. border-radius: 6px;
  261. &:hover {
  262. border-color: #409eff;
  263. }
  264. }
  265. &-icon {
  266. display: flex;
  267. flex-direction: column;
  268. row-gap: 4px;
  269. align-items: center;
  270. justify-content: center;
  271. width: 228px;
  272. height: 320px;
  273. font-size: 12px;
  274. color: #8c939d;
  275. background-color: #eee;
  276. .el-icon-plus {
  277. font-size: 16px;
  278. font-weight: bold;
  279. color: #000;
  280. }
  281. }
  282. .avatar {
  283. display: block;
  284. width: 178px;
  285. height: 178px;
  286. }
  287. }
  288. .tips {
  289. margin-top: -4px;
  290. font-size: 12px;
  291. color: #8c8c8c;
  292. }
  293. &-item {
  294. margin-bottom: 24px;
  295. }
  296. }
  297. }
  298. }
  299. </style>