caseCollection.vue 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. <template>
  2. <view class="container">
  3. <image class="bg" src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/images/bg_qestion.png" mode="widthFix"></image>
  4. <view class="fixed-top-box" :style="{background: bgColor }">
  5. <view class="status_bar" :style="{height: statusBarHeight}"></view>
  6. <view class="back-box" @click="goBack">
  7. <image src="@/static/image/back.png" mode=""></image>
  8. <text class="title">问卷调查</text>
  9. <text></text>
  10. </view>
  11. </view>
  12. <scroll-view class="content" scroll-y>
  13. <!-- 表单标题 -->
  14. <view class="form-header">
  15. <view class="form-title">{{ formTitle }}</view>
  16. <view class="form-tips">
  17. <view class="tip-item">请您根据患者真实情况选择并填写</view>
  18. <view class="tip-item">我们承诺对您及患者所提供的所有信息严格保密</view>
  19. </view>
  20. </view>
  21. <!-- 动态表单内容 -->
  22. <view class="form-section">
  23. <view
  24. class="form-item"
  25. v-for="(field, index) in formFields"
  26. :key="getFieldKey(field, index)">
  27. <view class="form-label">
  28. <text class="required" v-if="field.__config__.required">*</text>
  29. <text>{{ index + 1 }}、{{ field.__config__.label }}</text>
  30. </view>
  31. <!-- 单行文本输入 -->
  32. <template v-if="field.__config__.tag === 'el-input' && field.type !== 'textarea'">
  33. <input
  34. class="form-input"
  35. :value="formData[field.__vModel__] || ''"
  36. @input="(e) => onInputChange(field, e)"
  37. :placeholder="field.placeholder || '请输入' + field.__config__.label"
  38. type="text"
  39. :disabled="field.disabled"
  40. placeholder-class="text-placeholder"
  41. />
  42. </template>
  43. <!-- 多行文本输入 -->
  44. <template v-else-if="field.__config__.tag === 'el-input' && field.type === 'textarea'">
  45. <textarea
  46. class="form-textarea"
  47. :value="formData[field.__vModel__] || ''"
  48. @input="(e) => onInputChange(field, e)"
  49. :placeholder="field.placeholder || '请输入' + field.__config__.label"
  50. :maxlength="field.maxlength"
  51. :disabled="field.disabled"
  52. :auto-height="field.autosize ? true : false"
  53. placeholder-class="text-placeholder"
  54. />
  55. </template>
  56. <!-- 计数器 -->
  57. <template v-else-if="field.__config__.tag === 'el-input-number'">
  58. <view class="input-number-wrapper">
  59. <view class="input-number-btn" @click="(e) => decreaseNumber(field, index)" :class="{ disabled: isNumberMin(field) }">-</view>
  60. <input
  61. class="form-input input-number-input"
  62. :value="formData[field.__vModel__] || ''"
  63. @input="(e) => onNumberInputChange(field, e)"
  64. type="number"
  65. :placeholder="field.placeholder || '请输入' + field.__config__.label"
  66. :disabled="field.disabled"
  67. placeholder-class="text-placeholder"
  68. />
  69. <view class="input-number-btn" @click="(e) => increaseNumber(field, index)" :class="{ disabled: isNumberMax(field) }">+</view>
  70. </view>
  71. </template>
  72. <!-- 下拉选择 -->
  73. <template v-else-if="field.__config__.tag === 'el-select'">
  74. <picker
  75. mode="selector"
  76. :range="getSelectOptions(field)"
  77. range-key="label"
  78. :value="getSelectIndex(field)"
  79. @change="(e) => onSelectChange(field, e)">
  80. <view class="form-input picker-input" :class="{ placeholder: !formData[field.__vModel__] }">
  81. {{ getSelectLabel(field) || field.placeholder || '请选择' + field.__config__.label }}
  82. <image class="w48 h48" src="@/static/image/icon_my_more.png" mode=""></image>
  83. </view>
  84. </picker>
  85. </template>
  86. <!-- 滑块 -->
  87. <template v-else-if="field.__config__.tag === 'el-slider'">
  88. <view class="slider-wrapper">
  89. <slider
  90. :value="formData[field.__vModel__] || field.__config__.defaultValue || 0"
  91. :min="field.min || 0"
  92. :max="field.max || 100"
  93. :step="field.step || 1"
  94. :show-value="true"
  95. activeColor="#388BFF"
  96. @change="(e) => onSliderChange(field, e)"
  97. />
  98. </view>
  99. </template>
  100. <!-- 多选框组 -->
  101. <template v-else-if="field.__config__.tag === 'el-checkbox-group'">
  102. <checkbox-group @change="(e) => onCheckboxChange(field, e)" class="checkbox-group">
  103. <label
  104. class="checkbox-item"
  105. v-for="(option, optIndex) in getSelectOptions(field)"
  106. :key="optIndex">
  107. <checkbox
  108. :value="option.value"
  109. :checked="isCheckboxChecked(field, option.value)"
  110. color="#388BFF"
  111. />
  112. <text>{{ option.label }}</text>
  113. </label>
  114. </checkbox-group>
  115. </template>
  116. <!-- 单选框组 -->
  117. <template v-else-if="field.__config__.tag === 'el-radio-group'">
  118. <radio-group @change="(e) => onRadioChange(field, e)" class="radio-group">
  119. <label
  120. class="radio-item"
  121. v-for="(option, optIndex) in getSelectOptions(field)"
  122. :key="optIndex">
  123. <radio
  124. :value="option.value"
  125. :checked="formData[field.__vModel__] == option.value"
  126. color="#388BFF"
  127. />
  128. <text>{{ option.label }}</text>
  129. </label>
  130. </radio-group>
  131. </template>
  132. <!-- 文件上传 -->
  133. <template v-else-if="field.__config__.tag === 'el-upload'">
  134. <view class="upload-section">
  135. <view class="upload-item" v-for="(file, fileIndex) in getUploadFiles(field)" :key="fileIndex">
  136. <image class="uploaded-image" :src="file" mode="aspectFill" v-if="isImageFile(file)"></image>
  137. <view class="uploaded-file" v-else>
  138. <text class="file-name">{{ getFileName(file) }}</text>
  139. </view>
  140. <view class="delete-btn" @click="(e) => removeUploadFile(field, fileIndex, index)">×</view>
  141. </view>
  142. <view class="upload-item upload-placeholder" @click="(e) => chooseUploadFile(field, index)">
  143. <image class="w48 h48" src="@/static/image/icon_camera1.png" mode=""></image>
  144. <text class="upload-text">{{ field.__config__.buttonText || '点击上传' }}</text>
  145. </view>
  146. </view>
  147. </template>
  148. <!-- 开关 -->
  149. <template v-else-if="field.__config__.tag === 'el-switch'">
  150. <switch
  151. :checked="formData[field.__vModel__] || false"
  152. :disabled="field.disabled"
  153. :color="field['active-color'] || '#388BFF'"
  154. @change="(e) => onSwitchChange(field, e)"
  155. />
  156. </template>
  157. <!-- 时间选择 -->
  158. <template v-else-if="field.__config__.tag === 'el-time-picker' && !field['is-range']">
  159. <picker
  160. mode="time"
  161. :value="formData[field.__vModel__] || ''"
  162. @change="(e) => onTimePickerChange(field, e)">
  163. <view class="form-input picker-input" :class="{ placeholder: !formData[field.__vModel__] }">
  164. {{ formData[field.__vModel__] || field.placeholder || '请选择' + field.__config__.label }}
  165. <!-- <image class="w48 h48" src="@/static/image/icon_my_more.png" mode=""></image> -->
  166. </view>
  167. </picker>
  168. </template>
  169. <!-- 时间范围 -->
  170. <template v-else-if="field.__config__.tag === 'el-time-picker' && field['is-range']">
  171. <view class="time-range-wrapper">
  172. <picker
  173. mode="time"
  174. :value="getTimeRangeStart(field)"
  175. @change="(e) => onTimeRangeStartChange(field, e)">
  176. <view class="form-input picker-input time-range-input" :class="{ placeholder: !getTimeRangeStart(field) }">
  177. {{ getTimeRangeStart(field) || field['start-placeholder'] || '开始时间' }}
  178. <!-- <image class="w48 h48" src="@/static/image/icon_my_more.png" mode=""></image> -->
  179. </view>
  180. </picker>
  181. <text class="time-range-separator">{{ field['range-separator'] || '至' }}</text>
  182. <picker
  183. mode="time"
  184. :value="getTimeRangeEnd(field)"
  185. @change="(e) => onTimeRangeEndChange(field, e)">
  186. <view class="form-input picker-input time-range-input" :class="{ placeholder: !getTimeRangeEnd(field) }">
  187. {{ getTimeRangeEnd(field) || field['end-placeholder'] || '结束时间' }}
  188. <!-- <image class="w48 h48" src="@/static/image/icon_my_more.png" mode=""></image> -->
  189. </view>
  190. </picker>
  191. </view>
  192. </template>
  193. <!-- 日期选择 -->
  194. <template v-else-if="field.__config__.tag === 'el-date-picker' && field.type !== 'daterange'">
  195. <picker
  196. mode="date"
  197. :value="formData[field.__vModel__] || ''"
  198. :start="field['start-date'] || ''"
  199. :end="field['end-date'] || ''"
  200. @change="(e) => onDatePickerChange(field, e)">
  201. <view class="form-input picker-input" :class="{ placeholder: !formData[field.__vModel__] }">
  202. {{ formData[field.__vModel__] || field.placeholder || '请选择' + field.__config__.label }}
  203. <!-- <image class="w48 h48" src="@/static/image/icon_my_more.png" mode=""></image> -->
  204. </view>
  205. </picker>
  206. </template>
  207. <!-- 日期范围 -->
  208. <template v-else-if="field.__config__.tag === 'el-date-picker' && field.type === 'daterange'">
  209. <view class="date-range-wrapper">
  210. <picker
  211. mode="date"
  212. :value="getDateRangeStart(field)"
  213. @change="(e) => onDateRangeStartChange(field, e)">
  214. <view class="form-input picker-input date-range-input" :class="{ placeholder: !getDateRangeStart(field) }">
  215. {{ getDateRangeStart(field) || field['start-placeholder'] || '开始日期' }}
  216. <!-- <image class="w48 h48" src="@/static/image/icon_my_more.png" mode=""></image> -->
  217. </view>
  218. </picker>
  219. <text class="date-range-separator">{{ field['range-separator'] || '至' }}</text>
  220. <picker
  221. mode="date"
  222. :value="getDateRangeEnd(field)"
  223. @change="(e) => onDateRangeEndChange(field, e)">
  224. <view class="form-input picker-input date-range-input" :class="{ placeholder: !getDateRangeEnd(field) }">
  225. {{ getDateRangeEnd(field) || field['end-placeholder'] || '结束日期' }}
  226. <!-- <image class="w48 h48" src="@/static/image/icon_my_more.png" mode=""></image> -->
  227. </view>
  228. </picker>
  229. </view>
  230. </template>
  231. <!-- 评分 -->
  232. <template v-else-if="field.__config__.tag === 'el-rate'">
  233. <view class="rate-wrapper">
  234. <view
  235. class="rate-star"
  236. v-for="(star, starIndex) in (field.max || 5)"
  237. :key="starIndex"
  238. @click="(e) => setRate(field, starIndex + 1, index)"
  239. :class="{ active: (formData[field.__vModel__] || 0) >= (starIndex + 1) }">
  240. </view>
  241. <text class="rate-text" v-if="field['show-score']">{{ formData[field.__vModel__] || 0 }}分</text>
  242. </view>
  243. </template>
  244. </view>
  245. </view>
  246. </scroll-view>
  247. <!-- 提交按钮 -->
  248. <view class="submit-btn" @click="handleSubmit">提交</view>
  249. </view>
  250. </template>
  251. <script>
  252. import { submitCaseCollection, getCaseDetail } from '@/api/medicationSurvey.js'
  253. import defaultFormConfig from './formConfig.js'
  254. export default {
  255. data() {
  256. return {
  257. top: 0,
  258. statusBarHeight: uni.getSystemInfoSync().statusBarHeight + 'px',
  259. questionnaireId: '',
  260. formTitle: '病例收集',
  261. formFields: [],
  262. formData: {},
  263. caseDetail: null // 保存详情数据,用于获取 company_id, table_name 等
  264. }
  265. },
  266. onLoad(options) {
  267. if (options.id) {
  268. this.questionnaireId = options.id
  269. }
  270. if (options.title) {
  271. this.formTitle = decodeURIComponent(options.title)
  272. }
  273. this.loadDetail()
  274. },
  275. onPageScroll(e) {
  276. this.top = e.scrollTop
  277. },
  278. computed: {
  279. bgColor() {
  280. const opacity = Math.min(this.top / 30, 1)
  281. return `rgba(255, 255, 255, ${opacity})`
  282. }
  283. },
  284. methods: {
  285. getFieldKey(field, index) {
  286. // 在非 H5 平台,:key 不支持表达式,需要使用方法来生成 key
  287. return field && field.__config__ && field.__config__.formId ? field.__config__.formId : `field_${index}`
  288. },
  289. goBack() {
  290. uni.navigateBack()
  291. },
  292. initFormData() {
  293. // 初始化表单数据
  294. this.formFields.forEach(field => {
  295. const vModel = field.__vModel__
  296. if (!vModel) {
  297. console.warn('字段缺少 __vModel__ 属性', field)
  298. return
  299. }
  300. // 如果字段已存在值,跳过初始化(保留已有数据)
  301. if (this.formData[vModel] !== undefined && this.formData[vModel] !== null) {
  302. return
  303. }
  304. const tag = field.__config__.tag
  305. const defaultValue = field.__config__.defaultValue
  306. // 根据字段类型设置默认值
  307. switch (tag) {
  308. // 单行文本输入
  309. case 'el-input':
  310. if (field.type === 'textarea') {
  311. // 多行文本
  312. this.$set(this.formData, vModel, defaultValue !== undefined ? defaultValue : '')
  313. } else {
  314. // 单行文本
  315. this.$set(this.formData, vModel, defaultValue !== undefined ? defaultValue : '')
  316. }
  317. break
  318. // 数字输入(计数器)
  319. case 'el-input-number':
  320. if (defaultValue !== undefined) {
  321. this.$set(this.formData, vModel, Number(defaultValue))
  322. } else {
  323. // 根据 min 值设置默认值,如果没有 min 则默认为 0
  324. const minValue = field.min !== undefined ? Number(field.min) : null
  325. this.$set(this.formData, vModel, minValue)
  326. }
  327. break
  328. // 下拉选择
  329. case 'el-select':
  330. this.$set(this.formData, vModel, defaultValue !== undefined ? defaultValue : '')
  331. break
  332. // 滑块
  333. case 'el-slider':
  334. if (defaultValue !== undefined) {
  335. this.$set(this.formData, vModel, Number(defaultValue))
  336. } else {
  337. // 默认值为 min 值,如果没有 min 则默认为 0
  338. const minValue = field.min !== undefined ? Number(field.min) : 0
  339. this.$set(this.formData, vModel, minValue)
  340. }
  341. break
  342. // 多选框组
  343. case 'el-checkbox-group':
  344. if (defaultValue !== undefined && Array.isArray(defaultValue)) {
  345. this.$set(this.formData, vModel, [...defaultValue])
  346. } else {
  347. this.$set(this.formData, vModel, [])
  348. }
  349. break
  350. // 单选框组
  351. case 'el-radio-group':
  352. this.$set(this.formData, vModel, defaultValue !== undefined ? defaultValue : '')
  353. break
  354. // 文件上传
  355. case 'el-upload':
  356. if (defaultValue !== undefined && Array.isArray(defaultValue)) {
  357. this.$set(this.formData, vModel, [...defaultValue])
  358. } else if (defaultValue !== undefined && typeof defaultValue === 'string') {
  359. // 如果是字符串(逗号分隔),转换为数组
  360. this.$set(this.formData, vModel, defaultValue.split(',').filter(item => item.trim()))
  361. } else {
  362. this.$set(this.formData, vModel, [])
  363. }
  364. break
  365. // 开关
  366. case 'el-switch':
  367. this.$set(this.formData, vModel, defaultValue !== undefined ? Boolean(defaultValue) : false)
  368. break
  369. // 时间选择
  370. case 'el-time-picker':
  371. if (field['is-range']) {
  372. // 时间范围
  373. if (defaultValue !== undefined && Array.isArray(defaultValue)) {
  374. this.$set(this.formData, vModel, [...defaultValue])
  375. } else {
  376. this.$set(this.formData, vModel, ['', ''])
  377. }
  378. } else {
  379. // 单个时间
  380. this.$set(this.formData, vModel, defaultValue !== undefined ? defaultValue : '')
  381. }
  382. break
  383. // 日期选择
  384. case 'el-date-picker':
  385. if (field.type === 'daterange') {
  386. // 日期范围
  387. if (defaultValue !== undefined && Array.isArray(defaultValue)) {
  388. this.$set(this.formData, vModel, [...defaultValue])
  389. } else {
  390. this.$set(this.formData, vModel, ['', ''])
  391. }
  392. } else {
  393. // 单个日期
  394. this.$set(this.formData, vModel, defaultValue !== undefined ? defaultValue : '')
  395. }
  396. break
  397. // 评分
  398. case 'el-rate':
  399. if (defaultValue !== undefined) {
  400. this.$set(this.formData, vModel, Number(defaultValue))
  401. } else {
  402. this.$set(this.formData, vModel, 0)
  403. }
  404. break
  405. // 默认情况:字符串类型
  406. default:
  407. this.$set(this.formData, vModel, defaultValue !== undefined ? defaultValue : '')
  408. break
  409. }
  410. })
  411. },
  412. loadDefaultFormConfig() {
  413. // 加载本地默认表单配置
  414. try {
  415. if (defaultFormConfig && defaultFormConfig.fields && Array.isArray(defaultFormConfig.fields)) {
  416. this.formFields = defaultFormConfig.fields
  417. this.initFormData()
  418. console.log('已加载本地默认表单配置')
  419. return true
  420. }
  421. } catch (e) {
  422. console.error('加载本地默认表单配置失败', e)
  423. }
  424. return false
  425. },
  426. async loadDetail() {
  427. // if (!this.questionnaireId) {
  428. // // 如果没有问卷ID,使用默认配置
  429. // this.loadDefaultFormConfig()
  430. // return
  431. // }
  432. try {
  433. uni.showLoading({ title: '加载中...' })
  434. const res = await getCaseDetail({ id: this.questionnaireId })
  435. uni.hideLoading()
  436. if (res.code === 200 && res.data) {
  437. // 保存详情数据
  438. this.caseDetail = res.data
  439. // 设置表单标题
  440. this.formTitle = res.data.formName || this.formTitle
  441. let question = res.data.surveyQuestionnaireVersion
  442. let formConfigData = question.formJson || ''
  443. // 如果 formConfigData 是字符串,需要解析 JSON
  444. if (typeof formConfigData === 'string') {
  445. try {
  446. formConfigData = JSON.parse(formConfigData)
  447. } catch (e) {
  448. console.error('解析表单配置 JSON 失败', e)
  449. formConfigData = null
  450. }
  451. }
  452. // 设置表单字段
  453. if (formConfigData && formConfigData.fields && Array.isArray(formConfigData.fields)) {
  454. this.formFields = formConfigData.fields
  455. this.initFormData()
  456. } else {
  457. // 接口返回的数据格式不正确,使用默认配置
  458. console.warn('表单配置格式不正确,使用默认配置', formConfigData)
  459. if (!this.loadDefaultFormConfig()) {
  460. uni.showToast({
  461. icon: 'none',
  462. title: '表单配置加载失败'
  463. })
  464. }
  465. }
  466. // 如果有已保存的数据,填充表单
  467. if (res.data.formData) {
  468. Object.assign(this.formData, res.data.formData)
  469. }
  470. } else {
  471. // 接口返回错误,使用默认配置
  472. console.warn('接口返回错误,使用默认配置', res.msg)
  473. if (!this.loadDefaultFormConfig()) {
  474. uni.showToast({
  475. icon: 'none',
  476. title: res.msg || '加载失败,已使用默认配置'
  477. })
  478. }
  479. }
  480. } catch (e) {
  481. uni.hideLoading()
  482. console.error('加载详情失败,使用默认配置', e)
  483. // 接口请求失败,使用默认配置
  484. if (!this.loadDefaultFormConfig()) {
  485. uni.showToast({
  486. icon: 'none',
  487. title: '加载失败,已使用默认配置'
  488. })
  489. }
  490. }
  491. },
  492. getSelectOptions(field) {
  493. return field.__slot__?.options || []
  494. },
  495. getSelectIndex(field) {
  496. const value = this.formData[field.__vModel__]
  497. const options = this.getSelectOptions(field)
  498. const index = options.findIndex(opt => opt.value == value)
  499. return index >= 0 ? index : 0
  500. },
  501. getSelectLabel(field) {
  502. const value = this.formData[field.__vModel__]
  503. const options = this.getSelectOptions(field)
  504. const option = options.find(opt => opt.value == value)
  505. return option ? option.label : ''
  506. },
  507. onInputChange(field, e) {
  508. const value = e.detail.value || e.target.value || ''
  509. this.$set(this.formData, field.__vModel__, value)
  510. },
  511. onNumberInputChange(field, e) {
  512. const value = e.detail.value || e.target.value || ''
  513. const numValue = value === '' ? null : Number(value)
  514. // 如果输入的不是有效数字,保持原值
  515. if (value !== '' && isNaN(numValue)) {
  516. return
  517. }
  518. this.$set(this.formData, field.__vModel__, numValue)
  519. },
  520. onSelectChange(field, e) {
  521. const options = this.getSelectOptions(field)
  522. const index = e.detail.value
  523. if (options[index]) {
  524. this.$set(this.formData, field.__vModel__, options[index].value)
  525. }
  526. },
  527. onSliderChange(field, e) {
  528. this.$set(this.formData, field.__vModel__, e.detail.value)
  529. },
  530. onCheckboxChange(field, e) {
  531. this.$set(this.formData, field.__vModel__, e.detail.value)
  532. },
  533. onRadioChange(field, e) {
  534. this.$set(this.formData, field.__vModel__, e.detail.value)
  535. },
  536. isCheckboxChecked(field, value) {
  537. const checkedValues = this.formData[field.__vModel__] || []
  538. return checkedValues.includes(value)
  539. },
  540. getUploadFiles(field) {
  541. if (!field || !field.__vModel__) return []
  542. const files = this.formData[field.__vModel__] || []
  543. return Array.isArray(files) ? files : []
  544. },
  545. isImageFile(file) {
  546. if (typeof file === 'string') {
  547. return /\.(jpg|jpeg|png|gif|bmp|webp)$/i.test(file) || file.startsWith('data:image')
  548. }
  549. return false
  550. },
  551. getFileName(file) {
  552. if (typeof file === 'string') {
  553. const parts = file.split('/')
  554. return parts[parts.length - 1]
  555. }
  556. return '文件'
  557. },
  558. chooseUploadFile(field, formIndex) {
  559. // 如果 field 未定义,通过索引获取
  560. if (!field && formIndex !== undefined) {
  561. field = this.formFields[formIndex]
  562. }
  563. if (!field || !field.__vModel__) {
  564. console.error('上传字段未找到')
  565. return
  566. }
  567. const maxCount = field.multiple ? 9 : 3
  568. const currentFiles = this.getUploadFiles(field)
  569. const remaining = maxCount - currentFiles.length
  570. if (remaining <= 0) {
  571. uni.showToast({
  572. icon: 'none',
  573. title: '已达到最大上传数量'
  574. })
  575. return
  576. }
  577. const vm = this
  578. uni.chooseImage({
  579. //count: remaining,
  580. sizeType: ['compressed'],
  581. sourceType: ['album', 'camera'],
  582. success: (res) => {
  583. if (!res.tempFilePaths || res.tempFilePaths.length === 0) {
  584. uni.showToast({
  585. icon: 'none',
  586. title: '未选择文件'
  587. })
  588. return
  589. }
  590. uni.showLoading({ title: '上传中...' })
  591. const files = vm.formData[field.__vModel__] || []
  592. const requestPath = uni.getStorageSync('requestPath')
  593. const totalCount = res.tempFilePaths.length
  594. let completedCount = 0
  595. const uploadedUrls = []
  596. // 循环上传所有选择的图片
  597. res.tempFilePaths.forEach((filePath, index) => {
  598. uni.uploadFile({
  599. url: `${requestPath}/app/common/uploadOSS`,
  600. filePath: filePath,
  601. name: 'file',
  602. formData: {},
  603. success: (uploadRes) => {
  604. completedCount++
  605. try {
  606. const result = typeof uploadRes.data === 'string' ? JSON.parse(uploadRes.data) : uploadRes.data
  607. if (result.code == 200 && result.data && result.data.url) {
  608. uploadedUrls.push(result.data.url)
  609. } else if (result.code == 200 && result.url) {
  610. // 兼容不同的返回格式
  611. uploadedUrls.push(result.url)
  612. }
  613. } catch (e) {
  614. console.error('解析上传结果失败', e)
  615. }
  616. // 所有文件上传完成后,更新表单数据
  617. if (completedCount === totalCount) {
  618. uni.hideLoading()
  619. if (uploadedUrls.length > 0) {
  620. vm.$set(vm.formData, field.__vModel__, [...files, ...uploadedUrls])
  621. uni.showToast({
  622. icon: 'success',
  623. title: uploadedUrls.length === totalCount ? '上传成功' : `成功上传${uploadedUrls.length}/${totalCount}个文件`
  624. })
  625. } else {
  626. uni.showToast({
  627. icon: 'none',
  628. title: '上传失败'
  629. })
  630. }
  631. }
  632. },
  633. fail: (err) => {
  634. completedCount++
  635. console.error('上传失败', err)
  636. // 所有文件上传完成后,更新表单数据
  637. if (completedCount === totalCount) {
  638. uni.hideLoading()
  639. if (uploadedUrls.length > 0) {
  640. vm.$set(vm.formData, field.__vModel__, [...files, ...uploadedUrls])
  641. uni.showToast({
  642. icon: 'none',
  643. title: `成功上传${uploadedUrls.length}/${totalCount}个文件`
  644. })
  645. } else {
  646. uni.showToast({
  647. icon: 'none',
  648. title: '上传失败'
  649. })
  650. }
  651. }
  652. }
  653. })
  654. })
  655. },
  656. fail: (err) => {
  657. console.error('选择文件失败', err)
  658. uni.showToast({
  659. icon: 'none',
  660. title: '选择文件失败'
  661. })
  662. }
  663. })
  664. },
  665. removeUploadFile(field, fileIndex, formIndex) {
  666. // 如果 field 未定义,通过索引获取
  667. if (!field && formIndex !== undefined) {
  668. field = this.formFields[formIndex]
  669. }
  670. if (!field || !field.__vModel__) {
  671. console.error('上传字段未找到')
  672. return
  673. }
  674. const files = this.formData[field.__vModel__] || []
  675. if (fileIndex >= 0 && fileIndex < files.length) {
  676. files.splice(fileIndex, 1)
  677. this.$set(this.formData, field.__vModel__, files)
  678. }
  679. },
  680. // 计数器相关方法
  681. decreaseNumber(field, index) {
  682. // 如果 field 未定义,通过索引获取
  683. if (!field && index !== undefined) {
  684. field = this.formFields[index]
  685. }
  686. if (!field || !field.__vModel__) {
  687. console.error('计数器字段未找到')
  688. return
  689. }
  690. if (this.isNumberMin(field)) return
  691. const current = Number(this.formData[field.__vModel__] || 0)
  692. const step = field.step || 1
  693. const min = field.min !== undefined ? field.min : 0
  694. const newValue = Math.max(min, current - step)
  695. this.$set(this.formData, field.__vModel__, newValue)
  696. },
  697. increaseNumber(field, index) {
  698. // 如果 field 未定义,通过索引获取
  699. if (!field && index !== undefined) {
  700. field = this.formFields[index]
  701. }
  702. if (!field || !field.__vModel__) {
  703. console.error('计数器字段未找到')
  704. return
  705. }
  706. if (this.isNumberMax(field)) return
  707. const current = Number(this.formData[field.__vModel__] || 0)
  708. const step = field.step || 1
  709. const max = field.max !== undefined ? field.max : Infinity
  710. const newValue = Math.min(max, current + step)
  711. this.$set(this.formData, field.__vModel__, newValue)
  712. },
  713. isNumberMin(field) {
  714. if (!field || !field.__vModel__) return false
  715. const current = Number(this.formData[field.__vModel__] || 0)
  716. const min = field.min !== undefined ? field.min : 0
  717. return current <= min
  718. },
  719. isNumberMax(field) {
  720. if (!field || !field.__vModel__) return false
  721. const current = Number(this.formData[field.__vModel__] || 0)
  722. const max = field.max !== undefined ? field.max : Infinity
  723. return current >= max
  724. },
  725. // 开关相关方法
  726. onSwitchChange(field, e) {
  727. this.$set(this.formData, field.__vModel__, e.detail.value)
  728. },
  729. // 时间选择相关方法
  730. onTimePickerChange(field, e) {
  731. this.$set(this.formData, field.__vModel__, e.detail.value)
  732. },
  733. getTimeRangeStart(field) {
  734. const value = this.formData[field.__vModel__]
  735. if (Array.isArray(value) && value.length > 0) {
  736. return value[0]
  737. }
  738. return ''
  739. },
  740. getTimeRangeEnd(field) {
  741. const value = this.formData[field.__vModel__]
  742. if (Array.isArray(value) && value.length > 1) {
  743. return value[1]
  744. }
  745. return ''
  746. },
  747. onTimeRangeStartChange(field, e) {
  748. const value = this.formData[field.__vModel__] || []
  749. value[0] = e.detail.value
  750. this.$set(this.formData, field.__vModel__, [...value])
  751. },
  752. onTimeRangeEndChange(field, e) {
  753. const value = this.formData[field.__vModel__] || []
  754. value[1] = e.detail.value
  755. this.$set(this.formData, field.__vModel__, [...value])
  756. },
  757. // 日期选择相关方法
  758. onDatePickerChange(field, e) {
  759. this.$set(this.formData, field.__vModel__, e.detail.value)
  760. },
  761. getDateRangeStart(field) {
  762. const value = this.formData[field.__vModel__]
  763. if (Array.isArray(value) && value.length > 0) {
  764. return value[0]
  765. }
  766. return ''
  767. },
  768. getDateRangeEnd(field) {
  769. const value = this.formData[field.__vModel__]
  770. if (Array.isArray(value) && value.length > 1) {
  771. return value[1]
  772. }
  773. return ''
  774. },
  775. onDateRangeStartChange(field, e) {
  776. const value = this.formData[field.__vModel__] || []
  777. value[0] = e.detail.value
  778. this.$set(this.formData, field.__vModel__, [...value])
  779. },
  780. onDateRangeEndChange(field, e) {
  781. const value = this.formData[field.__vModel__] || []
  782. value[1] = e.detail.value
  783. this.$set(this.formData, field.__vModel__, [...value])
  784. },
  785. // 评分相关方法
  786. setRate(field, value, formIndex) {
  787. // 如果 field 未定义,通过索引获取
  788. if (!field && formIndex !== undefined) {
  789. field = this.formFields[formIndex]
  790. }
  791. if (!field || !field.__vModel__) {
  792. console.error('评分字段未找到')
  793. return
  794. }
  795. if (field.disabled) return
  796. this.$set(this.formData, field.__vModel__, value)
  797. },
  798. validateForm() {
  799. for (let field of this.formFields) {
  800. const value = this.formData[field.__vModel__]
  801. const fieldLabel = field.__config__.label || '该字段'
  802. const tag = field.__config__.tag
  803. const isRequired = field.__config__.required
  804. // 必填校验
  805. if (isRequired) {
  806. // 数组类型(多选框、文件上传、范围选择)
  807. if (tag === 'el-checkbox-group' || tag === 'el-upload') {
  808. const arr = tag === 'el-upload' ? this.getUploadFiles(field) : value
  809. if (!arr || !Array.isArray(arr) || arr.length === 0) {
  810. return { valid: false, message: tag === 'el-upload' ? `请上传${fieldLabel}` : `请选择${fieldLabel}` }
  811. }
  812. }
  813. // 范围选择(时间范围、日期范围)
  814. else if ((tag === 'el-time-picker' && field['is-range']) || (tag === 'el-date-picker' && field.type === 'daterange')) {
  815. const range = value || []
  816. if (!Array.isArray(range) || range.length < 2 || !range[0] || !range[1]) {
  817. return { valid: false, message: `请选择${fieldLabel}` }
  818. }
  819. }
  820. // 选择类型(下拉、单选框、时间、日期)
  821. else if (tag === 'el-select' || tag === 'el-radio-group' ||
  822. (tag === 'el-time-picker' && !field['is-range']) ||
  823. (tag === 'el-date-picker' && field.type !== 'daterange')) {
  824. if (!value && value !== 0 && value !== '0' && value !== false) {
  825. return { valid: false, message: `请选择${fieldLabel}` }
  826. }
  827. }
  828. // 文本输入
  829. else if (tag === 'el-input') {
  830. if (!value || (typeof value === 'string' && value.trim() === '')) {
  831. return { valid: false, message: `请输入${fieldLabel}` }
  832. }
  833. }
  834. // 数字类型(数字输入、滑块)
  835. else if (tag === 'el-input-number' || tag === 'el-slider') {
  836. if (value === null || value === undefined || value === '') {
  837. return { valid: false, message: tag === 'el-slider' ? `请设置${fieldLabel}` : `请输入${fieldLabel}` }
  838. }
  839. }
  840. // 评分
  841. else if (tag === 'el-rate') {
  842. if (!value || value === 0) {
  843. return { valid: false, message: `请为${fieldLabel}评分` }
  844. }
  845. }
  846. // 其他类型
  847. else if (!value && value !== 0 && value !== false) {
  848. return { valid: false, message: `请填写${fieldLabel}` }
  849. }
  850. }
  851. // 格式校验(必填和非必填都校验)
  852. if (value !== null && value !== undefined && value !== '') {
  853. // 文本长度校验
  854. if (tag === 'el-input' && typeof value === 'string') {
  855. if (field.maxlength && value.length > field.maxlength) {
  856. return { valid: false, message: `${fieldLabel}不能超过${field.maxlength}个字符` }
  857. }
  858. if (field.minlength && value.length < field.minlength) {
  859. return { valid: false, message: `${fieldLabel}不能少于${field.minlength}个字符` }
  860. }
  861. }
  862. // 数字范围校验
  863. else if ((tag === 'el-input-number' || tag === 'el-slider')) {
  864. const numValue = Number(value)
  865. if (!isNaN(numValue)) {
  866. if (field.min !== undefined && numValue < field.min) {
  867. return { valid: false, message: `${fieldLabel}不能小于${field.min}` }
  868. }
  869. if (field.max !== undefined && numValue > field.max) {
  870. return { valid: false, message: `${fieldLabel}不能大于${field.max}` }
  871. }
  872. }
  873. }
  874. }
  875. }
  876. return { valid: true }
  877. },
  878. async handleSubmit() {
  879. // 表单验证
  880. const validation = this.validateForm()
  881. if (!validation.valid) {
  882. uni.showToast({
  883. icon: 'none',
  884. title: validation.message
  885. })
  886. return
  887. }
  888. try {
  889. uni.showLoading({ title: '提交中...' })
  890. // 处理表单数据:将数组类型字段转换为逗号分隔的字符串
  891. const submitData = { ...this.formData }
  892. this.formFields.forEach(field => {
  893. const vModel = field.__vModel__
  894. const value = submitData[vModel]
  895. // 文件上传字段:数组转逗号分隔字符串
  896. if (field.__config__.tag === 'el-upload') {
  897. if (Array.isArray(value) && value.length > 0) {
  898. submitData[vModel] = value.join(',')
  899. } else if (value) {
  900. // 如果已经是字符串,保持不变
  901. submitData[vModel] = value
  902. } else {
  903. // 如果没有文件,设置为空字符串
  904. submitData[vModel] = ''
  905. }
  906. }
  907. // 多选框组:数组转逗号分隔字符串
  908. else if (field.__config__.tag === 'el-checkbox-group') {
  909. if (Array.isArray(value) && value.length > 0) {
  910. submitData[vModel] = value.join(',')
  911. } else if (value) {
  912. // 如果已经是字符串,保持不变
  913. submitData[vModel] = value
  914. } else {
  915. // 如果没有选择,设置为空字符串
  916. submitData[vModel] = ''
  917. }
  918. }
  919. })
  920. // 获取用户信息
  921. const userInfo = JSON.parse(uni.getStorageSync('userInfo') || '{}')
  922. const userName = userInfo.doctorName|| ''
  923. // 获取详情数据中的信息
  924. const companyId = this.caseDetail?.companyId
  925. const tableName = this.caseDetail?.surveyQuestionnaireVersion.tableName || ''
  926. const version = this.caseDetail?.surveyQuestionnaireVersion.versionNo || 0
  927. // 添加公用字段
  928. submitData.company_id = Number(companyId) || 0
  929. submitData.table_name = tableName
  930. submitData.user_id = userInfo.id || 0
  931. submitData.user_name = userName
  932. submitData.version_id = Number(version) || 0
  933. const res = await submitCaseCollection(submitData)
  934. uni.hideLoading()
  935. if (res.code === 200) {
  936. uni.showToast({
  937. icon: 'success',
  938. title: '提交成功'
  939. })
  940. setTimeout(() => {
  941. uni.navigateBack()
  942. }, 1500)
  943. } else {
  944. uni.showToast({
  945. icon: 'none',
  946. title: res.msg || '提交失败'
  947. })
  948. }
  949. } catch (e) {
  950. uni.hideLoading()
  951. uni.showToast({
  952. icon: 'none',
  953. title: '提交失败'
  954. })
  955. }
  956. }
  957. }
  958. }
  959. </script>
  960. <style lang="stylus">
  961. .text-placeholder {
  962. color: #C8C9CC !important;
  963. }
  964. checkbox .wx-checkbox-input {
  965. border-radius: 0 !important;
  966. }
  967. checkbox .wx-checkbox-input.wx-checkbox-input-checked {
  968. border-radius: 0 !important;
  969. }
  970. </style>
  971. <style lang="scss" scoped>
  972. .container {
  973. min-height: 100vh;
  974. display: flex;
  975. flex-direction: column;
  976. .bg {
  977. width: 100%;
  978. position: absolute;
  979. top: 0;
  980. left: 0;
  981. }
  982. }
  983. .status-bar {
  984. width: 100%;
  985. background: transparent;
  986. }
  987. .fixed-top-box {
  988. width: 100%;
  989. position: fixed;
  990. top: 0;
  991. left: 0;
  992. z-index: 1000;
  993. .back-box {
  994. height: 88upx;
  995. padding: 0 24upx;
  996. display: flex;
  997. align-items: center;
  998. justify-content: space-between;
  999. image {
  1000. width: 40upx;
  1001. height: 40upx;
  1002. }
  1003. .title {
  1004. font-family: PingFang SC, PingFang SC;
  1005. font-weight: 600;
  1006. font-size: 36rpx;
  1007. color: #333333;
  1008. }
  1009. }
  1010. }
  1011. .content {
  1012. margin-top: 168rpx;
  1013. flex: 1;
  1014. padding: 24rpx;
  1015. box-sizing: border-box;
  1016. }
  1017. .form-header {
  1018. margin-bottom: 32rpx;
  1019. .form-title {
  1020. font-family: PingFang SC, PingFang SC;
  1021. font-weight: 600;
  1022. font-size: 40rpx;
  1023. color: #333333;
  1024. margin-bottom: 32rpx;
  1025. text-align: center;
  1026. }
  1027. .form-tips {
  1028. .tip-item {
  1029. font-family: PingFang SC, PingFang SC;
  1030. font-weight: 400;
  1031. font-size: 28rpx;
  1032. color: #666666;
  1033. line-height: 44rpx;
  1034. text-align: center;
  1035. }
  1036. }
  1037. }
  1038. .form-section {
  1039. padding-bottom: 160rpx;
  1040. }
  1041. .form-item {
  1042. background: #fff;
  1043. padding: 24rpx 32rpx;
  1044. margin-bottom: 20rpx;
  1045. border-radius: 16rpx;
  1046. &:last-child {
  1047. margin-bottom: 0;
  1048. }
  1049. .form-label {
  1050. display: flex;
  1051. align-items: center;
  1052. font-size: 28rpx;
  1053. color: #333;
  1054. margin-bottom: 16rpx;
  1055. .required {
  1056. color: #FF5030;
  1057. margin-right: 4rpx;
  1058. }
  1059. }
  1060. .form-input {
  1061. width: 100%;
  1062. height: 72rpx;
  1063. padding: 0 24rpx;
  1064. border-radius: 12rpx;
  1065. border: 2rpx solid #F5F5F5;
  1066. font-size: 28rpx;
  1067. color: #333;
  1068. box-sizing: border-box;
  1069. &.placeholder {
  1070. color: #C8C9CC;
  1071. }
  1072. &.picker-input {
  1073. display: flex;
  1074. align-items: center;
  1075. justify-content: space-between;
  1076. }
  1077. }
  1078. .slider-wrapper {
  1079. padding: 20rpx 0;
  1080. }
  1081. .radio-group,
  1082. .checkbox-group {
  1083. display: flex;
  1084. flex-direction: column;
  1085. gap: 48rpx;
  1086. .radio-item,
  1087. .checkbox-item {
  1088. display: flex;
  1089. align-items: center;
  1090. gap: 16rpx;
  1091. font-size: 28rpx;
  1092. color: #333;
  1093. }
  1094. }
  1095. .form-textarea {
  1096. width: 100%;
  1097. min-height: 200rpx;
  1098. padding: 24rpx;
  1099. border-radius: 12rpx;
  1100. border: 2rpx solid #F5F5F5;
  1101. font-size: 28rpx;
  1102. color: #333;
  1103. box-sizing: border-box;
  1104. line-height: 1.6;
  1105. }
  1106. .input-number-wrapper {
  1107. display: flex;
  1108. align-items: center;
  1109. gap: 16rpx;
  1110. .input-number-btn {
  1111. width: 60rpx;
  1112. height: 60rpx;
  1113. display: flex;
  1114. align-items: center;
  1115. justify-content: center;
  1116. border: 2rpx solid #F5F5F5;
  1117. border-radius: 8rpx;
  1118. font-size: 32rpx;
  1119. color: #333;
  1120. background: #fff;
  1121. &.disabled {
  1122. opacity: 0.5;
  1123. color: #C8C9CC;
  1124. }
  1125. }
  1126. .input-number-input {
  1127. flex: 1;
  1128. }
  1129. }
  1130. .time-range-wrapper,
  1131. .date-range-wrapper {
  1132. display: flex;
  1133. align-items: center;
  1134. gap: 16rpx;
  1135. .time-range-input,
  1136. .date-range-input {
  1137. flex: 1;
  1138. }
  1139. .time-range-separator,
  1140. .date-range-separator {
  1141. font-size: 28rpx;
  1142. color: #666;
  1143. }
  1144. }
  1145. .rate-wrapper {
  1146. display: flex;
  1147. align-items: center;
  1148. gap: 16rpx;
  1149. .rate-star {
  1150. font-size: 48rpx;
  1151. color: #E0E0E0;
  1152. line-height: 1;
  1153. cursor: pointer;
  1154. &.active {
  1155. color: #FFD700;
  1156. }
  1157. }
  1158. .rate-text {
  1159. font-size: 28rpx;
  1160. color: #666;
  1161. }
  1162. }
  1163. .upload-section {
  1164. display: flex;
  1165. gap: 16rpx;
  1166. flex-wrap: wrap;
  1167. padding: 10rpx;
  1168. width: 100%;
  1169. .upload-item {
  1170. flex: 0 0 calc((100% - 32rpx) / 3);
  1171. width: calc((100% - 32rpx) / 3);
  1172. //height: calc((100% - 32rpx) / 3);
  1173. min-height: 200rpx;
  1174. border-radius: 8rpx;
  1175. overflow: hidden;
  1176. position: relative;
  1177. box-sizing: border-box;
  1178. .uploaded-image {
  1179. width: 100%;
  1180. height: 100%;
  1181. }
  1182. .uploaded-file {
  1183. width: 100%;
  1184. height: 100%;
  1185. background: #f5f5f5;
  1186. display: flex;
  1187. align-items: center;
  1188. justify-content: center;
  1189. padding: 16rpx;
  1190. box-sizing: border-box;
  1191. .file-name {
  1192. font-size: 24rpx;
  1193. color: #666;
  1194. text-align: center;
  1195. word-break: break-all;
  1196. }
  1197. }
  1198. .delete-btn {
  1199. position: absolute;
  1200. top: 0;
  1201. right:0;
  1202. width: 40rpx;
  1203. height: 40rpx;
  1204. background: rgba(0, 0, 0, 0.5);
  1205. border-radius: 50%;
  1206. display: flex;
  1207. align-items: center;
  1208. justify-content: center;
  1209. font-size: 32rpx;
  1210. color: #fff;
  1211. line-height: 1;
  1212. }
  1213. &.upload-placeholder {
  1214. background: #f5f5f5;
  1215. display: flex;
  1216. flex-direction: column;
  1217. align-items: center;
  1218. justify-content: center;
  1219. gap: 8rpx;
  1220. .upload-text {
  1221. font-size: 24rpx;
  1222. color: #999;
  1223. }
  1224. }
  1225. }
  1226. }
  1227. }
  1228. .submit-btn {
  1229. position: fixed;
  1230. bottom: 0;
  1231. left: 0;
  1232. right: 0;
  1233. height: 88rpx;
  1234. background: #388BFF;
  1235. border-radius: 200rpx;
  1236. display: flex;
  1237. align-items: center;
  1238. justify-content: center;
  1239. font-size: 32rpx;
  1240. color: #fff;
  1241. font-weight: 500;
  1242. z-index: 100;
  1243. margin: 40rpx 32rpx;
  1244. }
  1245. </style>