familyInfo.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <template>
  2. <view class="container">
  3. <uni-section title="基础信息" type="line">
  4. <uni-forms ref="infoForm" :rules="rules" :modelValue="formData" :label-width="90">
  5. <uni-forms-item required label="姓名" name="name">
  6. <uni-easyinput type="text" v-model="formData.name" placeholder="请输入姓名" />
  7. </uni-forms-item>
  8. <uni-forms-item required label="性别" name="sex">
  9. <uni-data-checkbox v-model="formData.sex" :localdata="sexs" />
  10. </uni-forms-item>
  11. <uni-forms-item name="birthday" label="出生日期">
  12. <uni-datetime-picker type="date" v-model="formData.birthday" />
  13. </uni-forms-item>
  14. <uni-forms-item required label="联系电话" name="phone"> <uni-easyinput type="text" v-model="formData.phone" placeholder="请输入联系电话" /> </uni-forms-item>
  15. </uni-forms>
  16. </uni-section>
  17. <uni-section title="地址信息" type="line">
  18. <uni-forms ref="addrForm" :modelValue="formData" :label-width="90">
  19. <uni-forms-item label="小区选择">
  20. <uni-data-picker v-model="formData.estateId" :map="pickerMap" :localdata="items" placeholder="请选择地址" popup-title="请选择地址" @change="onchange"></uni-data-picker>
  21. </uni-forms-item>
  22. <uni-forms-item label="楼栋选择" v-if="house.range && house.range.length > 0">
  23. <uni-data-picker v-model="formData.buildingId" :localdata="house.range" placeholder="请选择小区" popup-title="请选择小区" @change="selectChange"></uni-data-picker>
  24. </uni-forms-item>
  25. <uni-forms-item label="单元选择" v-if="house.unit && house.unit.length > 0">
  26. <uni-data-picker v-model="formData.unit" :localdata="house.unit" placeholder="请选择单元" popup-title="请选择单元" @change="unitChange"></uni-data-picker>
  27. </uni-forms-item>
  28. <uni-forms-item label="楼层选择" v-if="house.floor && house.unit.length > 0">
  29. <uni-data-picker v-model="formData.floor" :localdata="house.floor" placeholder="请选择楼层" popup-title="请选择楼层" @change="floorChange"></uni-data-picker>
  30. </uni-forms-item>
  31. <uni-forms-item label="门牌选择" v-if="house.number && house.number.length > 0">
  32. <uni-data-picker v-model="formData.houseId" :localdata="house.number" placeholder="请选择门牌" popup-title="请选择门牌" @change="houseChange"></uni-data-picker>
  33. </uni-forms-item>
  34. </uni-forms>
  35. </uni-section>
  36. <button class="btn" type="primary" @click="submit('infoForm')">提交</button>
  37. </view>
  38. </template>
  39. <script>
  40. import request from '../../api/user.js';
  41. import requestFamily from '../../api/family.js';
  42. export default {
  43. data() {
  44. return {
  45. formData: {},
  46. sexs: [
  47. { text: '男', value: 1 },
  48. { text: '女', value: 0 },
  49. ],
  50. items: [],
  51. pickerMap: {
  52. text: 'label',
  53. value: 'interId'
  54. },
  55. range: [],
  56. house: {},
  57. building: [],
  58. unit: '',
  59. residentReqId: '',
  60. residentId: '',
  61. // 校验规则
  62. rules: {
  63. name: {
  64. rules: [{
  65. required: true,
  66. errorMessage: '姓名不能为空'
  67. }]
  68. },
  69. sex: {
  70. rules: [{
  71. required: true,
  72. errorMessage: '性别不能为空'
  73. }]
  74. },
  75. phone: {
  76. rules: [{
  77. required: true,
  78. errorMessage: '电话不能为空'
  79. }]
  80. },
  81. },
  82. }
  83. },
  84. async onLoad(option) {
  85. this.residentId = option.residentId;
  86. this.residentReqId = option.residentReqId;
  87. if (option.residentId) {
  88. this.queryInfo();
  89. return;
  90. }
  91. if (option.residentReqId) {
  92. this.queryReqInfo();
  93. return;
  94. }
  95. // 设置默认地址信息
  96. const userinfo = uni.getStorageSync('userinfo');
  97. const { estateId, buildingId, unit, floor, houseId } = userinfo;
  98. this.formData = { estateId, buildingId, unit, floor, houseId };
  99. this.setAddr(userinfo);
  100. },
  101. async mounted() {
  102. const res = await request.addrTreeSelect();
  103. this.items = res.data;
  104. },
  105. methods: {
  106. async queryReqInfo() {
  107. const userinfo = await requestFamily.getReqInfoDetails({ residentReqId: this.residentReqId });
  108. this.formData = userinfo.data;
  109. this.setAddr(userinfo.data);
  110. },
  111. async queryInfo() {
  112. const userinfo = await requestFamily.getFamilyInfoDetails({ residentId: this.residentId });
  113. this.formData = userinfo.data;
  114. this.setAddr(userinfo.data);
  115. },
  116. // 设置地址
  117. async setAddr(data) {
  118. // 小区存在
  119. if (data.estateId && data.estateId !== null) {
  120. await this.onchange({ detail: { value: [{ value: data.estateId }] }, isUpdate: true });
  121. }
  122. // 楼栋存在
  123. if(data.buildingId && data.buildingId !== null) {
  124. await this.selectChange({ detail: { value: [{ value: data.buildingId }] }, isUpdate: true });
  125. }
  126. // 单元存在
  127. if(data.unit && data.unit !== null) {
  128. await this.unitChange({ detail: { value: [{ value: data.unit }] }, isUpdate: true });
  129. }
  130. // 楼层存在
  131. if(data.floor && data.floor !== null) {
  132. await this.floorChange({ detail: { value: [{ value: data.floor }] }, isUpdate: true });
  133. }
  134. // 门牌存在
  135. if(data.houseId && data.houseId !== null) {
  136. await this.houseChange({ detail: { value: [{ value: data.houseId }] } });
  137. }
  138. },
  139. // 初始化选择器
  140. initPicker(e) {
  141. if (e.includes('range')) this.$set(this.house, 'range', []);
  142. if (e.includes('unit')) this.$set(this.house, 'unit', []);
  143. if (e.includes('floor')) this.$set(this.house, 'floor', []);
  144. if (e.includes('number')) this.$set(this.house, 'number', []);
  145. },
  146. // 小区选择
  147. async onchange(e) {
  148. if (!e || e == '' || e.detail.value.length <= 0) {
  149. this.initPicker(['range', 'unit', 'floor', 'number']);
  150. return;
  151. }
  152. const val = e.detail.value[e.detail.value.length - 1].value;
  153. const res = await request.buildingList({ estateId: val });
  154. const range = res.rows.map(e => ({ ...e, text: `${e.number}栋`, value: e.buildingId }));
  155. this.$set(this.house, 'range', range);
  156. // 删除选项
  157. if (e.isUpdate) return;
  158. delete this.formData.buildingId
  159. delete this.formData.unit
  160. delete this.formData.floor
  161. delete this.formData.houseId
  162. },
  163. // 楼栋选择
  164. async selectChange(e) {
  165. if (!e || e == '' || e.detail.value.length <= 0) {
  166. this.initPicker(['unit', 'floor', 'number']);
  167. return;
  168. };
  169. const res = await request.houseList({ buildingId: e.detail.value[0].value });
  170. this.building = res.rows;
  171. const list = [];
  172. const numList = [];
  173. const unit = res.rows.filter(j => j.unit !== null).map(j => ({ ...j, text: `${j.unit}单元`, value: j.unit }));
  174. const number = res.rows.filter(j => j.number !== null).map(j => ({ ...j, text: `${j.number}号`, value: j.houseId }));
  175. unit.forEach(k => {
  176. const isunit = list.find(j => j.unit == k.unit);
  177. if (!isunit) list.push(k);
  178. });
  179. this.$set(this.house, 'unit', list);
  180. number.forEach(k => {
  181. const isunit = numList.find(j => j.number == k.number);
  182. if (!isunit) numList.push(k);
  183. });
  184. this.$set(this.house, 'number', numList);
  185. // 删除选项
  186. if (e.isUpdate) return;
  187. delete this.formData.unit
  188. delete this.formData.floor
  189. delete this.formData.houseId
  190. },
  191. // 单元选择
  192. unitChange(e) {
  193. if (!e || e == '' || e.detail.value.length <= 0) {
  194. this.initPicker([ 'floor', 'number']);
  195. return;
  196. }
  197. this.unit = e.detail.value[0].value;
  198. const list = [];
  199. const floor = this.building.filter(j => j.unit == e.detail.value[0].value).map(j => ({ ...j, text: `${j.floor}层`, value: j.floor }));
  200. floor.forEach(k => {
  201. const isunit = list.find(j => j.floor == k.floor);
  202. if (!isunit) list.push(k);
  203. });
  204. this.$set(this.house, 'floor', list);
  205. // 删除选项
  206. if (e.isUpdate) return;
  207. // delete this.formData.unit
  208. delete this.formData.floor
  209. delete this.formData.houseId
  210. },
  211. // 楼层选择
  212. floorChange(e) {
  213. if (!e || e == '' || e.detail.value.length <= 0) return;
  214. const list = [];
  215. const number = this.building.filter(j => j.floor == e.detail.value[0].value && j.unit == this.unit).map(j => ({ ...j, text: `${j.number}号`, value: j.houseId }));
  216. number.forEach(k => {
  217. const isunit = list.find(j => j.number == k.number);
  218. if (!isunit) list.push(k);
  219. });
  220. this.$set(this.house, 'number', list);
  221. // 删除选项
  222. if (e.isUpdate) return;
  223. // delete this.formData.unit
  224. // delete this.formData.floor
  225. delete this.formData.houseId
  226. },
  227. // 门牌选择
  228. houseChange(e) {
  229. if (!e || e == '' || e.detail.value.length <= 0) return;
  230. const dept = this.house.number.find(j => j.houseId == e.detail.value[0].value);
  231. this.formData.deptId = dept.deptId;
  232. },
  233. // 提交
  234. async submit(ref) {
  235. this.$refs[ref].validate(async (err, formdata) => {
  236. if (!err) {
  237. // 判断勾选后是否添加地址
  238. if (!this.formData.estateId || !this.formData.buildingId || !this.formData.houseId == null) {
  239. uni.showToast({
  240. title: '请填写完整地址信息',
  241. icon: 'error',
  242. duration: 2000,
  243. });
  244. return;
  245. }
  246. // 居民修改
  247. this.formData.reqType = this.formData.residentId || this.formData.residentReqId ? 1 : 0;
  248. const res = await requestFamily.familyInfo(this.formData);
  249. if (res.code && res.code == 200) {
  250. uni.showToast({
  251. title: this.formData.residentId ? '修改成功' : '添加成功',
  252. duration: 2000,
  253. });
  254. uni.navigateBack()
  255. }
  256. uni.showToast({
  257. title: res.details,
  258. icon: 'error',
  259. duration: 2000,
  260. });
  261. }
  262. })
  263. },
  264. }
  265. }
  266. </script>
  267. <style>
  268. .uni-section {
  269. padding-bottom: 10px;
  270. margin-bottom: 10px;
  271. }
  272. .uni-select__selector {
  273. z-index: 999 !important;
  274. }
  275. .uni-forms, .btn {
  276. width: 90%;
  277. margin: 10px auto;
  278. }
  279. </style>