local PluginBase = require("luaScript.Plugins.PluginBase") local PluginWechat = class("PluginWechat" , PluginBase) local PluginConfig = require("preload.PluginConfig") function PluginWechat:ctor() PluginWechat.super.ctor(self); self.plugin = nil self.AcountId = nil self.SessionId = nil self.isInstalled = false self.pluginKey = "PluginWechat"; end -- 启动插件 function PluginWechat:start() if isAndroidPlatform() then self.plugin = cc.PluginManager:getInstance():createPlugin("PluginWechat" , "com/ddgame/plugin/PluginWechat"); elseif isIOSPlatform() then self.plugin = cc.PluginManager:getInstance():createPlugin("PluginWechat" , "PluginWechat"); end if self.plugin then local developerInfo = { appId = PluginConfig.WeiXin.appId, appKey = PluginConfig.WeiXin.appKey, appSign = PluginConfig.WeiXin.appSign, appName = PluginConfig.WeiXin.appName, partnerId = PluginConfig.WeiXin.partnerId, universalLink = PluginConfig.WeiXin.universalLink, permissions = "snsapi_login", pluginKey = self.pluginKey, callback = handler(self, self.onResult), -- 回调函数 }; logD("PluginWechat:start(), developerInfo = ", table.tostring(developerInfo)) self.plugin:callVoid("initPlugin", developerInfo) end end -- 停止插件 function PluginWechat:stop() if self.plugin then cc.PluginManager:getInstance():removePlugin(self.plugin); self.plugin = nil; end end function PluginWechat:isSupport() return self.isInstalled == true; end function PluginWechat:isSupportPay() return false end -- 请求登录 function PluginWechat:login(tt) log("PluginWechat:login", table.tostring(tt)) if not self.isInstalled then showTooltip("未安装微信或者微信版本过低,请安装最新版本的微信后再试") return; end self:start(); if self.plugin then self.plugin:callVoid("login",tt or {}) end end -- 请求登出 function PluginWechat:logout(tt) log("PluginWechat:logout", table.tostring(tt)) -- IOS没做登出 end function PluginWechat:pay(tt) log("PluginWechat:pay", table.tostring(tt)) if not self.isInstalled then showTooltip("未安装微信或者微信版本过低,请安装最新版本的微信后再试") return; end self:start(); if self.plugin then self.plugin:callVoid("pay",tt or {}) end end -- 跳转至微信 function PluginWechat:gotoWeiXin() log("PluginWechat:gotoWeiXin() ") if not self.isInstalled then showTooltip("未安装微信或者微信版本过低,请安装最新版本的微信后再试") return; end self:start(); if self.plugin then self.plugin:callVoid("gotoWeiXin"); end end -- 分享链接给好友 function PluginWechat:shareGame(tt, shareCallbakc) log("PluginWechat: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 PluginWechat:onResult(result) log("PluginWechat: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 --是否绑定微信true代表绑定,false代表登录 if app.user.bindWeiXinIng or app.user.bindWeiXinTwice then local openid = msg.openid local unionid = msg.unionid local sex = msg.sex or 0 local nickname = msg.nickname or "" local headimgurl = msg.headimgurl or "" local access_token = msg.access_token or "" local refresh_token = msg.refresh_token or "" --app.user:updateUserInfo() app.user.bindWeiXinIng = false local tt = { openid = msg.openid, unionid = msg.unionid, nickname = msg.nickname or "", sex = tonumber(msg.sex) or 0, headimgurl = msg.headimgurl or "", areano = app.user.areano, gpsInfo = app.user.gpsInfo, access_token = msg.access_token or "", refresh_token = msg.refresh_token or "", phonenum = app.user.phonenum, password = app.user.password, deviceInfo = app.user.strUserDeviceInfo, } local weixinInfo = json.encode(tt); --绑定微信请求 local request = PhoneBindRequest:new() request.uid = app.user.loginInfo.uid request.checkcode = "" request.weixinInfo = weixinInfo request.rewardType = 5 -- 奖励类型:1. 房卡 2.金币 3. 活动房卡 4. RMB 5. 红包券 if app.user.bindWeiXinTwice then logD("yhj: 微信二次授权") request.bindType = BIND_TYPE.WEIXIN_TWICE_BIND request.account = msg.openid request.password = msg.unionid app.user.bindWeiXinTwice = false else logD("yhj: 普通微信绑定") request.bindType = BIND_TYPE.WEIXIN request.account = app.user.phonenum request.password = "" end -- app.user:dispatchEvent({name = "onWetChatBindRequest",request = request}); else 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 "" logD("yhj: 正常微信登录") app.user.loginType = 0 --app:dispatchEvent({name = "thirdLoginResutl" }); loginToken(); showTooltip("微信授权成功,正在登陆服务器,请稍后!") end 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 PluginWechat