/*
 * @Author: cclu
 * @Date: 2021-12-13 11:01:23
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-04-08 21:45:49
 * @FilePath: \cloud-platform\src\services\login.ts
 * @Description: 
 * 
 * Copyright (c) 2022 by cclu/杭州驿商科技, All Rights Reserved. 
 */
import request from '@/utils/request';
import CryptoJs from 'crypto-js';
import type { CurrentUser } from '@/models/user';
import { validatePassword } from '@/utils/format';

export type LoginParamsType = {
  UserPassport: string;
  UserPassWord: string;
  //   mobile: string;
  //   captcha: string;
};

export type LoginResult = {
  currentAuthority: string;
  status: string;
  currentUser?: CurrentUser;
  passwordRes?: any
};

export async function accountLogin(params: LoginParamsType): Promise<LoginResult> {
  let realParams: any = JSON.parse(JSON.stringify(params))
  let passwordRes: any = validatePassword(realParams.UserPassWord)


  params.UserPassWord = CryptoJs.MD5(params.UserPassWord + params.UserPassport).toString();
  const data = await request(`/Logging/UserLogin`, {
    method: 'POST',
    data: params,
    requestType: 'form',
  });

  if (data.Result_Code !== 100) {
    return {
      currentAuthority: 'admin',// params.UserPassport,
      status: data.Result_Desc,
    };
  }
  const user = { ...data.Result_Data };
  delete user.PassWord;
  delete user.EnabledRepeat;
  return {
    currentAuthority: 'admin',// data.Result_Data.UserName,
    status: 'ok',
    currentUser: user,
    passwordRes: passwordRes
  };
  // const resp = await request('/api/login/account', {
  //   method: 'POST',
  //   data: params,
  // });
  // return resp;
}

// export async function getFakeCaptcha(mobile: string) {
//   return request(`/api/login/captcha?mobile=${mobile}`);
// }
