index.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 required label="手机号" name="phone">
  12. <view class="phoneBox">
  13. <uni-easyinput disabled type="text" v-model="formData.phone" placeholder="手机号" />
  14. <button type="primary" :plain="true" style="border: none;" size="mini" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">获取手机号</button>
  15. </view>
  16. </uni-forms-item>
  17. <uni-forms-item name="birthday" label="出生日期">
  18. <uni-datetime-picker type="date" v-model="formData.birthday" />
  19. </uni-forms-item>
  20. </uni-forms>
  21. </uni-section>
  22. <uni-section title="地址信息" sub-title="(富裕街道居民请填写地址信息)" type="line">
  23. <uni-forms ref="addrForm" :modelValue="formData" :label-width="90">
  24. <uni-forms-item label="小区选择">
  25. <uni-data-picker v-model="formData.estateId" :map="pickerMap" :localdata="items" placeholder="请选择地址" popup-title="请选择地址" @change="onchange"></uni-data-picker>
  26. </uni-forms-item>
  27. <uni-forms-item label="楼栋选择">
  28. <uni-data-picker v-model="formData.buildingId" :localdata="range" placeholder="请选择小区" popup-title="请选择小区" @change="selectChange"></uni-data-picker>
  29. </uni-forms-item>
  30. <uni-forms-item label="单元选择" v-if="house.unit && house.unit.length > 0">
  31. <uni-data-picker v-model="formData.unit" :localdata="house.unit" placeholder="请选择单元" popup-title="请选择单元" @change="unitChange"></uni-data-picker>
  32. </uni-forms-item>
  33. <uni-forms-item label="楼层选择" v-if="house.floor && house.unit.length > 0">
  34. <uni-data-picker v-model="formData.floor" :localdata="house.floor" placeholder="请选择楼层" popup-title="请选择楼层" @change="floorChange"></uni-data-picker>
  35. </uni-forms-item>
  36. <uni-forms-item label="门牌选择" v-if="house.number && house.number !== ''">
  37. <uni-data-picker v-model="formData.houseId" :localdata="house.number" placeholder="请选择门牌" popup-title="请选择门牌" @change="houseChange"></uni-data-picker>
  38. </uni-forms-item>
  39. </uni-forms>
  40. </uni-section>
  41. <button class="btn" type="primary" @click="submit('infoForm')">提交</button>
  42. </view>
  43. </template>
  44. <script>
  45. import request from '../../api/user.js';
  46. import requestLogin from '../../api/login.js';
  47. const appid = uni.getAccountInfoSync().miniProgram.appId;
  48. export default {
  49. data() {
  50. return {
  51. role: '',
  52. path: null,
  53. formData: {},
  54. sexs: [
  55. { text: '男', value: 1 },
  56. { text: '女', value: 0 },
  57. ],
  58. items: [],
  59. pickerMap: {
  60. text: 'label',
  61. value: 'interId'
  62. },
  63. range: [],
  64. house: {},
  65. building: [],
  66. unit: '',
  67. // 校验规则
  68. rules: {
  69. name: {
  70. rules: [{
  71. required: true,
  72. errorMessage: '姓名不能为空'
  73. }]
  74. },
  75. sex: {
  76. rules: [{
  77. required: true,
  78. errorMessage: '性别不能为空'
  79. }]
  80. },
  81. phone: {
  82. rules: [{
  83. required: true,
  84. errorMessage: '电话不能为空'
  85. }]
  86. },
  87. },
  88. }
  89. },
  90. async mounted() {
  91. const res = await request.addrTreeSelect();
  92. this.items = res.data;
  93. },
  94. methods: {
  95. // 获取手机号
  96. async decryptPhoneNumber(e) {
  97. const res = await requestLogin.getPhone({ code: e.detail.code, appid: appid });
  98. this.$set(this.formData, 'phone', res.phone_info.phoneNumber);
  99. },
  100. // 小区选择
  101. async onchange(e) {
  102. const val = e.detail.value[e.detail.value.length - 1].value;
  103. const res = await request.buildingList({ estateId: val });
  104. this.range = res.rows.map(e => ({ ...e, text: `${e.number}栋`, value: e.buildingId }));
  105. },
  106. // 楼栋选择
  107. async selectChange(e) {
  108. if (!e || e == '' || e.detail.value.length <= 0) return;
  109. const res = await request.houseList({ buildingId: e.detail.value[0].value });
  110. this.building = res.rows;
  111. const list = [];
  112. const numList = [];
  113. const unit = res.rows.filter(j => j.unit !== null).map(j => ({ ...j, text: `${j.unit}单元`, value: j.unit }));
  114. const number = res.rows.filter(j => j.number !== null).map(j => ({ ...j, text: `${j.number}号`, value: j.houseId }));
  115. unit.forEach(k => {
  116. const isunit = list.find(j => j.unit == k.unit);
  117. if (!isunit) list.push(k);
  118. });
  119. this.$set(this.house, 'unit', list);
  120. number.forEach(k => {
  121. const isunit = numList.find(j => j.number == k.number);
  122. if (!isunit) numList.push(k);
  123. });
  124. this.$set(this.house, 'number', numList);
  125. },
  126. // 单元选择
  127. unitChange(e) {
  128. if (!e || e == '' || e.detail.value.length <= 0) return;
  129. this.unit = e.detail.value[0].value;
  130. const list = [];
  131. const floor = this.building.filter(j => j.unit == e.detail.value[0].value).map(j => ({ ...j, text: `${j.floor}层`, value: j.floor }));
  132. floor.forEach(k => {
  133. const isunit = list.find(j => j.floor == k.floor);
  134. if (!isunit) list.push(k);
  135. });
  136. this.$set(this.house, 'floor', list);
  137. },
  138. // 楼层选择
  139. floorChange(e) {
  140. if (!e || e == '' || e.detail.value.length <= 0) return;
  141. const list = [];
  142. 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 }));
  143. number.forEach(k => {
  144. const isunit = list.find(j => j.number == k.number);
  145. if (!isunit) list.push(k);
  146. });
  147. this.$set(this.house, 'number', list);
  148. },
  149. // 门牌选择
  150. houseChange(e) {
  151. if (!e || e == '' || e.detail.value.length <= 0) return;
  152. const dept = this.house.number.find(j => j.houseId == e.detail.value[0].value);
  153. this.formData.deptId = dept.deptId;
  154. },
  155. // 提交
  156. async submit(ref) {
  157. this.$refs[ref].validate(async (err, formdata) => {
  158. if (!err) {
  159. // 添加用户
  160. const res = await request.addUser(this.formData);
  161. uni.setStorageSync('token', res.data.token);
  162. uni.setStorageSync('userinfo', res.data.user);
  163. uni.setStorageSync('role', res.data.role);
  164. uni.showToast({
  165. title: '添加成功',
  166. duration: 2000,
  167. });
  168. if (this.path && this.path !== null) {
  169. uni.redirectTo({
  170. url: this.path
  171. })
  172. }
  173. uni.navigateBack()
  174. }
  175. })
  176. },
  177. },
  178. onLoad(option) {
  179. this.path == option.path;
  180. }
  181. }
  182. </script>
  183. <style>
  184. .uni-section {
  185. padding-bottom: 10px;
  186. margin-bottom: 10px;
  187. }
  188. .uni-select__selector {
  189. z-index: 999 !important;
  190. }
  191. .uni-forms, .btn {
  192. width: 90%;
  193. margin: 10px auto;
  194. }
  195. .phoneBox {
  196. display: flex;
  197. }
  198. </style>