<script setup>
// 2. 获取组件实例，通过 proxy 访问全局属性
import { ref} from "vue";
import * as GenerateTestUserSig from "@/debug/GenerateTestUserSig-es.js";
import { CallManager } from "@/TUICallKit/src/TUICallService/serve/callManager";
import { onLaunch, onShow, onHide, onError } from '@dcloudio/uni-app'
import { getCurrentInstance } from 'vue'
// 2. 获取组件实例，通过 proxy 访问全局属性
const { proxy } = getCurrentInstance()
const globalData = {
	SDKAppID: 0,
	userID: '',
	userSig: ''
};
let avatarUrl = ref("");  // 声明为响应式变量
uni.CallManager = new CallManager();
onLaunch(() => {
	
	uni.login({
	  provider: 'weixin',
	  success: function (loginRes) {
	    console.log('xx');
      console.log(loginRes.code);
      if( uni.getStorageSync('token')){
        userinfo(loginRes.code);
      }else{
      wxcode(loginRes.code)
      }
     
	    // 获取用户信息 
	   
	  }
	});
})
const wxcode = async (code) => {

    try {
    // 通过 proxy 调用全局的 apiUrl
    const res = await proxy.apiUrl({
      url: '/api/login/mnpLogin',
      method: 'POST',
      data: {
        code: code
      },
    }, false) // 不显示加载中
    uni.setStorageSync('token', res.data.token)
	uni.setStorageSync('userData', res.data)
	if(res.code==1){
		video(res.data)
	}
    console.log('请求结果：', res.data)
  } catch (err) {
  
   
  }

}
const userinfo = async (code) => {

    try {
    // 通过 proxy 调用全局的 apiUrl
    const res = await proxy.apiUrl({
      url: '/api/user/info',
      method: 'POST',
    }, false) // 不显示加载中
    if(res.code==-1){

    wxcode(code)
    }
	uni.setStorageSync('userData', res.data)
	if(res.code==1 && res.data.diagnosis){
		video(res.data)
	}
    
  } catch (err) {
  
   
  }

}
const video =async (data)=>{
	const res = await proxy.apiUrl({
	  url: '/api/tcm/getPatientSignature',
	  method: 'GET',
	  data: {
	     patient_id: data.diagnosis.patient_id
	  },
	}, false) 
	
	const {userId } = res.data;
	
	const { userSig, SDKAppID } = GenerateTestUserSig.genTestUserSig({
	  userID:userId,
	});
		console.log('获取签名成功:',userSig);
	getApp().globalData.userID = userId.value;
	getApp().globalData.userSig = userSig;
	getApp().globalData.SDKAppID = SDKAppID;
	
	await uni.CallManager.init({
	  sdkAppID: SDKAppID, // 替换为用户自己的 sdkAppID
	  userID: userId, // 替换为用户自己的 userID
	  userSig: userSig, // 替换为用户自己的 userSig
	  globalCallPagePath: "TUICallKit/src/Components/TUICallKit", // 替换为步骤一里注册的全局监听页面
	});
}
</script>