tab3Add.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <template>
  2. <div class="tab3Add">
  3. <div class="insideTop">
  4. 学籍管理 > {{ ruleForm.id ? "编辑" : "新增" }}学籍信息
  5. <div class="add">
  6. <el-button type="primary" @click="goBack">返 回</el-button>
  7. </div>
  8. </div>
  9. <div class="obstruct"></div>
  10. <!-- 主要内容 -->
  11. <div class="conten">
  12. <div class="con_top">基本信息</div>
  13. <!-- 表单 -->
  14. <el-form
  15. :model="ruleForm"
  16. :rules="rules"
  17. ref="ruleForm"
  18. label-width="120px"
  19. class="demo-ruleForm"
  20. >
  21. <el-form-item label="姓名:" prop="name">
  22. <el-input
  23. v-model="ruleForm.name"
  24. maxlength="25"
  25. show-word-limit
  26. style="width: 500px"
  27. ></el-input>
  28. </el-form-item>
  29. <el-form-item label="性别:" prop="sex">
  30. <el-radio v-model="ruleForm.sex" label="M">男</el-radio>
  31. <el-radio v-model="ruleForm.sex" label="F">女</el-radio>
  32. </el-form-item>
  33. <el-form-item label="政治面貌:" prop="politics">
  34. <el-select
  35. v-model="ruleForm.politics"
  36. placeholder="请选择"
  37. style="width: 500px"
  38. >
  39. <el-option label="中共党员" value="中共党员"></el-option>
  40. <el-option label="中共预备党员" value="中共预备党员"></el-option>
  41. <el-option label="共青团员" value="共青团员"></el-option>
  42. <el-option label="民革党员" value="民革党员"></el-option>
  43. <el-option label="民盟盟员" value="民盟盟员"></el-option>
  44. <el-option label="民建会员" value="民建会员"></el-option>
  45. <el-option label="民进会员" value="民进会员"></el-option>
  46. <el-option label="农工党党员" value="农工党党员"></el-option>
  47. <el-option label="致公党党员" value="致公党党员"></el-option>
  48. <el-option label="九三学社社员" value="九三学社社员"></el-option>
  49. <el-option label="台盟盟员" value="台盟盟员"></el-option>
  50. <el-option label="无党派人士" value="无党派人士"></el-option>
  51. <el-option label="群众" value="群众"></el-option>
  52. </el-select>
  53. </el-form-item>
  54. <el-form-item label="学籍号:" prop="num">
  55. <el-input
  56. v-model="ruleForm.num"
  57. maxlength="25"
  58. show-word-limit
  59. style="width: 500px"
  60. ></el-input>
  61. </el-form-item>
  62. <el-form-item label="专业:" prop="job">
  63. <el-input
  64. v-model="ruleForm.job"
  65. maxlength="25"
  66. show-word-limit
  67. style="width: 500px"
  68. ></el-input>
  69. </el-form-item>
  70. <el-form-item label="学年:">
  71. <i class="biaoshi biaoshi1"></i>
  72. <el-date-picker
  73. style="width: 500px"
  74. v-model="time"
  75. type="monthrange"
  76. range-separator="-"
  77. start-placeholder="开始日期"
  78. end-placeholder="结束日期"
  79. >
  80. </el-date-picker>
  81. </el-form-item>
  82. <!-- 图片和附件 -->
  83. <el-form-item label="学生照片">
  84. <i class="biaoshi biaoshi2"></i>
  85. <el-upload
  86. class="avatar-uploader"
  87. :action="baseURL + '/cms/student/upload'"
  88. :headers="{
  89. token,
  90. }"
  91. :show-file-list="true"
  92. :before-upload="beforethumbUpload"
  93. :on-success="upload_thumb_success"
  94. >
  95. <div v-if="ruleForm.thumb" class="imgdiv">
  96. <img
  97. style="max-width: 200px; max-height: 200px; display: block"
  98. :src="baseURL + ruleForm.thumb"
  99. />
  100. <i
  101. class="el-icon-circle-close"
  102. @click.stop="ruleForm.thumb = ''"
  103. ></i>
  104. </div>
  105. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  106. </el-upload>
  107. <p class="upHint">
  108. 格式要求:支持png、jpg、gif和jpeg的图片格式;最大支持20MB。
  109. </p>
  110. </el-form-item>
  111. </el-form>
  112. <!-- 富文本 -->
  113. <div class="con_top">
  114. 校园动态
  115. <!-- 富文本上传图片 -->
  116. <el-upload
  117. :headers="{
  118. token,
  119. }"
  120. accept=".png,.jpg,.jpeg,.gif"
  121. title="上传图片"
  122. class="upload-demo imgUplod"
  123. :action="baseURL + '/cms/student/upload'"
  124. multiple
  125. :before-upload="beforethumbUploadImg"
  126. :on-success="upload_thumb_successImg"
  127. >
  128. <i class="w-e-icon-image"></i>
  129. </el-upload>
  130. </div>
  131. <div id="div1" style="z-index: 1"></div>
  132. <!-- 底部按钮 -->
  133. <div class="con_btn">
  134. <el-button type="primary" @click="saveLeader">保 存</el-button>
  135. </div>
  136. </div>
  137. </div>
  138. </template>
  139. <script>
  140. import { studentSave, studentDetail } from '@/apis/tab3'
  141. import axios from '@/utils/request'
  142. import E from 'wangeditor'
  143. export default {
  144. name: 'tab3Add',
  145. components: {},
  146. data () {
  147. // 这里存放数据
  148. return {
  149. time: '',
  150. // 服务器前缀地址
  151. baseURL: '',
  152. token: '',
  153. ruleForm: {
  154. name: '',
  155. sex: 'M',
  156. politics: '',
  157. num: '',
  158. job: '',
  159. thumb: ''
  160. },
  161. rules: {
  162. name: [{ required: true, message: '不能为空', trigger: 'blur' }],
  163. sex: [{ required: true, message: '不能为空', trigger: 'blur' }],
  164. politics: [{ required: true, message: '不能为空', trigger: 'change' }],
  165. num: [{ required: true, message: '不能为空', trigger: 'blur' }],
  166. job: [{ required: true, message: '不能为空', trigger: 'blur' }]
  167. }
  168. }
  169. },
  170. // 监听属性 类似于data概念
  171. computed: {},
  172. // 监控data中的数据变化
  173. watch: {},
  174. // 方法集合
  175. methods: {
  176. // 富文本上传图片
  177. beforethumbUploadImg (file) {
  178. // console.log(998, file)
  179. // 限制图片大小和格式
  180. const sizeOk = file.size / 1024 / 1024 < 5
  181. const typeOk =
  182. file.type === 'image/png' ||
  183. (file.type === 'image/jpeg' && !file.name.includes('.jfif')) ||
  184. file.type === 'image/gif'
  185. return new Promise((resolve, reject) => {
  186. if (!typeOk) {
  187. this.$message.error('图片格式有误!')
  188. reject(file)
  189. } else if (!sizeOk) {
  190. this.$message.error('图片大小超过5M!')
  191. reject(file)
  192. } else if (file.name.length > 32) {
  193. this.$message.error('图片名字不能超过32个字!')
  194. reject(file)
  195. } else {
  196. resolve(file)
  197. }
  198. })
  199. },
  200. upload_thumb_successImg (data) {
  201. this.$message.success('上传成功')
  202. this.editor.txt.append(
  203. `<img src="${this.baseURL}${data.data.urlPath}" style="max-width:100%;display: block;margin: 10px auto;"/><p>&emsp;&emsp;</p>`
  204. )
  205. },
  206. // 点击保存
  207. async saveLeader () {
  208. if (this.ruleForm.thumb === '') {
  209. return this.$message.warning('图片不能为空')
  210. }
  211. if (!this.time) {
  212. return this.$message.warning('学年不能为空')
  213. }
  214. if (this.editor.txt.html() === '') {
  215. return this.$message.warning('校园动态不能为空')
  216. }
  217. try {
  218. await this.$refs.ruleForm.validate()
  219. const obj = {
  220. ...this.ruleForm,
  221. description: this.editor.txt.html(),
  222. tenure: this.time[0] + ',' + this.time[1]
  223. }
  224. const res = await studentSave(obj)
  225. if (res.code === 0) {
  226. this.$message.success('操作成功')
  227. this.$router.push('/layout/tab3')
  228. } else this.$message.warning(res.msg)
  229. // console.log(998, obj)
  230. } catch (error) {
  231. console.log(error)
  232. this.$message.warning('有字段不符合要求')
  233. }
  234. },
  235. // 上传图片
  236. beforethumbUpload (file) {
  237. // console.log(998, file)
  238. // 限制图片大小和格式
  239. const sizeOk = file.size / 1024 / 1024 < 20
  240. const typeOk =
  241. file.type === 'image/png' ||
  242. (file.type === 'image/jpeg' && !file.name.includes('.jfif')) ||
  243. file.type === 'image/gif'
  244. return new Promise((resolve, reject) => {
  245. if (!typeOk) {
  246. this.$message.error('照片格式有误!')
  247. reject(file)
  248. } else if (!sizeOk) {
  249. this.$message.error('照片大小超过20M!')
  250. reject(file)
  251. } else {
  252. resolve(file)
  253. }
  254. })
  255. },
  256. upload_thumb_success (data) {
  257. this.$message.success('上传成功')
  258. // console.log('图片上传成功', data.data.urlPath)
  259. this.ruleForm.thumb = data.data.urlPath
  260. },
  261. // 点击返回
  262. goBack () {
  263. this.$confirm('点击返回后,编辑的信息将无法保存,是否继续?', '提示', {
  264. confirmButtonText: '确定',
  265. cancelButtonText: '取消',
  266. type: 'warning'
  267. })
  268. .then(async () => {
  269. this.$router.push('/layout/tab3')
  270. })
  271. .catch(() => {
  272. this.$message.info('已取消')
  273. })
  274. }
  275. },
  276. // 生命周期 - 创建完成(可以访问当前this实例)
  277. created () {
  278. // 获取服务器前缀地址
  279. this.baseURL = axios.defaults.baseURL
  280. // 获取用户token
  281. this.token = localStorage.getItem('CQLJXU_token')
  282. },
  283. // 生命周期 - 挂载完成(可以访问DOM元素)
  284. async mounted () {
  285. // 初始化富文本
  286. // 富文本
  287. this.editor = new E('#div1')
  288. // 配置字体
  289. this.editor.config.fontNames = [
  290. '黑体',
  291. '仿宋',
  292. '楷体',
  293. '标楷体',
  294. '华文仿宋',
  295. '华文楷体',
  296. '宋体',
  297. '微软雅黑',
  298. 'Arial',
  299. 'Tahoma',
  300. 'Verdana',
  301. 'Times New Roman'
  302. ]
  303. this.editor.config.showLinkVideo = false
  304. this.editor.create()
  305. // 如果是编辑
  306. if (this.$route.query.id) {
  307. const res = await studentDetail(Number(this.$route.query.id))
  308. this.ruleForm = res.data
  309. console.log(998, res)
  310. // 富文本回显
  311. this.editor.txt.html(res.data.description)
  312. // 日期回显
  313. this.time = res.data.tenure.split(',')
  314. }
  315. },
  316. beforeCreate () {}, // 生命周期 - 创建之前
  317. beforeMount () {}, // 生命周期 - 挂载之前
  318. beforeUpdate () {}, // 生命周期 - 更新之前
  319. updated () {}, // 生命周期 - 更新之后
  320. beforeDestroy () {}, // 生命周期 - 销毁之前
  321. destroyed () {}, // 生命周期 - 销毁完成
  322. activated () {} // 如果页面有keep-alive缓存功能,这个函数会触发
  323. }
  324. </script>
  325. <style lang='less' scoped>
  326. .tab3Add {
  327. height: 100%;
  328. .conten {
  329. padding-bottom: 10px;
  330. overflow-y: auto;
  331. height: calc(100% - 32px);
  332. .con_top {
  333. position: relative;
  334. margin-bottom: 12px;
  335. height: 40px;
  336. line-height: 40px;
  337. padding-left: 20px;
  338. background-color: #fbfbfb;
  339. &::after {
  340. content: "*";
  341. position: absolute;
  342. top: -1px;
  343. left: 10px;
  344. color: #f56c6c;
  345. }
  346. .imgUplod {
  347. /deep/.w-e-icon-image {
  348. color: #999;
  349. display: inline-block;
  350. width: 40px;
  351. height: 40px;
  352. line-height: 40px;
  353. }
  354. /deep/.el-upload-list {
  355. display: none !important;
  356. }
  357. display: flex;
  358. justify-content: center;
  359. align-items: center;
  360. cursor: pointer;
  361. position: absolute;
  362. z-index: 9999;
  363. top: 53px;
  364. right: 121px;
  365. width: 40px;
  366. height: 40px;
  367. &:hover {
  368. background-color: #f6f6f6;
  369. /deep/.w-e-icon-image {
  370. color: black;
  371. }
  372. }
  373. }
  374. }
  375. .biaoshi1::before {
  376. left: -64px;
  377. top: -10px;
  378. }
  379. .biaoshi2::before {
  380. left: -78px;
  381. }
  382. /deep/.el-icon-plus {
  383. border: 1px dashed #ccc;
  384. }
  385. .avatar-uploader .el-upload {
  386. border-radius: 6px;
  387. cursor: pointer;
  388. position: relative;
  389. overflow: hidden;
  390. }
  391. .avatar-uploader .el-upload:hover {
  392. border-color: #3e5eb3;
  393. }
  394. .avatar-uploader-icon {
  395. font-size: 28px;
  396. color: #8c939d;
  397. width: 178px;
  398. height: 178px;
  399. line-height: 178px;
  400. text-align: center;
  401. }
  402. #div1 {
  403. width: 85%;
  404. margin: 0 auto;
  405. }
  406. .con_btn {
  407. width: 80px;
  408. margin: 15px auto 0;
  409. }
  410. .imgdiv {
  411. max-width: 200px;
  412. max-height: 200px;
  413. & > img {
  414. border: 5px solid #ccc;
  415. }
  416. }
  417. .el-icon-circle-close {
  418. font-size: 24px;
  419. }
  420. }
  421. }
  422. </style>