WeChatLoginHelper.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Runtime.InteropServices;
  5. using System;
  6. using AOT;
  7. public class WeChatLoginHelper
  8. {
  9. public static void Login()
  10. {
  11. #if UNITY_ANDROID
  12. using (var sdk = new AndroidJavaClass("com.xmjssvr.BowArrow.mi.WeChatSDK")) sdk.CallStatic("WeChatLogin");
  13. #endif
  14. #if UNITY_IPHONE
  15. if (!IsWechatInstalled_iOS())
  16. {
  17. Debug.Log("微信未安装");
  18. return;
  19. }
  20. WXResultHandler handler = new WXResultHandler(resultHandler);
  21. IntPtr fp = Marshal.GetFunctionPointerForDelegate(handler);
  22. _WechatLogin(fp);
  23. #endif
  24. }
  25. public static void OnLoginResp(string code)
  26. {
  27. LoginView.ins?.OnWxLoginResp(code);
  28. }
  29. #if UNITY_IPHONE
  30. //这个方法就是引用在 xcode 中编写的方法,与 iOS 连接的桥梁
  31. [DllImport("__Internal")]
  32. private static extern void _WechatLogin(IntPtr resultHandler);
  33. [DllImport("__Internal")]
  34. private static extern bool IsWechatInstalled_iOS();
  35. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  36. public delegate void WXResultHandler(string resultString);
  37. [MonoPInvokeCallback(typeof(WXResultHandler))]
  38. static void resultHandler(string content)
  39. {
  40. string[] args = content.Split(';');
  41. switch (args[0])
  42. {
  43. case "0":
  44. //WXLoginState.OK
  45. //Debug.Log("token:" + args[1]);
  46. LoginView.ins?.OnWxLoginResp(args[1]);
  47. break;
  48. case "-2":
  49. //WXLoginState.Denied
  50. Debug.Log(args[1]);
  51. break;
  52. case "-4":
  53. //WXLoginState.Cancel
  54. Debug.Log(args[1]);
  55. break;
  56. default:
  57. //WXLoginState.Other
  58. Debug.Log(args[1]);
  59. break;
  60. }
  61. }
  62. public enum WXLoginState
  63. {
  64. OK,
  65. Denied = -2,
  66. Cancel = -4,
  67. Other = -1
  68. }
  69. #endif
  70. }