checkstore.js 2.76 KB
import api from './checkstoreService';
import event from '../../utils/event.js'

const storeApi = new api();
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  properties: {
    // 弹窗标题
    storeId: {            // 属性名
      type: Number,     // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)
      value: 0     // 属性初始值(可选),如果未指定则会根据类型选择一个
    },
    productId: {
      type: Number,
      value: 0
    }
  },
  data: {
    // 弹窗显示控制
    content: "Sorry,您的位置不在门店范围内,不能选择门店自提方式购买该商品",
    cancelText: "线上购买",
    confirmText: "知道了",
    type: 0
  },
  /**
   * 组件的方法列表
   * 更新属性和数据的方法与更新页面数据的方法类似
   */
  methods: {
    init() {
      return new Promise((resolve) => {
        let param = {};
        this.dialog = this.selectComponent("#dialog");
        wx.getLocation({
          success: (res) => {
            param.user_latitude = res.latitude;
            param.user_longitude = res.longitude;
            param.store_id = this.properties.storeId;
            storeApi.checkStore(param, () => {
              wx.hideLoading();
            }).then(r => {
              if (!r.isInRange) {
                this.setData({
                  // 弹窗显示控制
                  content: "Sorry,您的位置不在门店范围内,不能选择门店自提方式购买该商品",
                  cancelText: "线上购买",
                  confirmText: "知道了",
                  type: 0
                });
                this.dialog.showDialog();
                resolve({result: false})
              } else {
                resolve({
                  result: true,
                  data: param
                })
              }
            });
          },
          fail: () => {
            this.setData({
              type: 1,
              content: "请确保手机打开定位,并允许微信获取您的位置信息",
              cancelText: "取消",
              confirmText: "去开启",
            })
            this.dialog.showDialog();
            resolve({result: false})
          }
        });
      })

    },
    cancelEvent() {
      if (this.data.type) {
        this.dialog.hideDialog();
      } else {
        wx.navigateTo({url: '/pages/productDetail/index?id=' + this.properties.productId})
      }
    },
    confirmEvent() {
      if (this.data.type) {
        wx.openSetting({
          complete: () => {
            this.dialog.hideDialog();
          }
        });
      } else {
        this.dialog.hideDialog();
      }
    }
  }
})