addFamilyMember.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <template>
  2. <view class="page-container">
  3. <!-- 基本信息区域 -->
  4. <view class="section">
  5. <view class="section-title">基本信息</view>
  6. <view class="form-card">
  7. <!-- 变更理由 -->
  8. <view class="form-item">
  9. <view class="item-label">变更理由<text class="required">*</text></view>
  10. <picker :range="dictData.changeReason" :range-key="'label'"
  11. :value="getDictIndex('changeReason', editForm.changeReason)"
  12. @change="handleDictChange('changeReason', $event)">
  13. <view class="item-picker">
  14. {{ getDictLabel('changeReason', editForm.changeReason) || '请选择' }}
  15. <text class="picker-arrow">▼</text>
  16. </view>
  17. </picker>
  18. </view>
  19. <!-- 成员姓名 -->
  20. <view class="form-item">
  21. <view class="item-label">成员姓名<text class="required">*</text></view>
  22. <input class="item-input" v-model="editForm.cyxm" placeholder="请输入"
  23. placeholder-class="input-placeholder" />
  24. </view>
  25. <!-- 证件类型 -->
  26. <view class="form-item">
  27. <view class="item-label">证件类型<text class="required">*</text></view>
  28. <picker :range="dictData.cyzjlx" :range-key="'label'"
  29. :value="getDictIndex('cyzjlx', editForm.cyzjlx)" @change="handleDictChange('cyzjlx', $event)">
  30. <view class="item-picker">
  31. {{ getDictLabel('cyzjlx', editForm.cyzjlx) || '请选择' }}
  32. <text class="picker-arrow">▼</text>
  33. </view>
  34. </picker>
  35. </view>
  36. <!-- 证件号码 -->
  37. <view class="form-item">
  38. <view class="item-label">证件号码<text class="required">*</text></view>
  39. <input class="item-input" v-model="editForm.cyzjhm" placeholder="请输入"
  40. placeholder-class="input-placeholder" maxlength="18" />
  41. </view>
  42. <!-- 成员性别 -->
  43. <view class="form-item">
  44. <view class="item-label">成员性别<text class="required">*</text></view>
  45. <picker :range="dictData.cyxb" :range-key="'label'" :value="getDictIndex('cyxb', editForm.cyxb)"
  46. @change="handleDictChange('cyxb', $event)">
  47. <view class="item-picker">
  48. {{ getDictLabel('cyxb', editForm.cyxb) || '请选择' }}
  49. <text class="picker-arrow">▼</text>
  50. </view>
  51. </picker>
  52. </view>
  53. <!-- 与户主关系 -->
  54. <view class="form-item">
  55. <view class="item-label">与户主关系<text class="required">*</text></view>
  56. <picker :range="dictData.yhzgx" :range-key="'label'" :value="getDictIndex('yhzgx', editForm.yhzgx)"
  57. @change="handleDictChange('yhzgx', $event)">
  58. <view class="item-picker">
  59. {{ getDictLabel('yhzgx', editForm.yhzgx) || '请选择' }}
  60. <text class="picker-arrow">▼</text>
  61. </view>
  62. </picker>
  63. </view>
  64. <!-- 成员备注 -->
  65. <view class="form-item">
  66. <view class="item-label">成员备注</view>
  67. <picker :range="dictData.cybz" :range-key="'label'" :value="getDictIndex('cybz', editForm.cybz)"
  68. @change="handleDictChange('cybz', $event)">
  69. <view class="item-picker">
  70. {{ getDictLabel('cybz', editForm.cybz) || '请选择' }}
  71. <text class="picker-arrow">▼</text>
  72. </view>
  73. </picker>
  74. </view>
  75. <!-- 是否共有人 -->
  76. <view class="form-item">
  77. <view class="item-label">是否共有人</view>
  78. <picker :range="dictData.sf" :range-key="'label'" :value="getDictIndex('sf', editForm.sfgyr)"
  79. @change="handleDictChange('sf', $event)">
  80. <view class="item-picker">
  81. {{ getDictLabel('sf', editForm.sfgyr) || '请选择' }}
  82. <text class="picker-arrow">▼</text>
  83. </view>
  84. </picker>
  85. </view>
  86. <!-- 成员备注说明 -->
  87. <view class="form-item form-item-textarea">
  88. <view class="item-label">成员备注说明</view>
  89. <textarea class="item-textarea" v-model="editForm.cybzsm" placeholder="请输入"
  90. placeholder-class="input-placeholder" />
  91. </view>
  92. </view>
  93. </view>
  94. <!-- 图片信息区域 -->
  95. <view class="section">
  96. <view class="section-title">图片信息</view>
  97. <view class="form-card">
  98. <!-- 身份证人像面 -->
  99. <view class="upload-item">
  100. <view class="upload-title">身份证人像面(可用于识别姓名和身份证号码,错误请自行修改)</view>
  101. <view class="upload-wrap">
  102. <view class="upload-box" v-for="(img, idx) in imageList.idFront.img" :key="idx">
  103. <image :src="img" class="upload-img" mode="aspectFill" />
  104. <view class="del-btn" @click.stop="delImg('idFront', idx)">×</view>
  105. </view>
  106. <view class="upload-box add-box" @click="handleUploadImage('idFront')">
  107. <view class="upload-plus">+</view>
  108. </view>
  109. </view>
  110. </view>
  111. <!-- 身份证国徽面 -->
  112. <view class="upload-item">
  113. <view class="upload-title">身份证国徽面</view>
  114. <view class="upload-wrap">
  115. <view class="upload-box" v-for="(img, idx) in imageList.idBack.img" :key="idx">
  116. <image :src="img" class="upload-img" mode="aspectFill" />
  117. <view class="del-btn" @click.stop="delImg('idBack', idx)">×</view>
  118. </view>
  119. <view class="upload-box add-box" @click="handleUploadImage('idBack')">
  120. <view class="upload-plus">+</view>
  121. </view>
  122. </view>
  123. </view>
  124. <!-- 证明材料 -->
  125. <view class="upload-item">
  126. <view class="upload-title required">*证明材料(修改请上传户口本对应页,删除请上传证明材料)</view>
  127. <view class="upload-wrap">
  128. <view class="upload-box" v-for="(img, idx) in imageList.certificate.img" :key="idx">
  129. <image :src="img" class="upload-img" mode="aspectFill" />
  130. <view class="del-btn" @click.stop="delImg('certificate', idx)">×</view>
  131. </view>
  132. <view class="upload-box add-box" @click="handleUploadImage('certificate')">
  133. <view class="upload-plus">+</view>
  134. </view>
  135. </view>
  136. </view>
  137. </view>
  138. </view>
  139. <!-- 底部添加按钮 -->
  140. <view class="submit-btn" @click="handleSubmit">添加</view>
  141. </view>
  142. </template>
  143. <script setup lang="ts">
  144. import { ref, computed } from 'vue';
  145. import { onLoad } from '@dcloudio/uni-app';
  146. import { uploadFile } from '@/utils/upload.js'
  147. import { getDictData, addfamilyMembersList } from '@/api/farmerApi.js';
  148. // ==================== 类型定义 ====================
  149. interface DictItem {
  150. value: string;
  151. label: string;
  152. }
  153. interface DictData {
  154. changeReason: DictItem[];
  155. cyzjlx: DictItem[];
  156. cyxb: DictItem[];
  157. yhzgx: DictItem[];
  158. cybz: DictItem[];
  159. sf: DictItem[];
  160. }
  161. interface EditForm {
  162. auditOpinion: string;
  163. bizStatus: string;
  164. changeReason: string;
  165. sfgyr: string;
  166. cyxm: string;
  167. cyzjlx: string;
  168. cyzjhm: string;
  169. cyxb: string;
  170. yhzgx: string;
  171. cybz: string;
  172. cybzsm: string;
  173. cbfbm?: any;
  174. }
  175. // ✅ 类型同步修改
  176. interface ImageItem {
  177. img: string[];
  178. number: string[];
  179. }
  180. interface ImageList {
  181. idFront: ImageItem;
  182. idBack: ImageItem;
  183. certificate: ImageItem;
  184. }
  185. type DictType = keyof DictData;
  186. // ==================== 响应式数据 ====================
  187. const member = ref<any>(null);
  188. const dictData = ref<DictData>({
  189. changeReason: [],
  190. cyzjlx: [],
  191. cyxb: [],
  192. yhzgx: [],
  193. cybz: [],
  194. sf: []
  195. });
  196. const editForm = ref<EditForm>({
  197. auditOpinion: '',
  198. bizStatus: '',
  199. changeReason: '',
  200. sfgyr: '',
  201. cyxm: '',
  202. cyzjlx: '',
  203. cyzjhm: '',
  204. cyxb: '',
  205. yhzgx: '',
  206. cybz: '',
  207. cybzsm: ''
  208. });
  209. // ✅ 统一结构:img 显示 / number 存后端ID
  210. const imageList = ref<ImageList>({
  211. idFront: { img: [], number: [] },
  212. idBack: { img: [], number: [] },
  213. certificate: { img: [], number: [] }
  214. });
  215. // ==================== 字典工具函数 ====================
  216. const transformDictFormat = (originalDict: Record<string | number, string>): DictItem[] => {
  217. if (!originalDict || typeof originalDict !== 'object') return [];
  218. return Object.entries(originalDict).map(([key, value]) => ({
  219. value: String(key),
  220. label: value as string
  221. }));
  222. };
  223. const getDictLabel = (dictType: DictType, value: string | number | undefined): string => {
  224. if (!value || !dictData.value[dictType]?.length) return '';
  225. const item = dictData.value[dictType].find(item => String(item.value) === String(value));
  226. return item?.label || '';
  227. };
  228. const getDictIndex = computed(() => {
  229. return (dictType: DictType, value: string) => {
  230. if (!value || !dictData.value[dictType].length) return 0;
  231. return dictData.value[dictType].findIndex(item => String(item.value) === String(value));
  232. };
  233. });
  234. const handleDictChange = (dictType: DictType, e: any) => {
  235. const list = dictData.value[dictType];
  236. if (!list.length) return;
  237. if (dictType === 'sf') {
  238. editForm.value.sfgyr = list[e.detail.value]?.value || '';
  239. } else if (dictType === 'changeReason') {
  240. editForm.value.changeReason = list[e.detail.value]?.value || '';
  241. } else {
  242. editForm.value[dictType] = list[e.detail.value]?.value || '';
  243. }
  244. };
  245. // ==================== 字典加载 ====================
  246. const loadDictData = async () => {
  247. try {
  248. const [resChangeReason, rescyzjlx, rescyxb, resyhzgx, resRemark, ressf] = await Promise.all([
  249. getDictData('dic_bgly'),
  250. getDictData('dic_zjlx'),
  251. getDictData('dic_xb'),
  252. getDictData('dic_yhzgx'),
  253. getDictData('dic_cybz'),
  254. getDictData('dic_sf')
  255. ]);
  256. dictData.value.changeReason = resChangeReason?.code === 200 ? transformDictFormat(resChangeReason.data) : [];
  257. dictData.value.cyzjlx = rescyzjlx?.code === 200 ? transformDictFormat(rescyzjlx.data) : [];
  258. dictData.value.cyxb = rescyxb?.code === 200 ? transformDictFormat(rescyxb.data) : [];
  259. dictData.value.yhzgx = resyhzgx?.code === 200 ? transformDictFormat(resyhzgx.data) : [];
  260. dictData.value.cybz = resRemark?.code === 200 ? transformDictFormat(resRemark.data) : [];
  261. dictData.value.sf = ressf?.code === 200 ? transformDictFormat(ressf.data) : [];
  262. } catch (err) {
  263. uni.showToast({ title: '字典加载失败', icon: 'none' });
  264. }
  265. };
  266. // ==================== ✅ 上传:分开存储 显示路径 + 提交ID ====================
  267. const handleUploadImage = async (type: keyof ImageList) => {
  268. try {
  269. const chooseRes = await uni.chooseImage({ count: 1, sizeType: ['compressed'] });
  270. const filePath = chooseRes.tempFilePaths[0];
  271. uni.showLoading({ title: '上传中...' });
  272. const result = await uploadFile(filePath);
  273. if (result.code === 200 && result.data.id) {
  274. // 🔥 本地路径 → 用于显示
  275. imageList.value[type].img.push(filePath);
  276. // 🔥 后端返回ID → 用于提交
  277. imageList.value[type].number.push(result.data.id);
  278. uni.showToast({ title: '上传成功', icon: 'success' });
  279. } else {
  280. uni.showToast({ title: '上传失败', icon: 'none' });
  281. }
  282. } catch (err) {
  283. console.error(err);
  284. uni.showToast({ title: '取消上传', icon: 'none' });
  285. } finally {
  286. uni.hideLoading();
  287. }
  288. };
  289. // ==================== ✅ 删除:同时删除 img 和 number,保持对应 ====================
  290. const delImg = (type: keyof ImageList, index: number) => {
  291. imageList.value[type].img.splice(index, 1);
  292. imageList.value[type].number.splice(index, 1);
  293. };
  294. // ==================== 表单校验 ====================
  295. const validateForm = (): boolean => {
  296. const { changeReason, cyxm, cyzjlx, cyzjhm, cyxb, yhzgx } = editForm.value;
  297. if (!changeReason) { uni.showToast({ title: '请选择变更理由', icon: 'none' }); return false; }
  298. if (!cyxm?.trim()) { uni.showToast({ title: '请输入成员姓名', icon: 'none' }); return false; }
  299. if (!cyzjlx) { uni.showToast({ title: '请选择证件类型', icon: 'none' }); return false; }
  300. if (!cyzjhm?.trim()) { uni.showToast({ title: '请输入证件号码', icon: 'none' }); return false; }
  301. if (!cyxb) { uni.showToast({ title: '请选择成员性别', icon: 'none' }); return false; }
  302. if (!yhzgx) { uni.showToast({ title: '请选择与户主关系', icon: 'none' }); return false; }
  303. if (imageList.value.certificate.number.length === 0) { uni.showToast({ title: '请上传证明材料', icon: 'none' }); return false; }
  304. return true;
  305. };
  306. // ==================== 提交接口 ====================
  307. const addFMList = async (addList: any) => {
  308. try {
  309. const res = await addfamilyMembersList(addList);
  310. if (res?.code === 200) return true;
  311. uni.showToast({ title: res?.msg || '提交失败', icon: 'none' });
  312. return false;
  313. } catch (err) {
  314. uni.showToast({ title: '网络异常', icon: 'none' });
  315. return false;
  316. }
  317. };
  318. // ==================== ✅ 提交:只拼接 ID 给后端 ====================
  319. const handleSubmit = async () => {
  320. if (!validateForm()) return;
  321. const submitData = {
  322. ...editForm.value,
  323. cbfbm: member.value.cbfbm,
  324. attachmentsfzfmFileIds: imageList.value.idFront.number,
  325. attachmentsfzzmFileIds: imageList.value.idBack.number,
  326. attachmentszmclFileIds: imageList.value.certificate.number
  327. };
  328. uni.showLoading({ title: '提交中...' });
  329. const ok = await addFMList(submitData);
  330. if (ok) {
  331. uni.hideLoading();
  332. uni.showToast({ title: '添加成功', icon: 'success' });
  333. resetForm();
  334. setTimeout(() => {
  335. uni.navigateBack({
  336. success: () => {
  337. const pages = getCurrentPages();
  338. const prevPage = pages[pages.length - 1];
  339. if (prevPage && typeof prevPage.onLoad === 'function') {
  340. prevPage.onLoad(prevPage.options);
  341. }
  342. }
  343. });
  344. }, 1500);
  345. }
  346. };
  347. // ==================== 重置 ====================
  348. const resetForm = () => {
  349. editForm.value = {
  350. auditOpinion: '', bizStatus: '', changeReason: '', sfgyr: '',
  351. cyxm: '', cyzjlx: '', cyzjhm: '', cyxb: '', yhzgx: '', cybz: '', cybzsm: ''
  352. };
  353. imageList.value = {
  354. idFront: { img: [], number: [] },
  355. idBack: { img: [], number: [] },
  356. certificate: { img: [], number: [] }
  357. };
  358. };
  359. // ==================== 初始化 ====================
  360. onLoad(async (options: any) => {
  361. await loadDictData();
  362. if (options.memberInfo) {
  363. member.value = JSON.parse(decodeURIComponent(options.memberInfo));
  364. console.log(111,member.value)
  365. const info = member.value;
  366. editForm.value = { ...editForm.value, ...info };
  367. }
  368. });
  369. </script>
  370. <style lang="scss" scoped>
  371. .page-container {
  372. padding: 20rpx;
  373. padding-bottom: 40rpx;
  374. background-color: #f5faff;
  375. }
  376. .section {
  377. margin-bottom: 40rpx;
  378. }
  379. .section-title {
  380. font-size: 36rpx;
  381. font-weight: bold;
  382. color: #333;
  383. margin-bottom: 20rpx;
  384. padding-left: 10rpx;
  385. }
  386. .form-card {
  387. background-color: #fff;
  388. border-radius: 24rpx;
  389. padding: 30rpx;
  390. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  391. }
  392. .form-item {
  393. display: flex;
  394. align-items: center;
  395. justify-content: space-between;
  396. padding: 30rpx 0;
  397. border-bottom: 1rpx solid #f0f0f0;
  398. &.form-item-textarea {
  399. align-items: flex-start;
  400. padding-bottom: 0;
  401. border-bottom: none;
  402. flex-direction: column;
  403. }
  404. }
  405. .item-label {
  406. font-size: 32rpx;
  407. color: #333;
  408. flex-shrink: 0;
  409. }
  410. .required {
  411. color: #f5222d;
  412. margin-left: 4rpx;
  413. }
  414. .item-input {
  415. flex: 1;
  416. text-align: right;
  417. font-size: 32rpx;
  418. color: #333;
  419. height: 40rpx;
  420. line-height: 40rpx;
  421. &::placeholder {
  422. color: #c9cdd4;
  423. }
  424. }
  425. .input-placeholder {
  426. color: #c9cdd4;
  427. font-size: 32rpx;
  428. }
  429. .item-picker {
  430. flex: 1;
  431. display: flex;
  432. align-items: center;
  433. justify-content: flex-end;
  434. font-size: 32rpx;
  435. color: #333;
  436. height: 40rpx;
  437. line-height: 40rpx;
  438. }
  439. .picker-arrow {
  440. margin-left: 10rpx;
  441. font-size: 28rpx;
  442. color: #333;
  443. }
  444. .item-textarea {
  445. width: 100%;
  446. min-height: 200rpx;
  447. font-size: 32rpx;
  448. color: #333;
  449. border: 1rpx solid #e5e6eb;
  450. border-radius: 12rpx;
  451. padding: 20rpx;
  452. box-sizing: border-box;
  453. margin-top: 20rpx;
  454. }
  455. .upload-item {
  456. margin-bottom: 40rpx;
  457. }
  458. .upload-title {
  459. font-size: 32rpx;
  460. color: #333;
  461. margin-bottom: 20rpx;
  462. line-height: 1.5;
  463. &.required::before {
  464. content: "*";
  465. color: #f5222d;
  466. margin-right: 4rpx;
  467. }
  468. }
  469. .upload-wrap {
  470. display: flex;
  471. flex-wrap: wrap;
  472. gap: 20rpx;
  473. align-items: center;
  474. }
  475. .upload-box {
  476. width: 280rpx;
  477. height: 280rpx;
  478. background-color: #f5f5f5;
  479. border-radius: 8rpx;
  480. display: flex;
  481. align-items: center;
  482. justify-content: center;
  483. overflow: hidden;
  484. position: relative;
  485. }
  486. .upload-plus {
  487. font-size: 80rpx;
  488. color: #333;
  489. font-weight: 300;
  490. }
  491. .upload-img {
  492. width: 100%;
  493. height: 100%;
  494. }
  495. .add-box {
  496. border: 2rpx dashed #ccc;
  497. }
  498. .del-btn {
  499. position: absolute;
  500. top: 0;
  501. right: 0;
  502. width: 50rpx;
  503. height: 50rpx;
  504. background: rgba(255, 0, 0, 0.7);
  505. color: #fff;
  506. font-size: 30rpx;
  507. display: flex;
  508. align-items: center;
  509. justify-content: center;
  510. }
  511. .submit-btn {
  512. width: 80%;
  513. height: 90rpx;
  514. background-color: #409eff;
  515. color: #fff;
  516. font-size: 36rpx;
  517. font-weight: 50;
  518. border-radius: 45rpx;
  519. display: flex;
  520. align-items: center;
  521. justify-content: center;
  522. margin: 0 auto;
  523. box-shadow: 0 4rpx 12rpx rgba(64, 158, 255, 0.3);
  524. }
  525. </style>