// #ifdef H5
import wx from 'weixin-jsapi'
// #endif
let dsHost = "";
// #ifdef H5
dsHost = location.hostname;
// #endif

export const path = `${location.origin}/service`
// get方法
export function get(url, data = {}) {
  url = path + url;
  return new Promise((resolve, reject) => {
    uni.request({
      url,
      data,
      header: {
        'Cache-Control': 'no-cache',
        'Ds-Host': dsHost,
      },
      success(res) {
        if(res.statusCode === 200){
          resolve(res.data);
        }else{
          reject();
          uni.showToast({
            icon: 'none',
            title: '请求数据失败!'
          });
        }
      },
      fail(err) {
        reject();
        uni.showToast({
          icon: 'none',
          title: '请求数据失败!'
        });
      }
    })
  });
}
// post方法
export function post(url, data = {}) {
  url = path + url;
  return new Promise((resolve, reject) => {
    uni.request({
      method: 'POST',
      url,
      data,
      header: {
        'Cache-Control': 'no-cache',
        'Ds-Host': dsHost,
      },
      success(res) {
        if(res.statusCode === 200){
          resolve(res.data);
        }else{
          reject();
          uni.showToast({
            icon: 'none',
            title: '请求数据失败!'
          });
        }
      },
      fail(err) {
        reject();
        uni.showToast({
          icon: 'none',
          title: '请求数据失败!'
        });
      }
    })
  });
}

// 扫一扫
export function SM(code = '') {
  if(code){
    return Promise.resolve(code);
  }
 // #ifndef H5
 return new Promise((resolve, reject) => {
   uni.scanCode({
     onlyFromCamera: true,
     success: function(res) {
       let str = res.result.replace(/[\s\/]/g, '') || 'none';
       str = str.replace(/CODABAR,/i, '');
       str = str.replace(/CODE_128,/i, '');
       resolve(str);
     },
     fail(err) {
       reject(err);
     }
   });
 });
 // #endif
 // #ifdef H5
 return new Promise((resolve, reject) => {
   let param = {
     requestUrl: location.href.split('#')[0]
   };
   post("/wechat/getJsConfig", param).then(res => {
     if (res) {
       wx.config({
         debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
         appId: res.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
         timestamp: res.timestamp, // 必填,生成签名的时间戳
         nonceStr: res.nonceStr, // 必填,生成签名的随机串
         signature: res.signature, // 必填,签名,见附录1
         jsApiList: res.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
       });
       wx.ready(function() {
         wx.scanQRCode({
           desc: "scanQRCode desc",
           needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
           scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
           success: function(res) {
             // 当needResult 为 1 时,扫码返回的结果
             let str = res.resultStr.replace(/[\s\/]/g, '') || 'none';
             str = str.replace(/CODABAR,/i, '');
             str = str.replace(/CODE_128,/i, '');
             resolve(str);
           },
           cancel(err){
             reject(err);
           }
         });
       });
     }
   })
 });
 // #endif
}