appment_main.js 10.9 KB
var e = getApp(),
  a = e.globalData.setting,
  os = a,
  t = e.request,
  d = e.globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inurl: a.url, //接口网址
    iurl: a.imghost, //服务器网址
    store: 0, //是否显示服务门店列表
    beautician: 0, //是否显示美容师列表
    beautician_name: "", //选中的美容师名称
    beauticianID: "", //美容师id
    bea_index: "", //美容师列表下标
    placeholder: "填写备注", //备注为空的placeholder
    store_list: [], //门店列表
    beautician_list: [], //美容师列表
    store_name: "", //选择的服务门店
    fir_pick_index: 0, //选择的门店下标
    curpage: 1, //当前分页数
    pageSize: 8, //页大小
    total: 0,
    ismore: 0, //是否加载完毕
    itemId: "", //服务id
    project_id: "", //项目id
    isScroll: true, //scroll-y是否可以滑动
    key_word: "", //是否按门店文字查询
    is_service_read: 0, //是否调用过门店接口
    is_search: 0, //是否通过key_word调用接口
    is_success: 0, //是否提交成功
    remarks: "", //备注
    storageId: "", //线下门店id
    url: "/pages/user/my_service/appment_main", //本页面地址路径用于选择时间页面跳转回来
    buyType: "", //项目类型
    time: "", //选择预约时间
    tment_count: "", //可预约人数
    lat: "", //纬度坐标
    lon: "", //经度坐标
    is_gps: 1, //是否开启gps
  },
  onclickstore: function() {
    var th = this;
    var store = th.data.store;
    if (store) {
      th.setData({
        store: 0,
        placeholder: "填写备注"
      })
      wx.hideLoading();
    } else {
      th.setData({
        beautician: 0,
        store: 1,
        placeholder: ""
      })
      if (th.data.store_list.length < 1) {
        wx.showLoading({
          title: '加载中',
        })
        th.query_store();
      }
    }
  },
  //输入的备注
  input_remarks: function(e) {
    var remarks = e.detail.value;
    this.setData({
      remarks: remarks
    })
  },
  //提交预约
  sub_success: function() {
    var th = this;
    var store = th.data.store_name; //门店名称
    var bea_name = th.data.beautician_name; //美容师名称
    var time = th.data.time; //预约时间
    //提交预约前的判断
    if (store == "") {
      getApp().my_warnning("请选择服务门店", 0, th);
      return false;
    } else if (bea_name == "") {
      getApp().my_warnning("请选择美容师", 0, th);
      return false;
    } else if (time == "") {
      getApp().my_warnning("请选择预约时间", 0, th);
      return false;
    } else {
      var url = th.data.inurl + "/api/weshop/marketing/reservation/reservation/insert"; //接口路径
      var serviceId = th.data.itemId; //服务id
      var beauticianID = th.data.beauticianID; //美容师id
      var buyType = th.data.buyType; //服务项目类型
      var storeId = a.stoid; //商家id
      var storageId = th.data.storageId; //门店id
      var userId = d.user_id; //用户id
      var remarks = th.data.remarks; //用户备注
      var project_id = th.data.project_id; //项目id

      var json = {
        "arrangeTime": time,
        "beauticianId": beauticianID,
        "buyType": buyType,
        "effectiveDay": "",
        "number": "",
        "projectId": project_id,
        "remark": remarks,
        "serviceId": serviceId,
        "states": 0,
        "storageId": storageId,
        "storeId": storeId,
        "userId": userId
      }
      var data = JSON.stringify(json);
      wx.request({
        url: url,
        data: json,
        method: 'post',
        header: {
          'content-type': 'application/json'
        }, // 设置请求的 header
        success: function(res) {
          if (res.data.code == 0) {
            th.setData({
              is_success: 1
            })
            setTimeout(function() {
              wx.redirectTo({
                url: "/pages/user/my_service/tment_order_list"
              });
            }, 800);
          } else {
            getApp().my_warnning(res.data.msg, 0, th);
          }
        }
      })
    }
  },
  goto: function(e) {
    var th = this;
    if (th.data.store_name == "") {
      getApp().my_warnning("请选择服务门店", 0, th);
    } else if (th.data.beautician_name == "") {
      getApp().my_warnning("请选择美容师", 0, th);
    } else {
      var url = e.currentTarget.dataset.url;
      getApp().goto(url);
    }
  },
  //点击选择门店
  choose_for_store: function(e) {
    var th = this;
    var index_c = e.currentTarget.dataset.ind;
    var fir_pick_index = th.data.fir_pick_index;
    //判断是否点击选中的门店,防止重复点击重复设置选中下标
    if (index_c == fir_pick_index) {
      return false;
    } else {
      th.setData({
        fir_pick_index: index_c,
        beautician_name: "",
        time: "",
        tment_count: ""
      })
    }
  },
  //确认选择门店
  choice_store: function() {
    var th = this;
    var index = th.data.fir_pick_index;
    var store_name = th.data.store_list[index].StorageName;
    var Id = th.data.store_list[index].Id;
    th.setData({
      store: 0,
      store_name: store_name,
      storageId: Id
    })
  },
  onReachBottom: function() {
    var th = this;
    if (this.data.total <= th.data.pageSize) return;
    if (this.data.ismore) return;

    wx.showLoading({
      title: '加载中...',
    })
    th.query_store();
  },
  //分页查询门店信息
  query_store: function() {
    var th = this;
    var itemId = th.data.itemId; //服务id
    var url = "/api/weshop/marketing/reservation/storage/page";
    var key_word = th.data.key_word;
    getApp().request.promiseGet(url, {
      data: {
        storeId: a.stoid,
        serviceId: itemId,
        latitude: th.data.lat,
        longitude: th.data.lon,
        page: th.data.curpage,
        pageSize: th.data.pageSize,
        keyWord: key_word
      }
    }).then(res => {
      wx.hideLoading();
      if (res.data.code == 0) {
        th.data.curpage++;
        var arr1 = th.data.store_list;
        var arr2 = res.data.data.pageData;
        var arr3 = [...arr1, ...arr2];
        var ismore = 0;
        if (arr3.length == res.data.data.total) ismore = 1
        th.setData({
          store_list: arr3,
          total: res.data.data.total,
          ismore: ismore,
          is_service_read: 1,
        }), wx.stopPullDownRefresh(); //停止下拉刷新
      } else {
        getApp().my_warnning(res.data.msg, 0, th);
      }
    })
  },
  //查询
  query_beautician: function() {
    var th = this;
    if (th.data.store_name == "") {
      getApp().my_warnning("请选择服务门店", 0, th);
    } else {
      if (th.data.beautician == 0) {
        th.setData({
          beautician: 1
        })
      } else {
        th.setData({
          beautician: 0
        })
      }
      wx.showLoading({
        title: '加载中',
      })
      var itemid = th.data.itemId; //正式使用的项目id
      var storageId = th.data.storageId; //正式使用的线下门店id
      var url = "/api/weshop/marketing/reservation/staff/page"; //接口地址
      getApp().request.promiseGet(url, {
        data: {
          storeId: a.stoid,
          userId: d.user_id,
          serviceId: itemid,
          storageId: storageId
        }
      }).then(res => {
        wx.hideLoading();
        if (res.data.code == 0) {
          th.setData({
            beautician_list: res.data.data.pageData
          })
          if (res.data.data.pageData.length < 1) {
            getApp().my_warnning("暂无美容师", 0, th);
          }
        } else {
          getApp().my_warnning("暂无美容师", 0, th);
        }
      })
    }
  },
  choice_beautician: function(e) {
    var th = this;
    var bea_index = e.currentTarget.dataset.baaindex;
    var bea_name = th.data.beautician_list[bea_index].StaffName;
    var BeauticianID = th.data.beautician_list[bea_index].Id;
    th.setData({
      beautician_name: bea_name,
      beautician: 0,
      bea_index: bea_index,
      beauticianID: BeauticianID,
      time: "",
      tment_count: ""
    })

  },
  //获取搜索门店输入的值
  input_store: function(e) {
    this.setData({
      key_word: e.detail.value
    })
  },
  //搜索门店
  search_store: function() {
    var th = this;
    var key_word = th.data.key_word;
    var store_list = th.data.store_list;
    th.setData({
      curpage: 1,
      is_search: 0,
      store_list: []
    })
    wx.showLoading({
      title: '加载中',
    })
    th.query_store();
    if (store_list.length < 1) {
      th.setData({
        is_search: 1
      })
    }
  },
  //美容师预约跳转页面
  nav_bea: function() {
    var th = this;
    var storageId = th.data.storageId; //线下门店id
    var itemId = th.data.itemId; //服务id
    if (storageId == "") {
      getApp().my_warnning("请选择服务门店", 0, th);
    } else {
      wx.navigateTo({
        url: "/pages/user/my_service/cosmetology_list?" + 'storageId=' + storageId + '&' + 'itemId=' + itemId
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var th = this;
    th.setData({
      itemId: options.service_id,
      buyType: options.BuyType,
      project_id: options.ProjectID,
    })
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        th.data.lat = res.latitude;
        th.data.lon = res.longitude;
        th.data.is_get_local_ok = 1;
        th.setData({
          is_gps: 1
        });
      },
      fail: function(res) {
        if (res.errCode == 2) {
          th.setData({
            is_gps: 0
          });
          if (th.data.is_gps == 0) {
            getApp().confirmBox("请开启GPS定位", null, 25000, !1);
          }
        } else {
          th.setData({
            is_gps: "3"
          });
        }

        th.data.is_get_local_ok = 1;
      }
    })
  },
  //查询剩下可预约人数
  query_more: function() {
    var th = this;
    var url = "/api/weshop/marketing/reservation/can/reservation/count";
    var beauticianID = th.data.beauticianID; //美容师id
    var projectID = th.data.itemId; //服务id
    var seekTime = th.data.time.substring(0, 10); //预约日期
    var storageId = th.data.storageId; //门店id
    var storeId = a.stoid; //商家id
    getApp().request.promiseGet(url, {
      data: {
        beauticianId: beauticianID,
        seekTime: seekTime,
        serviceId: projectID,
        storageId: storageId,
        storeId: storeId
      }
    }).then(res => {
      if (res.data.code == 0) {
        var tment_count = res.data.data.CanReservation;
        th.setData({
          tment_count: tment_count
        })
      } else {
        getApp().my_warnning(res.data.msg, 0, th);
      }
    })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var th = this;
    var seekTime = th.data.time; //预约日期
    if (seekTime != "") {
      th.query_more();
    }
  }
})