import wxjssdk from "weixin-js-sdk";
// import request from '../request.js'

import request from "../common/request";

import { baseUrl,appId } from "../common/config";

// import {AesDecrypt} from '../utils/aes_util.js';
// import CryptoJS from '../utils/aes_util.js';
import CryptoJS from "./aes_util.js";
// AesEncrypt
// AesDecrypt
// import { Decrypt, Encrypt } from './utils'

//引用全局的变量

// var appId = "wx43ca40dd2be85dd6";
// var appId = "wx43ca40dd2be85dd6";

// var appId = "wx658b72887c56f6b4";

const requestJSSDK = () => {
  return new Promise((resolve, reject) => {
    let currenturl =encodeURIComponent(window.location.href);
    let options = {
      header: {
        Authorization: "",
      },
    };
    options.header["Authorization"] = `Bearer ${uni.getStorageSync("token")}`;
    uni.request({
      url: `${baseUrl}mp/api/signature2?url=${currenturl}`, 

      method: "GET",
      header: options.header,
      success: (response) => {
        resolve(response?.data);
      },
      fail: (res) => {
        reject(res);
      },
      complete: (res) => {},
    });
  });
};
//访问得到参数
export const wxconfig = async () => {
  //当前路径
  let response = await requestJSSDK();
  wxjssdk.config({
    // debug: true,
    appId: appId,
    timestamp: response.data.timestamp,
    nonceStr: response.data.nonceStr,
    signature: response.data.signature,
    jsApiList: [
      // 'checkJsApi', //判断当前客户端版本是否支持指定JS接口
      "chooseImage",
      // 'updateAppMessageShareData', //分享接口
      // // 'chooseImageApi', //扫一扫接口
      "chooseWXPay", //微信支付
    ],
  });
};

// 选择图片方法
export const chooseImageApi = () => {
  return new Promise((resolve, reject) => {
    wxconfig();
    wxjssdk.ready(() => {
      wxjssdk.chooseImage({
        count: 1, // 默认9
        sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          console.log(res);
          resolve(res);
          // var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
        },
        cancel: function () {
          console.log("用户取消了");
          // throw new Error('Function not implemented.')
          // throw new Error('Function not implemented.')
        },
      });
    }),
      wxjssdk.error((res) => {
        console.log("config fail:", res);
        //config fail，抛出失败原因
        console.log("-------------失败------------");
        reject(res);
      });
  });
};

// 支付的方法
export const chooseWXPayApi = (payJson) => {
  return new Promise((resolve, reject) => {
    wxconfig();
    wxjssdk.ready(() => {
    //   console.log("微信验证");
      wxjssdk.chooseWXPay({
        // appId: appId,
        timestamp: payJson.timeStamp,
        nonceStr: payJson.nonceStr,
        package: payJson.package,
        signType: payJson.signType,
        paySign: payJson.paySign,
        success: function (res) {
          resolve(res);
        },
        cancel: function (res) {
          resolve("已取消支付");
        },
        fail: function (res) {
          resolve("支付失败了");
        },
      });
    }),
      wxjssdk.error((res) => {
        console.log("config fail:", res);
        //config fail，抛出失败原因
        console.log("-------------失败------------");
        reject(res);
      });
  });
};
