123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- import { api_sysAreaQueryAllProvince, api_userReceiveAddressUpdate, api_userReceiveAddressSave, api_userReceiveAddressDetail } from "../../api/login";
- Component({
- properties: {
- popupShow: {
- type: Boolean,
- value: false
- },
- editId: {
- type: String,
- value: ""
- }
- },
- data: {
- name: "",
- phoneNumber: "",
- showArea: false,
- areaList: [] as any, // 省市区
- province: "",
- city: "",
- region: "",
- provinceName: "",
- cityName: "",
- regionName: "",
- detailAddress: ""
- },
- pageLifetimes: {
- show() {
- this.getAreas()
- },
- },
- observers: {
- async editId(newVal: any) {
- if (newVal) {
- try {
- const { data } = await api_userReceiveAddressDetail(newVal)
- if (data.code === 200) {
- const params = data.data
- this.setData({
- phoneNumber: params.phoneNumber,
- name: params.name,
- province: params.province,
- city: params.city,
- region: params.region,
- provinceName: params.provinceName,
- cityName: params.cityName,
- regionName: params.regionName,
- detailAddress: params.detailAddress
- })
- }
- } catch (e: any) {
- console.log(e, 888)
- }
- }
- }
- },
- methods: {
- onDialogClose() {
- this.setData({
- popupShow: false,
- name: "",
- phoneNumber: "",
- province: "",
- city: "",
- region: "",
- provinceName: "",
- cityName: "",
- regionName: "",
- detailAddress: ""
- })
- },
- /** 显示选择地区 */
- onShowAreaList() {
- this.setData({
- showArea: true
- })
- },
- /** 关闭选择地区 */
- onCloseAreaList() {
- this.setData({
- showArea: false
- })
- },
- /** 确定选择地区 */
- submitArea(e: any) {
- const selectedOptions: any = e.detail.values
- if (!selectedOptions || !selectedOptions[selectedOptions.length - 1]) {
- wx.showToast({
- title: '未选中值',
- icon: 'none'
- })
- return
- }
- this.setData({
- province: selectedOptions[0].code,
- city: selectedOptions[1].code,
- region: selectedOptions[2].code,
- provinceName: selectedOptions[0].name,
- cityName: selectedOptions[1].name,
- regionName: selectedOptions[2].name,
- showArea: false
- })
- },
- /** 获取省市区 */
- async getAreas() {
- try {
- const { data } = await api_sysAreaQueryAllProvince({})
- this.setData({
- areaList: this.formateArea(data.data)
- })
- } catch {
- //
- }
- },
- formateArea(area: any[]) {
- const province_list: { [_: string]: string } = {};
- const city_list: { [_: string]: string } = {};
- const county_list: { [_: string]: string } = {};
- area.forEach((item: any) => {
- province_list[item.code] = item.name;
- });
- area.forEach((item: any) => {
- item.areas && item.areas.forEach((city: any) => {
- city_list[city.code] = city.name;
- });
- });
- area.forEach((item: any) => {
- item.areas && item.areas.forEach((city: any) => {
- city.areas && city.areas.forEach((county: any) => {
- county_list[county.code] = county.name;
- });
- });
- });
- return {
- province_list,
- city_list,
- county_list
- };
- },
- /** 最终提交 */
- async onSubmit() {
- try {
- const params = this.data
- if (!params.name) {
- wx.showToast({
- title: '请输入收货人',
- icon: "none"
- })
- return
- }
- if (!params.phoneNumber || !/^1[3456789]\d{9}$/.test(params.phoneNumber)) {
- wx.showToast({
- title: '请输入正确的手机号',
- icon: "none"
- })
- return
- }
- if (!params.province || !params.city || !params.region) {
- wx.showToast({
- title: '请选择地区',
- icon: "none"
- })
- return
- }
- if (!params.detailAddress) {
- wx.showToast({
- title: '请输入详细地址',
- icon: "none"
- })
- return
- }
- // 编辑
- let id
- if (params.editId) {
- id = params.editId
- await api_userReceiveAddressUpdate({
- id: params.editId,
- phoneNumber: params.phoneNumber,
- name: params.name,
- province: params.province,
- city: params.city,
- region: params.region,
- detailAddress: params.detailAddress,
- defaultStatus: false,
- postCode: "",
- })
- wx.showToast({
- title: '保存成功',
- icon: 'none'
- })
- } else {
- const { data } = await api_userReceiveAddressSave({
- phoneNumber: params.phoneNumber,
- name: params.name,
- province: params.province,
- city: params.city,
- region: params.region,
- detailAddress: params.detailAddress,
- defaultStatus: false,
- postCode: "",
- })
- id = data.data
- wx.showToast({
- title: '保存成功',
- icon: 'none'
- })
- }
- this.triggerEvent('addAddress', { addressInfo: { id, name: params.name, phoneNumber: params.phoneNumber, addressDes: params.provinceName + params.cityName + params.regionName + params.detailAddress } }, {})
- this.onDialogClose()
- } catch {
- //
- }
- },
- }
- })
|