chuanzuoye.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <div id="homework">
  3. <el-row>
  4. <el-col :span="24" class="style">
  5. <el-col :span="24" class="top">
  6. <NavBar v-show="navShow" :title="title" :isleftarrow="isleftarrow"> </NavBar>
  7. </el-col>
  8. <el-col :span="24" class="main">
  9. <el-col :span="24" class="leaveBtn"> </el-col>
  10. <el-col :span="24" class="leaveList">
  11. <!-- <upload-homework :form="form" @onConfirm="onsave"></upload-homework> -->
  12. <el-form ref="form" label-width="80px">
  13. <el-form-item label="文件上传" prop="zynumberfile">
  14. <!-- <upload :limit="1" :data="pic" type="picurl" :url="`/files/task/upload`" @upload="uploadSuccess" @delete="toDelete"></upload> -->
  15. <van-uploader v-model="pic" :after-read="afterRead" preview-size="160px" />
  16. </el-form-item>
  17. <div style="margin: 16px;">
  18. <el-form-item>
  19. <el-button type="primary" @click="onSubmit()">上传</el-button>
  20. </el-form-item>
  21. </div>
  22. </el-form>
  23. </el-col>
  24. <el-col :span="24" class="foot">
  25. <footInfo></footInfo>
  26. </el-col>
  27. </el-col>
  28. </el-col>
  29. </el-row>
  30. </div>
  31. </template>
  32. <script>
  33. import _ from 'lodash';
  34. import upload from '@frame/components/upload.vue';
  35. import NavBar from '@/layout/common/topInfo.vue';
  36. import footInfo from '@/layout/common/footInfo.vue';
  37. import { mapState, createNamespacedHelpers, mapGetters } from 'vuex';
  38. const { mapActions: mapUploadtask } = createNamespacedHelpers('uploadtask');
  39. const { mapActions: util } = createNamespacedHelpers('util');
  40. export default {
  41. name: 'homework',
  42. props: {},
  43. components: {
  44. NavBar, //头部导航
  45. footInfo,
  46. // upload,
  47. },
  48. data: () => ({
  49. pic: [],
  50. show: false,
  51. navShow: true,
  52. taskAnswer: null,
  53. title: '',
  54. isleftarrow: '',
  55. }),
  56. mounted() {
  57. this.title = this.$route.meta.title;
  58. this.isleftarrow = this.$route.meta.isleftarrow;
  59. },
  60. watch: {
  61. $route(to, from) {
  62. this.title = to.meta.title;
  63. this.isleftarrow = to.meta.isleftarrow;
  64. },
  65. },
  66. created() {
  67. this.search();
  68. },
  69. methods: {
  70. ...util({ modelFetch: 'fetch', taskUpload: 'taskupload' }),
  71. ...mapUploadtask({ list: 'query', add: 'create', fet: 'fetch', update: 'update' }),
  72. // 查找作业
  73. async search() {
  74. let taskAnswer = await this.modelFetch({ model: 'uploadtask', studentid: this.user.userid, lessonid: this.lessonid });
  75. // 说明已经上传
  76. if (taskAnswer.data) {
  77. const { data } = taskAnswer;
  78. this.$set(this, `taskAnswer`, data);
  79. const { picurl } = data;
  80. if (picurl) {
  81. if (_.isArray(picurl))
  82. this.$set(
  83. this,
  84. 'pic',
  85. picurl.map(i => ({ url: i }))
  86. );
  87. } else {
  88. this.$set(this, `pic`, [{ url: picurl }]);
  89. }
  90. }
  91. },
  92. //提交上传
  93. async onSubmit() {
  94. // 判断是否有上传作业
  95. let obj = {};
  96. if (!this.taskAnswer) {
  97. obj.termid = this.user.termid;
  98. obj.batchid = this.user.batchid;
  99. obj.classid = this.user.classid;
  100. obj.studentid = this.user.userid;
  101. obj.lessonid = this.lessonid;
  102. obj.lessonname = this.name;
  103. } else obj = _.cloneDeep(this.taskAnswer);
  104. let h = _.head(this.pic);
  105. obj.picurl = _.cloneDeep(this.pic.map(i => i.url));
  106. if (obj.picurl.length <= 0) {
  107. this.$message({
  108. showClose: true,
  109. message: '请上传图片',
  110. type: 'error',
  111. });
  112. return;
  113. }
  114. if (!obj.id) {
  115. let res = await this.add(obj);
  116. if (this.$checkRes(res)) {
  117. this.$notify({
  118. message: '提交成功',
  119. type: 'success',
  120. });
  121. } else {
  122. this.$notify({
  123. message: res.errmsg,
  124. type: 'danger',
  125. });
  126. }
  127. } else {
  128. let res = await this.update(obj);
  129. if (this.$checkRes(res)) {
  130. this.$notify({
  131. message: '提交成功',
  132. type: 'success',
  133. });
  134. } else {
  135. this.$notify({
  136. message: res.errmsg,
  137. type: 'danger',
  138. });
  139. }
  140. }
  141. },
  142. //手机上传至文件上传项目
  143. async afterRead(data) {
  144. // 此时可以自行将文件上传至服务器
  145. this.pic.splice(this.pic.length - 1, 1, { status: 'uploading', message: '上传中...' });
  146. const { file } = data;
  147. const res = await this.taskUpload(file);
  148. const { uri } = res.data;
  149. if (uri) this.pic.splice(this.pic.length - 1, 1, { url: uri });
  150. },
  151. uploadSuccess({ type, data }) {
  152. this.$set(this.pic, 0, { uri: data.uri });
  153. },
  154. toDelete(index) {
  155. this.pic.splice(index, 1);
  156. },
  157. },
  158. computed: {
  159. ...mapState(['user']),
  160. id() {
  161. return this.$route.query.id;
  162. },
  163. lessonid() {
  164. return this.$route.query.lessonid;
  165. },
  166. name() {
  167. return this.$route.query.name;
  168. },
  169. },
  170. };
  171. </script>
  172. <style lang="less" scoped>
  173. .style {
  174. width: 100%;
  175. min-height: 667px;
  176. position: relative;
  177. background-color: #f9fafc;
  178. }
  179. .top {
  180. height: 46px;
  181. overflow: hidden;
  182. }
  183. .main {
  184. min-height: 570px;
  185. }
  186. .main .leaveBtn {
  187. text-align: center;
  188. padding: 15px 0;
  189. }
  190. </style>