Преглед на файлове

添加ios支持ipad的微信登录

slambb преди 7 месеца
родител
ревизия
12224601a0

+ 6 - 6
Assets/BowArrow/Scenes/Login.unity

@@ -24578,9 +24578,9 @@ RectTransform:
   m_Father: {fileID: 2109231682}
   m_RootOrder: 0
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
-  m_AnchorMin: {x: 0, y: 0}
-  m_AnchorMax: {x: 0, y: 0}
-  m_AnchoredPosition: {x: 0, y: 0}
+  m_AnchorMin: {x: 0, y: 1}
+  m_AnchorMax: {x: 0, y: 1}
+  m_AnchoredPosition: {x: 160, y: -17.825}
   m_SizeDelta: {x: 160, y: 27}
   m_Pivot: {x: 1, y: 0.5}
 --- !u!114 &1417191191
@@ -37201,9 +37201,9 @@ RectTransform:
   m_Father: {fileID: 2109231682}
   m_RootOrder: 1
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
-  m_AnchorMin: {x: 0, y: 0}
-  m_AnchorMax: {x: 0, y: 0}
-  m_AnchoredPosition: {x: 0, y: 0}
+  m_AnchorMin: {x: 0, y: 1}
+  m_AnchorMax: {x: 0, y: 1}
+  m_AnchoredPosition: {x: 0, y: -90.325}
   m_SizeDelta: {x: 760, y: 100.7}
   m_Pivot: {x: 0, y: 0.5}
 --- !u!114 &2080044274

+ 19 - 1
Assets/BowArrow/Scripts/Manager/LoginMgr/LoginView.cs

@@ -6,6 +6,7 @@ using UnityEngine.SceneManagement;
 using System.Text.RegularExpressions;
 using Newtonsoft.Json.Linq;
 using JCUnityLib;
