index.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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="地址信息" 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="楼栋选择" v-if="house.range && house.range.length > 0">
  29. <uni-data-picker v-model="formData.buildingId" :localdata="house.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.length > 0">
  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. role: '',
  53. path: null,
  54. formData: {},
  55. sexs: [
  56. { text: '男', value: 1 },
  57. { text: '女', value: 0 },
  58. ],
  59. items: [],
  60. pickerMap: {
  61. text: 'label',
  62. value: 'interId'
  63. },
  64. // range: [],
  65. house: {},
  66. building: [],
  67. unit: '',
  68. // 校验规则
  69. rules: {
  70. name: {
  71. rules: [{
  72. required: true,
  73. errorMessage: '姓名不能为空'
  74. }]
  75. },
  76. sex: {
  77. rules: [{
  78. required: true,
  79. errorMessage: '性别不能为空'
  80. }]
  81. },
  82. phone: {
  83. rules: [{
  84. required: true,
  85. errorMessage: '电话不能为空'
  86. }]
  87. },
  88. },
  89. // 勾选成为居民
  90. isShowAddr: 0,
  91. isShowArr: [{ text: '是否富裕街道居民', value: 1 }],
  92. }
  93. },
  94. async mounted() {
  95. const res = await request.addrTreeSelect();
  96. this.items = res.data;
  97. this.role = uni.getStorageSync('role');
  98. if (this.role !== 'guest') this.queryInfo();
  99. },
  100. methods: {
  101. async queryInfo() {
  102. // const userinfo = await request.getUser();
  103. const userinfo = uni.getStorageSync('userinfo');
  104. this.formData = userinfo;
  105. this.setAddr(userinfo);
  106. this.isShowAddr = [1];
  107. },
  108. // 设置地址
  109. async setAddr(data) {
  110. // 小区存在
  111. if (data.estateId && data.estateId !== null) {
  112. await this.onchange({ detail: { value: [{ value: data.estateId }] } });
  113. }
  114. // 楼栋存在
  115. if(data.buildingId && data.buildingId !== null) {
  116. await this.selectChange({ detail: { value: [{ value: data.buildingId }] } });
  117. }
  118. // 单元存在
  119. if(data.unit && data.unit !== null) {
  120. await this.unitChange({ detail: { value: [{ value: data.unit }] } });
  121. }
  122. // 楼层存在
  123. if(data.floor && data.floor !== null) {
  124. await this.floorChange({ detail: { value: [{ value: data.floor }] } });
  125. }
  126. // 门牌存在
  127. if(data.houseId && data.houseId !== null) {
  128. await this.houseChange({ detail: { value: [{ value: data.houseId }] } });
  129. }
  130. },
  131. // 初始化选择器
  132. initPicker(e) {
  133. if (e.includes('range')) this.$set(this.house, 'range', []);
  134. if (e.includes('unit')) this.$set(this.house, 'unit', []);
  135. if (e.includes('floor')) this.$set(this.house, 'floor', []);
  136. if (e.includes('number')) this.$set(this.house, 'number', []);
  137. },
  138. // 获取手机号
  139. async decryptPhoneNumber(e) {
  140. const res = await requestLogin.getPhone({ code: e.detail.code, appid: appid });
  141. this.$set(this.formData, 'phone', res.phone_info.phoneNumber);
  142. },
  143. // 小区选择
  144. async onchange(e) {
  145. if (!e || e == '' || e.detail.value.length <= 0) {
  146. this.initPicker(['range', 'unit', 'floor', 'number']);
  147. return;
  148. }
  149. const val = e.detail.value[e.detail.value.length - 1].value;
  150. const res = await request.buildingList({ estateId: val });
  151. const range = res.rows.map(e => ({ ...e, text: `${e.number}栋`, value: e.buildingId }));
  152. this.$set(this.house, 'range', range);
  153. },
  154. // 楼栋选择
  155. async selectChange(e) {
  156. if (!e || e == '' || e.detail.value.length <= 0) {
  157. this.initPicker(['unit', 'floor', 'number']);
  158. return;
  159. };
  160. const res = await request.houseList({ buildingId: e.detail.value[0].value });
  161. this.building = res.rows;
  162. const list = [];
  163. const numList = [];
  164. const unit = res.rows.filter(j => j.unit !== null).map(j => ({ ...j, text: `${j.unit}单元`, value: j.unit }));
  165. const number = res.rows.filter(j => j.number !== null).map(j => ({ ...j, text: `${j.number}号`, value: j.houseId }));
  166. unit.forEach(k => {
  167. const isunit = list.find(j => j.unit == k.unit);
  168. if (!isunit) list.push(k);
  169. });
  170. this.$set(this.house, 'unit', list);
  171. number.forEach(k => {
  172. const isunit = numList.find(j => j.number == k.number);
  173. if (!isunit) numList.push(k);
  174. });
  175. this.$set(this.house, 'number', numList);
  176. },
  177. // 单元选择
  178. unitChange(e) {
  179. if (!e || e == '' || e.detail.value.length <= 0) {
  180. this.initPicker([ 'floor', 'number']);
  181. return;
  182. }
  183. this.unit = e.detail.value[0].value;
  184. const list = [];
  185. const floor = this.building.filter(j => j.unit == e.detail.value[0].value).map(j => ({ ...j, text: `${j.floor}层`, value: j.floor }));
  186. floor.forEach(k => {
  187. const isunit = list.find(j => j.floor == k.floor);
  188. if (!isunit) list.push(k);
  189. });
  190. this.$set(this.house, 'floor', list);
  191. },
  192. // 楼层选择
  193. floorChange(e) {
  194. if (!e || e == '' || e.detail.value.length <= 0) return;
  195. const list = [];
  196. 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 }));
  197. number.forEach(k => {
  198. const isunit = list.find(j => j.number == k.number);
  199. if (!isunit) list.push(k);
  200. });
  201. this.$set(this.house, 'number', list);
  202. },
  203. // 门牌选择
  204. houseChange(e) {
  205. if (!e || e == '' || e.detail.value.length <= 0) return;
  206. const dept = this.house.number.find(j => j.houseId == e.detail.value[0].value);
  207. this.formData.deptId = dept.deptId;
  208. },
  209. // 提交
  210. async submit(ref) {
  211. this.$refs[ref].validate(async (err, formdata) => {
  212. if (!err) {
  213. // 判断勾选后是否添加地址
  214. if (this.isShowAddr.length > 0) {
  215. if (!this.formData.estateId || !this.formData.buildingId || !this.formData.houseId == null) {
  216. uni.showToast({
  217. title: '请填写完整地址信息',
  218. icon: 'error',
  219. duration: 2000,
  220. });
  221. return;
  222. }
  223. }
  224. if (this.role == 'guest') {
  225. // 添加用户
  226. const res = await request.addUser(this.formData);
  227. uni.setStorageSync('token', res.data.token);
  228. uni.setStorageSync('userinfo', res.data.user);
  229. uni.setStorageSync('role', res.data.role);
  230. uni.showToast({
  231. title: '添加成功',
  232. duration: 2000
  233. });
  234. } else {
  235. console.log(132)
  236. // 修改用户
  237. const res = await request.updateUser(this.formData);
  238. uni.showToast({
  239. title: '修改成功',
  240. duration: 2000
  241. });
  242. }
  243. setTimeout(() => {
  244. uni.navigateBack();
  245. }, 1000)
  246. }
  247. })
  248. },
  249. },
  250. onLoad(option) {
  251. this.path == option.path;
  252. }
  253. }
  254. </script>
  255. <style>
  256. .uni-section {
  257. padding-bottom: 10px;
  258. margin-bottom: 10px;
  259. }
  260. .uni-select__selector {
  261. z-index: 999 !important;
  262. }
  263. .uni-forms, .btn {
  264. width: 90%;
  265. margin: 10px auto;
  266. }
  267. .phoneBox {
  268. display: flex;
  269. }
  270. .isShowAddr {
  271. width: 90%;
  272. margin: 0 auto;
  273. display: block;
  274. }
  275. </style>