index.vue 8.1 KB

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