123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- import { api_sysAreaQueryAllProvince, api_userReceiveAddressDetail, api_userReceiveAddressSave, api_userReceiveAddressUpdate } from "../../api/new";
- // pages/address/address-detail.ts
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- addressShow: false,
- addressAfterLeave: false,
- showDialog: false,
- cacheArea: [] as { cityCode: string, shiftCityCode: string }[], // 临时存储的对应关系
- showArea: false,
- showAreaAfterLeave: false,
- areaList: [] as any,
- currentValues: [] as any,
- // 添加地址表单信息
- id: "",
- name: '',
- phoneNumber: '',
- detailAddress: '',
- cityCode: null,
- cityName: "",
- provinceCode: null,
- provinceName: "",
- regionCode: '',
- regionName: "",
- btnLoading: false
- },
- /**
- * 生命周期函数--监听页面加载
- */
- async onLoad(options: any) {
- await this.getAreas()
- console.log(options, "options")
- if (options.receiveAddress) {
- this.setData({
- id: options.receiveAddress
- }, () => {
- this.getDetail()
- })
- }
- },
- /** 获取详情 */
- async getDetail() {
- try {
- if (!this.data.id) return
- const { data } = await api_userReceiveAddressDetail({ id: this.data.id })
- const result = data.data
- this.setData({
- name: result.name,
- phoneNumber: result.phoneNumber,
- detailAddress: result.detailAddress,
- cityCode: result.city,
- cityName: result.cityName,
- provinceCode: result.province,
- provinceName: result.provinceName,
- regionCode: result.region,
- regionName: result.regionName,
- }, () => {
- const cityCode: any = this.formateCityCode(true)
- this.setData({
- cityCode
- })
- })
- } catch {
- //
- }
- },
- /** 获取省市区 */
- async getAreas() {
- try {
- const { data } = await api_sysAreaQueryAllProvince({})
- const areaList: any = this.formateArea(data.data)
- const currentValues = []
- if (areaList?.province_list) {
- // 获取第一个键值对
- const firstKey = Object.keys(areaList?.province_list)[0];
- // 通过键获取值
- const firstValue = areaList?.province_list[firstKey];
- currentValues.push({
- code: firstKey,
- name: firstValue
- })
- }
- if (areaList?.city_list) {
- // 获取第一个键值对
- const firstKey = Object.keys(areaList?.city_list)[0];
- // 通过键获取值
- const firstValue = areaList?.city_list[firstKey];
- currentValues.push({
- code: firstKey,
- name: firstValue
- })
- }
- if (areaList?.county_list) {
- // 获取第一个键值对
- const firstKey = Object.keys(areaList?.county_list)[0];
- // 通过键获取值
- const firstValue = areaList?.county_list[firstKey];
- currentValues.push({
- code: firstKey,
- name: firstValue
- })
- }
- console.log(areaList,
- currentValues)
- this.setData({
- areaList,
- currentValues
- })
- } 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.regionCode && this.data.cityCode) {
- const cityCodeObj = this.data.cacheArea.find((item: any) => {
- return item[reverse ? "cityCode" : "shiftCityCode"] == this.data.cityCode
- })
- return cityCodeObj ? cityCodeObj[reverse ? "shiftCityCode" : "cityCode"] : ""
- }
- return this.data.cityCode
- },
- /** 显示选择地区 */
- onShowAreaList() {
- this.setData({
- showArea: true
- })
- },
- /** 关闭选择地区 */
- onCloseAreaList() {
- this.setData({
- showArea: false
- })
- },
- onAreaBeforeEnter() {
- this.setData({
- showAreaAfterLeave: false
- })
- },
- onAreaAfterLeave() {
- this.setData({
- showAreaAfterLeave: true
- })
- },
- /** 确定选择地区 */
- submitArea(e: any) {
- const selectedOptions: any = e.detail.values
- this.setData({
- provinceCode: selectedOptions[0].code,
- cityCode: selectedOptions[1].code,
- regionCode: selectedOptions[2]?.code || null,
- provinceName: selectedOptions[0].name || '',
- cityName: selectedOptions[1].name || '',
- regionName: selectedOptions[2]?.name || '',
- showArea: false,
- })
- },
- /** 创建/修改收货地址 */
- async onOperationAddress() {
- const addressForm = this.data
- try {
- if (!addressForm.name) {
- wx.showToast({
- title: '请输入收货人姓名',
- icon: "none"
- })
- return
- }
- if (!addressForm.phoneNumber || !/^1[3456789]\d{9}$/.test(addressForm.phoneNumber)) {
- wx.showToast({
- title: '请输入正确的手机号码',
- icon: "none"
- })
- return
- }
- if (!addressForm.provinceCode || !addressForm.cityCode) {
- wx.showToast({
- title: '请选择地区',
- icon: "none"
- })
- return
- }
- if (!addressForm.detailAddress) {
- wx.showToast({
- title: '请输入详细地址',
- icon: "none"
- })
- return
- }
- const citycode = this.formateCityCode()
- const params = {
- name: addressForm.name,
- phoneNumber: addressForm.phoneNumber,
- province: addressForm.provinceCode,
- city: citycode,
- region: addressForm.regionCode || '',
- detailAddress: addressForm.detailAddress
- }
- this.setData({
- btnLoading: true
- })
- const pages = getCurrentPages();
- const prevPage = pages[pages.length - 2]; // 获取上一个页面实例
- console.log(addressForm.id, "addressForm.id")
- if (addressForm.id) {
- await api_userReceiveAddressUpdate({
- id: addressForm.id,
- ...params
- })
- wx.showToast({
- title: '保存成功',
- icon: 'none'
- })
- prevPage?.setData({
- backParams: {
- receiveAddress: this.data.id,
- receiveAddressInfo: {
- addressDetail: this.data.id ? (addressForm.provinceName || '') + (addressForm.cityName || '') + (addressForm.regionName || '') + addressForm.detailAddress : '',
- name: addressForm?.name,
- phoneNumber: addressForm?.phoneNumber
- }
- }
- });
- } else {
- const { data } = await api_userReceiveAddressSave({
- ...params
- })
- wx.showToast({
- title: '保存成功',
- icon: 'none'
- })
- prevPage?.setData({
- backParams: {
- receiveAddress: data.data,
- receiveAddressInfo: {
- addressDetail: data.data ? (addressForm.provinceName || '') + (addressForm.cityName || '') + (addressForm.regionName || '') + addressForm.detailAddress : '',
- name: addressForm?.name,
- phoneNumber: addressForm?.phoneNumber
- }
- }
- });
- }
- setTimeout(() => {
- this.setData({
- btnLoading: false
- })
- wx.navigateBack()
- }, 1000);
- } catch (e) {
- //
- console.log(e, '1212')
- this.setData({
- btnLoading: false
- })
- }
- }
- })
|