index.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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="地址信息" 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" @nodeclick="onnodeclick"></uni-data-picker>
  26. </uni-forms-item>
  27. <uni-forms-item label="楼栋选择">
  28. <uni-data-select :localdata="range" @change="selectChange" v-model="formData.buildingId"></uni-data-select>
  29. </uni-forms-item>
  30. <uni-forms-item label="单元选择" v-if="house.unit && house.unit !== null">
  31. <uni-data-select :localdata="house.unit" @change="unitChange" v-model="formData.unit"></uni-data-select>
  32. </uni-forms-item>
  33. <uni-forms-item label="楼层选择" v-if="house.floor && house.floor !== null">
  34. <uni-data-select :localdata="house.floor" @change="floorChange" v-model="formData.floor"></uni-data-select>
  35. </uni-forms-item>
  36. <uni-forms-item label="门牌选择" v-if="house.number && house.number !== null">
  37. <uni-data-select :localdata="house.number" v-model="formData.houseId" @change="houseChange"></uni-data-select>
  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. this.role = uni.getStorageSync('role');
  94. if (this.role !== 'guest') this.queryInfo();
  95. },
  96. methods: {
  97. async queryInfo() {
  98. const userinfo = await request.getUser();
  99. this.formData = userinfo.data;
  100. const { estateId, buildingId, unit, floor, houseId } = userinfo.data;
  101. // 小区存在
  102. if (estateId) {
  103. const res = await request.buildingList({ estateId });
  104. this.range = res.rows.map(e => ({ ...e, text: `${e.number}栋`, value: e.buildingId }));
  105. }
  106. // 楼栋存在
  107. if(buildingId) {
  108. await this.selectChange(buildingId);
  109. }
  110. // 单元存在
  111. if(unit) {
  112. await this.unitChange(unit);
  113. }
  114. // 楼层存在
  115. if(floor) {
  116. await this.floorChange(floor);
  117. }
  118. // 门牌存在
  119. if(houseId) {
  120. await this.houseChange(houseId);
  121. }
  122. },
  123. // 获取手机号
  124. async decryptPhoneNumber(e) {
  125. const res = await requestLogin.getPhone({ code: e.detail.code, appid: appid });
  126. this.$set(this.formData, 'phone', res.phone_info.phoneNumber);
  127. },
  128. // 小区选择
  129. async onchange(e) {
  130. const val = e.detail.value[e.detail.value.length - 1].value;
  131. const res = await request.buildingList({ estateId: val });
  132. this.range = res.rows.map(e => ({ ...e, text: `${e.number}栋`, value: e.buildingId }));
  133. },
  134. onnodeclick(node) {},
  135. // 楼栋选择
  136. async selectChange(e) {
  137. if (!e || e == '') return;
  138. const res = await request.houseList({ buildingId: e });
  139. this.building = res.rows;
  140. const list = [];
  141. const unit = res.rows.filter(j => j.unit !== null).map(j => ({ ...j, text: `${j.unit}单元`, value: j.unit }));
  142. const number = res.rows.filter(j => j.number !== null).map(j => ({ ...j, text: `${j.number}号`, value: j.number }));
  143. if (unit && unit.length > 0) {
  144. unit.forEach(k => {
  145. const isunit = list.find(j => j.unit == k.unit);
  146. if (!isunit) list.push(k);
  147. });
  148. this.$set(this.house, 'unit', list);
  149. return;
  150. }
  151. if (number && number.length > 0) {
  152. number.forEach(k => {
  153. const isunit = list.find(j => j.number == k.number);
  154. if (!isunit) list.push(k);
  155. });
  156. this.$set(this.house, 'number', list);
  157. return;
  158. }
  159. },
  160. // 单元选择
  161. unitChange(e) {
  162. this.unit = e;
  163. const list = [];
  164. const floor = this.building.filter(j => j.unit == e).map(j => ({ ...j, text: `${j.floor}层`, value: j.floor }));
  165. floor.forEach(k => {
  166. const isunit = list.find(j => j.floor == k.floor);
  167. if (!isunit) list.push(k);
  168. });
  169. this.$set(this.house, 'floor', list);
  170. },
  171. // 楼层选择
  172. floorChange(e) {
  173. const list = [];
  174. const number = this.building.filter(j => j.floor == e && j.unit == this.unit).map(j => ({ ...j, text: `${j.number}号`, value: j.houseId }));
  175. number.forEach(k => {
  176. const isunit = list.find(j => j.number == k.number);
  177. if (!isunit) list.push(k);
  178. });
  179. this.$set(this.house, 'number', list);
  180. },
  181. // 门牌选择
  182. houseChange(e) {
  183. if (!e || e == '') return;
  184. const dept = this.house.number.find(j => j.houseId == e);
  185. if (dept) this.formData.deptId = dept.deptId;
  186. // this.$set(this.form, 'deptId', dept.deptId);
  187. },
  188. // 提交
  189. async submit(ref) {
  190. this.$refs[ref].validate(async (err, formdata) => {
  191. if (!err) {
  192. if (this.role == 'guest') {
  193. // 添加用户
  194. const res = await request.addUser(this.formData);
  195. uni.setStorageSync('token', res.data.token);
  196. uni.setStorageSync('userinfo', res.data.user);
  197. uni.setStorageSync('role', res.data.role);
  198. uni.showToast({
  199. title: '添加成功',
  200. duration: 2000,
  201. });
  202. if (this.path && this.path !== null) {
  203. uni.redirectTo({
  204. url: this.path
  205. })
  206. }
  207. } else {
  208. // 修改用户
  209. const res = await request.updateUser(this.formData);
  210. uni.showToast({
  211. title: '修改成功',
  212. duration: 2000,
  213. });
  214. }
  215. uni.navigateBack()
  216. }
  217. })
  218. },
  219. },
  220. onLoad(option) {
  221. this.path == option.path;
  222. }
  223. }
  224. </script>
  225. <style>
  226. .uni-section {
  227. padding-bottom: 10px;
  228. margin-bottom: 10px;
  229. }
  230. .uni-select__selector {
  231. z-index: 999 !important;
  232. }
  233. .uni-forms, .btn {
  234. width: 90%;
  235. margin: 10px auto;
  236. }
  237. .phoneBox {
  238. display: flex;
  239. }
  240. </style>