123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562 |
- using UnityEngine;
- using System.Collections;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using System.Reflection;
- namespace cn.sharesdk.unity3d
- {
- /// <summary>
- /// ShareSDK.
- /// </summary>
- public class ShareSDK : MonoBehaviour
- {
- public static string MobAppKey = "1b17e87d222c1";
- public static string WeChatAppKey = "wx47450ab76d65a1b3";
- public static string WeChatAppSecrete = "a3528b865b9c4db3f76400983b2bd72f";
- public static string QQAppKey = "1106316278";
- public static string QQAppSecrete = "MwrdToADs0yzeqxD";
- public static string QQZoneAppKey = "1106316278";
- public static string QQZoneAppSecreteForIOS = "MwrdToADs0yzeqxD";
- public static string QQZoneAppSecreteForAndroid = "MwrdToADs0yzeqxD";
- public static string SinaAppKey = "568898243";
- public static string SinaAppSecrete = "38a4f8204cc784f81f9f0daaf31e02e3";
- public static string FacebookAppKey = "138557386743472";
- public static string FacebookAppSecrete = "ad4f8d51485a5af6a1a48d011d08bb89";
- private int reqID;
- public DevInfoSet devInfo;
- public ShareSDKImpl shareSDKUtils;
- public EventHandler authHandler;
- public EventHandler shareHandler;
- public EventHandler showUserHandler;
- public EventHandler getFriendsHandler;
- public EventHandler followFriendHandler;
- void Awake()
- {
-
- }
- public void Initialize()
- {
- devInfo = new DevInfoSet();
- 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))
- {
- 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
- shareSDKUtils = new AndroidImpl(gameObject);
- #elif UNITY_IPHONE
- shareSDKUtils = new iOSImpl(gameObject);
- #endif
- shareSDKUtils.InitSDK(MobAppKey);
- shareSDKUtils.SetPlatformConfig(platformConfigs);
- }
- /// <summary>
- /// callback the specified data.
- /// </summary>
- /// <param name='data'>
- /// Data.
- /// </param>
- private void _Callback (string data)
- {
- if (data == null)
- {
- return;
- }
-
- Hashtable res = (Hashtable) ShareSDKMiniJSON.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"]);
- // Success = 1, Fail = 2, Cancel = 3
- 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;
- }
- }
- }
- /// <summary>
- /// Raises the error event.
- /// </summary>
- /// <param name="platform">Platform.</param>
- /// <param name="action">Action.</param>
- /// <param name="throwable">Throwable.</param>
- public void OnError (int reqID, PlatformType platform, int action, Hashtable throwable)
- {
- switch (action)
- {
- case 1:
- { // 1 == Platform.ACTION_AUTHORIZING
- if (authHandler != null)
- {
- authHandler(reqID, ResponseState.Fail, platform, throwable);
- }
- break;
- }
- case 2:
- { //2 == Platform.ACTION_GETTING_FRIEND_LIST
- if (getFriendsHandler != null)
- {
- getFriendsHandler(reqID, ResponseState.Fail, platform, throwable);
- }
- break;
- }
- case 6:
- { //6 == Platform.ACTION_FOLLOWING_USER
- if (followFriendHandler != null)
- {
- followFriendHandler(reqID, ResponseState.Fail, platform, throwable);
- }
- break;
- }
- case 8:
- { // 8 == Platform.ACTION_USER_INFOR
- if (showUserHandler != null)
- {
- showUserHandler(reqID, ResponseState.Fail, platform, throwable);
- }
- break;
- }
- case 9:
- { // 9 == Platform.ACTION_SHARE
- if (shareHandler != null)
- {
- shareHandler(reqID, ResponseState.Fail, platform, throwable);
- }
- break;
- }
- }
- }
- /// <summary>
- /// Raises the success event.
- /// </summary>
- /// <param name="platform">Platform.</param>
- /// <param name="action">Action.</param>
- /// <param name="res">Res.</param>
- public void OnComplete (int reqID, PlatformType platform, int action, Hashtable res)
- {
- switch (action)
- {
- case 1:
- { // 1 == Platform.ACTION_AUTHORIZING
- if (authHandler != null)
- {
- authHandler(reqID, ResponseState.Success, platform, null);
- }
- break;
- }
- case 2:
- { //2 == Platform.ACTION_GETTING_FRIEND_LIST
- if (getFriendsHandler != null)
- {
- getFriendsHandler(reqID, ResponseState.Success, platform, res);
- }
- break;
- }
- case 6:
- { //6 == Platform.ACTION_FOLLOWING_USER
- if (followFriendHandler != null)
- {
- followFriendHandler(reqID, ResponseState.Success, platform, res);
- }
- break;
- }
- case 8:
- { // 8 == Platform.ACTION_USER_INFOR
- if (showUserHandler != null)
- {
- showUserHandler(reqID, ResponseState.Success, platform, res);
- }
- break;
- }
- case 9:
- { // 9 == Platform.ACTION_SHARE
- if (shareHandler != null)
- {
- shareHandler(reqID, ResponseState.Success, platform, res);
- }
- break;
- }
- }
- }
- /// <summary>
- /// Raises the cancel event.
- /// </summary>
- /// <param name="platform">Platform.</param>
- /// <param name="action">Action.</param>
- public void OnCancel (int reqID, PlatformType platform, int action)
- {
- switch (action)
- {
- case 1:
- { // 1 == Platform.ACTION_AUTHORIZING
- if (authHandler != null)
- {
- authHandler(reqID, ResponseState.Cancel, platform, null);
- }
- break;
- }
- case 2:
- { //2 == Platform.ACTION_GETTING_FRIEND_LIST
- if (getFriendsHandler != null)
- {
- getFriendsHandler(reqID, ResponseState.Cancel, platform, null);
- }
- break;
- }
- case 6:
- { //6 == Platform.ACTION_FOLLOWING_USER
- if (followFriendHandler != null)
- {
- followFriendHandler(reqID, ResponseState.Cancel, platform, null);
- }
- break;
- }
- case 8:
- { // 8 == Platform.ACTION_USER_INFOR
- if (showUserHandler != null)
- {
- showUserHandler(reqID, ResponseState.Cancel, platform, null);
- }
- break;
- }
- case 9:
- { // 9 == Platform.ACTION_SHARE
- if (shareHandler != null)
- {
- shareHandler(reqID, ResponseState.Cancel, platform, null);
- }
- break;
- }
- }
- }
-
- /// <summary>
- /// init the ShareSDK.
- /// </summary>
- public void InitSDK (String appKey)
- {
- // if you don't add ShareSDK.xml in your assets folder, use the following line
- shareSDKUtils.InitSDK (appKey);
- }
- /// <summary>
- /// Sets the platform config.
- /// </summary>
- public void SetPlatformConfig (Hashtable configInfo)
- {
- shareSDKUtils.SetPlatformConfig (configInfo);
- }
-
- /// <summary>
- /// Authorize the specified type, observer and resultHandler.
- /// </summary>
- /// <param name='type'>
- /// Type.
- /// </param>
- /// <param name='observer'>
- /// Observer.
- /// </param>
- /// <param name='resultHandler'>
- /// Result handler.
- /// </param>
- public int Authorize (PlatformType platform)
- {
- reqID ++;
- shareSDKUtils.Authorize(reqID, platform);
- return reqID;
- }
-
- /// <summary>
- /// Cancel authorized
- /// </summary>
- /// <param name='type'>
- /// Type.
- /// </param>
- public void CancelAuthorize (PlatformType platform)
- {
- shareSDKUtils.CancelAuthorize(platform);
- }
-
- /// <summary>
- /// Has authorized
- /// </summary>
- /// <returns>
- /// true has authorized, otherwise not authorized.
- /// </returns>
- /// <param name='type'>
- /// Type.
- /// </param>
- public bool IsAuthorized (PlatformType platform)
- {
- return shareSDKUtils.IsAuthorized(platform);
- }
- public bool IsClientValid (PlatformType platform)
- {
- return shareSDKUtils.IsClientValid(platform);
- }
-
- /// <summary>
- /// Gets the user info.
- /// </summary>
- /// <param name='type'>
- /// Type.
- /// </param>
- /// <param name='callback'>
- /// Callback.
- /// </param>
- public int GetUserInfo (PlatformType platform)
- {
- reqID ++;
- shareSDKUtils.GetUserInfo(reqID, platform);
- return reqID;
- }
- /// <summary>
- /// Shares the content.
- /// </summary>
- /// <param name='type'>
- /// Type.
- /// </param>
- /// <param name='content'>
- /// Content.
- /// </param>
- /// <param name='resultHandler'>
- /// Callback.
- /// </param>
- public int ShareContent(PlatformType platform, ShareContent content)
- {
- reqID ++;
- shareSDKUtils.ShareContent(reqID, platform, content);
- return reqID;
- }
- /// <summary>
- /// Shares the content.
- /// </summary>
- /// <param name='type'>
- /// Type.
- /// </param>
- /// <param name='content'>
- /// Content.
- /// </param>
- /// <param name='resultHandler'>
- /// Callback.
- /// </param>
- public int ShareContent(PlatformType[] platforms, ShareContent content)
- {
- reqID ++;
- shareSDKUtils.ShareContent(reqID, platforms, content);
- return reqID;
- }
-
- /// <summary>
- /// Shows the share menu of using onekeyshare.
- /// </summary>
- /// <param name='types'>
- /// Types.
- /// </param>
- /// <param name='content'>
- /// Content.
- /// </param>
- /// <param name='callback'>
- /// Callback.
- /// </param>
- public int ShowPlatformList (PlatformType[] platforms, ShareContent content, int x, int y)
- {
- reqID ++;
- shareSDKUtils.ShowPlatformList(reqID, platforms, content, x, y);
- return reqID;
- }
-
- /// <summary>
- /// Shows the share view of using onekeyshare.
- /// </summary>
- /// <param name='type'>
- /// Type.
- /// </param>
- /// <param name='content'>
- /// Content.
- /// </param>
- /// <param name='callback'>
- /// Callback.
- /// </param>
- public int ShowShareContentEditor (PlatformType platform, ShareContent content)
- {
- reqID ++;
- shareSDKUtils.ShowShareContentEditor(reqID, platform, content);
- return reqID;
- }
- /// <summary>
- /// share according to the name of node<Content> in ShareContent.xml(you can find it in Xcode) [only valid in iOS temporarily][此接口暂时仅支持iOS环境]
- /// </summary>
- /// <param name='platform'>
- /// Platform Type
- /// </param>
- /// <param name='contentName'>
- /// the name of node<Content> in ShareContent.xml file
- /// </param>
- /// <param name='customFields'>
- /// your share customFields which will be replace in ShareContent.xml
- /// </param>
- public int ShareWithContentName (PlatformType platform, string contentName, Hashtable customFields)
- {
- reqID++;
- shareSDKUtils.ShareWithContentName (reqID, platform, contentName, customFields);
- return reqID;
- }
- /// <summary>
- /// share according to the name of node<Content> in ShareContent.xml(you can find it in Xcode) (only valid in iOS temporarily)(此接口暂时仅支持iOS环境)
- /// </summary>
- /// </param>
- /// <param name='contentName'>
- /// the name of node<Content> in ShareContent.xml file
- /// </param>
- /// <param name='customFields'>
- /// your share customFields which will be replace in ShareContent.xml
- /// </param>
- /// <param name='platforms'>
- /// Platform Types
- /// </param>
- /// <param name='x','y'>
- /// the coordinates of the share menu
- /// </param>
- public int ShowPlatformListWithContentName (string contentName, Hashtable customFields, PlatformType[] platforms, int x, int y)
- {
- reqID++;
- shareSDKUtils.ShowPlatformListWithContentName (reqID, contentName, customFields, platforms, x, y);
- return reqID;
- }
- /// <summary>
- /// share according to the name of node<Content> in ShareContent.xml file (only valid in iOS temporarily)(此接口暂时仅支持iOS环境)
- /// </summary>
- /// <param name='platform'>
- /// Platform Type
- /// </param>
- /// <param name='contentName'>
- /// the name of node<Content> in ShareContent.xml file
- /// </param>
- /// <param name='customFields'>
- /// your share customFields which will be replace in ShareContent.xml
- /// </param>
- public int ShowShareContentEditorWithContentName (PlatformType platform, string contentName, Hashtable customFields)
- {
- reqID++;
- shareSDKUtils.ShowShareContentEditorWithContentName (reqID, platform, contentName, customFields);
- return reqID;
- }
- /// <summary>
- /// Gets the friends.
- /// </summary>
- /// <param name="type">Type.</param>
- /// <param name="count">Count.</param>
- /// <param name="page">Page.</param>
- public int GetFriendList (PlatformType platform, int count, int page)
- {
- reqID ++;
- shareSDKUtils.GetFriendList (reqID, platform, count, page);
- return reqID;
- }
- /// <summary>
- /// Follows the friend.
- /// </summary>
- /// <param name="type">Type.</param>
- /// <param name="account">Account.</param>
- public int AddFriend (PlatformType platform, String account)
- {
- reqID ++;
- shareSDKUtils.AddFriend (reqID, platform, account);
- return reqID;
- }
- /// <summary>
- /// Gets the auth info.
- /// </summary>
- /// <param name="type">Type.</param>
- public Hashtable GetAuthInfo (PlatformType platform)
- {
- return shareSDKUtils.GetAuthInfo (platform);
- }
- /// <summary>
- /// Close the SSO when authorize.
- /// </summary>
- /// <param name="open">If set to <c>true</c> open.</param>
- public void DisableSSO(Boolean open)
- {
- shareSDKUtils.DisableSSO (open);
- }
-
- /// <summary>
- /// Event result listener.
- /// </summary>
- public delegate void EventHandler (int reqID, ResponseState state, PlatformType type, Hashtable data);
- }
- }
|