ShareSDK.cs 15 KB

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