| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 | using System;using System.IO;using System.Text;using UnityEngine;using System.Reflection;using System.Collections;using System.Collections.Generic;namespace cn.sharesdk.unity3d {	public delegate void sendWXRequestToken(string uid, string token);	public delegate void sendWXRefreshToken(string token);	public class ShareSDK : MonoBehaviour	{		//版本号,每次发布新版本都需要更新		public static string version = "3.0.1";#if UNITY_IPHONE || UNITY_IOS		public string appKey = "3276d3e413040";		public string appSecret = "4280a3a6df667cfce37528dec03fd9c3";		public string mobNetLater = "2";		public string mobTwitterVer = "2";		public List<string> customAssociatedDomains = new List<string>();		public GetWXRequestTokenHanlerEvent wxRequestHandler;		public GetWXRefreshTokenHanlerEvent wxRefreshTokenHandler;		public GetShareCommandHanlerEvent shareCommandHandler;#endif		private int reqID;		public DevInfoSet devInfo;		public ShareSDKImpl sdk;		public EventHandler authHandler;		public EventHandler shareHandler;		public EventHandler showUserHandler;		public EventHandler getFriendsHandler;		public EventHandler followFriendHandler;		public EventHandler clientValidForAndroidHandler;		void Awake() {			Type type = devInfo.GetType();			Hashtable platformConfigs = new Hashtable();			FieldInfo[] devInfoFields = type.GetFields();			foreach (FieldInfo devInfoField in devInfoFields) {				DevInfo info = (DevInfo) devInfoField.GetValue(devInfo);				int platformId = (int)info.GetType().GetField("type").GetValue(info);				FieldInfo[] fields = info.GetType().GetFields();				Hashtable table = new Hashtable();				foreach (FieldInfo field in fields) {					if ("type".EndsWith(field.Name)) {						continue;					} else if ("Enable".EndsWith(field.Name) || "ShareByAppClient".EndsWith(field.Name) || "BypassApproval".EndsWith(field.Name) || "WithShareTicket".EndsWith(field.Name)) {						table.Add(field.Name, Convert.ToString(field.GetValue(info)).ToLower());					} else {						table.Add(field.Name, Convert.ToString(field.GetValue(info)));					}				}				platformConfigs.Add(platformId, table);			}#if UNITY_ANDROID			sdk = new AndroidImpl(gameObject);			sdk.PrepareLoopShare();			sdk.setChannelId();#elif UNITY_IPHONE			sdk = new iOSImpl(gameObject);#endif			sdk.SetPlatformConfig(platformConfigs);		}		public delegate void EventHandler(int reqID, ResponseState state, PlatformType type, Hashtable data);		private void _Callback(string data) {			if (data == null) {				return;			}			Hashtable res = (Hashtable)MiniJSON.jsonDecode(data);			if (res == null || res.Count <= 0) {				return;			}			int status = Convert.ToInt32(res["status"]);			int reqID = Convert.ToInt32(res["reqID"]);			PlatformType platform = (PlatformType)Convert.ToInt32(res["platform"]);			int action = Convert.ToInt32(res["action"]);			switch(status) {				case 1: {					Console.WriteLine(data);					Hashtable resp = (Hashtable)res["res"];					OnComplete(reqID, platform, action, resp);					break;				}				case 2: {					Console.WriteLine(data);					Hashtable throwable = (Hashtable)res["res"];					OnError(reqID, platform, action, throwable);					break;				}				case 3: {					OnCancel(reqID, platform, action);					break;				}			}		}		public void OnError(int reqID, PlatformType platform, int action, Hashtable throwable) {			switch (action) {				case 1: {					if (authHandler != null) {						authHandler(reqID, ResponseState.Fail, platform, throwable);					}					break;				}				case 2: {					if (getFriendsHandler != null) {						getFriendsHandler(reqID, ResponseState.Fail, platform, throwable);					}					break;				}				case 6: {					if (followFriendHandler != null) {						followFriendHandler(reqID, ResponseState.Fail, platform, throwable);					}					break;				}				case 9: {					if (shareHandler != null) {						shareHandler(reqID, ResponseState.Fail, platform, throwable);					}					break;				}				case 8: {					if (showUserHandler != null) {						showUserHandler(reqID, ResponseState.Fail, platform, throwable);					}					break;				}			}		}		public void OnComplete(int reqID, PlatformType platform, int action, Hashtable res) {			switch (action) {				case 1: {					if (authHandler != null) {						authHandler(reqID, ResponseState.Success, platform, res);					}					break;				}				case 2: {					if (getFriendsHandler != null) {						getFriendsHandler(reqID, ResponseState.Success, platform, res);					}					break;				}				case 6: {					if (followFriendHandler != null) {						followFriendHandler(reqID, ResponseState.Success, platform, res);					}					break;				}				case 9: {					if (shareHandler != null) {						shareHandler(reqID, ResponseState.Success, platform, res);					}					break;				}				case 8: {					if (showUserHandler != null) {						showUserHandler(reqID, ResponseState.Success, platform, res);					}					break;				}				case 12: {                    if (clientValidForAndroidHandler != null) {                        clientValidForAndroidHandler(reqID,ResponseState.Success,platform, res);                    }                    break;                }#if UNITY_IPHONE				case 11: {					shareCommandHandler(res);					break;				}				case 10: {					int isRefresh = Convert.ToInt32(res["isRefreshToken"]);					if (isRefresh == 1) {						String uid = Convert.ToString(res["uid"]);						wxRefreshTokenHandler(uid, sendWXRefreshTokenMethod);					} else {						String authCode = Convert.ToString(res["authCode"]);						wxRequestHandler(authCode, sendWXRequestTokenMehtod);					}					break;				}#endif			}		}		public void OnCancel(int reqID, PlatformType platform, int action) {			switch (action) {				case 1: {					if (authHandler != null) {						authHandler(reqID, ResponseState.Cancel, platform, null);					}					break;				}				case 2: {					if (getFriendsHandler != null) {						getFriendsHandler(reqID, ResponseState.Cancel, platform, null);					}					break;				}				case 6: {					if (followFriendHandler != null) {						followFriendHandler(reqID, ResponseState.Cancel, platform, null);					}					break;				}				case 9: {					if (shareHandler != null) {						shareHandler(reqID, ResponseState.Cancel, platform, null);					}					break;				}				case 8: {					if (showUserHandler != null) {						showUserHandler(reqID, ResponseState.Cancel, platform, null);					}					break;				}			}		}		public void InitSDK(string appKey) {			//shareSDKUtils.InitSDK (appKey);		}		public void InitSDK(string appKey, string appSecret) {			//shareSDKUtils.InitSDK (appKey,appSecret);		}		public void SetPlatformConfig(Hashtable configInfo) {			sdk.SetPlatformConfig(configInfo);		}		public int Authorize(PlatformType platform) {			reqID ++;			sdk.Authorize(reqID, platform);			return reqID;		}		public void CancelAuthorize(PlatformType platform) {			sdk.CancelAuthorize(platform);		}		public bool IsAuthorized(PlatformType platform) {			return sdk.IsAuthorized(platform);		}		public bool IsClientValid(PlatformType platform) {			return sdk.IsClientValid(platform);		}		public int GetUserInfo(PlatformType platform) {			reqID ++;			sdk.GetUserInfo(reqID, platform);			return reqID;		}		public int ShareContent(PlatformType platform, ShareContent content) {			reqID ++;			sdk.ShareContent(reqID, platform, content);			return reqID;		}		public int ShareContent(PlatformType[] platforms, ShareContent content) {			reqID ++;			sdk.ShareContent(reqID, platforms, content);			return reqID;		}		public int ShareContentWithActivity(PlatformType platform, ShareContent content) {			reqID++;#if UNITY_IPHONE			sdk.ShareContentWithActivity(reqID, platform, content);#endif			return reqID;		}		public int ShowPlatformList(PlatformType[] platforms, ShareContent content, int x, int y) {			reqID++;			sdk.ShowPlatformList(reqID, platforms, content, x, y);			return reqID;		}		public int ShowShareContentEditor(PlatformType platform, ShareContent content) {			reqID++;			sdk.ShowShareContentEditor(reqID, platform, content);			return reqID;		}		public int ShareWithContentName(PlatformType platform, string contentName, Hashtable customFields) {			reqID++;			sdk.ShareWithContentName(reqID, platform, contentName, customFields);			return reqID;		}		public int ShowPlatformListWithContentName(string contentName, Hashtable customFields, PlatformType[] platforms, int x, int y) {			reqID++;			sdk.ShowPlatformListWithContentName(reqID, contentName, customFields, platforms, x, y);			return reqID;		}		public int ShowShareContentEditorWithContentName(PlatformType platform, string contentName, Hashtable customFields) {			reqID++;			sdk.ShowShareContentEditorWithContentName(reqID, platform, contentName, customFields);			return reqID;		}		public int GetFriendList(PlatformType platform, int count, int page) {			reqID++;			sdk.GetFriendList(reqID, platform, count, page);			return reqID;		}		public int AddFriend(PlatformType platform, string account) {			reqID++;			sdk.AddFriend(reqID, platform, account);			return reqID;		}		public Hashtable GetAuthInfo(PlatformType platform) {			return sdk.GetAuthInfo(platform);		}		public void DisableSSO(bool open) {			sdk.DisableSSO(open);		}		public void openMiniProgram(string userName, string path, int miniProgramType) {			sdk.openMiniProgram(userName, path, miniProgramType);		}		public int isClientValidForAndroid(PlatformType platform) {         	reqID++;#if UNITY_ANDROID         	sdk.isClientValidForAndroid(reqID,platform);#endif         	         	return reqID;       	}#if UNITY_IPHONE || UNITY_IOS		public delegate void GetWXRequestTokenHanlerEvent(string authCode, sendWXRequestToken send);		public delegate void GetWXRefreshTokenHanlerEvent(string authCode, sendWXRefreshToken send);		public delegate void GetShareCommandHanlerEvent(Hashtable data);		public void ShareWithCommand(Hashtable customFields) {			sdk.shareSDKWithCommand(customFields);		}#endif		public void getWXRequestToken() {			sdk.getWXRequestToken();		}		public void getWXRefreshToken() {			sdk.getWXRefreshToken();		}		public void sendWXRequestTokenMehtod(string uid, string token) {			sdk.sendWXRequestToken(uid, token);		}		public void sendWXRefreshTokenMethod(string token) {			sdk.sendWXRefreshToken(token);		}	}}
 |