double.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <view class="container">
  3. <uni-forms ref="baseForm" :modelValue="formData" :label-width="100">
  4. <uni-forms-item v-for="item in fileds" :label="item.title" :name="item.name" :key="item.name">
  5. <uni-easyinput v-if="!item.formatter && !item.type" type="text" v-model="formData[item.name]" :placeholder="`请输入${item.title}`" />
  6. <uni-data-checkbox v-model="formData[item.name]" v-if="item.formatter && item.type == 'checkbox'" :multiple="item.multiple || false" :localdata="item.dict" />
  7. <uni-data-select v-model="formData[item.name]" v-if="item.formatter && item.type == 'select'" :localdata="item.dict"></uni-data-select>
  8. <view class="upVideo" v-if="item.type == 'video' && !formData[item.name]" @click="upVideo">
  9. <uni-icons color="#cacaca" class="videoIcon" type="videocam" size="30"></uni-icons>
  10. </view>
  11. <video controls v-if="item.type == 'video' && formData[item.name]" class="uploadVideo" :src="filesUrl + formData[item.name]"></video>
  12. </uni-forms-item>
  13. </uni-forms>
  14. <button class="btn" type="primary" @click="submitForm">提交</button>
  15. </view>
  16. </template>
  17. <script>
  18. import request from '../../api/report.js';
  19. import { BASE_URL } from '../../env.js';
  20. export default {
  21. data() {
  22. return {
  23. baseUrl: BASE_URL.url,
  24. filesUrl: BASE_URL.fileUrl,
  25. formData: {},
  26. fileds: [
  27. { name: 'name', title: '姓名' },
  28. { name: 'sex', title: '性别', formatter: 'dict:user_sex_type', type: 'checkbox' },
  29. { name: 'phone', title: '联系电话' },
  30. { name: 'workUnit', title: '工作单位' },
  31. { name: 'reportLocation', title: '报道位置' },
  32. { name: 'reportType', title: '报道类别', formatter: 'dict:baodao_type', type: 'select' },
  33. { name: 'residence', title: '居住地' },
  34. { name: 'content', title: '报道内容' },
  35. { name: 'videoPath', title: '影片', type: 'video' },
  36. ]
  37. }
  38. },
  39. async mounted() {
  40. const res = await request.getDyStatus();
  41. if (res.data) {
  42. uni.showToast({
  43. title: '不能重复报道',
  44. icon: 'error',
  45. duration: 2000,
  46. });
  47. setTimeout(() => {
  48. uni.navigateBack();
  49. }, 1000)
  50. return;
  51. }
  52. await this.setDict();
  53. },
  54. onShow() {},
  55. methods: {
  56. async setDict() {
  57. this.fileds = await Promise.all(this.fileds.map(async e => {
  58. if (e.formatter && e.formatter.includes('dict')) {
  59. const dictType = e.formatter.split(':')[1];
  60. const res = await request.getDict(dictType);
  61. if (res.code == 200) e.dict = res.data.map(l => ({ ...l, value: l.dictValue, text: l.dictLabel }));
  62. }
  63. return e;
  64. }));
  65. },
  66. async upVideo() {
  67. const _this = this;
  68. wx.chooseMedia({
  69. count: 1, //上传视频的个数
  70. mediaType:['video'], //限制上传的类型为video
  71. sourceType:['album', 'camera'], //视频选择来源
  72. maxDuration: 30, //拍摄限制时间
  73. camera: 'back', //采用后置摄像头
  74. success:function(res){
  75. const token = uni.getStorageSync('token');
  76. //获取临时存放的视频资源
  77. let tempFilePath=res.tempFiles[0].tempFilePath
  78. // 上传视频
  79. uni.uploadFile({
  80. url: _this.baseUrl + '/api/activity/photo',
  81. name: 'file',
  82. header: {
  83. 'Authorization': `Bearer ${token}`
  84. },
  85. filePath: tempFilePath,
  86. success: (uploadFileRes) => {
  87. const obj = JSON.parse(uploadFileRes.data);
  88. _this.$set(_this.formData, 'videoPath', obj.imgUrl);
  89. },
  90. fail() {
  91. wx.showToast({
  92. title: '上传失败'
  93. })
  94. },
  95. complete() {
  96. wx.hideLoading();
  97. }
  98. });
  99. },
  100. })
  101. },
  102. async submitForm() {
  103. const res = await request.submitdy(this.formData);
  104. if (res.code == 200) {
  105. uni.showToast({
  106. title: '提交成功',
  107. icon: 'success',
  108. duration: 2000,
  109. });
  110. setTimeout(() => {
  111. uni.navigateBack();
  112. }, 1000)
  113. }
  114. }
  115. },
  116. // 页面生命周期中onReachBottom(页面滚动到底部的事件)
  117. onReachBottom() {}
  118. }
  119. </script>
  120. <style>
  121. .uni-forms {
  122. width: 90%;
  123. margin: 10px auto;
  124. }
  125. .btn {
  126. width: 90%;
  127. margin: 0 auto;
  128. }
  129. .upVideo {
  130. width: 100px;
  131. height: 100px;
  132. border: 1px solid #cacaca;
  133. border-radius: 12px;
  134. }
  135. .videoIcon {
  136. display: block;
  137. text-align: center;
  138. line-height: 100px;
  139. }
  140. .uploadVideo {
  141. width: 100px;
  142. height: 100px;
  143. }
  144. </style>