+using AdaptUI;
 /* 登录界面 */
 public class LoginView : MonoBehaviour
 {
@@ -57,6 +58,22 @@ public class LoginView : MonoBehaviour
         //设置微信登录状态按钮
         transform.Find("Input Contain/otherLogin/BtnWxLogin").gameObject.SetActive(CommonConfig.AppArea == 0 && WeChatLoginHelper.IsWechatInstalled());
 
+        //这里初始化时候,分别配置当前的微信AppID和UniversalLink
+        if (WeChatLoginHelper.IsWechatInstalled())
+        {
+            string applicationIdentifier = Application.identifier; // 真机下这样更稳定
+            if (applicationIdentifier.Contains("PadBowArrow"))
+            {
+                // iPad版
+                WeChatLoginHelper.InitWeChat("wx64d1835dbd218aec", "https://xmjssvr.cn/PadBowArrow/");
+            }
+            else
+            {
+                // 默认普通版
+                WeChatLoginHelper.InitWeChat("wxfe29f3f64e3c5d16", "https://xmjssvr.cn/BowArrow/");
+            }
+        }
+
     }
 
     void OnDestroy() {
@@ -219,8 +236,9 @@ public class LoginView : MonoBehaviour
     private string _loginToken = null;
     public void OnWxLoginResp(string loginCode) {
         if (string.IsNullOrWhiteSpace(loginCode)) return;
+        string currentAppID = WeChatLoginHelper.CurrentAppID;
         transform.Find("MaskWxLogin").gameObject.SetActive(true);
-        StartCoroutine(LoginController.Instance.LoginByWX(loginCode, (res) => {
+        StartCoroutine(LoginController.Instance.LoginByWX_WithAppID(loginCode, currentAppID, (res) => {
             Debug.Log($"wxlogin service rescode {res.code}, msg {res.msg}");
             if (res.code == 0) {
                 JObject data = (JObject)res.data;

+ 13 - 1
Assets/BowArrow/Scripts/Manager/LoginMgr/WeChatLoginHelper.cs

@@ -6,6 +6,8 @@ using System;
 using AOT;
 public class WeChatLoginHelper
 {
+    public static string CurrentAppID { get; private set; }
+
     public static bool IsWechatInstalled()
     {
 #if UNITY_ANDROID
@@ -27,7 +29,14 @@ public class WeChatLoginHelper
 #endif
 
     }
-
+    //如果是iphone 动态处理
+    public static void InitWeChat(string appId, string universalLink)
+    {
+        CurrentAppID = appId;
+#if UNITY_IPHONE && !UNITY_EDITOR
+        _RegisterWeChat(appId, universalLink);
+#endif
+    }
 
     public static void Login()
     {
@@ -56,6 +65,9 @@ public class WeChatLoginHelper
 
 #if UNITY_IPHONE
     //这个方法就是引用在 xcode 中编写的方法,与 iOS 连接的桥梁
+    [DllImport("__Internal")]
+    private static extern void _RegisterWeChat(string appid, string universalLink);
+
     [DllImport("__Internal")]
     private static extern void _WechatLogin(IntPtr resultHandler);
 

+ 19 - 0
Assets/BowArrow/Scripts/Network/HttpController/LoginController.cs

@@ -37,6 +37,25 @@ public class LoginController : JCUnityLib.Singleton<LoginController>
             if (callback != null) callback(requestResult);
         }
     }
+    public IEnumerator LoginByWX_WithAppID(string loginCode, string appid, Action<RequestResult> callback)
+    {
+        string url = CommonConfig.gateServerURL + "/gameLogin/loginAppIDByWX";
+        WWWForm form = new WWWForm();
+        form.AddField("loginCode", loginCode);
+        form.AddField("serverIndex", CommonConfig.serverIndex);
+        form.AddField("appid", appid);
+        using (UnityWebRequest request = UnityWebRequest.Post(url, form))
+        {
+            request.timeout = 10;
+            yield return request.SendWebRequest();
+            RequestResult requestResult = new RequestResult();
+            if (request.result == UnityWebRequest.Result.Success)
+            {
+                requestResult = JsonConvert.DeserializeObject<RequestResult>(request.downloadHandler.text);
+            }
+            if (callback != null) callback(requestResult);
+        }
+    }
 
     public IEnumerator LoginByApple(string identityToken, string email, string fullName, Action<RequestResult> callback)
     {

+ 1 - 1
Assets/Plugins/iOS/Editor/XCodePostProcessBuild.cs

@@ -40,7 +40,7 @@ public static class XCodePostProcessBuild
         if (applicationIdentifier.Contains("PadBowArrow"))
         {
             // iPad版
-            SetInfoList(path, "com.xmjssvr.PadBowArrow", "wx9999999999999999");  // 这里填写 iPad 版的 wx id
+            SetInfoList(path, "com.xmjssvr.PadBowArrow", "wx64d1835dbd218aec");  // 这里填写 iPad 版的 wx id
         }
         else
         {

+ 15 - 3
Assets/Plugins/iOS/MyAppController.mm

@@ -19,7 +19,7 @@ IMPL_APP_CONTROLLER_SUBCLASS (MyAppController)
     [super application:application didFinishLaunchingWithOptions:launchOptions];
 	// TODO 
 	
-	NSString *weichatId = [NSString stringWithFormat:@"%s", "wxfe29f3f64e3c5d16"];
+/* 	NSString *weichatId = [NSString stringWithFormat:@"%s", "wxfe29f3f64e3c5d16"];
     
     BOOL installed = [WXApi isWXAppInstalled];
     NSLog(@"installed result: %@", installed?@"true":@"false");
@@ -28,7 +28,8 @@ IMPL_APP_CONTROLLER_SUBCLASS (MyAppController)
     
     NSLog(@"result: %@", result?@"true":@"false");
 	
-	NSLog(@"result: %@",@"didFinishLaunchingWithOptions MyAppController初始化");
+	NSLog(@"result: %@",@"didFinishLaunchingWithOptions MyAppController初始化"); */
+	NSLog(@"Unity iOS didFinishLaunching 正常启动");
     return YES;
 }
 // - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
@@ -127,7 +128,18 @@ IMPL_APP_CONTROLLER_SUBCLASS (MyAppController)
 
 // 为回避C++命名粉碎,以C linkage进行声明
 extern "C" {
-
+	//动态注册信息
+	void _RegisterWeChat(const char* appid, const char* link) 
+	{
+		NSString *nsAppId = [NSString stringWithUTF8String:appid];
+		NSString *nsLink = [NSString stringWithUTF8String:link];
+		
+		BOOL installed = [WXApi isWXAppInstalled];
+		NSLog(@"微信是否安装: %@", installed ? @"是" : @"否");
+
+		BOOL result = [WXApi registerApp:nsAppId universalLink:nsLink];
+		NSLog(@"微信注册结果: %@", result ? @"成功" : @"失败");
+	}
 	//微信登录
 	void _WechatLogin(WXResultHandler resultHandler)
 	{