123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <template>
- <view class="container">
- <uni-section title="基础信息" type="line">
- <uni-forms ref="infoForm" :rules="rules" :modelValue="formData" :label-width="90">
- <uni-forms-item required label="姓名" name="name">
- <uni-easyinput type="text" v-model="formData.name" placeholder="请输入姓名" />
- </uni-forms-item>
- <uni-forms-item required label="性别" name="sex">
- <uni-data-checkbox v-model="formData.sex" :localdata="sexs" />
- </uni-forms-item>
- <uni-forms-item required label="手机号" name="phone">
- <view class="phoneBox">
- <uni-easyinput disabled type="text" v-model="formData.phone" placeholder="手机号" />
- <button type="primary" :plain="true" style="border: none;" size="mini" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">获取手机号</button>
- </view>
- </uni-forms-item>
- <uni-forms-item name="birthday" label="出生日期">
- <uni-datetime-picker type="date" v-model="formData.birthday" />
- </uni-forms-item>
- </uni-forms>
- </uni-section>
- <uni-section title="地址信息" type="line">
- <uni-forms ref="addrForm" :modelValue="formData" :label-width="90">
- <uni-forms-item label="小区选择">
- <uni-data-picker v-model="formData.estateId" :map="pickerMap" :localdata="items" placeholder="请选择地址" popup-title="请选择地址" @change="onchange" @nodeclick="onnodeclick"></uni-data-picker>
- </uni-forms-item>
- <uni-forms-item label="楼栋选择">
- <uni-data-select :localdata="range" @change="selectChange" v-model="formData.buildingId"></uni-data-select>
- </uni-forms-item>
- <uni-forms-item label="单元选择" v-if="house.unit && house.unit !== null">
- <uni-data-select :localdata="house.unit" @change="unitChange" v-model="formData.unit"></uni-data-select>
- </uni-forms-item>
- <uni-forms-item label="楼层选择" v-if="house.floor && house.floor !== null">
- <uni-data-select :localdata="house.floor" @change="floorChange" v-model="formData.floor"></uni-data-select>
- </uni-forms-item>
- <uni-forms-item label="门牌选择" v-if="house.number && house.number !== null">
- <uni-data-select :localdata="house.number" v-model="formData.houseId" @change="houseChange"></uni-data-select>
- </uni-forms-item>
- </uni-forms>
- </uni-section>
- <button class="btn" type="primary" @click="submit('infoForm')">提交</button>
- </view>
- </template>
- <script>
- import request from '../../api/user.js';
- import requestLogin from '../../api/login.js';
- const appid = uni.getAccountInfoSync().miniProgram.appId;
- export default {
- data() {
- return {
- role: '',
- path: null,
- formData: {},
- sexs: [
- { text: '男', value: 1 },
- { text: '女', value: 0 },
- ],
- items: [],
- pickerMap: {
- text: 'label',
- value: 'interId'
- },
- range: [],
- house: {},
- building: [],
- unit: '',
- // 校验规则
- rules: {
- name: {
- rules: [{
- required: true,
- errorMessage: '姓名不能为空'
- }]
- },
- sex: {
- rules: [{
- required: true,
- errorMessage: '性别不能为空'
- }]
- },
- phone: {
- rules: [{
- required: true,
- errorMessage: '电话不能为空'
- }]
- },
- },
- }
- },
- async mounted() {
- const res = await request.addrTreeSelect();
- this.items = res.data;
- this.role = uni.getStorageSync('role');
- if (this.role !== 'guest') this.queryInfo();
- },
- methods: {
- async queryInfo() {
- const userinfo = await request.getUser();
- this.formData = userinfo.data;
- const { estateId, buildingId, unit, floor, houseId } = userinfo.data;
- // 小区存在
- if (estateId) {
- const res = await request.buildingList({ estateId });
- this.range = res.rows.map(e => ({ ...e, text: `${e.number}栋`, value: e.buildingId }));
- }
- // 楼栋存在
- if(buildingId) {
- await this.selectChange(buildingId);
- }
- // 单元存在
- if(unit) {
- await this.unitChange(unit);
- }
- // 楼层存在
- if(floor) {
- await this.floorChange(floor);
- }
- // 门牌存在
- if(houseId) {
- await this.houseChange(houseId);
- }
- },
- // 获取手机号
- async decryptPhoneNumber(e) {
- const res = await requestLogin.getPhone({ code: e.detail.code, appid: appid });
- this.$set(this.formData, 'phone', res.phone_info.phoneNumber);
- },
- // 小区选择
- async onchange(e) {
- const val = e.detail.value[e.detail.value.length - 1].value;
- const res = await request.buildingList({ estateId: val });
- this.range = res.rows.map(e => ({ ...e, text: `${e.number}栋`, value: e.buildingId }));
- },
- onnodeclick(node) {},
- // 楼栋选择
- async selectChange(e) {
- if (!e || e == '') return;
- const res = await request.houseList({ buildingId: e });
- this.building = res.rows;
- const list = [];
- const unit = res.rows.filter(j => j.unit !== null).map(j => ({ ...j, text: `${j.unit}单元`, value: j.unit }));
- const number = res.rows.filter(j => j.number !== null).map(j => ({ ...j, text: `${j.number}号`, value: j.number }));
- if (unit && unit.length > 0) {
- unit.forEach(k => {
- const isunit = list.find(j => j.unit == k.unit);
- if (!isunit) list.push(k);
- });
- this.$set(this.house, 'unit', list);
- return;
- }
- if (number && number.length > 0) {
- number.forEach(k => {
- const isunit = list.find(j => j.number == k.number);
- if (!isunit) list.push(k);
- });
- this.$set(this.house, 'number', list);
- return;
- }
-
- },
- // 单元选择
- unitChange(e) {
- this.unit = e;
- const list = [];
- const floor = this.building.filter(j => j.unit == e).map(j => ({ ...j, text: `${j.floor}层`, value: j.floor }));
- floor.forEach(k => {
- const isunit = list.find(j => j.floor == k.floor);
- if (!isunit) list.push(k);
- });
- this.$set(this.house, 'floor', list);
- },
- // 楼层选择
- floorChange(e) {
- const list = [];
- const number = this.building.filter(j => j.floor == e && j.unit == this.unit).map(j => ({ ...j, text: `${j.number}号`, value: j.houseId }));
- number.forEach(k => {
- const isunit = list.find(j => j.number == k.number);
- if (!isunit) list.push(k);
- });
- this.$set(this.house, 'number', list);
- },
- // 门牌选择
- houseChange(e) {
- if (!e || e == '') return;
- const dept = this.house.number.find(j => j.houseId == e);
- if (dept) this.formData.deptId = dept.deptId;
- // this.$set(this.form, 'deptId', dept.deptId);
- },
- // 提交
- async submit(ref) {
- this.$refs[ref].validate(async (err, formdata) => {
- if (!err) {
- if (this.role == 'guest') {
- // 添加用户
- const res = await request.addUser(this.formData);
- uni.setStorageSync('token', res.data.token);
- uni.setStorageSync('userinfo', res.data.user);
- uni.setStorageSync('role', res.data.role);
- uni.showToast({
- title: '添加成功',
- duration: 2000,
- });
- if (this.path && this.path !== null) {
- uni.redirectTo({
- url: this.path
- })
- }
- } else {
- // 修改用户
- const res = await request.updateUser(this.formData);
- uni.showToast({
- title: '修改成功',
- duration: 2000,
- });
- }
- uni.navigateBack()
- }
- })
- },
- },
- onLoad(option) {
- this.path == option.path;
- }
- }
- </script>
- <style>
- .uni-section {
- padding-bottom: 10px;
- margin-bottom: 10px;
- }
- .uni-select__selector {
- z-index: 999 !important;
- }
- .uni-forms, .btn {
- width: 90%;
- margin: 10px auto;
- }
- .phoneBox {
- display: flex;
- }
- </style>
|