powerfun-unity/Assets/Scripts/Mobile/LoginControllerMobile.cs

205 lines
8.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Assets.Scripts;
using Assets.Scripts.Mobile.Interface;
using DG.Tweening;
using Facebook.Unity;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LoginControllerMobile : MonoBehaviour, INativeOnMobileWxLoginResp
{
// Start is called before the first frame update
ScrollRect loginScrollView;
Transform thirdLoginPage;
public WeChatController weChat { get; private set; }
void Awake()
{
loginScrollView = transform.Find("Panel/LoginContainer/LoginScrollView").GetComponent<ScrollRect>();
thirdLoginPage = loginScrollView.content.Find("FormContainer-Login/Mask/FormContainer-third");
//UIManager.AddEvent(loginScrollView.content.Find("FormContainer-Login/Mask/FormContainer/BtnThirdLogin").gameObject,
// UnityEngine.EventSystems.EventTriggerType.PointerClick,
// (b)=> ShowThirdLoginPage());
//UIManager.AddEvent(loginScrollView.content.Find("FormContainer-Login/Mask/FormContainer-third/BtnReturn").gameObject,
// UnityEngine.EventSystems.EventTriggerType.PointerClick,
// (b) => HideThirdLoginPage());
UIManager.AddEvent(loginScrollView.content.Find("FormContainer-Login/Mask/FormContainer/otherContainer/Wechat").gameObject,
UnityEngine.EventSystems.EventTriggerType.PointerClick,
(b) => goWxLogin());
}
private void Start()
{
//Utils.showToast(null,"123",duration:10,showSeconds: true);
transform.Find("MobileInfo/BatteryText").GetComponent<Text>().text =
$"{Math.Round(SystemInfo.batteryLevel * 100, 0)}%";
transform.Find("MobileInfo/TimeText").GetComponent<Text>().text =
DateTime.Now.ToString("HH:mm");
weChat = App.weChatController;
weChat.Init(App.WxAppId);
#if UNITY_IOS
UIManager.AddEvent(transform.Find("Panel/LoginContainer/LoginScrollView/Viewport/Content/FormContainer-Login/Mask/FormContainer/otherContainer/Apple").gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, b =>
{
#if UNITY_EDITOR
GetComponent<LoginController>()
.OnAppleIdLoginCheck("000515.7d1f47f5171b4d66a4bb4a4e7e2c6899.0830", "eyJraWQiOiJZdXlYb1kiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FwcGxlaWQuYXBwbGUuY29tIiwiYXVkIjoiY29tLlpoaVhpbmdQYWkuUG93ZXJGdW4iLCJleHAiOjE2NDAwNDk3NzEsImlhdCI6MTYzOTk2MzM3MSwic3ViIjoiMDAwNTE1LjdkMWY0N2Y1MTcxYjRkNjZhNGJiNGE0ZTdlMmM2ODk5LjA4MzAiLCJjX2hhc2giOiJhekhJdTZNVE1OVGs4Q3RxNndIeHlRIiwiYXV0aF90aW1lIjoxNjM5OTYzMzcxLCJub25jZV9zdXBwb3J0ZWQiOnRydWV9.WM23STabsJqSV1FjPvYRXo3kOB2Cfo7HsPDUuJx8UlqTUthbif1uvhEFpFMF8G5B5_yl3kkmaDtv0nQA25DkDiYowfPVV8gkLx2IMMOm6rVqV5E7kA6xNa64pwgIHDnTSjnn862_DEb4QVkAfi-rXsbkZ1q-EpH5MG-5GLR3Ktq_1n59kQcJrvuvUEpbv8IbSzRLh4VVZLY5rmaoddZ2kg5hw234cwGCmiMxHi55tVHAiGnqgq74IY7HXz3udwRGufaFXpkYbdBuL5-wqAp1SQlQhwpTzhZ5JKr2y4ukejYPFZzLfnkUX0xhLmH8uooKb-TjOa5qaFKGXT0hXh4NHw");
#else
App.weChatController.SignWithApple();
#endif
});
#else
transform.Find("Panel/LoginContainer/LoginScrollView/Viewport/Content/FormContainer-Login/Mask/FormContainer/otherContainer/Apple").gameObject.SetActive(false);
#endif
UIManager.AddEvent(transform.Find("Panel/LoginContainer/LoginScrollView/Viewport/Content/FormContainer-Login/Mask/FormContainer/otherContainer/Facebook").gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, b =>
{
FB.LogOut();
FB.LogInWithReadPermissions(callback: onFaceBookResp);
});
//App.weChatController.Init(App.WxAppId);
//var s = App.weChatController.isApplicationAvilible("com.gugoumainapp","powerfun").ToString();
//Utils.showToast(null, SystemInfo.batteryLevel.ToString());
CheckUpdate();
}
private async void CheckUpdate()
{
#if UNITY_ANDROID
//transform.Find("Panel/LoginContainer/LoginScrollView/Viewport/Content/FormContainer-Login/Mask/FormContainer/BtnThirdLogin")
// .gameObject.SetActive(true);
#endif
var r = await ConfigHelper.userApi.GetVersions();
if (r.result)
{
#if UNITY_ANDROID || UNITY_IOS
#if UNITY_ANDROID
var info = r.data.Value<JObject>("UnityApk");
Debug.Log(info.Value<string>("Version"));
if (Utils.isUpdate(App.AppVersion, info.Value<string>("Version")))
{
transform.Find("Panel").gameObject.SetActive(false);
transform.Find("Version").gameObject.SetActive(false);
UIManager.ShowAndroidUpdate(info);
}
App.WorkoutsUrl = r.data.Value<JObject>("Apk").Value<string>("Url");
#elif UNITY_IOS
//var sw = r.data.Value<string>("ThirdPartSwitch");
//transform.Find("Panel/LoginContainer/LoginScrollView/Viewport/Content/FormContainer-Login/Mask/FormContainer/BtnThirdLogin")
// .gameObject.SetActive(sw.Equals("on") && App.weChatController.IsWeChatAppInstalled());
//transform.Find("Panel/LoginContainer/LoginScrollView/Viewport/Content/FormContainer-Login/Mask/FormContainer/BtnThirdLogin")
// .gameObject.SetActive(true);
var info = r.data.Value<JObject>("UnityIos");
if (Utils.isUpdate(App.AppVersion, info.Value<string>("Version")))
{
Application.OpenURL(info.Value<string>("Url"));
Application.Quit();
}
App.WorkoutsUrl = r.data.Value<JObject>("Ios").Value<string>("Url");
#endif
#endif
}
//if (r.result)
//{
// //#if !UNITY_EDITOR
// var ver = r.data.Value<JObject>("Version").ToObject<UpdateModel>();
// if (ver != null && ver.HasUpdate)
// {
// App.UpdateObject = ver;
// Debug.Log(Application.persistentDataPath + "/PowerFun.exe");
// UIManager.ShowDownloadModal();
// }
// //#endif
// App.notifyContent = r.data.Value<string>("Content");
// if (r.data.Value<int>("Id") != notifyId)//r.data.Value<int>("Id") != notifyId
// {
// UIManager.ShowNewsModal();
// PlayerPrefs.SetInt("notifyId", r.data.Value<int>("Id"));
// }
//}
}
// Update is called once per frame
void Update()
{
}
private void ShowThirdLoginPage()
{
thirdLoginPage.DOLocalMoveY(0, 0.3f);
}
public void HideThirdLoginPage()
{
thirdLoginPage.DOLocalMoveY(-301, 0.3f);
}
private void goWxLogin()
{
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
if (weChat.IsWeChatAppInstalled())
{
weChat.WxLogin("snsapi_userinfo", "");
}
#endif
}
/// <summary>
/// FaceBook登录返回
/// </summary>
/// <param name="res"></param>
private void onFaceBookResp(ILoginResult res)
{
Debug.Log(res.ToString());
if (string.IsNullOrEmpty(res.Error))
{
if (res.Cancelled)
{
Utils.showToast(null, App.GetLocalString("User deauthorisation"));
}
else
{
GetComponent<LoginController>()
.OnFacebookLoginCheck(res.ResultDictionary["user_id"].ToString(), res.ResultDictionary["access_token"].ToString());
Debug.Log("用戶Id" + res.ResultDictionary["user_id"]);
}
}
else
{
Utils.showToast(null, res.Error);
}
}
/// <summary>
/// 从ios或者安卓接收code在c#里不建议调用
/// </summary>
/// <param name="res"></param>
public void OnMobileWxLoginResp(string res)
{
if (res.Contains("true;"))
{
GetComponent<LoginController>().goWxLoginWithCode(res.Replace("true;", ""), "mobile");
}
else
{
Utils.showToast(null, res.Replace("false;", ""));
}
}
public void OnAppleResp(string res)
{
#if UNITY_IOS
var sp = res.Replace("true;", "").Split(',');
if (sp.Length == 2)
{
GetComponent<LoginController>().OnAppleIdLoginCheck(sp[0],sp[1]);
}
//Utils.showToast(null, "苹果返回"+res);
#endif
}
}