index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 }] }, isUpdate: true });
  113. }
  114. // 楼栋存在
  115. if(data.buildingId && data.buildingId !== null) {
  116. await this.selectChange({ detail: { value: [{ value: data.buildingId }] }, isUpdate: true });
  117. }
  118. // 单元存在
  119. if(data.unit && data.unit !== null) {
  120. await this.unitChange({ detail: { value: [{ value: data.unit }] }, isUpdate: true });
  121. }
  122. // 楼层存在
  123. if(data.floor && data.floor !== null) {
  124. await this.floorChange({ detail: { value: [{ value: data.floor }] }, isUpdate: true });
  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. if (!res) {
  142. uni.showToast({
  143. title: '获取失败',
  144. icon: 'error',
  145. duration: 2000,
  146. });
  147. return
  148. }
  149. if (res.errcode !== 0) {
  150. uni.showToast({
  151. title: res.errMsg,
  152. icon: 'error',
  153. duration: 2000,
  154. });
  155. return
  156. }
  157. this.$set(this.formData, 'phone', res.phone_info.phoneNumber);
  158. },
  159. // 小区选择
  160. async onchange(e) {
  161. if (!e || e == '' || e.detail.value.length <= 0) {
  162. this.initPicker(['range', 'unit', 'floor', 'number']);
  163. return;
  164. }
  165. const val = e.detail.value[e.detail.value.length - 1].value;
  166. const res = await request.buildingList({ estateId: val });
  167. const range = res.rows.map(e => ({ ...e, text: `${e.number}栋`, value: e.buildingId }));
  168. this.$set(this.house, 'range', range);
  169. // 删除选项
  170. if (e.isUpdate) return;
  171. delete this.formData.buildingId
  172. delete this.formData.unit
  173. delete this.formData.floor
  174. delete this.formData.houseId
  175. },
  176. // 楼栋选择
  177. async selectChange(e) {
  178. if (!e || e == '' || e.detail.value.length <= 0) {
  179. this.initPicker(['unit', 'floor', 'number']);
  180. return;
  181. };
  182. const res = await request.houseList({ buildingId: e.detail.value[0].value });
  183. this.building = res.rows;
  184. const list = [];
  185. const numList = [];
  186. const unit = res.rows.filter(j => j.unit !== null).map(j => ({ ...j, text: `${j.unit}单元`, value: j.unit }));
  187. const number = res.rows.filter(j => j.number !== null).map(j => ({ ...j, text: `${j.number}号`, value: j.houseId }));
  188. unit.forEach(k => {
  189. const isunit = list.find(j => j.unit == k.unit);
  190. if (!isunit) list.push(k);
  191. });
  192. this.$set(this.house, 'unit', list);
  193. number.forEach(k => {
  194. const isunit = numList.find(j => j.number == k.number);
  195. if (!isunit) numList.push(k);
  196. });
  197. this.$set(this.house, 'number', numList);
  198. // 删除选项
  199. if (e.isUpdate) return;
  200. delete this.formData.unit
  201. delete this.formData.floor
  202. delete this.formData.houseId
  203. },
  204. // 单元选择
  205. unitChange(e) {
  206. if (!e || e == '' || e.detail.value.length <= 0) {
  207. this.initPicker([ 'floor', 'number']);
  208. return;
  209. }
  210. this.unit = e.detail.value[0].value;
  211. const list = [];
  212. const floor = this.building.filter(j => j.unit == e.detail.value[0].value).map(j => ({ ...j, text: `${j.floor}层`, value: j.floor }));
  213. floor.forEach(k => {
  214. const isunit = list.find(j => j.floor == k.floor);
  215. if (!isunit) list.push(k);
  216. });
  217. this.$set(this.house, 'floor', list);
  218. // 删除选项
  219. if (e.isUpdate) return;
  220. // delete this.formData.unit
  221. delete this.formData.floor
  222. delete this.formData.houseId
  223. },
  224. // 楼层选择
  225. floorChange(e) {
  226. if (!e || e == '' || e.detail.value.length <= 0) return;
  227. const list = [];
  228. 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 }));
  229. number.forEach(k => {
  230. const isunit = list.find(j => j.number == k.number);
  231. if (!isunit) list.push(k);
  232. });
  233. this.$set(this.house, 'number', list);
  234. // 删除选项
  235. if (e.isUpdate) return;
  236. // delete this.formData.unit
  237. // delete this.formData.floor
  238. delete this.formData.houseId
  239. },
  240. // 门牌选择
  241. houseChange(e) {
  242. if (!e || e == '' || e.detail.value.length <= 0) return;
  243. const dept = this.house.number.find(j => j.houseId == e.detail.value[0].value);
  244. this.formData.deptId = dept.deptId;
  245. },
  246. // 提交
  247. async submit(ref) {
  248. this.$refs[ref].validate(async (err, formdata) => {
  249. if (!err) {
  250. // 判断勾选后是否添加地址
  251. if (this.isShowAddr.length > 0) {
  252. if (!this.formData.estateId || !this.formData.buildingId || !this.formData.houseId == null) {
  253. uni.showToast({
  254. title: '请填写完整地址信息',
  255. icon: 'error',
  256. duration: 2000,
  257. });
  258. return;
  259. }
  260. }
  261. if (this.role == 'guest') {
  262. // 添加用户
  263. const res = await request.addUser(this.formData);
  264. uni.setStorageSync('token', res.data.token);
  265. uni.setStorageSync('userinfo', res.data.user);
  266. uni.setStorageSync('role', res.data.role);
  267. uni.showToast({
  268. title: '添加成功',
  269. duration: 2000
  270. });
  271. } else {
  272. // 修改用户
  273. const res = await request.updateUser(this.formData);
  274. uni.showToast({
  275. title: '修改成功',
  276. duration: 2000
  277. });
  278. }
  279. setTimeout(() => {
  280. uni.navigateBack();
  281. }, 1000)
  282. }
  283. })
  284. },
  285. },
  286. onLoad(option) {
  287. this.path == option.path;
  288. }
  289. }
  290. </script>
  291. <style>
  292. .uni-section {
  293. padding-bottom: 10px;
  294. margin-bottom: 10px;
  295. }
  296. .uni-select__selector {
  297. z-index: 999 !important;
  298. }
  299. .uni-forms, .btn {
  300. width: 90%;
  301. margin: 10px auto;
  302. }
  303. .phoneBox {
  304. display: flex;
  305. }
  306. .isShowAddr {
  307. width: 90%;
  308. margin: 0 auto;
  309. display: block;
  310. }
  311. </style>