<template>
  <view>

  </view>
</template>

<script setup>
  // 添加路由拦截
  import '@/interceptor/routeInterceptor.js'
  import { encryptByEnAESLogin } from '@/utils/index.js'
  import { onLoad } from '@dcloudio/uni-app'
  import { defaultColor } from '@/static/js/theme.js'
  import { api_wechatLoginEncrypt, api_loginEncrypt, api_systemConfiguration, api_getDictionary} from "@/http/api.js"
  import { useWechatAuth } from '@/share/useWechatAuth.js'
  import { useLoginSuccess } from '@/share/useLoginSuccess.js'
  import { useSetTitle } from '@/share/useSetTitle.js'
  
  useSetTitle()
  const { wechatAuth } = useWechatAuth()
  const { loginSuccess } = useLoginSuccess()

  /**
   * 微信登录
   */
  function wechatLoginEncrypt() {
    const url = window.location.href.split("?");
		let wenhaohoumian = window.location.href.split("?code="); //截取问号地址数组
		let codeStr = wenhaohoumian[1]; //地址数组赋值
		let loginType = url[1].split("#/")
		let str = loginType[0].split("=")
		console.log(777878,str)
		if(str[0]=='loginType'){
			uni.setStorageSync('loginType',str[1])
			if (!url[2]) {
			  wechatAuth();
			} else {
				codeStr = codeStr.split("&");
				codeStr = codeStr[0];
			  uni.showLoading({
			    title: "登录中",
			    mask: true,
			  });
			  api_wechatLoginEncrypt({
					code:codeStr
				}).then(res => {
			    uni.hideLoading();
			    if (res.status == 200) {
			      loginSuccess(res.user);
						getConfig();
						changeGroup();
			    } else if (res.status == 501) {
			      uni.showModal({
			        title: '提示',
			        content: '您的账号被删除，请联系管理员',
			        showCancel: false,
			        confirmColor: defaultColor,
			        confirmText: '取消',
			      });
			    } else if (res.status == 555) {
			      uni.reLaunch({
			        url: `/pages/initBind/initBind?wechat=${res.wechat}`
			      })
			    } else {
			      uni.showToast({
			        icon: 'none',
			        title: res.remarks || '请求数据失败！'
			      });
			    }
			  });
			}
		}else{
			uni.setStorageSync('loginType','2')
			if (!url[1]) {
			  wechatAuth();
			} else {
				codeStr = codeStr.split("&");
				codeStr = codeStr[0];
			  uni.showLoading({
			    title: "登录中",
			    mask: true,
			  });
			  api_wechatLoginEncrypt({
					code:codeStr
				}).then(res => {
			    uni.hideLoading();
			    if (res.status == 200) {
			      loginSuccess(res.user);
						getConfig();
						changeGroup();
			    } else if (res.status == 501) {
			      uni.showModal({
			        title: '提示',
			        content: '您的账号被删除，请联系管理员',
			        showCancel: false,
			        confirmColor: defaultColor,
			        confirmText: '取消',
			      });
			    } else if (res.status == 555) {
			      uni.reLaunch({
			        url: `/pages/initBind/initBind?wechat=${res.wechat}`
			      })
			    } else {
			      uni.showToast({
			        icon: 'none',
			        title: res.remarks || '请求数据失败！'
			      });
			    }
			  });
			}
		}

  }
  
  /**
   * 账号密码登录
   */
  function loginEncrypt(username, password) {
    uni.showLoading({
      title: "登录中",
      mask: true,
    });
    let postData = {
        k: encryptByEnAESLogin(JSON.stringify({username, password}))
    };
    api_loginEncrypt(postData).then(res => {
      uni.hideLoading();
			getConfig();
			changeGroup();
      if (res.state == 200) {
        loginSuccess(res.data);
      } else {
        uni.showToast({
          icon: 'none',
          title: res.remarks || '请求数据失败！'
        });
      }
    });
  }
	
	// 获取配置项
	function getConfig(){
		api_systemConfiguration({
			idx: 0,
			sum: 9999,
		}).then(res=>{
			uni.setStorageSync('sysData',JSON.stringify(res.list))
		})
	}
	
	// 获取工作组
	function changeGroup(){
		let postData = {
			"key": 'usertype',
			"type": "list",
		};
		api_getDictionary(postData).then((data) => {
			uni.setStorageSync('groupData',JSON.stringify(data))
		});
	}
	
  onLoad((option) => {
    uni.clearStorageSync();
    // 获取当前页面的实例
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    // 获取URL参数
    const options = currentPage.options;
    if (process.env.NODE_ENV === 'development' && options.username && options.password) {
      // http://localhost:8081/user/#/?username=liaomingming&password=Dstech@123
      // 本地开发调试用
    	loginEncrypt(options.username, options.password)
    } else {
    	wechatLoginEncrypt();
    }

  })
</script>
