ShareSDK.cs 15 KB

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