ShareSDK.cs 14 KB

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