ShareSDK.cs 15 KB

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