ShareSDK.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using UnityEngine;
  5. using System.Reflection;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. namespace cn.sharesdk.unity3d {
  9. public delegate void sendWXRequestToken(string uid, string token);
  10. public delegate void sendWXRefreshToken(string token);
  11. public class ShareSDK : MonoBehaviour
  12. {
  13. //版本号,每次发布新版本都需要更新
  14. public static string version = "3.0.1";
  15. #if UNITY_IPHONE || UNITY_IOS
  16. public string appKey = "3276d3e413040";
  17. public string appSecret = "4280a3a6df667cfce37528dec03fd9c3";
  18. public string mobNetLater = "2";
  19. public string mobTwitterVer = "2";
  20. public List<string> customAssociatedDomains = new List<string>();
  21. public GetWXRequestTokenHanlerEvent wxRequestHandler;
  22. public GetWXRefreshTokenHanlerEvent wxRefreshTokenHandler;
  23. public GetShareCommandHanlerEvent shareCommandHandler;
  24. #endif
  25. private int reqID;
  26. public DevInfoSet devInfo;
  27. public ShareSDKImpl sdk;
  28. public EventHandler authHandler;
  29. public EventHandler shareHandler;
  30. public EventHandler showUserHandler;
  31. public EventHandler getFriendsHandler;
  32. public EventHandler followFriendHandler;
  33. public EventHandler clientValidForAndroidHandler;
  34. void Awake() {
  35. Type type = devInfo.GetType();
  36. Hashtable platformConfigs = new Hashtable();
  37. FieldInfo[] devInfoFields = type.GetFields();
  38. foreach (FieldInfo devInfoField in devInfoFields) {
  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. if ("type".EndsWith(field.Name)) {
  45. continue;
  46. } else if ("Enable".EndsWith(field.Name) || "ShareByAppClient".EndsWith(field.Name) || "BypassApproval".EndsWith(field.Name) || "WithShareTicket".EndsWith(field.Name)) {
  47. table.Add(field.Name, Convert.ToString(field.GetValue(info)).ToLower());
  48. } else {
  49. table.Add(field.Name, Convert.ToString(field.GetValue(info)));
  50. }
  51. }
  52. platformConfigs.Add(platformId, table);
  53. }
  54. #if UNITY_ANDROID
  55. sdk = new AndroidImpl(gameObject);
  56. sdk.PrepareLoopShare();
  57. sdk.setChannelId();
  58. #elif UNITY_IPHONE
  59. sdk = new iOSImpl(gameObject);
  60. #endif
  61. sdk.SetPlatformConfig(platformConfigs);
  62. }
  63. public delegate void EventHandler(int reqID, ResponseState state, PlatformType type, Hashtable data);
  64. private void _Callback(string data) {
  65. if (data == null) {
  66. return;
  67. }
  68. Hashtable res = (Hashtable)MiniJSON.jsonDecode(data);
  69. if (res == null || res.Count <= 0) {
  70. return;
  71. }
  72. int status = Convert.ToInt32(res["status"]);
  73. int reqID = Convert.ToInt32(res["reqID"]);
  74. PlatformType platform = (PlatformType)Convert.ToInt32(res["platform"]);
  75. int action = Convert.ToInt32(res["action"]);
  76. switch(status) {
  77. case 1: {
  78. Console.WriteLine(data);
  79. Hashtable resp = (Hashtable)res["res"];
  80. OnComplete(reqID, platform, action, resp);
  81. break;
  82. }
  83. case 2: {
  84. Console.WriteLine(data);
  85. Hashtable throwable = (Hashtable)res["res"];
  86. OnError(reqID, platform, action, throwable);
  87. break;
  88. }
  89. case 3: {
  90. OnCancel(reqID, platform, action);
  91. break;
  92. }
  93. }
  94. }
  95. public void OnError(int reqID, PlatformType platform, int action, Hashtable throwable) {
  96. switch (action) {
  97. case 1: {
  98. if (authHandler != null) {
  99. authHandler(reqID, ResponseState.Fail, platform, throwable);
  100. }
  101. break;
  102. }
  103. case 2: {
  104. if (getFriendsHandler != null) {
  105. getFriendsHandler(reqID, ResponseState.Fail, platform, throwable);
  106. }
  107. break;
  108. }
  109. case 6: {
  110. if (followFriendHandler != null) {
  111. followFriendHandler(reqID, ResponseState.Fail, platform, throwable);
  112. }
  113. break;
  114. }
  115. case 9: {
  116. if (shareHandler != null) {
  117. shareHandler(reqID, ResponseState.Fail, platform, throwable);
  118. }
  119. break;
  120. }
  121. case 8: {
  122. if (showUserHandler != null) {
  123. showUserHandler(reqID, ResponseState.Fail, platform, throwable);
  124. }
  125. break;
  126. }
  127. }
  128. }
  129. public void OnComplete(int reqID, PlatformType platform, int action, Hashtable res) {
  130. switch (action) {
  131. case 1: {
  132. if (authHandler != null) {
  133. authHandler(reqID, ResponseState.Success, platform, res);
  134. }
  135. break;
  136. }
  137. case 2: {
  138. if (getFriendsHandler != null) {
  139. getFriendsHandler(reqID, ResponseState.Success, platform, res);
  140. }
  141. break;
  142. }
  143. case 6: {
  144. if (followFriendHandler != null) {
  145. followFriendHandler(reqID, ResponseState.Success, platform, res);
  146. }
  147. break;
  148. }
  149. case 9: {
  150. if (shareHandler != null) {
  151. shareHandler(reqID, ResponseState.Success, platform, res);
  152. }
  153. break;
  154. }
  155. case 8: {
  156. if (showUserHandler != null) {
  157. showUserHandler(reqID, ResponseState.Success, platform, res);
  158. }
  159. break;
  160. }
  161. case 12: {
  162. if (clientValidForAndroidHandler != null) {
  163. clientValidForAndroidHandler(reqID,ResponseState.Success,platform, res);
  164. }
  165. break;
  166. }
  167. #if UNITY_IPHONE
  168. case 11: {
  169. shareCommandHandler(res);
  170. break;
  171. }
  172. case 10: {
  173. int isRefresh = Convert.ToInt32(res["isRefreshToken"]);
  174. if (isRefresh == 1) {
  175. String uid = Convert.ToString(res["uid"]);
  176. wxRefreshTokenHandler(uid, sendWXRefreshTokenMethod);
  177. } else {
  178. String authCode = Convert.ToString(res["authCode"]);
  179. wxRequestHandler(authCode, sendWXRequestTokenMehtod);
  180. }
  181. break;
  182. }
  183. #endif
  184. }
  185. }
  186. public void OnCancel(int reqID, PlatformType platform, int action) {
  187. switch (action) {
  188. case 1: {
  189. if (authHandler != null) {
  190. authHandler(reqID, ResponseState.Cancel, platform, null);
  191. }
  192. break;
  193. }
  194. case 2: {
  195. if (getFriendsHandler != null) {
  196. getFriendsHandler(reqID, ResponseState.Cancel, platform, null);
  197. }
  198. break;
  199. }
  200. case 6: {
  201. if (followFriendHandler != null) {
  202. followFriendHandler(reqID, ResponseState.Cancel, platform, null);
  203. }
  204. break;
  205. }
  206. case 9: {
  207. if (shareHandler != null) {
  208. shareHandler(reqID, ResponseState.Cancel, platform, null);
  209. }
  210. break;
  211. }
  212. case 8: {
  213. if (showUserHandler != null) {
  214. showUserHandler(reqID, ResponseState.Cancel, platform, null);
  215. }
  216. break;
  217. }
  218. }
  219. }
  220. public void InitSDK(string appKey) {
  221. //shareSDKUtils.InitSDK (appKey);
  222. }
  223. public void InitSDK(string appKey, string appSecret) {
  224. //shareSDKUtils.InitSDK (appKey,appSecret);
  225. }
  226. public void SetPlatformConfig(Hashtable configInfo) {
  227. sdk.SetPlatformConfig(configInfo);
  228. }
  229. public int Authorize(PlatformType platform) {
  230. reqID ++;
  231. sdk.Authorize(reqID, platform);
  232. return reqID;
  233. }
  234. public void CancelAuthorize(PlatformType platform) {
  235. sdk.CancelAuthorize(platform);
  236. }
  237. public bool IsAuthorized(PlatformType platform) {
  238. return sdk.IsAuthorized(platform);
  239. }
  240. public bool IsClientValid(PlatformType platform) {
  241. return sdk.IsClientValid(platform);
  242. }
  243. public int GetUserInfo(PlatformType platform) {
  244. reqID ++;
  245. sdk.GetUserInfo(reqID, platform);
  246. return reqID;
  247. }
  248. public int ShareContent(PlatformType platform, ShareContent content) {
  249. reqID ++;
  250. sdk.ShareContent(reqID, platform, content);
  251. return reqID;
  252. }
  253. public int ShareContent(PlatformType[] platforms, ShareContent content) {
  254. reqID ++;
  255. sdk.ShareContent(reqID, platforms, content);
  256. return reqID;
  257. }
  258. public int ShareContentWithActivity(PlatformType platform, ShareContent content) {
  259. reqID++;
  260. #if UNITY_IPHONE
  261. sdk.ShareContentWithActivity(reqID, platform, content);
  262. #endif
  263. return reqID;
  264. }
  265. public int ShowPlatformList(PlatformType[] platforms, ShareContent content, int x, int y) {
  266. reqID++;
  267. sdk.ShowPlatformList(reqID, platforms, content, x, y);
  268. return reqID;
  269. }
  270. public int ShowShareContentEditor(PlatformType platform, ShareContent content) {
  271. reqID++;
  272. sdk.ShowShareContentEditor(reqID, platform, content);
  273. return reqID;
  274. }
  275. public int ShareWithContentName(PlatformType platform, string contentName, Hashtable customFields) {
  276. reqID++;
  277. sdk.ShareWithContentName(reqID, platform, contentName, customFields);
  278. return reqID;
  279. }
  280. public int ShowPlatformListWithContentName(string contentName, Hashtable customFields, PlatformType[] platforms, int x, int y) {
  281. reqID++;
  282. sdk.ShowPlatformListWithContentName(reqID, contentName, customFields, platforms, x, y);
  283. return reqID;
  284. }
  285. public int ShowShareContentEditorWithContentName(PlatformType platform, string contentName, Hashtable customFields) {
  286. reqID++;
  287. sdk.ShowShareContentEditorWithContentName(reqID, platform, contentName, customFields);
  288. return reqID;
  289. }
  290. public int GetFriendList(PlatformType platform, int count, int page) {
  291. reqID++;
  292. sdk.GetFriendList(reqID, platform, count, page);
  293. return reqID;
  294. }
  295. public int AddFriend(PlatformType platform, string account) {
  296. reqID++;
  297. sdk.AddFriend(reqID, platform, account);
  298. return reqID;
  299. }
  300. public Hashtable GetAuthInfo(PlatformType platform) {
  301. return sdk.GetAuthInfo(platform);
  302. }
  303. public void DisableSSO(bool open) {
  304. sdk.DisableSSO(open);
  305. }
  306. public void openMiniProgram(string userName, string path, int miniProgramType) {
  307. sdk.openMiniProgram(userName, path, miniProgramType);
  308. }
  309. public int isClientValidForAndroid(PlatformType platform) {
  310. reqID++;
  311. #if UNITY_ANDROID
  312. sdk.isClientValidForAndroid(reqID,platform);
  313. #endif
  314. return reqID;
  315. }
  316. #if UNITY_IPHONE || UNITY_IOS
  317. public delegate void GetWXRequestTokenHanlerEvent(string authCode, sendWXRequestToken send);
  318. public delegate void GetWXRefreshTokenHanlerEvent(string authCode, sendWXRefreshToken send);
  319. public delegate void GetShareCommandHanlerEvent(Hashtable data);
  320. public void ShareWithCommand(Hashtable customFields) {
  321. sdk.shareSDKWithCommand(customFields);
  322. }
  323. #endif
  324. public void getWXRequestToken() {
  325. sdk.getWXRequestToken();
  326. }
  327. public void getWXRefreshToken() {
  328. sdk.getWXRefreshToken();
  329. }
  330. public void sendWXRequestTokenMehtod(string uid, string token) {
  331. sdk.sendWXRequestToken(uid, token);
  332. }
  333. public void sendWXRefreshTokenMethod(string token) {
  334. sdk.sendWXRefreshToken(token);
  335. }
  336. }
  337. }