ShareSDK.cs 13 KB

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