import request from "@/utils/request";
import md5 from "md5";
function sortParams(params) {
  return Object.keys(params)
    .filter((k) => params[k] !== "") // 过滤空参数
    .sort() // 排序
    .reduce((obj, key) => {
      obj[key] = params[key];
      return obj;
    }, {});
}
function sort_ASCII(obj) {
  var arr = new Array();
  var num = 0;
  for (let i in obj) {
    arr[num] = i;
    num++;
  }
  var sortArr = arr.sort();
  var sortObj = {};
  for (let i in sortArr) {
    sortObj[sortArr[i]] = obj[sortArr[i]];
  }
  return sortObj;
}
/**
 * 排序参数并生成签名
 */
function generateSign(params = {}) {
  // 随机字符串
  const nonce_str = Math.random().toString(36).substr(2);

  // 排序参数并转 URL 键值对
  const strTemp = sortParams(Object.assign(params, { nonce_str }));

  console.log(strTemp);
  let arr = sort_ASCII(strTemp);
  let str = "";
  for (let key in arr) {
    if (arr[key] || arr[key] === 0) {
      str = str + key + "=" + arr[key] + "&";
    }
  }
  console.log(str + "appKey=1f26fe1a6a36daf7651d6437bb6a5fa6");
  // 生成 MD5 签名
  let sign = md5(str + "appKey=1f26fe1a6a36daf7651d6437bb6a5fa6").toUpperCase();
  // const sign = md5(
  //   `${strTemp}&appKey=1f26fe1a6a36daf7651d6437bb6a5fa6`
  // ).toUpperCase();
  return {
    ...params,
    nonce_str,
    sign,
  };
}

// 登录
export function login(data) {
  return request({
    url: "/user/login/smsCodeLogin",
    method: "post",
    data,
  });
}
// 趋势板块
export function trendPlate(data) {
  return request({
    url: "/stock/trendPlate",
    method: "post",
    data,
  });
}
// 个股涨跌分页查询
export function riseLossesDetail(data) {
  return request({
    url: "/stock/riseLossesDetail",
    method: "post",
    data,
  });
}
// 涨跌统计
export function riseLossesStatistics(data) {
  return request({
    url: "/stock/riseLossesStatistics",
    method: "post",
    data,
  });
}
// 个股详情
export function stockDetails(data) {
  return request({
    url: "/stock/stockDetails",
    method: "post",
    data,
  });
}
// 个股
export function originalIssueStockDetail(data) {
  return request({
    url: "/stock/originalIssueStockDetail",
    method: "post",
    data,
  });
}
// 个股详情
export function getByCode(data) {
  return request({
    url: "/stock/getByCode",
    method: "post",
    data,
  });
}
// 个股排名变化
export function stockSortChange(id) {
  return request({
    url: "/stock/stockSortChange/" + id,
    method: "get",
  });
}
// 交易记录
export function findPage(data) {
  return request({
    url: "/transaction/findPage",
    method: "post",
    data,
  });
}

// 批量删除交易记录
export function delByIds(data) {
  return request({
    url: "/transaction/delByIds",
    method: "post",
    data,
  });
}
// 查看交易详情
export function getDetailById(id) {
  return request({
    url: "/transaction/getDetailById/" + id,
    method: "get",
  });
}
// 保存或修改交易记录
export function saveOrUpdate(data) {
  return request({
    url: "/transaction/saveOrUpdate",
    method: "post",
    data,
  });
}

export function kLine(data) {
  return request({
    url: "/stock/kLine",
    method: "post",
    data,
  });
}
// 交易统计盈利、亏损
export function profitOrLoss(data) {
  return request({
    url: "/transaction/profitOrLoss",
    method: "post",
    data,
  });
}
// 交易统计收益率
export function totalEarnings(data) {
  return request({
    url: "/transaction/totalEarnings",
    method: "post",
    data,
  });
}
// 收益折线图
export function earningsLine(data) {
  return request({
    url: "/transaction/earningsLine",
    method: "post",
    data,
  });
}
// 收藏数据
export function query(data) {
  return request({
    url: "/collect/query",
    method: "post",
    data,
  });
}
// 收藏

export function addQuery(data) {
  return request({
    url: "/collect/add",
    method: "post",
    data,
  });
}

// 取消收藏
export function delQuery(data) {
  return request({
    url: "/collect/del",
    method: "post",
    data,
  });
}
// 查询是否收藏某个code
export function queryIsCollect(data) {
  return request({
    url: "/collect/queryIsCollect",
    method: "post",
    data,
  });
}

// 获取验证码
export function sendSms(data) {
  return request({
    url: "/login/sendSms",
    method: "post",
    data,
  });
}
// 板块排名变化
export function dongCaiSortChange(id) {
  return request({
    url: "/stock/dongCaiSortChange/" + id,
    method: "get",
  });
}
// 板块涨跌区域统计
export function ChangeriseLossesStatistics(data) {
  return request({
    url: "/stock/dongCaiSortChange/riseLossesStatistics" + data,
    method: "get",
  });
}

// 板块指数（k线图）
export function dongCaiSortChangekLine(id) {
  return request({
    url: "/stock/dongCaiSortChange/kLine/" + id,
    method: "get",
  });
}
//

// 板块涨跌停个股
export function ChangedongCaiRiseLosses(data) {
  return request({
    url: "/stock/dongCaiSortChange/dongCaiRiseLosses" + data,
    method: "get",
  });
}
