activity-detail.vue 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  1. <template>
  2. <view class="page-container">
  3. <!-- 页面滚动区域 -->
  4. <scroll-view class="page-scroll" scroll-y>
  5. <!-- 地块信息卡片 -->
  6. <view class="info-card">
  7. <view class="card-title">
  8. <text>地块信息</text>
  9. </view>
  10. <view class="info-item">
  11. <text class="info-label">地块名称</text>
  12. <text class="info-value">{{ formData.fieldName || '未知' }}</text>
  13. </view>
  14. <view class="info-item">
  15. <text class="info-label">作物名称</text>
  16. <text class="info-value">{{ formData.growCrops || '未知' }}</text>
  17. </view>
  18. <view class="info-item">
  19. <text class="info-label">负责人</text>
  20. <text class="info-value">{{ formData.manager || '未知' }}</text>
  21. </view>
  22. </view>
  23. <!-- 任务填写表单 -->
  24. <view class="form-card">
  25. <view class="card-title">
  26. <text>任务信息</text>
  27. </view>
  28. <!-- 任务名称 -->
  29. <view class="form-item">
  30. <view class="form-label required">任务名称</view>
  31. <input
  32. v-model="formData.taskName"
  33. placeholder="请输入任务名称,例如:水稻田施肥"
  34. :disabled="pageMode === 'view'"
  35. class="form-input"
  36. />
  37. </view>
  38. <!-- 任务类型 -->
  39. <view class="form-item" @click="pageMode !== 'view' && showTaskTypeSelector()">
  40. <view class="form-label required">任务类型</view>
  41. <view class="select-wrapper">
  42. <!-- <input
  43. :value="formData.typeName"
  44. :placeholder="dictLoading ? '加载中...' : '请选择任务类型'"
  45. readonly
  46. class="form-input select-input"
  47. /> -->
  48. <view class="form-input select-input">
  49. {{ formData.typeName || (dictLoading ? '加载中...' : '请选择任务类型') }}
  50. </view>
  51. <view v-if="pageMode !== 'view'" class="select-arrow">
  52. <text>▼</text>
  53. </view>
  54. </view>
  55. </view>
  56. <!-- 执行时间 -->
  57. <view class="form-item" @click="pageMode !== 'view' && selectExecuteTime()">
  58. <view class="form-label required">执行时间</view>
  59. <view class="select-wrapper">
  60. <!-- <input
  61. :value="formattedExecuteTime"
  62. placeholder="请选择任务计划时间"
  63. readonly
  64. class="form-input select-input"
  65. readonly
  66. /> -->
  67. <view class="form-input select-input">
  68. {{ formattedExecuteTime || '请选择任务计划时间' }}
  69. </view>
  70. <view v-if="pageMode !== 'view'" class="select-arrow">
  71. <text>选择</text>
  72. </view>
  73. </view>
  74. </view>
  75. <!-- 负责人选择 -->
  76. <view class="form-item" @click="pageMode !== 'view' && showUserSelector()">
  77. <view class="form-label required">负责人</view>
  78. <view class="select-wrapper">
  79. <!-- <input
  80. :value="formData.assigneeName"
  81. placeholder="请选择负责人"
  82. readonly
  83. class="form-input select-input"
  84. /> -->
  85. <view class="form-input select-input">
  86. {{ formData.assigneeName || '请选择负责人' }}
  87. </view>
  88. <view v-if="pageMode !== 'view'" class="select-arrow">
  89. <text>选择</text>
  90. </view>
  91. </view>
  92. </view>
  93. <!-- 任务说明 -->
  94. <view class="form-item">
  95. <view class="form-label">任务说明</view>
  96. <textarea
  97. v-model="formData.remark"
  98. placeholder="请输入任务要点,例如:每亩用肥20kg"
  99. :disabled="pageMode === 'view'"
  100. class="form-textarea"
  101. maxlength="200"
  102. ></textarea>
  103. <view class="char-count">{{ (formData.remark || '').length }}/200</view>
  104. </view>
  105. <!-- 任务完成情况 -->
  106. <view class="section-divider"></view>
  107. <view class="section-title">
  108. <text>任务完成情况</text>
  109. </view>
  110. <!-- 完成状态选择 - 新建和编辑模式 -->
  111. <view class="form-item" v-if="pageMode === 'create' || pageMode === 'edit'">
  112. <view class="form-label">完成状态</view>
  113. <view class="radio-group">
  114. <view
  115. class="radio-item"
  116. v-for="(item, index) in completionStatusOptions"
  117. :key="index"
  118. @click="selectCompletionStatus(item.value)"
  119. >
  120. <view class="radio-circle" :class="{'radio-checked': formData.completionStatus === item.value}">
  121. <view v-if="formData.completionStatus === item.value" class="radio-dot"></view>
  122. </view>
  123. <text class="radio-label">{{ item.label }}</text>
  124. </view>
  125. </view>
  126. </view>
  127. <!-- 查看模式显示完成状态 -->
  128. <view class="form-item" v-if="pageMode === 'view'">
  129. <view class="form-label">完成状态</view>
  130. <view class="status-completed">
  131. <text class="status-icon">✓</text>
  132. <text>已完成</text>
  133. </view>
  134. </view>
  135. <!-- 完成时间 -->
  136. <view class="form-item" v-if="formData.completionStatus === '1'">
  137. <view class="form-label" :class="{'required': (pageMode === 'create' || pageMode === 'edit') && formData.completionStatus === '1'}">完成时间</view>
  138. <view class="select-wrapper" @click="(pageMode === 'create' || pageMode === 'edit') && formData.completionStatus === '1' && selectCompletionTime()">
  139. <!-- <input
  140. :value="formattedCompletionTime"
  141. placeholder="请选择实际完成时间"
  142. readonly
  143. class="form-input select-input"
  144. /> -->
  145. <view class="form-input select-input">
  146. {{ formattedCompletionTime || '请选择实际完成时间' }}
  147. </view>
  148. <view v-if="(pageMode === 'create' || pageMode === 'edit') && formData.completionStatus === '1'" class="select-arrow">
  149. <text>选择</text>
  150. </view>
  151. </view>
  152. </view>
  153. <!-- 完成说明 -->
  154. <view class="form-item" v-if="formData.completionStatus === '1'">
  155. <view class="form-label" :class="{'required': (pageMode === 'create' || pageMode === 'edit') && formData.completionStatus === '1'}">完成说明</view>
  156. <textarea
  157. v-model="formData.completionDesc"
  158. placeholder="请输入完成说明,例如:已完成并拍照记录"
  159. :disabled="pageMode === 'view'"
  160. class="form-textarea"
  161. maxlength="300"
  162. ></textarea>
  163. <view class="char-count">{{ (formData.completionDesc || '').length }}/300</view>
  164. <view class="form-error" v-if="formErrors.completionDesc">
  165. {{ formErrors.completionDesc }}
  166. </view>
  167. </view>
  168. <!-- 现场图片 -->
  169. <view class="form-item" v-if="formData.completionStatus === '1'">
  170. <view class="form-label">现场图片</view>
  171. <!-- 新建和编辑模式 -->
  172. <view v-if="pageMode === 'create' || pageMode === 'edit'" class="image-upload">
  173. <view class="image-list">
  174. <view
  175. class="image-preview"
  176. v-for="(item, index) in formData.images"
  177. :key="index"
  178. @click="previewImage(item, index)"
  179. >
  180. <!-- <image :src="getImageUrl(item)" mode="aspectFill"/> -->
  181. <image :src="item.url" mode="aspectFill"/>
  182. <view class="delete-btn" @click.stop="deletePic(index)">
  183. <text>×</text>
  184. </view>
  185. </view>
  186. <view
  187. v-if="formData.images.length < 6"
  188. class="upload-btn"
  189. @click="chooseImage"
  190. >
  191. <text class="upload-icon">+</text>
  192. <text class="upload-text">添加图片</text>
  193. </view>
  194. </view>
  195. <view class="upload-tip">最多可上传6张图片</view>
  196. </view>
  197. <!-- 查看模式 -->
  198. <view v-else-if="pageMode === 'view' && formData.images && formData.images.length > 0" class="image-view">
  199. <view class="image-list">
  200. <view
  201. class="image-preview"
  202. v-for="(item, index) in formData.images"
  203. :key="index"
  204. @click="previewImage(item, index)"
  205. >
  206. <!-- <image :src="getImageUrl(item)" mode="aspectFill"/> -->
  207. <image :src="item.url" mode="aspectFill"/>
  208. </view>
  209. </view>
  210. </view>
  211. <!-- 无图片提示 -->
  212. <view v-else class="no-images">
  213. <text>暂无图片</text>
  214. </view>
  215. </view>
  216. </view>
  217. <!-- 底部占位 -->
  218. <view class="bottom-safe"></view>
  219. </scroll-view>
  220. <!-- 底部提交按钮 -->
  221. <view class="footer-safe" v-if="pageMode !== 'view'">
  222. <view class="footer-content">
  223. <button
  224. class="submit-button"
  225. :class="{'loading': isSubmitting}"
  226. @click="submitForm"
  227. :disabled="isSubmitting"
  228. >
  229. {{ isSubmitting ? '提交中...' : submitButtonText }}
  230. </button>
  231. </view>
  232. </view>
  233. <!-- 遮罩层 -->
  234. <view v-if="showTaskTypePicker || showDateTimeSelector || showUserPicker" class="picker-mask" @click="closePickers"></view>
  235. <!-- 任务类型选择弹窗 -->
  236. <view v-if="showTaskTypePicker" class="picker-popup">
  237. <view class="picker-header">
  238. <text class="picker-cancel" @click="showTaskTypePicker = false">取消</text>
  239. <text class="picker-title">选择任务类型</text>
  240. <text class="picker-confirm" @click="confirmTaskType">确定</text>
  241. </view>
  242. <view class="picker-content">
  243. <view v-if="dictLoading" class="picker-loading">
  244. <text>加载中...</text>
  245. </view>
  246. <view
  247. v-else
  248. class="picker-item"
  249. v-for="(item, index) in taskTypeOptions"
  250. :key="index"
  251. :class="{'selected': tempTaskTypeIndex === index}"
  252. @click="tempTaskTypeIndex = index"
  253. >
  254. <text>{{ item.dictLabel }}</text>
  255. <text v-if="tempTaskTypeIndex === index" class="check-mark">✓</text>
  256. </view>
  257. </view>
  258. </view>
  259. <!-- 用户选择弹窗 -->
  260. <view v-if="showUserPicker" class="picker-popup">
  261. <view class="picker-header">
  262. <text class="picker-cancel" @click="showUserPicker = false">取消</text>
  263. <text class="picker-title">选择负责人</text>
  264. <text class="picker-confirm" @click="confirmUser">确定</text>
  265. </view>
  266. <view class="picker-content">
  267. <view v-if="usersLoading" class="picker-loading">
  268. <text>加载中...</text>
  269. </view>
  270. <view v-else-if="userList.length === 0" class="picker-empty">
  271. <text>暂无可选负责人</text>
  272. </view>
  273. <view
  274. v-else
  275. class="picker-item"
  276. v-for="(user, index) in userList"
  277. :key="user.userId"
  278. :class="{'selected': tempUserIndex === index}"
  279. @click="tempUserIndex = index"
  280. >
  281. <text>{{ user.userName }}</text>
  282. <text v-if="tempUserIndex === index" class="check-mark">✓</text>
  283. </view>
  284. </view>
  285. </view>
  286. <!-- 日期时间选择弹窗 -->
  287. <view v-if="showDateTimeSelector" class="picker-popup datetime-picker">
  288. <view class="picker-header">
  289. <text class="picker-cancel" @click="cancelDateTime">取消</text>
  290. <text class="picker-title">选择时间</text>
  291. <text class="picker-confirm" @click="confirmDateTime">确定</text>
  292. </view>
  293. <view class="datetime-picker-content">
  294. <picker-view
  295. class="datetime-picker-view"
  296. :value="dateTimePickerValue"
  297. @change="onDateTimePickerChange"
  298. :indicator-style="'height: 80rpx;'"
  299. :mask-style="'background-image: linear-gradient(180deg, rgba(255, 255, 255, 0.95), rgba(255, 255, 255, 0.6)), linear-gradient(0deg, rgba(255, 255, 255, 0.95), rgba(255, 255, 255, 0.6));'"
  300. >
  301. <picker-view-column>
  302. <view class="picker-view-item" v-for="(item, index) in dateTimePickerRange[0]" :key="'year-'+index">
  303. <text class="picker-item-text">{{ item }}</text>
  304. </view>
  305. </picker-view-column>
  306. <picker-view-column>
  307. <view class="picker-view-item" v-for="(item, index) in dateTimePickerRange[1]" :key="'month-'+index">
  308. <text class="picker-item-text">{{ item }}</text>
  309. </view>
  310. </picker-view-column>
  311. <picker-view-column>
  312. <view class="picker-view-item" v-for="(item, index) in dateTimePickerRange[2]" :key="'day-'+index">
  313. <text class="picker-item-text">{{ item }}</text>
  314. </view>
  315. </picker-view-column>
  316. <picker-view-column>
  317. <view class="picker-view-item" v-for="(item, index) in dateTimePickerRange[3]" :key="'hour-'+index">
  318. <text class="picker-item-text">{{ item }}</text>
  319. </view>
  320. </picker-view-column>
  321. <picker-view-column>
  322. <view class="picker-view-item" v-for="(item, index) in dateTimePickerRange[4]" :key="'min-'+index">
  323. <text class="picker-item-text">{{ item }}</text>
  324. </view>
  325. </picker-view-column>
  326. </picker-view>
  327. </view>
  328. </view>
  329. </view>
  330. </template>
  331. <script setup>
  332. import { ref, reactive, computed, onMounted } from 'vue'
  333. import api from "@/config/api.js";
  334. import { onShow, onLoad } from '@dcloudio/uni-app'
  335. import { getAgriculturalTasksById, addAgriculturalTask, updateAgriculturalTask } from '@/api/services/activity.js';
  336. import { getUsersByPlotId, getUserInfo } from '@/api/services/user.js';
  337. import { useDict } from '@/utils/composables/useDict';
  338. import storage from "@/utils/storage.js";
  339. // 使用字典组合式函数
  340. const { dictData, dictLoading, loadDict } = useDict(['task_type', 'task_status'])
  341. // 页面模式:create-新建, edit-编辑, view-查看
  342. const pageMode = ref('create')
  343. // 选择器显示状态
  344. const showTaskTypePicker = ref(false)
  345. const showDateTimeSelector = ref(false)
  346. const showUserPicker = ref(false)
  347. // 临时选择的任务类型索引
  348. const tempTaskTypeIndex = ref(0)
  349. const tempUserIndex = ref(0)
  350. // 时间选择器相关
  351. const currentTimeType = ref('')
  352. const dateTimePickerValue = ref([0, 0, 0, 0, 0])
  353. const dateTimePickerRange = ref([])
  354. // 表单数据
  355. const formData = reactive({
  356. // 任务ID,新建任务时为空
  357. id: '',
  358. // 地块基础信息
  359. plotId: '',
  360. fieldName: '',
  361. growCrops: '',
  362. manager: '',
  363. // 任务信息
  364. taskName: '',
  365. typeName: '',
  366. typeNameId: '',
  367. executeTime: new Date(),
  368. remark: '',
  369. // 完成情况
  370. taskStatus: '0',
  371. completionStatus: '0',
  372. completionTime: new Date(),
  373. completionDesc: '',
  374. images: [],
  375. taskImages: '',
  376. // 负责人信息
  377. assigneeId: '',
  378. assigneeName: '',
  379. // 创建人信息
  380. create_by: ''
  381. })
  382. // 完成状态选项
  383. const completionStatusOptions = [
  384. { label: '待完成', value: '0' },
  385. { label: '已完成', value: '1' }
  386. ]
  387. // 是否正在提交
  388. const isSubmitting = ref(false)
  389. // 表单验证错误
  390. const formErrors = reactive({
  391. completionDesc: ''
  392. })
  393. // 用户列表
  394. const userList = ref([])
  395. const usersLoading = ref(false)
  396. // 任务类型列表
  397. const taskStatusList = ref([])
  398. // 计算属性
  399. const pageTitle = computed(() => {
  400. switch (pageMode.value) {
  401. case 'create':
  402. return '新建农事任务'
  403. case 'edit':
  404. return '编辑农事任务'
  405. default:
  406. return '农事任务详情'
  407. }
  408. })
  409. const submitButtonText = computed(() => {
  410. switch (pageMode.value) {
  411. case 'create':
  412. return '创建任务'
  413. case 'edit':
  414. return '保存修改'
  415. default:
  416. return ''
  417. }
  418. })
  419. const formattedExecuteTime = computed(() => {
  420. return formatDateTime(formData.executeTime)
  421. })
  422. const formattedCompletionTime = computed(() => {
  423. return formatDateTime(formData.completionTime)
  424. })
  425. const taskTypeOptions = computed(() => {
  426. return dictData.task_type
  427. })
  428. const taskTypeIndex = computed(() => {
  429. console.log("taskTypeOptions", taskTypeOptions.value)
  430. const index = taskTypeOptions.value.findIndex(item => item.dictValue === formData.typeNameId)
  431. console.log("查找:", index)
  432. return index >= 0 ? index : 0
  433. })
  434. // 初始化字典数据
  435. loadDict().then(() => {
  436. console.log("dictData:",dictData.task_type);
  437. // 字典加载完成后,如果没有设置任务类型,则默认选择第一个
  438. if (!formData.typeName && dictData.task_type && dictData.task_type.length > 0) {
  439. const firstType = dictData.task_type[0]
  440. taskStatusList.value = dictData.task_type
  441. console.log("dictData", dictData.task_status[0].dictLabel)
  442. formData.typeName = firstType.dictLabel
  443. formData.typeNameId = firstType.dictValue
  444. }
  445. })
  446. // uni-app 生命周期
  447. onLoad((options) => {
  448. console.log('页面加载,接收参数:', options)
  449. // 设置页面模式
  450. if (options.mode) {
  451. pageMode.value = options.mode
  452. }
  453. // 设置地块基础信息
  454. formData.fieldName = decodeURIComponent(options.fieldName === 'undefined' ? '未选择地块' : options.fieldName)
  455. formData.growCrops = decodeURIComponent(options.growCrops === 'undefined' ? '未选择地块' : options.growCrops)
  456. formData.manager = decodeURIComponent(options.manager === 'undefined' ? '未选择地块' : options.manager)
  457. formData.plotId = parseInt(options.plotId || '1')
  458. formData.farmId = parseInt(options.farmId || '1')
  459. // 加载用户列表
  460. loadUserList(formData.farmId)
  461. // 如果是编辑或查看模式,获取任务详情
  462. if (options.id && options.id !== 'new') {
  463. formData.id = options.id
  464. loadTaskDetail(options.id)
  465. } else if (options.id === 'new') {
  466. // 加载当前登录用户
  467. loadAssigneeInfo()
  468. }
  469. // 设置导航栏标题
  470. uni.setNavigationBarTitle({
  471. title: pageTitle.value
  472. })
  473. // 初始化时间选择器数据
  474. initDateTimeRange()
  475. })
  476. // 方法定义
  477. // 跨端安全解析日期
  478. const normalizeToDate = (input) => {
  479. if (!input) return new Date()
  480. let dateObj = input
  481. if (typeof input === 'string') {
  482. // 兼容 iOS:将 2025-08-12 12:30:00 转为 2025/08/12 12:30:00
  483. dateObj = input.replace(/-/g, '/')
  484. }
  485. if (typeof input === 'number') {
  486. // 秒级时间戳转毫秒
  487. if (input.toString().length === 10) {
  488. dateObj = input * 1000
  489. }
  490. }
  491. const d = new Date(dateObj)
  492. return isNaN(d.getTime()) ? new Date() : d
  493. }
  494. // 加载任务详情
  495. const loadTaskDetail = (taskId) => {
  496. uni.showLoading({
  497. title: '加载中...',
  498. mask: true
  499. })
  500. getAgriculturalTasksById(taskId).then(res => {
  501. if (res.data.code === 200) {
  502. const taskDetail = res.data.data
  503. // 设置表单数据
  504. Object.assign(formData, {
  505. ...taskDetail,
  506. manager: formData.manager,
  507. completionStatus: taskDetail.taskStatus
  508. })
  509. // 转换任务类型值
  510. if (taskStatusList.value && taskStatusList.value.length > 0) {
  511. const typeDict = taskStatusList.value.find(item => item.dictValue === taskDetail.typeName.toString())
  512. if (typeDict) {
  513. formData.typeName = typeDict.dictLabel
  514. formData.typeNameId = typeDict.dictValue
  515. }
  516. }
  517. // 处理图片数据
  518. if (formData.taskImages) {
  519. try {
  520. const imageUrls = formData.taskImages.split(',')
  521. formData.images = imageUrls.filter(url => url && url.trim()).map(url => ({
  522. url: url.trim(),
  523. status: 'success'
  524. }))
  525. console.log('解析后的图片数据:', formData.images)
  526. } catch (e) {
  527. console.error('解析图片数据失败:', e)
  528. formData.images = []
  529. }
  530. } else {
  531. formData.images = []
  532. }
  533. console.log("formData.assigneeId", formData.assigneeId)
  534. console.log("!formData.assigneeName", formData.assigneeName)
  535. // 如果有负责人ID但没有负责人名称,获取负责人信息
  536. if (formData.assigneeId && !formData.assigneeName) {
  537. loadAssigneeInfo(formData.assigneeId)
  538. }
  539. uni.hideLoading();
  540. } else {
  541. uni.hideLoading();
  542. uni.showToast({
  543. title: res.data.msg || '获取任务详情失败',
  544. icon: 'none'
  545. });
  546. // 失败后返回上一页
  547. setTimeout(() => {
  548. uni.navigateBack();
  549. }, 1500);
  550. }
  551. }).catch(err => {
  552. console.error('获取任务详情失败:', err);
  553. uni.hideLoading();
  554. uni.showToast({
  555. title: '获取任务详情失败',
  556. icon: 'none'
  557. });
  558. // 失败后返回上一页
  559. setTimeout(() => {
  560. uni.navigateBack();
  561. }, 1500);
  562. });
  563. }
  564. // 加载负责人信息
  565. const loadAssigneeInfo = (userId) => {
  566. getUserInfo(userId).then(res => {
  567. console.log("你是谁:",res);
  568. if (res.data.code === 200) {
  569. const userData = res.data.data;
  570. formData.assigneeName = userData.userName || '未知用户';
  571. formData.assigneeId = userData.userId;
  572. }
  573. }).catch(err => {
  574. console.error('获取负责人信息失败:', err);
  575. formData.assigneeName = '未知用户';
  576. });
  577. }
  578. // 加载用户列表
  579. const loadUserList = (farmId) => {
  580. usersLoading.value = true;
  581. const params = {
  582. pageNum: 1,
  583. pageSize: 10,
  584. deptId: farmId
  585. }
  586. getUsersByPlotId(params).then(res => {
  587. console.log("加载用户:",res);
  588. if (res.data.code === 200) {
  589. userList.value = res.data.rows || [];
  590. // 设置默认选中第一个用户
  591. if (userList.value.length > 0 && !formData.assigneeId) {
  592. formData.assigneeId = userList.value[0].userId;
  593. formData.assigneeName = userList.value[0].userName;
  594. }
  595. } else {
  596. console.error('获取用户列表失败:', res.data.msg);
  597. uni.showToast({
  598. title: '获取用户列表失败',
  599. icon: 'none'
  600. });
  601. }
  602. }).catch(err => {
  603. console.error('获取用户列表失败:', err);
  604. uni.showToast({
  605. title: '获取用户列表失败',
  606. icon: 'none'
  607. });
  608. }).finally(() => {
  609. usersLoading.value = false;
  610. });
  611. }
  612. // 初始化时间选择器范围数据
  613. const initDateTimeRange = () => {
  614. // 年份:从2020年开始显示10年
  615. const startYear = 2020;
  616. const years = [];
  617. for (let i = 0; i < 10; i++) {
  618. years.push((startYear + i) + '年');
  619. }
  620. // 月份:1-12月
  621. const months = [];
  622. for (let i = 1; i <= 12; i++) {
  623. months.push(i + '月');
  624. }
  625. // 日期:1-31日
  626. const days = [];
  627. for (let i = 1; i <= 31; i++) {
  628. days.push(i + '日');
  629. }
  630. // 小时:0-23时
  631. const hours = [];
  632. for (let i = 0; i <= 23; i++) {
  633. hours.push(i.toString().padStart(2, '0') + '时');
  634. }
  635. // 分钟:0-59分
  636. const minutes = [];
  637. for (let i = 0; i <= 59; i++) {
  638. minutes.push(i.toString().padStart(2, '0') + '分');
  639. }
  640. dateTimePickerRange.value = [years, months, days, hours, minutes];
  641. console.log('日期选择器范围:', {
  642. years: years.length,
  643. months: months.length,
  644. days: days.length,
  645. hours: hours.length,
  646. minutes: minutes.length
  647. });
  648. }
  649. // 初始化日期时间选择器
  650. const initDateTimePicker = (timestamp) => {
  651. const date = normalizeToDate(timestamp);
  652. const currentYear = date.getFullYear();
  653. const currentMonth = date.getMonth() + 1; // 1-12
  654. const currentDate = date.getDate(); // 1-31
  655. const currentHour = date.getHours();
  656. const currentMinute = date.getMinutes();
  657. // 基准年份,用于计算索引
  658. const startYear = 2020;
  659. // 如果当前年份小于起始年份或大于结束年份,调整为合法范围内
  660. let yearIndex = currentYear - startYear;
  661. if (yearIndex < 0) yearIndex = 0;
  662. if (yearIndex >= 10) yearIndex = 9;
  663. console.log('初始化日期选择器:', {
  664. timestamp,
  665. year: currentYear,
  666. month: currentMonth,
  667. date: currentDate,
  668. hour: currentHour,
  669. minute: currentMinute,
  670. yearIndex
  671. });
  672. // 设置选择器的初始值
  673. dateTimePickerValue.value = [
  674. yearIndex, // 年份索引
  675. currentMonth - 1, // 月份索引(0-11)
  676. currentDate - 1, // 日期索引(0-30)
  677. currentHour, // 小时索引
  678. currentMinute // 分钟索引
  679. ];
  680. console.log('选择器初始值:', dateTimePickerValue.value);
  681. // 更新日期范围
  682. updateDaysRange();
  683. }
  684. // 更新日期范围(根据选择的年月)
  685. const updateDaysRange = () => {
  686. const yearIndex = dateTimePickerValue.value[0];
  687. const monthIndex = dateTimePickerValue.value[1];
  688. // 计算实际年月
  689. const year = 2020 + yearIndex;
  690. const month = monthIndex + 1; // 索引为0-11,实际月份为1-12
  691. // 获取该月的天数
  692. const daysInMonth = new Date(year, month, 0).getDate();
  693. // 更新天数范围
  694. const days = [];
  695. for (let i = 1; i <= daysInMonth; i++) {
  696. days.push(i + '日');
  697. }
  698. // 更新日期列
  699. dateTimePickerRange.value[2] = days;
  700. // 如果当前选择的日期超过了该月的天数,调整为该月最后一天
  701. if (dateTimePickerValue.value[2] >= daysInMonth) {
  702. dateTimePickerValue.value[2] = daysInMonth - 1;
  703. }
  704. console.log('更新日期范围:', {
  705. year,
  706. month,
  707. daysInMonth,
  708. daysLength: days.length
  709. });
  710. }
  711. // 显示任务类型选择器
  712. const showTaskTypeSelector = () => {
  713. tempTaskTypeIndex.value = taskTypeIndex.value;
  714. showTaskTypePicker.value = true;
  715. }
  716. // 确认任务类型选择
  717. const confirmTaskType = () => {
  718. const selectedType = taskTypeOptions.value[tempTaskTypeIndex.value];
  719. console.log("选中:",selectedType);
  720. formData.typeName = selectedType.dictLabel;
  721. formData.typeNameId = selectedType.dictValue; // 设置typeNameId为dictValue值
  722. showTaskTypePicker.value = false;
  723. }
  724. // 选择执行时间
  725. const selectExecuteTime = () => {
  726. currentTimeType.value = 'executeTime';
  727. initDateTimePicker(formData.executeTime);
  728. showDateTimeSelector.value = true;
  729. }
  730. // 选择完成时间
  731. const selectCompletionTime = () => {
  732. currentTimeType.value = 'completionTime';
  733. initDateTimePicker(formData.completionTime);
  734. showDateTimeSelector.value = true;
  735. }
  736. // 日期时间选择器变更
  737. const onDateTimePickerChange = (e) => {
  738. dateTimePickerValue.value = e.detail.value;
  739. // 当年月变更时,更新日期范围
  740. updateDaysRange();
  741. }
  742. // 确认时间选择
  743. const confirmDateTime = () => {
  744. const values = dateTimePickerValue.value;
  745. // 计算实际日期时间
  746. const year = 2020 + values[0];
  747. const month = values[1] + 1; // 索引为0-11,实际月份为1-12
  748. const date = values[2] + 1; // 索引为0-30,实际日期为1-31
  749. const hour = values[3];
  750. const minute = values[4];
  751. console.log('确认日期时间:', {
  752. selectedValues: values,
  753. convertedDate: `${year}-${month}-${date} ${hour}:${minute}`
  754. });
  755. // 创建日期对象
  756. const selectedTime = new Date(year, month - 1, date, hour, minute).getTime();
  757. // 更新相应的表单字段
  758. if (currentTimeType.value === 'executeTime') {
  759. formData.executeTime = selectedTime;
  760. } else if (currentTimeType.value === 'completionTime') {
  761. formData.completionTime = selectedTime;
  762. }
  763. // 关闭选择器
  764. showDateTimeSelector.value = false;
  765. uni.showToast({
  766. title: '时间已设置',
  767. icon: 'success',
  768. duration: 1500
  769. });
  770. }
  771. // 取消时间选择
  772. const cancelDateTime = () => {
  773. showDateTimeSelector.value = false;
  774. }
  775. // 显示用户选择器
  776. const showUserSelector = () => {
  777. tempUserIndex.value = userList.value.findIndex(user => user.userId === formData.assigneeId);
  778. showUserPicker.value = true;
  779. }
  780. // 确认用户选择
  781. const confirmUser = () => {
  782. const selectedUser = userList.value[tempUserIndex.value];
  783. formData.assigneeId = selectedUser.userId;
  784. formData.assigneeName = selectedUser.userName;
  785. showUserPicker.value = false;
  786. }
  787. // 关闭所有选择器
  788. const closePickers = () => {
  789. showTaskTypePicker.value = false;
  790. showDateTimeSelector.value = false;
  791. showUserPicker.value = false;
  792. }
  793. // 选择完成状态
  794. const selectCompletionStatus = (value) => {
  795. formData.completionStatus = value;
  796. formData.taskStatus = value; // 同时设置taskStatus字段
  797. if (value === '1') {
  798. formData.completionTime = new Date();
  799. }
  800. }
  801. // 格式化日期时间
  802. /**
  803. * 日期格式化工具(兼容 iOS 和安卓)
  804. * @param {string|number|Date} date - 日期对象 / 时间戳 / 日期字符串
  805. * @param {string} format - 格式模板,默认 'yyyy-MM-dd HH:mm:ss'
  806. * @returns {string} 格式化后的日期
  807. */
  808. const formatDateTime = (date, format = 'yyyy-MM-dd HH:mm') => {
  809. if (!date) return '';
  810. // 如果是字符串,做 iOS 兼容(将 2025-08-12 替换为 2025/08/12)
  811. if (typeof date === 'string') {
  812. date = date.replace(/-/g, '/');
  813. }
  814. // 如果是数字(时间戳),判断是否是秒级
  815. if (typeof date === 'number') {
  816. if (date.toString().length === 10) {
  817. date *= 1000; // 秒转毫秒
  818. }
  819. }
  820. // 转换为 Date 对象
  821. date = new Date(date);
  822. if (isNaN(date.getTime())) return '';
  823. const map = {
  824. 'yyyy': date.getFullYear(),
  825. 'MM': String(date.getMonth() + 1).padStart(2, '0'),
  826. 'dd': String(date.getDate()).padStart(2, '0'),
  827. 'HH': String(date.getHours()).padStart(2, '0'),
  828. 'mm': String(date.getMinutes()).padStart(2, '0'),
  829. // 'ss': String(date.getSeconds()).padStart(2, '0')
  830. };
  831. return format.replace(/yyyy|MM|dd|HH|mm/g, match => map[match]);
  832. }
  833. // 选择图片(优化跨平台兼容性)
  834. const chooseImage = () => {
  835. uni.chooseImage({
  836. count: 6 - formData.images.length,
  837. sizeType: ['original', 'compressed'],
  838. sourceType: ['album', 'camera'],
  839. success: (res) => {
  840. console.log('选择图片成功:', res);
  841. console.log('tempFiles:', res.tempFiles);
  842. console.log('tempFilePaths:', res.tempFilePaths);
  843. // 验证文件类型和大小
  844. const validFiles = [];
  845. const invalidFiles = [];
  846. const maxSize = 5 * 1024 * 1024; // 5MB 最大限制
  847. // 兼容不同平台的文件信息获取方式
  848. if (res.tempFiles && res.tempFiles.length > 0) {
  849. // 检查每个文件
  850. res.tempFiles.forEach((file, index) => {
  851. console.log(`文件 ${index}:`, file);
  852. // 获取文件路径
  853. const filePath = res.tempFilePaths[index];
  854. // 获取文件名(兼容不同平台)
  855. let fileName = '';
  856. if (file.name) {
  857. fileName = file.name;
  858. } else if (file.path) {
  859. // 从路径中提取文件名
  860. fileName = file.path.split('/').pop();
  861. } else if (filePath) {
  862. fileName = filePath.split('/').pop();
  863. }
  864. console.log(`文件名: ${fileName}, 大小: ${file.size}`);
  865. // 检查文件类型(兼容没有扩展名的情况)
  866. let isImage = true;
  867. if (fileName) {
  868. isImage = /\.(jpg|jpeg|png|gif|bmp|webp)$/i.test(fileName);
  869. }
  870. // 检查文件大小
  871. const fileSize = file.size || 0;
  872. const isValidSize = fileSize <= maxSize;
  873. if (isImage && isValidSize) {
  874. validFiles.push(filePath);
  875. } else {
  876. invalidFiles.push({
  877. path: filePath,
  878. size: fileSize,
  879. name: fileName,
  880. reason: !isImage ? '文件格式不支持' : `文件大于5MB (${(fileSize / 1024 / 1024).toFixed(2)}MB)`
  881. });
  882. }
  883. });
  884. } else {
  885. // 如果没有 tempFiles,直接使用 tempFilePaths(某些平台可能不返回详细信息)
  886. console.warn('未获取到 tempFiles,直接使用 tempFilePaths');
  887. res.tempFilePaths.forEach(path => {
  888. validFiles.push(path);
  889. });
  890. }
  891. console.log('有效文件:', validFiles);
  892. console.log('无效文件:', invalidFiles);
  893. // 显示无效文件提示
  894. if (invalidFiles.length > 0) {
  895. const reasons = invalidFiles.map(f => `${f.name}: ${f.reason}`).join('\n');
  896. uni.showModal({
  897. title: '部分文件无效',
  898. content: `${invalidFiles.length}个文件无效:\n${reasons}`,
  899. showCancel: false
  900. });
  901. }
  902. // 如果有有效文件,则上传
  903. if (validFiles.length > 0) {
  904. uploadImages(validFiles);
  905. } else if (invalidFiles.length > 0) {
  906. uni.showToast({
  907. title: '没有可上传的文件',
  908. icon: 'none',
  909. duration: 2000
  910. });
  911. }
  912. },
  913. fail: (err) => {
  914. console.error('选择图片失败:', err);
  915. uni.showToast({
  916. title: '选择图片失败',
  917. icon: 'none',
  918. duration: 2000
  919. });
  920. }
  921. });
  922. }
  923. // 上传图片到服务器(优化跨平台兼容性)
  924. const uploadImages = (tempFilePaths) => {
  925. uni.showLoading({
  926. title: '上传中...',
  927. mask: true
  928. });
  929. // 上传成功的图片计数
  930. let successCount = 0;
  931. let failCount = 0;
  932. const totalFiles = tempFilePaths.length;
  933. const newImages = [];
  934. // 遍历处理每张图片
  935. tempFilePaths.forEach((path, index) => {
  936. // 调用上传API
  937. uni.uploadFile({
  938. url: api.serve + '/file/upload',
  939. filePath: path,
  940. name: 'file',
  941. formData: {
  942. type: 'task'
  943. },
  944. header: {
  945. 'Authorization': `Bearer ${storage.getAccessToken()}`
  946. },
  947. success: (res) => {
  948. try {
  949. console.log('上传原始响应:', res);
  950. console.log('响应数据类型:', typeof res.data);
  951. console.log('响应状态码:', res.statusCode);
  952. let response;
  953. // 兼容不同平台的响应格式
  954. if (typeof res.data === 'string') {
  955. // H5 端返回字符串,需要解析
  956. response = JSON.parse(res.data);
  957. } else if (typeof res.data === 'object') {
  958. // Android/鸿蒙端可能直接返回对象
  959. response = res.data;
  960. } else {
  961. throw new Error('未知的响应格式');
  962. }
  963. console.log('解析后的响应:', response);
  964. // 检查响应是否成功
  965. if (response.code === 200 || response.code === '200') {
  966. // 兼容不同的数据结构
  967. let imageUrl = '';
  968. // 情况1: response.data 是字符串(直接是URL)
  969. if (typeof response.data === 'string') {
  970. imageUrl = response.data;
  971. }
  972. // 情况2: response.data 是对象,包含 url 字段
  973. else if (response.data && response.data.url) {
  974. imageUrl = response.data.url;
  975. }
  976. // 情况3: response 直接包含 url 字段
  977. else if (response.url) {
  978. imageUrl = response.url;
  979. }
  980. // 情况4: response.data 是数组
  981. else if (Array.isArray(response.data) && response.data.length > 0) {
  982. imageUrl = response.data[0].url || response.data[0];
  983. }
  984. if (imageUrl) {
  985. console.log('上传成功,图片URL:', imageUrl);
  986. // 上传成功,将图片信息添加到数组
  987. newImages.push({
  988. url: imageUrl,
  989. path: path,
  990. status: 'success',
  991. fileName: (response.data && response.data.fileName) || ''
  992. });
  993. successCount++;
  994. } else {
  995. console.error('无法从响应中提取图片URL:', response);
  996. failCount++;
  997. uni.showToast({
  998. title: '图片URL解析失败',
  999. icon: 'none',
  1000. duration: 2000
  1001. });
  1002. }
  1003. } else {
  1004. failCount++;
  1005. console.error('上传失败,错误信息:', response.msg || response.message);
  1006. uni.showToast({
  1007. title: response.msg || response.message || '上传失败',
  1008. icon: 'none',
  1009. duration: 2000
  1010. });
  1011. }
  1012. } catch (e) {
  1013. failCount++;
  1014. console.error('解析响应失败:', e);
  1015. console.error('原始响应数据:', res.data);
  1016. uni.showToast({
  1017. title: `解析失败: ${e.message}`,
  1018. icon: 'none',
  1019. duration: 2000
  1020. });
  1021. }
  1022. },
  1023. fail: (err) => {
  1024. failCount++;
  1025. console.error('上传请求失败:', err);
  1026. uni.showToast({
  1027. title: '上传请求失败',
  1028. icon: 'none',
  1029. duration: 2000
  1030. });
  1031. },
  1032. complete: () => {
  1033. // 当所有文件都已处理完成
  1034. if (successCount + failCount === totalFiles) {
  1035. uni.hideLoading();
  1036. if (newImages.length > 0) {
  1037. // 将新上传的图片添加到已有图片列表
  1038. formData.images = [...formData.images, ...newImages];
  1039. // 更新taskImages字段,将图片URL用逗号连接
  1040. formData.taskImages = formData.images.map(img => img.url).join(',');
  1041. console.log('所有图片上传完成:', formData.images);
  1042. console.log('taskImages:', formData.taskImages);
  1043. // 显示成功提示
  1044. uni.showToast({
  1045. title: `成功上传${successCount}张图片`,
  1046. icon: 'success',
  1047. duration: 2000
  1048. });
  1049. } else {
  1050. // 全部失败
  1051. uni.showToast({
  1052. title: '图片上传失败,请重试',
  1053. icon: 'none',
  1054. duration: 2000
  1055. });
  1056. }
  1057. // 如果有部分失败
  1058. if (failCount > 0 && successCount > 0) {
  1059. uni.showToast({
  1060. title: `成功${successCount}张,失败${failCount}张`,
  1061. icon: 'none',
  1062. duration: 2000
  1063. });
  1064. }
  1065. }
  1066. }
  1067. });
  1068. });
  1069. }
  1070. // 删除图片
  1071. const deletePic = (index) => {
  1072. uni.showModal({
  1073. title: '确认删除',
  1074. content: '确定要删除这张图片吗?',
  1075. success: (res) => {
  1076. if (res.confirm) {
  1077. formData.images.splice(index, 1);
  1078. // 更新taskImages字段
  1079. formData.taskImages = formData.images.map(img => img.url).join(',');
  1080. uni.showToast({
  1081. title: '已删除',
  1082. icon: 'none'
  1083. });
  1084. }
  1085. }
  1086. });
  1087. }
  1088. // 获取图片URL
  1089. const getImageUrl = (item) => {
  1090. // 默认返回url或path
  1091. return api.upload + item.url;
  1092. }
  1093. // 预览图片
  1094. const previewImage = (item, index) => {
  1095. // 获取所有图片的完整URL
  1096. // const urls = formData.images.map(file => this.getImageUrl(file));
  1097. const urls = formData.images.map(item => item.url);
  1098. uni.previewImage({
  1099. urls: urls,
  1100. current: index
  1101. });
  1102. }
  1103. // 表单验证
  1104. const validateForm = () => {
  1105. // 重置错误信息
  1106. formErrors.completionDesc = '';
  1107. // 基本字段验证
  1108. if (!formData.taskName.trim()) {
  1109. uni.showToast({
  1110. title: '请输入任务名称',
  1111. icon: 'none'
  1112. });
  1113. return false;
  1114. }
  1115. if (!formData.typeName) {
  1116. uni.showToast({
  1117. title: '请选择任务类型',
  1118. icon: 'none'
  1119. });
  1120. return false;
  1121. }
  1122. // 验证负责人
  1123. if (!formData.assigneeId) {
  1124. uni.showToast({
  1125. title: '请选择负责人',
  1126. icon: 'none'
  1127. });
  1128. return false;
  1129. }
  1130. // 新建和编辑模式且已完成状态下的验证
  1131. if ((pageMode.value === 'create' || pageMode.value === 'edit') && formData.taskStatus === '1') {
  1132. // 验证完成说明
  1133. if (!formData.completionDesc.trim()) {
  1134. formErrors.completionDesc = '请填写完成说明';
  1135. uni.showToast({
  1136. title: '请填写完成说明',
  1137. icon: 'none'
  1138. });
  1139. return false;
  1140. }
  1141. // 验证是否上传了图片
  1142. if (!formData.images || formData.images.length === 0) {
  1143. uni.showToast({
  1144. title: '请上传至少一张现场图片',
  1145. icon: 'none'
  1146. });
  1147. return false;
  1148. }
  1149. }
  1150. return true;
  1151. }
  1152. // 提交表单
  1153. const submitForm = () => {
  1154. if (!validateForm()) {
  1155. return;
  1156. }
  1157. isSubmitting.value = true;
  1158. // 构建提交数据
  1159. const submitData = {
  1160. // 如果是编辑模式,需要提供ID
  1161. ...(formData.id ? { id: formData.id } : {}),
  1162. farmId: formData.farmId,
  1163. plotId: formData.plotId,
  1164. fieldName: formData.fieldName,
  1165. growCrops: formData.growCrops,
  1166. taskName: formData.taskName,
  1167. taskImages: formData.taskImages,
  1168. typeName: formData.typeNameId,
  1169. taskStatus: formData.taskStatus,
  1170. executeTime: formatDateTime(formData.executeTime),
  1171. assigneeId: formData.assigneeId,
  1172. assigneeName:formData.assigneeName,
  1173. remark: formData.remark,
  1174. completionTime: formData.taskStatus === '1' ? formatDateTime(formData.completionTime) : null,
  1175. completionDesc: formData.taskStatus === '1' ? formData.completionDesc : null,
  1176. create_by: formData.create_by || null
  1177. };
  1178. console.log('提交数据:', submitData);
  1179. uni.showLoading({
  1180. title: '提交中...',
  1181. mask: true
  1182. });
  1183. // 根据模式决定是创建还是更新
  1184. const requestPromise = pageMode.value === 'create'
  1185. ? addAgriculturalTask(submitData)
  1186. : updateAgriculturalTask(submitData);
  1187. requestPromise.then(res => {
  1188. isSubmitting.value = false;
  1189. uni.hideLoading();
  1190. if (res.data.code === 200) {
  1191. const successMessage = pageMode.value === 'create' ? '任务创建成功' : '保存修改成功';
  1192. uni.showToast({
  1193. title: successMessage,
  1194. icon: 'success',
  1195. duration: 1500
  1196. });
  1197. setTimeout(() => {
  1198. uni.navigateBack();
  1199. }, 1500);
  1200. } else {
  1201. uni.showToast({
  1202. title: res.data.msg || '操作失败',
  1203. icon: 'none'
  1204. });
  1205. }
  1206. }).catch(err => {
  1207. console.error('提交表单失败:', err);
  1208. isSubmitting.value = false;
  1209. uni.hideLoading();
  1210. uni.showToast({
  1211. title: '操作失败,请稍后重试',
  1212. icon: 'none'
  1213. });
  1214. });
  1215. }
  1216. </script>
  1217. <style scoped>
  1218. .page-container {
  1219. height: 100vh;
  1220. background-color: #F5F5F5;
  1221. display: flex;
  1222. flex-direction: column;
  1223. }
  1224. .page-scroll {
  1225. flex: 1;
  1226. overflow: hidden;
  1227. }
  1228. /* 地块基础信息卡片 */
  1229. .info-card {
  1230. background: #F9F9F9;
  1231. margin: 24rpx;
  1232. padding: 24rpx;
  1233. border-radius: 8rpx;
  1234. border: 1rpx solid #E5E5E5;
  1235. }
  1236. .card-title {
  1237. font-size: 30rpx;
  1238. font-weight: 600;
  1239. color: #333333;
  1240. margin-bottom: 16rpx;
  1241. padding-bottom: 12rpx;
  1242. border-bottom: 1rpx solid #E5E5E5;
  1243. }
  1244. .info-item {
  1245. display: flex;
  1246. justify-content: space-between;
  1247. align-items: center;
  1248. margin-bottom: 12rpx;
  1249. padding: 8rpx 0;
  1250. }
  1251. .info-item:last-child {
  1252. margin-bottom: 0;
  1253. }
  1254. .info-label {
  1255. font-size: 28rpx;
  1256. color: #666666;
  1257. flex-shrink: 0;
  1258. }
  1259. .info-value {
  1260. font-size: 28rpx;
  1261. color: #333333;
  1262. font-weight: 500;
  1263. }
  1264. /* 任务填写表单 */
  1265. .form-card {
  1266. background: #FFFFFF;
  1267. margin: 0 24rpx 24rpx;
  1268. padding: 24rpx;
  1269. border-radius: 8rpx;
  1270. border: 1rpx solid #E5E5E5;
  1271. }
  1272. .section-divider {
  1273. height: 1rpx;
  1274. background: #F0F0F0;
  1275. margin: 32rpx 0 24rpx;
  1276. }
  1277. .section-title {
  1278. font-size: 30rpx;
  1279. font-weight: 600;
  1280. color: #333333;
  1281. margin-bottom: 16rpx;
  1282. padding-bottom: 12rpx;
  1283. border-bottom: 1rpx solid #F0F0F0;
  1284. }
  1285. .form-item {
  1286. margin-bottom: 24rpx;
  1287. }
  1288. .form-item:last-child {
  1289. margin-bottom: 0;
  1290. }
  1291. .form-label {
  1292. font-size: 28rpx;
  1293. color: #333333;
  1294. margin-bottom: 12rpx;
  1295. font-weight: 500;
  1296. }
  1297. .form-label.required::before {
  1298. content: '*';
  1299. color: #FF6B6B;
  1300. margin-right: 4rpx;
  1301. }
  1302. .form-input {
  1303. width: 100%;
  1304. line-height: 80rpx;
  1305. height: 80rpx;
  1306. background: #F8F8F8;
  1307. border: 1rpx solid #E5E5E5;
  1308. border-radius: 8rpx;
  1309. padding: 0 16rpx;
  1310. font-size: 28rpx;
  1311. color: #333333;
  1312. box-sizing: border-box;
  1313. }
  1314. .form-input:focus {
  1315. background: #FFFFFF;
  1316. border-color: #3BB44A;
  1317. }
  1318. .form-input::placeholder {
  1319. color: #CCCCCC;
  1320. }
  1321. .form-input:disabled {
  1322. background: #F8F8F8;
  1323. color: #999999;
  1324. }
  1325. .select-wrapper {
  1326. position: relative;
  1327. width: 100%;
  1328. }
  1329. .select-input {
  1330. cursor: pointer;
  1331. }
  1332. .select-arrow {
  1333. position: absolute;
  1334. right: 16rpx;
  1335. top: 50%;
  1336. transform: translateY(-50%);
  1337. color: #999999;
  1338. font-size: 20rpx;
  1339. pointer-events: none;
  1340. }
  1341. .form-textarea {
  1342. width: 100%;
  1343. min-height: 120rpx;
  1344. background: #F8F8F8;
  1345. border: 1rpx solid #E5E5E5;
  1346. border-radius: 8rpx;
  1347. padding: 16rpx;
  1348. font-size: 28rpx;
  1349. color: #333333;
  1350. box-sizing: border-box;
  1351. resize: none;
  1352. line-height: 1.5;
  1353. }
  1354. .form-textarea:focus {
  1355. background: #FFFFFF;
  1356. border-color: #3BB44A;
  1357. }
  1358. .form-textarea::placeholder {
  1359. color: #CCCCCC;
  1360. }
  1361. .form-textarea:disabled {
  1362. background: #F8F8F8;
  1363. color: #999999;
  1364. }
  1365. .char-count {
  1366. font-size: 24rpx;
  1367. color: #CCCCCC;
  1368. text-align: right;
  1369. margin-top: 8rpx;
  1370. }
  1371. .form-error {
  1372. font-size: 24rpx;
  1373. color: #FF6B6B;
  1374. margin-top: 8rpx;
  1375. }
  1376. /* 单选按钮样式 */
  1377. .radio-group {
  1378. display: flex;
  1379. gap: 32rpx;
  1380. }
  1381. .radio-item {
  1382. display: flex;
  1383. align-items: center;
  1384. cursor: pointer;
  1385. }
  1386. .radio-circle {
  1387. width: 32rpx;
  1388. height: 32rpx;
  1389. border: 2rpx solid #CCCCCC;
  1390. border-radius: 50%;
  1391. display: flex;
  1392. align-items: center;
  1393. justify-content: center;
  1394. margin-right: 8rpx;
  1395. }
  1396. .radio-circle.radio-checked {
  1397. border-color: #3BB44A;
  1398. background-color: #3BB44A;
  1399. }
  1400. .radio-dot {
  1401. width: 12rpx;
  1402. height: 12rpx;
  1403. background-color: #FFFFFF;
  1404. border-radius: 50%;
  1405. }
  1406. .radio-label {
  1407. font-size: 28rpx;
  1408. color: #333333;
  1409. }
  1410. /* 完成状态样式 */
  1411. .status-completed {
  1412. display: flex;
  1413. align-items: center;
  1414. color: #3BB44A;
  1415. font-size: 28rpx;
  1416. }
  1417. .status-icon {
  1418. width: 32rpx;
  1419. height: 32rpx;
  1420. background: #3BB44A;
  1421. color: #FFFFFF;
  1422. border-radius: 50%;
  1423. display: flex;
  1424. align-items: center;
  1425. justify-content: center;
  1426. margin-right: 8rpx;
  1427. font-size: 20rpx;
  1428. line-height: 1;
  1429. }
  1430. /* 图片上传样式 */
  1431. .image-upload, .image-view {
  1432. width: 100%;
  1433. }
  1434. .image-list {
  1435. display: flex;
  1436. flex-wrap: wrap;
  1437. gap: 16rpx;
  1438. }
  1439. .image-preview {
  1440. position: relative;
  1441. width: 160rpx;
  1442. height: 160rpx;
  1443. border-radius: 8rpx;
  1444. overflow: hidden;
  1445. background: #F5F5F5;
  1446. }
  1447. .image-preview image {
  1448. width: 100%;
  1449. height: 100%;
  1450. }
  1451. .delete-btn {
  1452. position: absolute;
  1453. top: -6rpx;
  1454. right: -6rpx;
  1455. width: 32rpx;
  1456. height: 32rpx;
  1457. background: rgba(0, 0, 0, 0.6);
  1458. color: #FFFFFF;
  1459. border-radius: 50%;
  1460. display: flex;
  1461. align-items: center;
  1462. justify-content: center;
  1463. font-size: 20rpx;
  1464. line-height: 1;
  1465. }
  1466. .upload-btn {
  1467. width: 160rpx;
  1468. height: 160rpx;
  1469. background: #F8F8F8;
  1470. border: 2rpx dashed #DDDDDD;
  1471. border-radius: 8rpx;
  1472. display: flex;
  1473. flex-direction: column;
  1474. align-items: center;
  1475. justify-content: center;
  1476. }
  1477. .upload-icon {
  1478. font-size: 32rpx;
  1479. margin-bottom: 8rpx;
  1480. color: #999999;
  1481. }
  1482. .upload-text {
  1483. font-size: 24rpx;
  1484. color: #999999;
  1485. }
  1486. .upload-tip {
  1487. font-size: 24rpx;
  1488. color: #999999;
  1489. margin-top: 12rpx;
  1490. }
  1491. .no-images {
  1492. padding: 40rpx 0;
  1493. text-align: center;
  1494. color: #CCCCCC;
  1495. font-size: 28rpx;
  1496. }
  1497. /* 底部安全区域 */
  1498. .bottom-safe {
  1499. height: 120rpx;
  1500. }
  1501. .footer-safe {
  1502. background: #FFFFFF;
  1503. border-top: 1rpx solid #E5E5E5;
  1504. padding-bottom: constant(safe-area-inset-bottom);
  1505. padding-bottom: env(safe-area-inset-bottom);
  1506. }
  1507. .footer-content {
  1508. padding: 24rpx;
  1509. }
  1510. .submit-button {
  1511. width: 100%;
  1512. height: 88rpx;
  1513. background: #3BB44A;
  1514. color: #FFFFFF;
  1515. border: none;
  1516. border-radius: 8rpx;
  1517. font-size: 32rpx;
  1518. font-weight: 600;
  1519. display: flex;
  1520. align-items: center;
  1521. justify-content: center;
  1522. }
  1523. .submit-button:active {
  1524. background: #2D8C3C;
  1525. }
  1526. .submit-button:disabled,
  1527. .submit-button.loading {
  1528. background: #CCCCCC;
  1529. }
  1530. /* 选择器弹窗样式 */
  1531. .picker-mask {
  1532. position: fixed;
  1533. top: 0;
  1534. left: 0;
  1535. width: 100%;
  1536. height: 100%;
  1537. background: rgba(0, 0, 0, 0.5);
  1538. z-index: 999;
  1539. }
  1540. .picker-popup {
  1541. position: fixed;
  1542. bottom: 0;
  1543. left: 0;
  1544. width: 100%;
  1545. background: #FFFFFF;
  1546. border-radius: 16rpx 16rpx 0 0;
  1547. z-index: 1000;
  1548. padding-bottom: constant(safe-area-inset-bottom);
  1549. padding-bottom: env(safe-area-inset-bottom);
  1550. }
  1551. .picker-header {
  1552. display: flex;
  1553. justify-content: space-between;
  1554. align-items: center;
  1555. padding: 24rpx;
  1556. border-bottom: 1rpx solid #E5E5E5;
  1557. }
  1558. .picker-cancel, .picker-confirm {
  1559. font-size: 30rpx;
  1560. color: #3BB44A;
  1561. }
  1562. .picker-title {
  1563. font-size: 32rpx;
  1564. font-weight: 600;
  1565. color: #333333;
  1566. }
  1567. .picker-content {
  1568. max-height: 400rpx;
  1569. overflow-y: auto;
  1570. }
  1571. .picker-item {
  1572. display: flex;
  1573. justify-content: space-between;
  1574. align-items: center;
  1575. padding: 24rpx;
  1576. border-bottom: 1rpx solid #F0F0F0;
  1577. font-size: 30rpx;
  1578. color: #333333;
  1579. }
  1580. .picker-item.selected {
  1581. color: #3BB44A;
  1582. }
  1583. .picker-item:last-child {
  1584. border-bottom: none;
  1585. }
  1586. .check-mark {
  1587. color: #3BB44A;
  1588. font-size: 28rpx;
  1589. }
  1590. /* 日期时间选择器样式 */
  1591. .datetime-picker {
  1592. height: 60vh;
  1593. }
  1594. .datetime-picker-content {
  1595. height: calc(100% - 100rpx);
  1596. padding: 0;
  1597. }
  1598. .datetime-picker-view {
  1599. width: 100%;
  1600. height: 100%;
  1601. }
  1602. .picker-view-item {
  1603. display: flex;
  1604. align-items: center;
  1605. justify-content: center;
  1606. height: 80rpx;
  1607. font-size: 32rpx;
  1608. color: #333333;
  1609. }
  1610. .picker-item-text {
  1611. font-size: 32rpx;
  1612. color: #333333;
  1613. line-height: 80rpx;
  1614. height: 80rpx;
  1615. text-align: center;
  1616. }
  1617. /* 添加加载中样式 */
  1618. .picker-loading {
  1619. display: flex;
  1620. justify-content: center;
  1621. align-items: center;
  1622. padding: 30rpx 0;
  1623. color: #999;
  1624. font-size: 28rpx;
  1625. }
  1626. .picker-empty {
  1627. padding: 30rpx 0;
  1628. text-align: center;
  1629. color: #CCCCCC;
  1630. font-size: 28rpx;
  1631. }
  1632. </style>