// pages/empty/login.js
var network = require("../../utils/net.js");
var app = getApp();

function requestLoading(method, params, successaction) {
  wx.showLoading({
    title: "加载中",
  })

  var data = {
    'id': 1,
    'method': method,
    'params': params
  };

  wx.request({
    url: app.globalData.baseUrl,
    data: data,
    header: {
      'content-type': 'application/json',
      'cookie': app.globalData.cookie
    },
    method: 'POST',
    success: function(res) {
      if (res.data.error != null) {
        if (res.data.error.message == '没有对应关系，请先绑定') {
          wx.navigateTo({
            url: '/pages/login/login',
          })
        } else {
          wx.showModal({
            showCancel: false,
            title: '执行出错',
            content: res.data.error.message,
          })
        }
      } else {
        successaction(res.data)
      }
    },
    fail: function(res) {
      console.log(res)
      wx.showModal({
        title: '执行出错',
        content: res,
      })
    },
    complete: function(res) {
      wx.hideLoading()
    },
  })
}

function GetBindCustomer(that) {
  let method = '/MainSystem/B3WeChatMiniProgram/Rpcs/CommonRpc/GetBindCustomer';
  let params = [app.globalData.appID, app.globalData.openID];
  requestLoading(method, params, function(res) {
    if (res.result != null) {
      app.globalData.globalCustomerID = res.result.ID;
      app.globalData.phoneNum = res.result.WeixinUser_Phone;
      // 获取cookie值
      WeixinLogin(function(res) {
        setCurrentCustomerID();
        wx.switchTab({
          url: '/pages/homePage/homePage',
        })
      });
    }
  })
}
// 跳转前获取客户自己的ID 存储后在创建订单时 基本信息带出使用 
function setCurrentCustomerID() {
  let method = "/MainSystem/B3MiniProgramRpc/XuRpcs/Customer/AccountRpc/GetCurrentCustomerID";
  let params = [];
  network.transfer_request(method, params, function(res) {
    console.log("当前客户信息==" + res.result);
    app.globalData.userID = res.result;
  })
}

// 获取cookie值 参数1、appID 2、openID
function WeixinLogin(successtion) {
  let method = '/MainSystem/B3MiniProgramRpc/Rpcs/CommonRpc/WeixinLogin';
  let params = [app.globalData.appID, app.globalData.openID];
  network.transfer_request(method, params, function(res) {
    app.globalData.cookie = res.result;
    successtion();
  })
}

function load(that) {
  wx.login({
    success: function(res) {
      let code = res.code;
      let method = '/MainSystem/B3WeChatMiniProgram/Rpcs/CommonRpc/GetMiniOpenIdByCode';
      let params = [app.globalData.appID, code];
      // 获取openID
      network.requestLoading(method, params, function(res) {
        app.globalData.openID = res.result;
        // 获取绑定客户ID及用户电话
        GetBindCustomer(that)
      })
    }
  });
}

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    load(this);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading();
    //doSomeThing
    load(this);
    wx.stopPullDownRefresh();
    wx.hideNavigationBarLoading();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})