| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 | 
							- import { api_sysAreaQueryAllProvince, api_userReceiveAddressUpdate, api_userReceiveAddressSave, api_userReceiveAddressDetail } from "../../api/login";
 
- Component({
 
-   options: {
 
-     styleIsolation: 'shared'
 
-   },
 
-   properties: {
 
-     popupShow: {
 
-       type: Boolean,
 
-       value: false
 
-     },
 
-     editId: {
 
-       type: String,
 
-       value: ""
 
-     }
 
-   },
 
-   data: {
 
-     name: "",
 
-     phoneNumber: "",
 
-     cacheArea: [] as { cityCode: string, shiftCityCode: string }[], // 临时存储的对应关系
 
-     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
 
-             })
 
-             // 回显市区
 
-             this.setData({
 
-               city: this.formateCityCode(true)
 
-             })
 
-           }
 
-         } 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[0]) {
 
-         wx.showToast({
 
-           title: '未选中值',
 
-           icon: 'none'
 
-         })
 
-         return
 
-       }
 
-       if (selectedOptions[0].code == this.data.province && selectedOptions[1].code == this.data.city && (selectedOptions[2]?.code || "") == this.data.region) {
 
-         this.setData({
 
-           showArea: false
 
-         })
 
-         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, index: number) => {
 
-           let code = city.code + ""
 
-           // 某些数据不标准 这里需要转换一下
 
-           if (code[4] !== "0" || code[5] !== "0") {
 
-             // 现在把区域的数据改为市的
 
-             const newCode = code.substring(0, 2) + (index < 10 ? `a${index}` : index < 20 ? `b${index - 10}` : index < 30 ? `c${index - 20}` : `d${index - 30}`) + "00";
 
-             this.data.cacheArea.push({
 
-               cityCode: code,
 
-               shiftCityCode: newCode
 
-             })
 
-             code = newCode
 
-           }
 
-           city_list[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
 
-       };
 
-     },
 
-     // 转换
 
-     formateCityCode(reverse?: boolean) {
 
-       if (!this.data.region && this.data.city) {
 
-         const cityCodeObj = this.data.cacheArea.find((item: any) => {
 
-           return item[reverse ? "cityCode" : "shiftCityCode"] == this.data.city
 
-         })
 
-         return cityCodeObj ? cityCodeObj[reverse ? "shiftCityCode" : "cityCode"] : ""
 
-       }
 
-       return this.data.city
 
-     },
 
-     /** 最终提交 */
 
-     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) {
 
-           wx.showToast({
 
-             title: '请选择地区',
 
-             icon: "none"
 
-           })
 
-           return
 
-         }
 
-         if (!params.detailAddress) {
 
-           wx.showToast({
 
-             title: '请输入详细地址',
 
-             icon: "none"
 
-           })
 
-           return
 
-         }
 
-         wx.showLoading({
 
-           mask: true,
 
-           title: "",
 
-         });
 
-         // 转换CityCode
 
-         const citycode = this.formateCityCode()
 
-         // 编辑
 
-         let id
 
-         if (params.editId) {
 
-           id = params.editId
 
-           await api_userReceiveAddressUpdate({
 
-             id: params.editId,
 
-             phoneNumber: params.phoneNumber,
 
-             name: params.name,
 
-             province: params.province,
 
-             city: citycode,
 
-             region: params.region,
 
-             detailAddress: params.detailAddress,
 
-             defaultStatus: false,
 
-             postCode: "",
 
-           })
 
-         } else {
 
-           const { data } = await api_userReceiveAddressSave({
 
-             phoneNumber: params.phoneNumber,
 
-             name: params.name,
 
-             province: params.province,
 
-             city: citycode,
 
-             region: params.region,
 
-             detailAddress: params.detailAddress,
 
-             defaultStatus: false,
 
-             postCode: "",
 
-           })
 
-           id = data.data
 
-         }
 
-         wx.hideLoading()
 
-         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 {
 
-         wx.hideLoading()
 
-         // 
 
-       }
 
-     },
 
-   }
 
- })
 
 
  |