'use strict';

exports.__esModule = true;

var _DB = require('./../DB');

var _DB2 = _interopRequireDefault(_DB);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var Bridge = {
  /**
   * 定制功能
   */
  platform: 'wq',
  config: function config() {
    var self = this;
    /* eslint-disable */
    wq.config({ auth: false });
    wq.ready(function () {
      self.addBackPress();
    });
    /* eslint-enable */
  },
  // 判断是否是主页
  isHomePage: function isHomePage(callback, rule) {
    if (rule && window.location.href.indexOf(rule) >= 0) {
      callback(true);
      return;
    }
    callback(false);
  },
  // 获得版本信息
  getAppVersion: function getAppVersion() {
    return window.navigator.appVersion;
  },
  // 返回首页
  goHome: function goHome() {
    window.history.go(-1);
  },
  // 退出到登陆页面
  logOut: function logOut() {
    console.log('logOut方法仅在app上工作');
  },
  // 打开新的窗口
  openWindow: function openWindow() {
    var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

    wq.openWindow(params); // eslint-disable-line
  },
  // 关闭窗口
  closeWindow: function closeWindow() {
    wq.closeWindow(); // eslint-disable-line
  },
  /**
    * 修改原生标题
    * @param {Object} params {title: '自定义标题', visiable: '0' 隐藏  '1' 展示, left: { show: false 隐藏返回按钮 true 显示返回按钮}}
    */
  setTitle: function setTitle(params) {
    wq.setTitle(params);
  },
  // 客户端返回绑定
  addBackPress: function addBackPress(callback) {
    var self = this;
    if (wq.onHistoryBack) {
      // eslint-disable-line
      wq.onHistoryBack(function () {
        // eslint-disable-line
        if (callback) {
          callback();
          self.addBackPress();
        } else {
          self.back();
          self.addBackPress();
        }
        return false;
      });
    }
  },
  // 客户端移除返回绑定
  removeBackPress: function removeBackPress() {
    if (wq.onHistoryBack) {
      // eslint-disable-line
      wq.onHistoryBack(function () {
        // eslint-disable-line
        return true;
      });
    }
  },
  /**
    * 获取当前地理位置
    * @param {Object} params
    * params: {
    * type {String}: 'wgs84'|'gcj02'坐标类型微信默认使用国际坐标'wgs84',
    * cache {Number}: 默认60秒缓存防重复定位
    * }
    * @returns {Object} {latitude: '纬度', longitude: '经度', speed:'速度', accuracy:'位置精度'}
    */
  getLocation: function getLocation() {
    var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

    var self = this;
    // 先从cookie中读取位置信息
    var appLocation = _DB2.default.getCookie('app_location');
    if (appLocation === 'undefined') {
      _DB2.default.removeCookie('app_location');
      appLocation = '';
    }
    try {
      if (appLocation) appLocation = JSON.parse(appLocation);
    } catch (error) {
      appLocation = '';
    }
    if (appLocation) {
      if (params.success) params.success(appLocation);
      return;
    }
    // 调用定位
    if (self.locating) return;
    self.locating = true;
    console.log('调用定位...');
    wq.getLocation({ // eslint-disable-line
      // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
      type: 'gcj02',
      success: function success(res) {
        self.locating = false;
        // 将位置信息存储到cookie中60秒
        if (res.latitude && res.latitude) {
          if (params.cache) _DB2.default.setCookie('app_location', JSON.stringify(res), params.cache || 60);
          if (params.success) params.success(res);
        } else {
          if (params.fail) params.fail(res);else self.showToast('没有获取到经纬度', { mask: false });
        }
      },
      fail: function fail(res) {
        self.locating = false;
        if (params.fail) params.fail(res);else self.showToast('定位失败,请检查微信定位权限是否开启', { mask: false });
      },
      complete: function complete(res) {
        self.locating = false;
        if (params.complete) params.complete(res);
      }
    });
  },
  /*
   * 扫描二维码并返回结果
   * 返回：{resultStr:''}
   * */
  scanQRCode: function scanQRCode() {
    var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

    var self = this;
    wq.scanQRCode({ // eslint-disable-line
      needResult: params.needResult || 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果
      scanType: params.scanType || ['qrCode', 'barCode'],
      desc: params.desc || '二维码／条码',
      success: function success(res) {
        if (!params.success) return;
        params.success(res);
      },
      fail: function fail(res) {
        if (params.fail) params.fail(res);else self.showToast(res.errMsg, { mask: false });
      },
      cancel: function cancel(res) {
        if (params.cancel) params.cancel(res);
      },
      complete: function complete(res) {
        if (params.complete) params.complete(res);
      }
    });
  },

  chooseImage: function chooseImage(params) {
    wq.chooseImage(params); // eslint-disable-line
  },
  uploadImage: function uploadImage(params) {
    wq.uploadImage(params); // eslint-disable-line
  },
  previewImage: function previewImage(params) {
    wq.previewImage(params); // eslint-disable-line
  }
};

exports.default = Bridge;
module.exports = exports['default'];