local PluginBase = require("luaScript.Plugins.PluginBase") local PluginWechatShare = class("PluginWechatShare" , PluginBase) function PluginWechatShare:ctor() PluginWechatShare.super.ctor(self); self.appId = ""; self.appKey = ""; self.plugin = nil self.AcountId = nil self.SessionId = nil self.isInstalled = false self.pluginKey = "PluginWechatShare"; end -- 启动插件 function PluginWechatShare:start(appid, secret) logD("PluginWechatShare::start()", appid, secret); self.appId = appid or self.appId; self.appKey = secret or self.appKey; if isAndroidPlatform() then self.plugin = cc.PluginManager:getInstance():createPlugin("PluginWechatShare" , "com/ddgame/plugin/PluginWechat"); elseif isIOSPlatform() then self.plugin = cc.PluginManager:getInstance():createPlugin("PluginWechatShare" , "PluginWechat"); end if self.plugin then local developerInfo = { appId = self.appId, appKey = self.appKey, appSign = "", appName = "", partnerId = "", universalLink = "", permissions = "snsapi_userinfo", pluginKey = self.pluginKey, callback = handler(self, self.onResult), -- 回调函数 }; logD("PluginWechatShare::start(), developerInfo = ", table.tostring(developerInfo)); self.plugin:callVoid("initPlugin", developerInfo) end end -- 停止插件 function PluginWechatShare:stop() if self.plugin then cc.PluginManager:getInstance():removePlugin(self.plugin); self.plugin = nil; end end -- 分享链接给好友 function PluginWechatShare:shareGame(tt, shareCallbakc) logD("PluginWechatShare:shareGame(), ", table.tostring(tt)) if not self.isInstalled then showTooltip("未安装微信或者微信版本过低,请安装最新版本的微信后再试") return; end -- 重新初始化 self:start(); if self.plugin then self.shareCallbakc = shareCallbakc self.plugin:callVoid("shareGame", tt or {}); end end -- 回调函数 function PluginWechatShare:onResult(result) logD("PluginWechatShare:onResult() result = ", result) local resultJson = json.decode(result); if not resultJson then return end local pluginKey = tostring(resultJson.pluginKey) if pluginKey ~= self.pluginKey then return end local code = tonumber(resultJson.code); local msg = json.decode(resultJson.message); if code == self.Code.INIT_SUCCESS then self.isInstalled = true elseif code == self.Code.INIT_FAIL then self.isInstalled = false elseif code == self.Code.LOGIN_SUCCESS then --[[app.user.openid = msg.openid app.user.unionid = msg.unionid app.user.sex = msg.sex or 0 app.user.nickname = msg.nickname or "" app.user.headimgurl = msg.headimgurl or "" app.user.access_token = msg.access_token or "" app.user.refresh_token = msg.refresh_token or "" app.user.loginType = 0 app:dispatchEvent({name = "thirdLoginResutl" }); loginToken(); showTooltip("微信授权成功,正在登陆服务器,请稍后!")--]] elseif code == self.Code.LOGIN_FAIL then --showTooltip("微信登录失敗") app:dispatchEvent({name = "thirdLoginResutl" }); elseif code == self.Code.SHARE_SUCCESS then showTooltip("微信分享成功") if self.shareCallbakc and type(self.shareCallbakc) == "function" then self.shareCallbakc() end elseif code == self.Code.SHARE_CANCEL then showTooltip("微信分享取消") elseif code == self.Code.SHARE_FAIL then showTooltip("微信分享失败") end end return PluginWechatShare