tips-nearby-store-comp.js 5.77 KB
import {
  getBindtapData,
} from '../../utils/util';
let app = getApp();

Component({
  properties: {
    // 这里定义了innerText属性,属性值可以在组件使用时指定
    innerText: {
      type: String,
      value: 'default value',
    },
    wishInfo: {
      type: Object,
      value: {},
    },
    location: {
      type: Object,
      value: {},
    },
    //是否选择模式
    selectedMode: {
      type: Boolean,
      value: false
    }
  },
  data: {
    // 这里是一些组件内部数据
    someData: {},
    searchMode: 1, // 查询模式 1.根据位置自动查询 2.省市区查询
    provinceId: "",
    cityId: "",
    districtId: "",
    provinceList: [],
    cityList: [],
    districtList: [],
    provinceIndex: 0,
    cityIndex: 0,
    districtIndex: 0,
    addressList: [],
    total: 0,
    curAddress: null,
    localCity: ""
  },
  methods: {
    /**
     * 请求门店地址
     */
    queryShop() {
      this.setData({
        curAddress: ""
      })
      return new Promise((resolve, reject) => {
        let {
          location,
          wishInfo,
          provinceId,
          cityId,
          districtId,
        } = this.data;
        app.post({
          url: app.api.storeQuery,
          data: {
            couponId: wishInfo.couponId,
            latitude: location.latitude,
            longitude: location.longitude,
            provinceId: provinceId,
            cityId: cityId,
            districtId: districtId,
            page: 1,
            size: 100,
          }
        }).then((result) => {
          this.setData({
            addressList: result.list,
            total: result.total,
            localCity: result.city
          })
          resolve();
        })
      });
    },

    /**
     * 选择地址 item
     */
    onSelectHandler(evt) {
      let item = getBindtapData(evt);
      let index = getBindtapData(evt, "index");
      let addressList = this.data.addressList;
      addressList.forEach((element, idx) => {
        element.selected = index == idx;
      });

      this.setData({
        addressList: addressList,
        curAddress: item
      })
    },

    /**
     * 提交门店
     * 预约
     */
    onSubmitHandler(evt) {
      let curAddress = this.data.curAddress;
      if (!curAddress) {
        wx.showToast({
          title: "请选择门店",
          icon: 'none'
        });
        return;
      }
      let wishInfo = this.properties.wishInfo;
      app.post({
        url: app.api.wishbillStoreAppoint,
        data: {
          instanceCode: wishInfo.instanceCode,
          storeCode: curAddress.storeCode,
          storeName: curAddress.storeName,
          storeAddress: curAddress.storeAddress,
          latitude: curAddress.latitude,
          longitude: curAddress.longitude
        }
      }).then((result) => {
        this.triggerEvent('evtcomp', {
          name: "_evt_submit_store_complete"
        })
      });
    },

    /**
     * 显示位置
     * @param {*} e
     */
    onShowLocHandler(evt) {
      let item = getBindtapData(evt);

      wx.openLocation({
        latitude: item.latitude, // 纬度,范围为-90~90,负数表示南纬
        longitude: item.longitude, // 经度,范围为-180~180,负数表示西经
        scale: 18, // 缩放比例
        name: item.storeName, // 位置名
        address: item.storeAddress, // 地址的详细说明
        success: function (res) {
          // success
        }
      })
    },

    // 转换成手动模式
    onSearchHandModeHandler() {
      this.setData({
        searchMode: 2
      });
      this.queryShop();
    },

    // 转换为自动模式
    onSearchAutoModeHandler() {
      this.setData({
        searchMode: 1,
        provinceId: "",
        cityId: "",
        districtId: "",
      })
      this.queryShop();
    },

    getProvince() {
      return new Promise((resolve, reject) => {
        app.post({
          url: app.api.provinceQuery
        }).then((result) => {
          this.setData({
            provinceList: result
          })
          console.log("getProvince result:", result);
        })
      });
    },

    getCity() {
      return new Promise((resolve, reject) => {
        app.post({
          url: app.api.cityQuery,
          data: {
            parentId: this.data.provinceId
          }
        }).then((result) => {
          this.setData({
            cityList: result
          })
        })
      });
    },

    getDistrict() {
      return new Promise((resolve, reject) => {
        app.post({
          url: app.api.districtQuery,
          data: {
            parentId: this.data.cityId
          }
        }).then((result) => {
          this.setData({
            districtList: result
          })
        })
      });
    },

    bindPickerChangeProvince(e) {
      let index = e.detail.value;
      this.setData({
        provinceIndex: index,
        provinceId: this.data.provinceList[index].id,
        cityList: [],
        cityIndex: -1,
        districtList: [],
        districtIndex: -1,
      })
      this.getCity();
    },

    bindPickerChangeCity(e) {
      let index = e.detail.value;
      this.setData({
        cityIndex: index,
        cityId: this.data.cityList[index].id,
        districtList: [],
        districtIndex: -1,
      })
      this.getDistrict();
    },

    bindPickerChangeDistrict(e) {
      let index = e.detail.value;
      this.setData({
        districtIndex: index,
        districtId: this.data.districtList[index].id,
      })
      this.queryShop();
    },



    // 这里是一个自定义方法
    customMethod() {
      this.triggerEvent('evtcomp', {
        name: "_evt_custom"
      })
    },
    // 隐藏蒙层
    hideMask() {
      this.triggerEvent('evtcomp', {
        name: "_evt_hide_mask"
      });
    }
  },
  //
  ready() {
    this.getProvince();
  }
})