questionnaireForm.vue 38 KB

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