/*
 * @Description: 这里输入文件功能
 * @Author: zhoupeng
 * @Date: 2020-05-14 14:03:09
 * @LastEditTime: 2020-05-22 18:26:04
 */

import wx from 'weixin-js-sdk';
import * as Service from '../services/index';
var AppId = '';
var Timestamp = '';
var Signature = '';
var Noncestr = '';

//分享链接地址
const shareTitle = '开饭啦！ 顶流校宠搏喂出道，你为谁撑腰？';
const shareDetail = '你的校宠你的爱，为了口粮不能败！';
const shareURL = 'http://marketing.chongaibao.com/#/./schoolPet/index';
const shareIcon =
  'http://marketing.chongaibao.com/newpetpassportpro/pub/vote/image/fb5b90e941c045a48b11f7d20e132d91';

//获取url的参数
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split('&');
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split('=');
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  return false;
}
/*
 * 微信授权判断
 */
export const wechatAuthorization = async () => {
  //1，走授权，通过url
  try {
    const { rc, data } = await Service.getConfig({
      url: window.location.href.split('#')[0],
    });
    if (rc === 0) {
      AppId = data.appId;
      Timestamp = data.timestamp;
      Signature = data.signature;
      Noncestr = data.noncestr;

      console.log('obj:', AppId, Timestamp, Signature, Noncestr);
      wx.config({
        //beta: true,
        debug: false,
        appId: AppId,
        timestamp: Timestamp,
        nonceStr: Noncestr,
        signature: Signature,
        // 这里是把所有的方法都写出来了 如果只需要一个方法可以只写一个
        jsApiList: ['checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage'],
      });
      wx.ready(function() {
        console.log('设置config成功');

        wx.onMenuShareTimeline({
          title: shareTitle, // 分享标题
          link: shareURL, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          imgUrl: shareIcon, // 分享图标
          success: function() {
            // 用户点击了分享后执行的回调函数
          },
        });
        wx.onMenuShareAppMessage({
          title: shareTitle, // 分享标题
          link: shareURL, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          desc: shareDetail, // 分享描述
          imgUrl: shareIcon, // 分享图标
          type: 'link', // 分享类型,music、video或link，不填默认为link
          dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
          success: function() {
            // 用户点击了分享后执行的回调函数
          },
        });
      });
      wx.error(function(res) {
        console.log(res);
      });
    }
  } catch (e) {}
};

/*
 *获取openid 存储
 */
export const wechatGetOpenId = async callBack => {
  //获取参数
  var code = getQueryVariable('code');
  Service.getAppId({
    code,
  }).then(({ rc, msg, data }) => {
    console.log('rcdata', data);
    if (rc === 0) {
      callBack();
      var openid = data ? data.openid : '';
      var nickname = data ? data.nickname : '';
      var subscribe = data ? data.subscribe : '';

      var storage = window.sessionStorage;
      storage.setItem('openId', openid);
      storage.setItem('niceName', nickname);
      storage.setItem('subscribe', subscribe);
    } else {
      console.log('getAppId Error:', rc, msg, data);
    }
  });
};

/******
 * other
 */
export const onPlay = (flag, fun) => {
  window.sessionStorage.setItem('playFlag', !flag);
  if (typeof fun === 'function') {
    fun(!flag);
  }
};
export const getPlayFlag = flag => {
  let playFlag = window.sessionStorage.getItem('playFlag');
  if (!playFlag) {
    playFlag = 'false';
  }
  return playFlag === 'false' ? false : true;
};

/*
 *设置Img前缀
 */

const URI = 'http://marketing.chongaibao.com/newpetpassportpro/pub/vote/image/';
export const setImg = str => {
  return `${URI}${str}`;
};
