index.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. console.log(e)
  102. const res = await requestLogin.getPhone({ code: e.detail.code, appid: appid });
  103. if (!res) {
  104. uni.showToast({
  105. title: '获取失败',
  106. icon: 'error',
  107. duration: 2000,
  108. });
  109. return
  110. }
  111. if (res && res.errcode !== 0) {
  112. uni.showToast({
  113. title: res.errmsg,
  114. icon: 'error',
  115. duration: 2000,
  116. });
  117. return
  118. }
  119. this.$set(this.formData, 'phone', res.phone_info.phoneNumber);
  120. },
  121. // 小区选择
  122. async onchange(e) {
  123. const val = e.detail.value[e.detail.value.length - 1].value;
  124. const res = await request.buildingList({ estateId: val });
  125. this.range = res.rows.map(e => ({ ...e, text: `${e.number}栋`, value: e.buildingId }));
  126. // 删除选项
  127. // if (e.isUpdate) return;
  128. delete this.formData.buildingId
  129. delete this.formData.unit
  130. delete this.formData.floor
  131. delete this.formData.houseId
  132. },
  133. // 楼栋选择
  134. async selectChange(e) {
  135. if (!e || e == '' || e.detail.value.length <= 0) return;
  136. const res = await request.houseList({ buildingId: e.detail.value[0].value });
  137. this.building = res.rows;
  138. const list = [];
  139. const numList = [];
  140. const unit = res.rows.filter(j => j.unit !== null).map(j => ({ ...j, text: `${j.unit}单元`, value: j.unit }));
  141. const number = res.rows.filter(j => j.number !== null).map(j => ({ ...j, text: `${j.number}号`, value: j.houseId }));
  142. unit.forEach(k => {
  143. const isunit = list.find(j => j.unit == k.unit);
  144. if (!isunit) list.push(k);
  145. });
  146. this.$set(this.house, 'unit', list);
  147. number.forEach(k => {
  148. const isunit = numList.find(j => j.number == k.number);
  149. if (!isunit) numList.push(k);
  150. });
  151. this.$set(this.house, 'number', numList);
  152. // delete this.formData.buildingId
  153. delete this.formData.unit
  154. delete this.formData.floor
  155. delete this.formData.houseId
  156. },
  157. // 单元选择
  158. unitChange(e) {
  159. if (!e || e == '' || e.detail.value.length <= 0) return;
  160. this.unit = e.detail.value[0].value;
  161. const list = [];
  162. const floor = this.building.filter(j => j.unit == e.detail.value[0].value).map(j => ({ ...j, text: `${j.floor}层`, value: j.floor }));
  163. floor.forEach(k => {
  164. const isunit = list.find(j => j.floor == k.floor);
  165. if (!isunit) list.push(k);
  166. });
  167. this.$set(this.house, 'floor', list);
  168. // delete this.formData.buildingId
  169. // delete this.formData.unit
  170. delete this.formData.floor
  171. delete this.formData.houseId
  172. },
  173. // 楼层选择
  174. floorChange(e) {
  175. if (!e || e == '' || e.detail.value.length <= 0) return;
  176. const list = [];
  177. 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 }));
  178. number.forEach(k => {
  179. const isunit = list.find(j => j.number == k.number);
  180. if (!isunit) list.push(k);
  181. });
  182. this.$set(this.house, 'number', list);
  183. // delete this.formData.buildingId
  184. // delete this.formData.unit
  185. // delete this.formData.floor
  186. delete this.formData.houseId
  187. },
  188. // 门牌选择
  189. houseChange(e) {
  190. if (!e || e == '' || e.detail.value.length <= 0) return;
  191. const dept = this.house.number.find(j => j.houseId == e.detail.value[0].value);
  192. this.formData.deptId = dept.deptId;
  193. },
  194. // 提交
  195. async submit(ref) {
  196. this.$refs[ref].validate(async (err, formdata) => {
  197. if (!err) {
  198. // 判断勾选后是否添加地址
  199. if (this.isShowAddr.length > 0) {
  200. if (!this.formData.estateId || !this.formData.buildingId || !this.formData.houseId == null) {
  201. uni.showToast({
  202. title: '请填写完整地址信息',
  203. icon: 'error',
  204. duration: 2000,
  205. });
  206. return;
  207. }
  208. }
  209. // 添加用户
  210. const res = await request.addUser(this.formData);
  211. uni.setStorageSync('token', res.data.token);
  212. uni.setStorageSync('userinfo', res.data.user);
  213. uni.setStorageSync('role', res.data.role);
  214. uni.showToast({
  215. title: '添加成功',
  216. duration: 2000,
  217. });
  218. if (this.path && this.path !== null) {
  219. uni.redirectTo({
  220. url: this.path
  221. })
  222. }
  223. uni.navigateBack()
  224. }
  225. })
  226. },
  227. },
  228. onLoad(option) {
  229. this.path == option.path;
  230. }
  231. }
  232. </script>
  233. <style>
  234. .uni-section {
  235. padding-bottom: 10px;
  236. margin-bottom: 10px;
  237. }
  238. .uni-select__selector {
  239. z-index: 999 !important;
  240. }
  241. .uni-forms, .btn {
  242. width: 90%;
  243. margin: 10px auto;
  244. }
  245. .phoneBox {
  246. display: flex;
  247. }
  248. .isShowAddr {
  249. width: 90%;
  250. margin: 0 auto;
  251. display: block;
  252. }
  253. </style>