You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.4 KiB

  1. local PluginBase = require("luaScript.Plugins.PluginBase")
  2. local PluginWechatShare = class("PluginWechatShare" , PluginBase)
  3. function PluginWechatShare:ctor()
  4. PluginWechatShare.super.ctor(self);
  5. self.appId = "";
  6. self.appKey = "";
  7. self.plugin = nil
  8. self.AcountId = nil
  9. self.SessionId = nil
  10. self.isInstalled = false
  11. self.pluginKey = "PluginWechatShare";
  12. end
  13. -- 启动插件
  14. function PluginWechatShare:start(appid, secret)
  15. logD("PluginWechatShare::start()", appid, secret);
  16. self.appId = appid or self.appId;
  17. self.appKey = secret or self.appKey;
  18. if isAndroidPlatform() then
  19. self.plugin = cc.PluginManager:getInstance():createPlugin("PluginWechatShare" , "com/ddgame/plugin/PluginWechat");
  20. elseif isIOSPlatform() then
  21. self.plugin = cc.PluginManager:getInstance():createPlugin("PluginWechatShare" , "PluginWechat");
  22. end
  23. if self.plugin then
  24. local developerInfo =
  25. {
  26. appId = self.appId,
  27. appKey = self.appKey,
  28. appSign = "",
  29. appName = "",
  30. partnerId = "",
  31. universalLink = "",
  32. permissions = "snsapi_userinfo",
  33. pluginKey = self.pluginKey,
  34. callback = handler(self, self.onResult), -- 回调函数
  35. };
  36. logD("PluginWechatShare::start(), developerInfo = ", table.tostring(developerInfo));
  37. self.plugin:callVoid("initPlugin", developerInfo)
  38. end
  39. end
  40. -- 停止插件
  41. function PluginWechatShare:stop()
  42. if self.plugin then
  43. cc.PluginManager:getInstance():removePlugin(self.plugin);
  44. self.plugin = nil;
  45. end
  46. end
  47. -- 分享链接给好友
  48. function PluginWechatShare:shareGame(tt, shareCallbakc)
  49. logD("PluginWechatShare:shareGame(), ", table.tostring(tt))
  50. if not self.isInstalled then
  51. showTooltip("未安装微信或者微信版本过低,请安装最新版本的微信后再试")
  52. return;
  53. end
  54. -- 重新初始化
  55. self:start();
  56. if self.plugin then
  57. self.shareCallbakc = shareCallbakc
  58. self.plugin:callVoid("shareGame", tt or {});
  59. end
  60. end
  61. -- 回调函数
  62. function PluginWechatShare:onResult(result)
  63. logD("PluginWechatShare:onResult() result = ", result)
  64. local resultJson = json.decode(result);
  65. if not resultJson then
  66. return
  67. end
  68. local pluginKey = tostring(resultJson.pluginKey)
  69. if pluginKey ~= self.pluginKey then
  70. return
  71. end
  72. local code = tonumber(resultJson.code);
  73. local msg = json.decode(resultJson.message);
  74. if code == self.Code.INIT_SUCCESS then
  75. self.isInstalled = true
  76. elseif code == self.Code.INIT_FAIL then
  77. self.isInstalled = false
  78. elseif code == self.Code.LOGIN_SUCCESS then
  79. --[[app.user.openid = msg.openid
  80. app.user.unionid = msg.unionid
  81. app.user.sex = msg.sex or 0
  82. app.user.nickname = msg.nickname or ""
  83. app.user.headimgurl = msg.headimgurl or ""
  84. app.user.access_token = msg.access_token or ""
  85. app.user.refresh_token = msg.refresh_token or ""
  86. app.user.loginType = 0
  87. app:dispatchEvent({name = "thirdLoginResutl" });
  88. loginToken();
  89. showTooltip("微信授权成功,正在登陆服务器,请稍后!")--]]
  90. elseif code == self.Code.LOGIN_FAIL then
  91. --showTooltip("微信登录失敗")
  92. app:dispatchEvent({name = "thirdLoginResutl" });
  93. elseif code == self.Code.SHARE_SUCCESS then
  94. showTooltip("微信分享成功")
  95. if self.shareCallbakc and type(self.shareCallbakc) == "function" then
  96. self.shareCallbakc()
  97. end
  98. elseif code == self.Code.SHARE_CANCEL then
  99. showTooltip("微信分享取消")
  100. elseif code == self.Code.SHARE_FAIL then
  101. showTooltip("微信分享失败")
  102. end
  103. end
  104. return PluginWechatShare