double.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <view class="container">
  3. <uni-forms ref="baseForm" :modelValue="formData" :rules="rules" :label-width="80">
  4. <uni-forms-item v-for="item in fileds" :label="item.title" :name="item.name" :key="item.name" required>
  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 :disabled="btnText == '已报到'" class="btn" type="primary" @click="submitForm">{{ btnText }}</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. btnText: '提交',
  24. baseUrl: BASE_URL.url,
  25. filesUrl: BASE_URL.fileUrl,
  26. formData: {},
  27. fileds: [
  28. { name: 'name', title: '姓名' },
  29. { name: 'sex', title: '性别', formatter: 'dict:user_sex_type', type: 'checkbox' },
  30. { name: 'phone', title: '联系电话' },
  31. { name: 'workUnit', title: '工作单位' },
  32. { name: 'reportLocation', title: '报到位置' },
  33. { name: 'reportType', title: '报到类别', formatter: 'dict:baodao_type', type: 'select' },
  34. { name: 'residence', title: '居住地' },
  35. { name: 'content', title: '报到内容' },
  36. // { name: 'videoPath', title: '影片', type: 'video' },
  37. ],
  38. rules: {
  39. name: {
  40. rules:[{ required: true, errorMessage: '请填写姓名' }]
  41. },
  42. sex: {
  43. rules:[{ required: true, errorMessage: '请选择性别' }]
  44. },
  45. phone: {
  46. rules:[{ required: true, errorMessage: '请填写联系电话' }]
  47. },
  48. workUnit: {
  49. rules:[{ required: true, errorMessage: '请填写工作单位' }]
  50. },
  51. reportLocation: {
  52. rules:[{ required: true, errorMessage: '请填写报到位置' }]
  53. },
  54. reportType: {
  55. rules:[{ required: true, errorMessage: '请选择报到类别' }]
  56. },
  57. residence: {
  58. rules:[{ required: true, errorMessage: '请填写居住地' }]
  59. },
  60. content: {
  61. rules:[{ required: true, errorMessage: '请填写报到内容' }]
  62. },
  63. // videoPath: {
  64. // rules:[{ required: true, errorMessage: '请上传影片' }]
  65. // }
  66. }
  67. }
  68. },
  69. async mounted() {
  70. const res = await request.getDyStatus();
  71. if (res.data) {
  72. this.formData = { ...res.data, sex: String(res.data.sex), reportType: String(res.data.reportType) };;
  73. this.btnText = '已报到';
  74. }
  75. await this.setDict();
  76. },
  77. onShow() {},
  78. methods: {
  79. async setDict() {
  80. this.fileds = await Promise.all(this.fileds.map(async e => {
  81. if (e.formatter && e.formatter.includes('dict')) {
  82. const dictType = e.formatter.split(':')[1];
  83. const res = await request.getDict(dictType);
  84. if (res.code == 200) e.dict = res.data.map(l => ({ ...l, value: l.dictValue, text: l.dictLabel }));
  85. }
  86. return e;
  87. }));
  88. },
  89. async upVideo() {
  90. const _this = this;
  91. wx.chooseMedia({
  92. count: 1, //上传视频的个数
  93. mediaType:['video'], //限制上传的类型为video
  94. sourceType:['album', 'camera'], //视频选择来源
  95. maxDuration: 30, //拍摄限制时间
  96. camera: 'back', //采用后置摄像头
  97. success:function(res){
  98. const token = uni.getStorageSync('token');
  99. //获取临时存放的视频资源
  100. let tempFilePath=res.tempFiles[0].tempFilePath
  101. // 上传视频
  102. uni.uploadFile({
  103. url: _this.baseUrl + '/activity/photo',
  104. name: 'file',
  105. header: {
  106. 'Authorization': `Bearer ${token}`
  107. },
  108. filePath: tempFilePath,
  109. success: (uploadFileRes) => {
  110. const obj = JSON.parse(uploadFileRes.data);
  111. _this.$set(_this.formData, 'videoPath', obj.imgUrl);
  112. },
  113. fail() {
  114. wx.showToast({
  115. title: '上传失败'
  116. })
  117. },
  118. complete() {
  119. wx.hideLoading();
  120. }
  121. });
  122. },
  123. })
  124. },
  125. submitForm() {
  126. this.$refs.baseForm.validate().then(async valid=>{
  127. const res = await request.submitdy(this.formData);
  128. if (res.code == 200) {
  129. uni.showToast({
  130. title: '提交成功',
  131. icon: 'success',
  132. duration: 2000,
  133. });
  134. setTimeout(() => {
  135. uni.navigateBack();
  136. }, 1000)
  137. }
  138. }).catch(err =>{
  139. console.log('表单错误信息:', err);
  140. })
  141. }
  142. },
  143. // 页面生命周期中onReachBottom(页面滚动到底部的事件)
  144. onReachBottom() {}
  145. }
  146. </script>
  147. <style>
  148. .uni-forms {
  149. width: 90%;
  150. margin: 10px auto;
  151. }
  152. .btn {
  153. width: 90%;
  154. margin: 0 auto;
  155. }
  156. .upVideo {
  157. width: 100px;
  158. height: 100px;
  159. border: 1px solid #cacaca;
  160. border-radius: 12px;
  161. }
  162. .videoIcon {
  163. display: block;
  164. text-align: center;
  165. line-height: 100px;
  166. }
  167. .uploadVideo {
  168. width: 100px;
  169. height: 100px;
  170. }
  171. </style>