function formatNumber(n) {
  const str = n.toString();
  return str[1] ? str : `0${str}`;
}

export function objToUrl(obj) {
  let uri = '';
  let keys = Object.keys(obj);
  keys.forEach(item => {
    uri += '&' + item + '=' + obj[item];
  });

  uri = uri.substr(1);

  return uri;
}

export function formatTime(date) {
  date = new Date(date.replace(/-/g, '/'));
  let year1 = date.getFullYear();
  let month1 = date.getMonth() + 1;
  let day1 = date.getDate();
  let hour1 = date.getHours();
  let minute1 = date.getMinutes();
  let second1 = date.getSeconds();

  let [year, month, day, hour, minute, second] = [year1, month1, day1, hour1, minute1, second1].map(formatNumber);

  return {
    year, month, day, hour, minute, second
  };
}

// 支付订阅消息
export function subscribePayMessage(callback){
  console.log(!wx.requestSubscribeMessage)
  if(!wx.requestSubscribeMessage){
    console.log('版本比较低')
    callback()
  }else{
    wx.requestSubscribeMessage({
      tmplIds: ['SU4O1wMXACAe4bYwOljGxFHlfRgFGAiPUj5Kk3cL0Lg'],
      success (res) {
        console.log(res)
      },
      fail (err){
        console.log(err)
      },
      complete (res){
        callback(res)
      }
    })
  }
}
// 核销订阅消息
export function subscribeHxMessage(callback){
  console.log(!wx.requestSubscribeMessage)
  if(!wx.requestSubscribeMessage){
    console.log('版本比较低')
    callback()
  }else{
    wx.requestSubscribeMessage({
      tmplIds: ['eg649nRuLVNuW1KvWYL2EQXZb2N93Mw95x5krYpiFs4'],
      success (res) {
        console.log(res)
      },
      fail (err){
        console.log(err)
      },
      complete (res){
        callback(res)
      }
    })
  }
}
