ShareSDK.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Text;
  7. using System.Reflection;
  8. namespace cn.sharesdk.unity3d
  9. {
  10. /// <summary>
  11. /// ShareSDK.
  12. /// </summary>
  13. public class ShareSDK : MonoBehaviour
  14. {
  15. public static string MobAppKey = "1b17e87d222c1";
  16. public static string QQAppKey = "100371282";
  17. public static string QQAppSecrete = "aed9b0303e3ed1e27bae87c33761161d";
  18. public static string QQZoneAppKey = "100371282";
  19. public static string QQZoneAppSecreteForIOS = "aed9b0303e3ed1e27bae87c33761161d";
  20. public static string QQZoneAppSecreteForAndroid = "ae36f4ee3946e1cbb98d6965b0b2ff5c";
  21. public static string SinaAppKey = "568898243";
  22. public static string SinaAppSecrete = "38a4f8204cc784f81f9f0daaf31e02e3";
  23. public static string FacebookAppKey = "107704292745179";
  24. public static string FacebookAppSecrete = "38053202e1a5fe26c80c753071f0b573";
  25. private int reqID;
  26. public DevInfoSet devInfo;
  27. public ShareSDKImpl shareSDKUtils;
  28. public EventHandler authHandler;
  29. public EventHandler shareHandler;
  30. public EventHandler showUserHandler;
  31. public EventHandler getFriendsHandler;
  32. public EventHandler followFriendHandler;
  33. void Awake()
  34. {
  35. }
  36. public void Initialize()
  37. {
  38. devInfo = new DevInfoSet();
  39. Type type = devInfo.GetType();
  40. Hashtable platformConfigs = new Hashtable();
  41. FieldInfo[] devInfoFields = type.GetFields();
  42. foreach (FieldInfo devInfoField in devInfoFields)
  43. {
  44. DevInfo info = (DevInfo)devInfoField.GetValue(devInfo);
  45. int platformId = (int)info.GetType().GetField("type").GetValue(info);
  46. FieldInfo[] fields = info.GetType().GetFields();
  47. Hashtable table = new Hashtable();
  48. foreach (FieldInfo field in fields)
  49. {
  50. if ("type".EndsWith(field.Name))
  51. {
  52. continue;
  53. }
  54. else if ("Enable".EndsWith(field.Name) || "ShareByAppClient".EndsWith(field.Name) || "BypassApproval".EndsWith(field.Name))
  55. {
  56. table.Add(field.Name, Convert.ToString(field.GetValue(info)).ToLower());
  57. }
  58. else
  59. {
  60. table.Add(field.Name, Convert.ToString(field.GetValue(info)));
  61. }
  62. }
  63. platformConfigs.Add(platformId, table);
  64. }
  65. #if UNITY_ANDROID
  66. shareSDKUtils = new AndroidImpl(gameObject);
  67. #elif UNITY_IPHONE
  68. shareSDKUtils = new iOSImpl(gameObject);
  69. #endif
  70. shareSDKUtils.InitSDK(MobAppKey);
  71. shareSDKUtils.SetPlatformConfig(platformConfigs);
  72. }
  73. /// <summary>
  74. /// callback the specified data.
  75. /// </summary>
  76. /// <param name='data'>
  77. /// Data.
  78. /// </param>
  79. private void _Callback (string data)
  80. {
  81. if (data == null)
  82. {
  83. return;
  84. }
  85. Hashtable res = (Hashtable) ShareSDKMiniJSON.jsonDecode(data);
  86. if (res == null || res.Count <= 0)
  87. {
  88. return;
  89. }
  90. int status = Convert.ToInt32(res["status"]);
  91. int reqID = Convert.ToInt32(res["reqID"]);
  92. PlatformType platform = (PlatformType)Convert.ToInt32(res["platform"]);
  93. int action = Convert.ToInt32(res["action"]);
  94. // Success = 1, Fail = 2, Cancel = 3
  95. switch(status)
  96. {
  97. case 1:
  98. {
  99. Console.WriteLine(data);
  100. Hashtable resp = (Hashtable) res["res"];
  101. OnComplete(reqID, platform, action, resp);
  102. break;
  103. }
  104. case 2:
  105. {
  106. Console.WriteLine(data);
  107. Hashtable throwable = (Hashtable) res["res"];
  108. OnError(reqID, platform, action, throwable);
  109. break;
  110. }
  111. case 3:
  112. {
  113. OnCancel(reqID, platform, action);
  114. break;
  115. }
  116. }
  117. }
  118. /// <summary>
  119. /// Raises the error event.
  120. /// </summary>
  121. /// <param name="platform">Platform.</param>
  122. /// <param name="action">Action.</param>
  123. /// <param name="throwable">Throwable.</param>
  124. public void OnError (int reqID, PlatformType platform, int action, Hashtable throwable)
  125. {
  126. switch (action)
  127. {
  128. case 1:
  129. { // 1 == Platform.ACTION_AUTHORIZING
  130. if (authHandler != null)
  131. {
  132. authHandler(reqID, ResponseState.Fail, platform, throwable);
  133. }
  134. break;
  135. }
  136. case 2:
  137. { //2 == Platform.ACTION_GETTING_FRIEND_LIST
  138. if (getFriendsHandler != null)
  139. {
  140. getFriendsHandler(reqID, ResponseState.Fail, platform, throwable);
  141. }
  142. break;
  143. }
  144. case 6:
  145. { //6 == Platform.ACTION_FOLLOWING_USER
  146. if (followFriendHandler != null)
  147. {
  148. followFriendHandler(reqID, ResponseState.Fail, platform, throwable);
  149. }
  150. break;
  151. }
  152. case 8:
  153. { // 8 == Platform.ACTION_USER_INFOR
  154. if (showUserHandler != null)
  155. {
  156. showUserHandler(reqID, ResponseState.Fail, platform, throwable);
  157. }
  158. break;
  159. }
  160. case 9:
  161. { // 9 == Platform.ACTION_SHARE
  162. if (shareHandler != null)
  163. {
  164. shareHandler(reqID, ResponseState.Fail, platform, throwable);
  165. }
  166. break;
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// Raises the success event.
  172. /// </summary>
  173. /// <param name="platform">Platform.</param>
  174. /// <param name="action">Action.</param>
  175. /// <param name="res">Res.</param>
  176. public void OnComplete (int reqID, PlatformType platform, int action, Hashtable res)
  177. {
  178. switch (action)
  179. {
  180. case 1:
  181. { // 1 == Platform.ACTION_AUTHORIZING
  182. if (authHandler != null)
  183. {
  184. authHandler(reqID, ResponseState.Success, platform, null);
  185. }
  186. break;
  187. }
  188. case 2:
  189. { //2 == Platform.ACTION_GETTING_FRIEND_LIST
  190. if (getFriendsHandler != null)
  191. {
  192. getFriendsHandler(reqID, ResponseState.Success, platform, res);
  193. }
  194. break;
  195. }
  196. case 6:
  197. { //6 == Platform.ACTION_FOLLOWING_USER
  198. if (followFriendHandler != null)
  199. {
  200. followFriendHandler(reqID, ResponseState.Success, platform, res);
  201. }
  202. break;
  203. }
  204. case 8:
  205. { // 8 == Platform.ACTION_USER_INFOR
  206. if (showUserHandler != null)
  207. {
  208. showUserHandler(reqID, ResponseState.Success, platform, res);
  209. }
  210. break;
  211. }
  212. case 9:
  213. { // 9 == Platform.ACTION_SHARE
  214. if (shareHandler != null)
  215. {
  216. shareHandler(reqID, ResponseState.Success, platform, res);
  217. }
  218. break;
  219. }
  220. }
  221. }
  222. /// <summary>
  223. /// Raises the cancel event.
  224. /// </summary>
  225. /// <param name="platform">Platform.</param>
  226. /// <param name="action">Action.</param>
  227. public void OnCancel (int reqID, PlatformType platform, int action)
  228. {
  229. switch (action)
  230. {
  231. case 1:
  232. { // 1 == Platform.ACTION_AUTHORIZING
  233. if (authHandler != null)
  234. {
  235. authHandler(reqID, ResponseState.Cancel, platform, null);
  236. }
  237. break;
  238. }
  239. case 2:
  240. { //2 == Platform.ACTION_GETTING_FRIEND_LIST
  241. if (getFriendsHandler != null)
  242. {
  243. getFriendsHandler(reqID, ResponseState.Cancel, platform, null);
  244. }
  245. break;
  246. }
  247. case 6:
  248. { //6 == Platform.ACTION_FOLLOWING_USER
  249. if (followFriendHandler != null)
  250. {
  251. followFriendHandler(reqID, ResponseState.Cancel, platform, null);
  252. }
  253. break;
  254. }
  255. case 8:
  256. { // 8 == Platform.ACTION_USER_INFOR
  257. if (showUserHandler != null)
  258. {
  259. showUserHandler(reqID, ResponseState.Cancel, platform, null);
  260. }
  261. break;
  262. }
  263. case 9:
  264. { // 9 == Platform.ACTION_SHARE
  265. if (shareHandler != null)
  266. {
  267. shareHandler(reqID, ResponseState.Cancel, platform, null);
  268. }
  269. break;
  270. }
  271. }
  272. }
  273. /// <summary>
  274. /// init the ShareSDK.
  275. /// </summary>
  276. public void InitSDK (String appKey)
  277. {
  278. // if you don't add ShareSDK.xml in your assets folder, use the following line
  279. shareSDKUtils.InitSDK (appKey);
  280. }
  281. /// <summary>
  282. /// Sets the platform config.
  283. /// </summary>
  284. public void SetPlatformConfig (Hashtable configInfo)
  285. {
  286. shareSDKUtils.SetPlatformConfig (configInfo);
  287. }
  288. /// <summary>
  289. /// Authorize the specified type, observer and resultHandler.
  290. /// </summary>
  291. /// <param name='type'>
  292. /// Type.
  293. /// </param>
  294. /// <param name='observer'>
  295. /// Observer.
  296. /// </param>
  297. /// <param name='resultHandler'>
  298. /// Result handler.
  299. /// </param>
  300. public int Authorize (PlatformType platform)
  301. {
  302. reqID ++;
  303. shareSDKUtils.Authorize(reqID, platform);
  304. return reqID;
  305. }
  306. /// <summary>
  307. /// Cancel authorized
  308. /// </summary>
  309. /// <param name='type'>
  310. /// Type.
  311. /// </param>
  312. public void CancelAuthorize (PlatformType platform)
  313. {
  314. shareSDKUtils.CancelAuthorize(platform);
  315. }
  316. /// <summary>
  317. /// Has authorized
  318. /// </summary>
  319. /// <returns>
  320. /// true has authorized, otherwise not authorized.
  321. /// </returns>
  322. /// <param name='type'>
  323. /// Type.
  324. /// </param>
  325. public bool IsAuthorized (PlatformType platform)
  326. {
  327. return shareSDKUtils.IsAuthorized(platform);
  328. }
  329. public bool IsClientValid (PlatformType platform)
  330. {
  331. return shareSDKUtils.IsClientValid(platform);
  332. }
  333. /// <summary>
  334. /// Gets the user info.
  335. /// </summary>
  336. /// <param name='type'>
  337. /// Type.
  338. /// </param>
  339. /// <param name='callback'>
  340. /// Callback.
  341. /// </param>
  342. public int GetUserInfo (PlatformType platform)
  343. {
  344. reqID ++;
  345. shareSDKUtils.GetUserInfo(reqID, platform);
  346. return reqID;
  347. }
  348. /// <summary>
  349. /// Shares the content.
  350. /// </summary>
  351. /// <param name='type'>
  352. /// Type.
  353. /// </param>
  354. /// <param name='content'>
  355. /// Content.
  356. /// </param>
  357. /// <param name='resultHandler'>
  358. /// Callback.
  359. /// </param>
  360. public int ShareContent(PlatformType platform, ShareContent content)
  361. {
  362. reqID ++;
  363. shareSDKUtils.ShareContent(reqID, platform, content);
  364. return reqID;
  365. }
  366. /// <summary>
  367. /// Shares the content.
  368. /// </summary>
  369. /// <param name='type'>
  370. /// Type.
  371. /// </param>
  372. /// <param name='content'>
  373. /// Content.
  374. /// </param>
  375. /// <param name='resultHandler'>
  376. /// Callback.
  377. /// </param>
  378. public int ShareContent(PlatformType[] platforms, ShareContent content)
  379. {
  380. reqID ++;
  381. shareSDKUtils.ShareContent(reqID, platforms, content);
  382. return reqID;
  383. }
  384. /// <summary>
  385. /// Shows the share menu of using onekeyshare.
  386. /// </summary>
  387. /// <param name='types'>
  388. /// Types.
  389. /// </param>
  390. /// <param name='content'>
  391. /// Content.
  392. /// </param>
  393. /// <param name='callback'>
  394. /// Callback.
  395. /// </param>
  396. public int ShowPlatformList (PlatformType[] platforms, ShareContent content, int x, int y)
  397. {
  398. reqID ++;
  399. shareSDKUtils.ShowPlatformList(reqID, platforms, content, x, y);
  400. return reqID;
  401. }
  402. /// <summary>
  403. /// Shows the share view of using onekeyshare.
  404. /// </summary>
  405. /// <param name='type'>
  406. /// Type.
  407. /// </param>
  408. /// <param name='content'>
  409. /// Content.
  410. /// </param>
  411. /// <param name='callback'>
  412. /// Callback.
  413. /// </param>
  414. public int ShowShareContentEditor (PlatformType platform, ShareContent content)
  415. {
  416. reqID ++;
  417. shareSDKUtils.ShowShareContentEditor(reqID, platform, content);
  418. return reqID;
  419. }
  420. /// <summary>
  421. /// share according to the name of node<Content> in ShareContent.xml(you can find it in Xcode) [only valid in iOS temporarily][此接口暂时仅支持iOS环境]
  422. /// </summary>
  423. /// <param name='platform'>
  424. /// Platform Type
  425. /// </param>
  426. /// <param name='contentName'>
  427. /// the name of node<Content> in ShareContent.xml file
  428. /// </param>
  429. /// <param name='customFields'>
  430. /// your share customFields which will be replace in ShareContent.xml
  431. /// </param>
  432. public int ShareWithContentName (PlatformType platform, string contentName, Hashtable customFields)
  433. {
  434. reqID++;
  435. shareSDKUtils.ShareWithContentName (reqID, platform, contentName, customFields);
  436. return reqID;
  437. }
  438. /// <summary>
  439. /// share according to the name of node<Content> in ShareContent.xml(you can find it in Xcode) (only valid in iOS temporarily)(此接口暂时仅支持iOS环境)
  440. /// </summary>
  441. /// </param>
  442. /// <param name='contentName'>
  443. /// the name of node<Content> in ShareContent.xml file
  444. /// </param>
  445. /// <param name='customFields'>
  446. /// your share customFields which will be replace in ShareContent.xml
  447. /// </param>
  448. /// <param name='platforms'>
  449. /// Platform Types
  450. /// </param>
  451. /// <param name='x','y'>
  452. /// the coordinates of the share menu
  453. /// </param>
  454. public int ShowPlatformListWithContentName (string contentName, Hashtable customFields, PlatformType[] platforms, int x, int y)
  455. {
  456. reqID++;
  457. shareSDKUtils.ShowPlatformListWithContentName (reqID, contentName, customFields, platforms, x, y);
  458. return reqID;
  459. }
  460. /// <summary>
  461. /// share according to the name of node<Content> in ShareContent.xml file (only valid in iOS temporarily)(此接口暂时仅支持iOS环境)
  462. /// </summary>
  463. /// <param name='platform'>
  464. /// Platform Type
  465. /// </param>
  466. /// <param name='contentName'>
  467. /// the name of node<Content> in ShareContent.xml file
  468. /// </param>
  469. /// <param name='customFields'>
  470. /// your share customFields which will be replace in ShareContent.xml
  471. /// </param>
  472. public int ShowShareContentEditorWithContentName (PlatformType platform, string contentName, Hashtable customFields)
  473. {
  474. reqID++;
  475. shareSDKUtils.ShowShareContentEditorWithContentName (reqID, platform, contentName, customFields);
  476. return reqID;
  477. }
  478. /// <summary>
  479. /// Gets the friends.
  480. /// </summary>
  481. /// <param name="type">Type.</param>
  482. /// <param name="count">Count.</param>
  483. /// <param name="page">Page.</param>
  484. public int GetFriendList (PlatformType platform, int count, int page)
  485. {
  486. reqID ++;
  487. shareSDKUtils.GetFriendList (reqID, platform, count, page);
  488. return reqID;
  489. }
  490. /// <summary>
  491. /// Follows the friend.
  492. /// </summary>
  493. /// <param name="type">Type.</param>
  494. /// <param name="account">Account.</param>
  495. public int AddFriend (PlatformType platform, String account)
  496. {
  497. reqID ++;
  498. shareSDKUtils.AddFriend (reqID, platform, account);
  499. return reqID;
  500. }
  501. /// <summary>
  502. /// Gets the auth info.
  503. /// </summary>
  504. /// <param name="type">Type.</param>
  505. public Hashtable GetAuthInfo (PlatformType platform)
  506. {
  507. return shareSDKUtils.GetAuthInfo (platform);
  508. }
  509. /// <summary>
  510. /// Close the SSO when authorize.
  511. /// </summary>
  512. /// <param name="open">If set to <c>true</c> open.</param>
  513. public void DisableSSO(Boolean open)
  514. {
  515. shareSDKUtils.DisableSSO (open);
  516. }
  517. /// <summary>
  518. /// Event result listener.
  519. /// </summary>
  520. public delegate void EventHandler (int reqID, ResponseState state, PlatformType type, Hashtable data);
  521. }
  522. }