index.vue 8.3 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"></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. 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. const userinfo = uni.getStorageSync('userinfo');
  100. this.formData = userinfo;
  101. this.setAddr(userinfo);
  102. },
  103. // 设置地址
  104. async setAddr(data) {
  105. // 小区存在
  106. if (data.estateId && data.estateId !== null) {
  107. const res = await request.buildingList({ estateId: data.estateId });
  108. this.range = res.rows.map(e => ({ ...e, text: `${e.number}栋`, value: e.buildingId }));
  109. }
  110. // 楼栋存在
  111. if(data.buildingId && data.buildingId !== null) {
  112. await this.selectChange({ detail: { value: [{ value: data.buildingId }] } });
  113. }
  114. // 单元存在
  115. if(data.unit && data.unit !== null) {
  116. await this.unitChange({ detail: { value: [{ value: data.unit }] } });
  117. }
  118. // 楼层存在
  119. if(data.floor && data.floor !== null) {
  120. await this.floorChange({ detail: { value: [{ value: data.floor }] } });
  121. }
  122. // 门牌存在
  123. if(data.houseId && data.houseId !== null) {
  124. await this.houseChange({ detail: { value: [{ value: data.houseId }] } });
  125. }
  126. },
  127. // 获取手机号
  128. async decryptPhoneNumber(e) {
  129. const res = await requestLogin.getPhone({ code: e.detail.code, appid: appid });
  130. this.$set(this.formData, 'phone', res.phone_info.phoneNumber);
  131. },
  132. // 小区选择
  133. async onchange(e) {
  134. if (!e || e == '' || e.detail.value.length <= 0) return;
  135. const val = e.detail.value[e.detail.value.length - 1].value;
  136. const res = await request.buildingList({ estateId: val });
  137. this.range = res.rows.map(e => ({ ...e, text: `${e.number}栋`, value: e.buildingId }));
  138. },
  139. // 楼栋选择
  140. async selectChange(e) {
  141. if (!e || e == '' || e.detail.value.length <= 0) return;
  142. const res = await request.houseList({ buildingId: e.detail.value[0].value });
  143. this.building = res.rows;
  144. const list = [];
  145. const numList = [];
  146. const unit = res.rows.filter(j => j.unit !== null).map(j => ({ ...j, text: `${j.unit}单元`, value: j.unit }));
  147. const number = res.rows.filter(j => j.number !== null).map(j => ({ ...j, text: `${j.number}号`, value: j.houseId }));
  148. unit.forEach(k => {
  149. const isunit = list.find(j => j.unit == k.unit);
  150. if (!isunit) list.push(k);
  151. });
  152. this.$set(this.house, 'unit', list);
  153. number.forEach(k => {
  154. const isunit = numList.find(j => j.number == k.number);
  155. if (!isunit) numList.push(k);
  156. });
  157. this.$set(this.house, 'number', numList);
  158. },
  159. // 单元选择
  160. unitChange(e) {
  161. if (!e || e == '' || e.detail.value.length <= 0) return;
  162. this.unit = e.detail.value[0].value;
  163. const list = [];
  164. const floor = this.building.filter(j => j.unit == e.detail.value[0].value).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. if (!e || e == '' || e.detail.value.length <= 0) return;
  174. const list = [];
  175. 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 }));
  176. number.forEach(k => {
  177. const isunit = list.find(j => j.number == k.number);
  178. if (!isunit) list.push(k);
  179. });
  180. this.$set(this.house, 'number', list);
  181. },
  182. // 门牌选择
  183. houseChange(e) {
  184. if (!e || e == '' || e.detail.value.length <= 0) return;
  185. const dept = this.house.number.find(j => j.houseId == e.detail.value[0].value);
  186. this.formData.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>