addTravel.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <template>
  2. <view class="travel-item">
  3. <view class="travel-item-header">
  4. <text class="title">
  5. {{ title }}明细({{ index + 1 }})
  6. </text>
  7. <text v-if="length > 1" class="control" @click="handleDelete(index)">
  8. 删除
  9. </text>
  10. </view>
  11. <wk-form
  12. ref="form"
  13. :fields="fieldArr"
  14. class="form"
  15. @change="handlerChange" />
  16. <view v-if="type === 'examine'" class="img-content">
  17. <view class="img-content-title" @click="addFile">
  18. <view class="left">
  19. <image :src="$static('images/icon/image.png')" class="pic-icon" />
  20. 发票图片文件
  21. </view>
  22. <view class="right add-icon">
  23. <text class="wk wk-plus" />
  24. </view>
  25. </view>
  26. <wk-image-content
  27. v-if="imgList.length > 0"
  28. show-delete
  29. :list="imgList"
  30. @delete="deleteFile" />
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. /**
  36. * event
  37. * ------ 事件名 -- 参数 ------------------- 事件描述 -------------------
  38. * delete index(当前的序列号) 删除当前明细
  39. * type(类型)
  40. * change {Object} 明细发生改变
  41. * ---------------------------------------------
  42. *
  43. * props itemData {Object} 默认详情数据
  44. * type {String} 类型
  45. * index {Number} 当前序列号
  46. */
  47. import { FileDeleteById } from 'API/file'
  48. import Fields from 'UTIL/fields'
  49. import WkFile from '@/utils/file.js'
  50. import moment from 'moment'
  51. export default {
  52. name: 'TravelAdd',
  53. props: {
  54. itemData: {
  55. type: Object,
  56. default: () => {}
  57. },
  58. type: {
  59. type: String,
  60. default: 'travel'
  61. },
  62. index: {
  63. type: Number,
  64. default: 0
  65. },
  66. length: {
  67. type: Number,
  68. default: 0
  69. }
  70. },
  71. data() {
  72. // 交通工具
  73. const transportation = ['飞机', '火车', '汽车', '其他'];
  74. // 行程属性
  75. const trafficType = ['单程', '往返'];
  76. return {
  77. title: '',
  78. intDays: '',
  79. travelForm: [
  80. new Fields({name: '交通工具', formType: 'select', fieldName: 'vehicle', setting: transportation}),
  81. new Fields({name: '单程往返', formType: 'select', fieldName: 'trip', setting: trafficType}),
  82. new Fields({name: '出发城市', formType: 'text', fieldName: 'startAddress'}),
  83. new Fields({name: '目的城市', formType: 'text', fieldName: 'endAddress'}),
  84. new Fields({name: '开始时间', formType: 'datetime', fieldName: 'startTime'}),
  85. new Fields({name: '结束时间', formType: 'datetime', fieldName: 'endTime'}),
  86. new Fields({name: '时长(天)', formType: 'floatnumber', fieldName: 'duration', precisions: 1}),
  87. new Fields({name: '备注', formType: 'textarea', fieldName: 'description'}),
  88. ],
  89. examineForm: [
  90. new Fields({name: '出发城市', formType: 'text', fieldName: 'startAddress'}),
  91. new Fields({name: '目的城市', formType: 'text', fieldName: 'endAddress'}),
  92. new Fields({name: '开始时间', formType: 'datetime', fieldName: 'startTime'}),
  93. new Fields({name: '结束时间', formType: 'datetime', fieldName: 'endTime'}),
  94. new Fields({name: '交通费(元)', formType: 'floatnumber', fieldName: 'traffic', defaultValue: '0.00', precisions: 2}),
  95. new Fields({name: '住宿费(元)', formType: 'floatnumber', fieldName: 'stay', defaultValue: '0.00', precisions: 2}),
  96. new Fields({name: '餐饮费(元)', formType: 'floatnumber', fieldName: 'diet', defaultValue: '0.00', precisions: 2}),
  97. new Fields({name: '其他费用(元)', formType: 'floatnumber', fieldName: 'other', defaultValue: '0.00', precisions: 2}),
  98. new Fields({name: '费用明细描述', formType: 'textarea', fieldName: 'description'}),
  99. ],
  100. fieldArr: [],
  101. imgList: [],
  102. fileBatchId: null
  103. }
  104. },
  105. created() {
  106. if (this.type === 'travel') {
  107. this.fieldArr = this.travelForm
  108. this.title = '行程'
  109. } else if (this.type === 'examine') {
  110. this.fieldArr = this.examineForm
  111. this.title = '费用'
  112. }
  113. this.$nextTick(() => {
  114. // 给表单设置默认值
  115. if (this.itemData.img) {
  116. this.imgList = [].concat(this.itemData.img)
  117. if (this.imgList.length > 0) {
  118. this.fileBatchId = this.imgList[0].batchId
  119. }
  120. }
  121. this.setDefaultForm(this.itemData)
  122. })
  123. },
  124. methods: {
  125. /**
  126. * 设置默认值
  127. * @param {Object} data
  128. */
  129. setDefaultForm(data) {
  130. const form = {}
  131. this.fieldArr.forEach(o => {
  132. if (data.hasOwnProperty(o.fieldName)) {
  133. let val = data[o.fieldName]
  134. if (o.formType === 'select') {
  135. form[o.fieldName] = this.$isEmpty(data[o.fieldName]) ? [] : [{ label: val, value: val }]
  136. } else {
  137. form[o.fieldName] = data[o.fieldName]
  138. }
  139. }
  140. })
  141. console.log('form: ', form)
  142. this.$nextTick(() => {
  143. this.$refs.form.setForm(form)
  144. })
  145. },
  146. handleDelete(index) {
  147. this.$emit('delete', index)
  148. },
  149. addFile() {
  150. let params = {type: 'img'}
  151. if (this.fileBatchId) params.batchId = this.fileBatchId
  152. let fileObj = new WkFile(params)
  153. fileObj.choose().then(res => {
  154. this.imgList = this.imgList.concat(res)
  155. this.fileBatchId = res[0].batchId
  156. // console.log('res', res, this.imgList)
  157. this.handlerChange()
  158. })
  159. },
  160. deleteFile(index) {
  161. console.log('oooo', this.imgList, index)
  162. const fileId = this.imgList[index].fileId
  163. this.imgList.splice(index, 1)
  164. this.handlerChange()
  165. FileDeleteById({
  166. id: fileId
  167. }).then().catch()
  168. },
  169. calcTimeDiff(item) {
  170. // if (item.startTime && item.endTime) {
  171. // const startDate = moment(item.startTime)
  172. // const endDate = moment(item.endTime)
  173. // const diff = endDate.diff(startDate, 'hours')
  174. // let intDays = Math.floor(diff / 24)
  175. // const remainder = diff % 24
  176. // if (remainder >= 4 && remainder < 8) {
  177. // intDays += 0.5
  178. // } else if (remainder >= 8) {
  179. // intDays += 1
  180. // }
  181. // item.duration = intDays
  182. // if (this.intDays != intDays) {
  183. // this.intDays = intDays
  184. // this.$refs.form.setFormVal('duration', intDays)
  185. // }
  186. // }
  187. },
  188. handlerChange(data) {
  189. // console.log('args: ', data)
  190. this.$refs.form.getForm().then(res => {
  191. const data = res.entity
  192. if (this.imgList.length > 0 && this.fileBatchId) {
  193. data.batchId = this.fileBatchId
  194. data.img = this.imgList
  195. }
  196. if (this.type === 'travel') {
  197. this.calcTimeDiff(data)
  198. }
  199. this.$emit('change', data, this.index)
  200. }).catch()
  201. }
  202. }
  203. }
  204. </script>
  205. <style scoped lang="scss">
  206. .form {
  207. margin: 0;
  208. padding-bottom: 20rpx;
  209. }
  210. .travel-item {
  211. .travel-item-header {
  212. font-size: 28rpx;
  213. color: $gray;
  214. padding: 18rpx 30rpx;
  215. background-color: #F3F5FA;
  216. @include left;
  217. .title {
  218. flex: 1;
  219. }
  220. .control {
  221. color: $theme-color;
  222. }
  223. }
  224. .img-content {
  225. background-color: white;
  226. overflow: hidden;
  227. padding: 0 30rpx 20rpx;
  228. margin-top: -20rpx;
  229. .img-content-title {
  230. padding: 15rpx 0;
  231. @include left;
  232. .left {
  233. flex: 1;
  234. @include left;
  235. .pic-icon {
  236. width: 38rpx;
  237. height: 38rpx;
  238. margin-right: 15rpx;
  239. }
  240. }
  241. .add-icon {
  242. width: 38rpx;
  243. height: 38rpx;
  244. background-color: #E1E1E1;
  245. border-radius: 50%;
  246. @include center;
  247. .wk {
  248. font-size: $wk-font-mini;
  249. line-height: normal;
  250. color: white;
  251. }
  252. }
  253. }
  254. }
  255. }
  256. </style>