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.

898 lines
27 KiB

  1. require("luaScript.Protocol.ProtocolCommon")
  2. local User = class("User" , require("luaScript.Protocol.Protocol"))
  3. -- 命令集合
  4. local UserCmd = {
  5. --[[/**
  6. * 请求登录
  7. * <pre>
  8. * 请求: {@code LoginRequest}
  9. * 返回:{@code LoginResponse}
  10. * </pre>
  11. */--]]
  12. LoginRequest = 0x0001;
  13. --[[/**
  14. * 登录返回结果
  15. * <pre>
  16. * 推送: {@code LoginResponse}
  17. * </pre>
  18. */--]]
  19. LoginResponse = 0x0002;
  20. --[[/**
  21. * 手机号码登录错误信息
  22. * <pre>
  23. * 推送: {@code PhoneLoginErrResponse}
  24. * </pre>
  25. */--]]
  26. PhoneLoginErrResponse = 0x0003;
  27. --[[/**
  28. * 绑定手机,微信,二次授权
  29. * <pre>
  30. * 推送: {@code PhoneBindRequest}
  31. * </pre>
  32. */--]]
  33. PhoneBindRequest = 0x0020;
  34. --[[/**
  35. * 绑定手机,微信,二次授权结果
  36. * <pre>
  37. * 推送: {@code PhoneBindResponse}
  38. * </pre>
  39. */--]]
  40. PhoneBindResponse = 0x0021;
  41. --[[/**
  42. * 用一个账号同时登陆时,踢出最早登陆的设备
  43. * <pre>
  44. * 推送: {@code IntPacket}
  45. * </pre>
  46. */--]]
  47. GAME_COMMAND_CLIENT_KICT_MULTI_LOGIN = 0x0043;
  48. --[[/**
  49. * 聊天内容
  50. * <pre>
  51. * 推送: {@code ChatMessageResponse}
  52. * </pre>
  53. */--]]
  54. GAME_COMMAND_CHAT_MESSAGE = 0x8004,
  55. --[[/**
  56. * 修改手机号/忘记密码
  57. * <pre>
  58. * 推送: {@code changePlayerInfoRequest}
  59. * </pre>
  60. */--]]
  61. GAME_COMMAND_CHANGE_PLAYER_INFO = 0x0023;
  62. --[[/**
  63. * 修改手机号/忘记密码
  64. * <pre>
  65. * 推送: {@code changePlayerInfoResponse}
  66. * </pre>
  67. */--]]
  68. GAME_COMMAND_CHANGE_INFO_RESPONSE= 0x0024;
  69. }
  70. -- 登陆请求
  71. LoginRequest = defClass("LoginRequest"
  72. -- 固定为0
  73. , defVar("uid", VT_Int, 0)
  74. -- 微信登陆 openId
  75. , defVar("openId", VT_String, "")
  76. -- 微信登陆 unionId
  77. , defVar("unionId", VT_String, "")
  78. -- 微信登陆 weixinInfo
  79. , defVar("weixinInfo", VT_String, "")
  80. -- 用户登录方式
  81. --0: 微信登录
  82. --1: 手机密码登录。openId为对应的手机号,unionId为密码
  83. --2: 手机验证码登录。openId为对应的手机号,unionId为验证码
  84. , defVar("loginType", VT_Short, 0)
  85. -- 验证码
  86. , defVar("checkcode", VT_String, "")
  87. )
  88. -- 登陆回复
  89. LoginResponse = defClass("LoginResponse"
  90. -- 用户id, 根据openid 以及unionid映射得到
  91. , defVar("uid", VT_Int, 0)
  92. -- 桌子id
  93. , defVar("tid", VT_Int, 0)
  94. -- 服务id
  95. , defVar("serverId", VT_Int, 0)
  96. -- 服务等级
  97. , defVar("serverLevel", VT_Short, 0)
  98. -- 游戏id, 若上述tid 与gameid取值都不为0时,表示用户在游戏中
  99. , defVar("gameId", VT_Short, 0)
  100. -- string格式, 目前只有roomCard信息(历史消耗房卡数量,当前房卡数量)
  101. , defVar("userInfo", VT_String, "")
  102. -- string格式, json数据,token,PHP登录使用
  103. , defVar("jsonData", VT_String, "")
  104. -- byte matchFlag; //是否在比赛中0: 不在 1:在
  105. , defVar("matchFlag", VT_Byte, "")
  106. -- 历史房卡数
  107. , localVar("historyCardNum", VT_Int, "")
  108. -- 当前房卡数
  109. , localVar("curCardNum", VT_Int, "")
  110. -- 当前悠闲豆
  111. , localVar("curBeanNum", VT_Int, 0)
  112. -- 当前礼券
  113. , localVar("curLiquanNum", VT_Int, 0)
  114. )
  115. -- 手机号码登录结果
  116. PhoneLoginErrResponse = defClass("PhoneLoginErrResponse"
  117. -- 登录结果
  118. -- 1、电话号码未绑定
  119. -- 2、密码错误
  120. -- 3、黑名单用户
  121. -- 4、验证码错误
  122. , defVar("ret", VT_Short, 0)
  123. )
  124. -- 绑定手机号码请求
  125. PhoneBindRequest = defClass("PhoneBindRequest"
  126. -- 用户id
  127. , defVar("uid", VT_Int, 0)
  128. -- 绑定唯一标识
  129. --, defVar("phonenum", VT_String, "")
  130. , defVar("account", VT_String, "")
  131. -- 密码 unionId
  132. , defVar("password", VT_String, "")
  133. -- 验证码
  134. , defVar("checkcode", VT_Int, "")
  135. -- weixinInfo
  136. , defVar("weixinInfo", VT_String, "")
  137. -- 奖励类型:1. 房卡 2.金币 3. 活动房卡 4. RMB 5. 红包券
  138. , defVar("rewardType", VT_Short, 0)
  139. -- 绑定类型
  140. -- 0:手机绑定,account:手机号,weixinInfo 必须有值,3微信绑定
  141. , defVar("bindType", VT_Short, 5)
  142. )
  143. -- 绑定手机号码结果
  144. PhoneBindResponse = defClass("PhoneBindResponse"
  145. -- 0: 绑定成功
  146. -- 1: 验证码错误
  147. -- 2: 已绑定过手机号
  148. -- 6:手机绑定此微信
  149. , defVar("ret", VT_Short, 0)
  150. -- 用户信息,绑定成功后会包含信息
  151. , defVar("userInfo", VT_String, "")
  152. -- 奖励金币,只有在ret = 0 时有效,取值0表示没有奖励
  153. , defVar("rewardCoin", VT_Int, 0)
  154. )
  155. --改变玩家信息request
  156. changePlayerInfoRequest = defClass("changePlayerInfoRequest"
  157. -- uid
  158. , defVar("uid", VT_Int, 0)
  159. -- 微信登陆 openId
  160. , defVar("openId", VT_String, "")
  161. -- 微信登陆 unionId
  162. , defVar("unionId", VT_String, "")
  163. -- 请求类型
  164. , defVar("loginType", VT_Short, 0)
  165. -- checkcode
  166. , defVar("checkcode", VT_String, "")
  167. -- 微信登陆 weixinInfo
  168. , defVar("weixinInfo", VT_String, "")
  169. )
  170. --改变玩家信息结果
  171. changePlayerInfoResponse = defClass("changePlayerInfoResponse"
  172. -- 返回类型
  173. , defVar("ret", VT_Short, 0)
  174. --请求类型
  175. , defVar("requestType", VT_Short, 0)
  176. --用户信息
  177. , defVar("userInfo", VT_String, "")
  178. )
  179. ------------------------- 玩家相关 -------------------------
  180. -- 重新组织 userInfo 里面的内容
  181. function User:updateUserInfo()
  182. local tt =
  183. {
  184. openid = self.openid,
  185. unionid = self.unionid,
  186. nickname = self.nickname,
  187. sex = self.sex,
  188. headimgurl = self.headimgurl,
  189. areano = self.areano,
  190. gpsInfo = self.gpsInfo,
  191. access_token = self.access_token,
  192. refresh_token = self.refresh_token,
  193. phonenum = self.phonenum,
  194. password = self.password,
  195. deviceInfo = self.strUserDeviceInfo,
  196. --如果是注册手机登录,登录后是5需改为手机登录,这样下次使用缓存登录发给服务器是手机登录,而如果是微信登录就还用原来的登录类型缓存
  197. loginType = self.loginType == LoginType.LOGIN_TYPE_PHONE_REGISTER and LoginType.LOGIN_TYPE_PHONE or self.loginType,
  198. --新增MD5登录
  199. isMd5PassWord = self.isMd5PassWord
  200. }
  201. self.userInfo = json.encode(tt);
  202. logD("User:updateUserInfo()", tostring(self.userInfo))
  203. end
  204. -- 更新GPS信息
  205. function User:updateGpsInfo(ttGpsInfo)
  206. ttGpsInfo = ttGpsInfo or {gpsStatus = 0, x = 0, y = 0 , addr = ""}
  207. logD("User:updateGpsInfo() ttGpsInfo = ", table.tostring(ttGpsInfo))
  208. self.gpsInfo = ttGpsInfo
  209. self:updateUserInfo()
  210. end
  211. function User:isGpsInfoNewest(gpsInfo)
  212. logD("User:isGpsInfoSameWithLoc() gpsInfo = ", table.tostring(gpsInfo))
  213. logD("User:isGpsInfoSameWithLoc() self.gpsInfo = ", table.tostring(self.gpsInfo))
  214. logD("User:isGpsInfoSameWithLoc() gpsInfo.x = ", string.format("%.6f", gpsInfo.x))
  215. logD("User:isGpsInfoSameWithLoc() self.gpsInfo.x = ", string.format("%.6f", self.gpsInfo.x))
  216. logD("User:isGpsInfoSameWithLoc() gpsInfo.y = ", string.format("%.6f", gpsInfo.y))
  217. logD("User:isGpsInfoSameWithLoc() self.gpsInfo.y = ", string.format("%.6f", self.gpsInfo.y))
  218. if gpsInfo
  219. and gpsInfo.gpsStatus
  220. and gpsInfo.x
  221. and gpsInfo.y
  222. and gpsInfo.gpsStatus == self.gpsInfo.gpsStatus
  223. and string.format("%.6f", gpsInfo.x) == string.format("%.6f", self.gpsInfo.x)
  224. and string.format("%.6f", gpsInfo.y) == string.format("%.6f", self.gpsInfo.y) then
  225. return true
  226. end
  227. return false
  228. end
  229. --凡是登录前,修改的用户数据都不要本地缓存。
  230. function User:userLoginEx()
  231. logD("User:userLoginEx()")
  232. if not self.openid or self.openid == "" then
  233. logD("User:userLoginEx() return by openid is nil or empty")
  234. return
  235. end
  236. -- local curTime = os.time()
  237. -- if curTime - self.lastLoginTime <= 5 then -- 5秒内不能重复登录
  238. -- showTooltip("5秒内不能重复登录")
  239. -- return
  240. -- end
  241. -- self.lastLoginTime = curTime;
  242. -- 兼容旧代码,将配置信息往这里拷贝一份
  243. self.notice = app.serverConfigs.notice;
  244. self.scroll = app.serverConfigs.scroll;
  245. self.gameId = app.serverConfigs.gameId;
  246. self.areano = app.serverConfigs.areano;
  247. self.phpInterface = app.serverConfigs.phpInterface;
  248. self:updateUserInfo();
  249. -- 请求登录
  250. local request = LoginRequest:new();
  251. request.uid = 0;
  252. request.openId = self.openid;
  253. request.unionId = self.unionid;
  254. request.weixinInfo = self.userInfo
  255. request.loginType = self.loginType
  256. request.checkcode = self.checkcode or ""
  257. --如果是验证码登录/密码登录,检查内存是否有密码,有密码则当从后台断网回来前台的时候或者启动游戏自动登录两个情况,自动转化为密码登录。
  258. if (self.loginType == LoginType.LOGIN_TYPE_PHONE_CHECKCODE or self.loginType == LoginType.LOGIN_TYPE_PHONE)
  259. and self.password and self.password ~= "" and string.len(self.password) > 5 then
  260. logD("yhj User:userLoginEx() AppBase didEnterForground")
  261. self.loginType = LoginType.LOGIN_TYPE_PHONE
  262. self.isMd5PassWord = "1"
  263. self:updateUserInfo()
  264. local tt = json.decode(self.userInfo)
  265. --如果是验证码登录unionid是?,就走不通了。所以先取用户信息里面的手机数据先
  266. request.openId = tt.phonenum or self.openid;
  267. request.unionId = tt.password or self.unionId;
  268. request.loginType = self.loginType
  269. request.weixinInfo = self.userInfo
  270. elseif self.loginType == LoginType.LOGIN_TYPE_ID and self.password and self.password ~= ""
  271. and string.len(self.password) > 5 then
  272. logD("yhj User:userLoginEx() AppBase didEnterForground")
  273. self.isMd5PassWord = "1"
  274. self:updateUserInfo()
  275. local tt = json.decode(self.userInfo)
  276. --断线重连self.loginInfo.uid肯定是有值,如果是杀掉进程重进,就没有值,这个时候取userinfo里面的值
  277. request.openId = self.loginInfo.uid ~= 0 and self.loginInfo.uid or tt.openid;
  278. request.unionId = tt.password or self.unionId;
  279. request.loginType = self.loginType
  280. request.weixinInfo = self.userInfo
  281. end
  282. app.waitDialogManager:showWaitNetworkDialog("登录中...")
  283. logD("yhj:User:userLoginEx() request = ", table.tostring(request))
  284. self:sendResponse{cmd = UserCmd.LoginRequest , sender = request};
  285. end
  286. -- 登陆结果
  287. --凡是登录成功后,修改的用户数据都要本地缓存。特别是手机,密码,ID等会影响到登录的
  288. function User:onLoginResponse(status, response)
  289. -- local curTime = os.time()
  290. -- self.lastLoginTime = curTime;
  291. logD("User:onLoginResponse()", tostring(status), table.tostring(response))
  292. local curTime = os.time()
  293. if curTime - self.lastLoginTime <= 5 then -- 5秒内不能重复登录
  294. logD("5秒内收到两次登录消息")
  295. showTooltip("5秒内不能重复登录")
  296. return
  297. end
  298. self.lastLoginTime = curTime;
  299. local jsonString = response.jsonData
  300. local jsonData = json.decode(jsonString)
  301. if jsonData then
  302. self.openid = jsonData.openid
  303. self.unionid = jsonData.unionid
  304. self.sex = jsonData.sex or self.sex
  305. self.nickname = jsonData.nickname or self.nickname
  306. self.nickname = string.getShortName(self.nickname, 99)
  307. self.headimgurl = jsonData.headimgurl or self.headimgurl
  308. self.phonenum = jsonData.phonenum
  309. self.password = jsonData.password or ""
  310. self.authorize = jsonData.authorize --是否需要二次授权,0则需要
  311. end
  312. if self.nickname == "" then
  313. logD(" nickname == null ")
  314. self.nickname = "未知昵称"
  315. self.sex = 1
  316. self.headimgurl = ""
  317. end
  318. self:updateUserInfo();
  319. -- 关闭延迟的等待
  320. app.waitDialogManager:closeAllNetWait()
  321. -- 保存玩家的登录信息
  322. logD("User:onLoginResponse self.loginInfo()1", self.loginInfo)
  323. self.loginInfo = response:updateTo(self.loginInfo)
  324. -- 启动语音插件
  325. -- app.plugin:startVoice();
  326. --传送经纬度信息给EapSDK ljx
  327. if G_EapSdkMgr and not isWin32Platform() then
  328. if self.gpsInfo.gpsStatus == GpsStatus.enable then
  329. if (self.gpsInfo.y <= 180 and self.gpsInfo.y >= -180) and (self.gpsInfo.x <= 90 and self.gpsInfo.x >= -90) then
  330. G_EapSdkMgr:SetLocationInfo(self.gpsInfo.x, self.gpsInfo.y)
  331. end
  332. end
  333. end
  334. -- 启动腾讯语音
  335. app.plugin:startGVoice();
  336. self:startHandle()
  337. local ttExtInfo = string.split(self.loginInfo.userInfo, ",")
  338. self.loginInfo.historyCardNum = ttExtInfo[1];
  339. self.loginInfo.curCardNum = ttExtInfo[2];
  340. self.loginInfo.curJingbiNum = ttExtInfo[3] or 0
  341. --金币场等级
  342. self.serverLevel = response.serverLevel
  343. -- 写入玩家ID到本地
  344. -- 下次启动游戏时会检测玩家ID是否特殊ID
  345. logD("User:onLoginResponse self.loginInfo()2", self.loginInfo)
  346. saveUserId(self.loginInfo.uid);
  347. local jsonData = response.jsonData
  348. local tt = json.decode(jsonData) or {}
  349. self.loginInfo.token = tt.token or "unkown"
  350. if self.loginType == LoginType.LOGIN_TYPE_WEIXIN or self.loginType == LoginType.LOGIN_TYPE_PHONE
  351. or self.loginType == LoginType.LOGIN_TYPE_PHONE_REGISTER or self.loginType == LoginType.LOGIN_TYPE_PHONE_CHECKCODE
  352. or self.loginType == LoginType.LOGIN_TYPE_ID then
  353. -- app.php:login()
  354. -- -- 发送登录成功的事件
  355. -- self:dispatchEvent({name = "onLoginSuccessed"})
  356. -- self:dispatchEvent({name = "onLoginSuccessedMessage"})
  357. self:sendPhpIpBox()
  358. self:sendPhpIdentityCheck()
  359. end
  360. end
  361. local scheduler = cc.Director:getInstance():getScheduler()
  362. local _handle = nil;
  363. local beat_time = 1;
  364. function User:startHandle()
  365. self:closeHandle();
  366. _handle = scheduler:scheduleScriptFunc(function ()
  367. app.plugin:Update()
  368. end, beat_time, false)
  369. end
  370. function User:closeHandle()
  371. if _handle then
  372. scheduler:unscheduleScriptEntry(_handle)
  373. _handle = nil
  374. end
  375. end
  376. -- 请求IP盒子接口
  377. function User:sendPhpIpBox()
  378. local isResponse = false
  379. local function onHttpResponse(status, response)
  380. app.waitDialogManager:closeAllNetWait()
  381. if status == "successed" then
  382. isResponse = true;
  383. local data = json.decode(response)
  384. if data and data.code == 0 then
  385. app.php:login()
  386. -- 发送登录成功的事件
  387. self:dispatchEvent({name = "onLoginSuccessed"})
  388. self:dispatchEvent({name = "onLoginSuccessedMessage"})
  389. else
  390. showTooltip(data.error or "验证失败")
  391. end
  392. end
  393. end
  394. local taskId;
  395. local function onDelayPostCallBack(id)
  396. taskId = id;
  397. runDelayWithTime(function()
  398. if not isResponse then --没有数据回来 默认超时
  399. logD("超时未收到IP盒子回应, 直接登陆")
  400. -- 取消之前的连接
  401. if taskId then
  402. cc.CURLManager:getInstance():cancelTask(taskId)
  403. end
  404. app.php:login()
  405. -- 发送登录成功的事件
  406. self:dispatchEvent({name = "onLoginSuccessed"})
  407. self:dispatchEvent({name = "onLoginSuccessedMessage"})
  408. end
  409. end, 5)
  410. end
  411. local params = {
  412. app_id = getAppId(),
  413. action = "ipbox.ipPass",
  414. uid = self.loginInfo.uid,
  415. }
  416. local phpUrl = getGlobalPhpUrl()
  417. httpPost(phpUrl, params, onHttpResponse, onDelayPostCallBack)
  418. end
  419. -- 请求实名认证接口
  420. function User:sendPhpIdentityCheck()
  421. local function onHttpResponse(status, response)
  422. app.waitDialogManager:closeAllNetWait()
  423. if status == "successed" then
  424. local data = json.decode(response)
  425. if data and data.code == 200 then
  426. self.isRealname = data.result.isRealname
  427. self.isAdult = data.result.isAdult;
  428. --已实名且已成年发送电竞SDK接口 ljx
  429. if G_EapSdkMgr and not isWin32Platform() then
  430. if self.isAdult == 1 and self.isRealname == 1 then
  431. G_EapSdkMgr:VerifyRealInfo(data.result.name, data.result.card)
  432. end
  433. end
  434. else
  435. showTooltip(data.error or "验证失败")
  436. end
  437. end
  438. end
  439. local params = {
  440. app_id = getAppId(),
  441. action = "identity.check",
  442. uid = self.loginInfo.uid,
  443. }
  444. local phpUrl = getGlobalPhpUrl()
  445. httpPost(phpUrl, params, onHttpResponse)
  446. end
  447. -- 请求申请注销账号
  448. function User:sendSubmitLogOut(callBack)
  449. local function onHttpResponse(status, response)
  450. app.waitDialogManager:closeAllNetWait()
  451. if status == "successed" then
  452. --申请成功 注销状态改为1 注销时间更新
  453. local data = json.decode(response)
  454. if data.code == 200 then
  455. self.logOut.status = 1
  456. self.logOut.logOutTime = data.result.endtime
  457. callBack()
  458. else
  459. showTooltip(data.error)
  460. end
  461. end
  462. end
  463. local params = {
  464. app_id = getAppId(),
  465. action = "group.ApCancelAccount",
  466. uid = self.loginInfo.uid,
  467. }
  468. local phpUrl = getGlobalPhpUrl()
  469. httpPost(phpUrl, params, onHttpResponse)
  470. end
  471. -- 请求取消撤销账号
  472. function User:sendRevokeLogOut(callBack)
  473. local function onHttpResponse(status, response)
  474. app.waitDialogManager:closeAllNetWait()
  475. if status == "successed" then
  476. local data = json.decode(response)
  477. if data.code == 200 then
  478. --撤销成功 注销状态改为0 注销时间更新
  479. self.logOut.status = 0
  480. self.logOut.logOutTime = "0"
  481. callBack()
  482. end
  483. end
  484. end
  485. local params = {
  486. app_id = getAppId(),
  487. action = "group.CeCancelAccount",
  488. uid = self.loginInfo.uid,
  489. }
  490. local phpUrl = getGlobalPhpUrl()
  491. httpPost(phpUrl, params, onHttpResponse)
  492. end
  493. -- 使用手机号码登录失败
  494. function User:onPhoneLoginErrResponse(status, response)
  495. -- 关闭延迟的等待
  496. app.waitDialogManager:closeAllNetWait()
  497. -- 重置本地账号数据
  498. self.openid = "";
  499. self.unionid = "";
  500. saveUserInfo("local_password","");
  501. self.loginType = 0
  502. self.checkcode = ""
  503. self.headimgurl = ""
  504. self.nickname = ""
  505. app.userManager:setLoginRecordCode(0)
  506. local errCode = response.ret
  507. self:dispatchEvent({name = "onPhoneLoginErrResponse", errCode = errCode});
  508. end
  509. function User:reqestBindApp(bindType, openId)
  510. if not self.loginInfo or not self.loginInfo.uid then
  511. return
  512. end
  513. local request = PhoneBindRequest:new()
  514. request.uid = tonumber(self.loginInfo.uid);
  515. request.account = tostring(openId)
  516. request.bindType = tonumber(bindType)
  517. request.weixinInfo = self.userInfo;
  518. logD("User:reqestBindApp() request = ", table.tostring(request))
  519. self:sendResponse{cmd = UserCmd.PhoneBindRequest , sender = request};
  520. end
  521. -- 绑定手机号
  522. function User:reqestBindPhone(phonenum, checkcode)
  523. if not phonenum or not checkcode then
  524. return
  525. end
  526. local request = PhoneBindRequest:new()
  527. request.uid = self.loginInfo.uid
  528. request.account = phonenum
  529. request.password = ""
  530. request.checkcode = checkcode
  531. request.weixinInfo = app.user.userInfo
  532. request.rewardType = 5 -- 奖励类型:1. 房卡 2.金币 3. 活动房卡 4. RMB 5. 红包券
  533. logD("User:reqestBindPhone() request = ", table.tostring(request))
  534. self:sendResponse{cmd = UserCmd.PhoneBindRequest , sender = request};
  535. end
  536. -- 绑定的结果
  537. function User:onBindResponse(status, response)
  538. logD("User:onBindResponse() response = ", table.tostring(response))
  539. --[[ [0] = "绑定成功",
  540. [1] = "验证码错误",
  541. [2] = "已绑定过手机号,无需再次绑定",
  542. [5] = "该手机号已经被其他游戏ID绑定,请更换手机号再次绑定",
  543. [6] = "绑定成功",
  544. [7] = "该微信号已绑定过其他游戏账号",
  545. [8] = "绑定成功",
  546. --]]
  547. if response.ret == BIND_TYPE_RESULT.PHONE then --手机绑定成功
  548. local stUserInfo = json.decode(response.userInfo)
  549. if stUserInfo then
  550. self.phonenum = stUserInfo.phonenum
  551. self.password = stUserInfo.password
  552. else
  553. logE("User:onBindResponse() stUserInfo is nil")
  554. end
  555. self:updateUserInfo();
  556. -- 更新本地保存的数据
  557. local tt = {}
  558. tt.forceCover = true
  559. app.user:dispatchEvent({name = "updateWxUserInfo",response = tt})
  560. elseif response.ret == BIND_TYPE_RESULT.WEIXIN_TWICE_BIND then --二次授权
  561. logD("yhj User:onBindResponse() WEIXIN_TWICE_BIND SUCCESS")
  562. -- local stUserInfo = json.decode(response.userInfo)
  563. -- if stUserInfo then
  564. -- self:updateWeiXinInfo(stUserInfo)
  565. -- self:updateUserInfo();
  566. -- -- 更新本地保存的数据
  567. -- local tt = {}
  568. -- tt.forceCover = true
  569. -- app.user:dispatchEvent({name = "updateWxUserInfo",response = tt})
  570. -- end
  571. app:dispatchEvent({name = "showConfirmDialogNotClose"})
  572. elseif response.ret == BIND_TYPE_RESULT.WEIXIN then --普通微信绑定
  573. logD("yhj User:onBindResponse() WEIXIN SUCCESS")
  574. local stUserInfo = json.decode(response.userInfo)
  575. if stUserInfo then
  576. self:updateWeiXinInfo(stUserInfo)
  577. self:updateUserInfo();
  578. -- 更新本地保存的数据
  579. local tt = {}
  580. tt.forceCover = true
  581. app.user:dispatchEvent({name = "updateWxUserInfo",response = tt})
  582. end
  583. app:dispatchEvent({name = "showConfirmDialogNotClose"})
  584. elseif response.ret == BIND_TYPE_RESULT.USER_SET_PASSWORD then --用户设置密码
  585. logD("yhj User:onBindResponse() USER_SET_PASSWORD SUCCESS")
  586. local stUserInfo = json.decode(response.userInfo)
  587. if stUserInfo then
  588. self.password = stUserInfo.password
  589. else
  590. logE("User:onBindResponse() stUserInfo is nil")
  591. end
  592. self:updateUserInfo();
  593. -- 更新本地保存的数据
  594. local tt = {}
  595. tt.forceCover = true
  596. app.user:dispatchEvent({name = "updateWxUserInfo",response = tt})
  597. else--失败情况暂时只有分发提示事件
  598. logE("yhj User:onBindResponse() Bind Failed!")
  599. end
  600. self:dispatchEvent({name = "onBindResponse", response = response});
  601. end
  602. --微信新增字段
  603. function User:updateWeiXinInfo(weixinInfo)
  604. if weixinInfo then
  605. self.unionid = weixinInfo.unionid or self.unionid
  606. self.openid = weixinInfo.openid or self.openid
  607. self.nickname = weixinInfo.nickname or self.nickname
  608. self.headimgurl = weixinInfo.headimgurl or self.headimgurl
  609. self.sex = weixinInfo.sex or self.sex
  610. self.access_token = weixinInfo.access_token or self.access_token
  611. self.refresh_token = weixinInfo.refresh_token or self.refresh_token
  612. else
  613. logE("weixinInfo is nil")
  614. end
  615. end
  616. -- 改变信息的结果(修改手机号/忘记密码返回)
  617. function User:onChangePlayerInfoResponse(status, response)
  618. logD("yhj:User:onChangePlayerInfoResponse() response = ", table.tostring(response))
  619. if response.ret == 0 then
  620. local stUserInfo = json.decode(response.userInfo)
  621. local requestType = response.requestType
  622. if stUserInfo then
  623. if requestType == ChangePlayerInfo.REPLCACE_PHONE then
  624. self.phonenum = stUserInfo.phonenum
  625. self:updateUserInfo()
  626. -- 更新本地保存的数据
  627. local tt = {}
  628. tt.forceCover = true
  629. tt.userInfo = app.user.userInfo
  630. app.user:dispatchEvent({name = "updateWxUserInfo",response = tt})
  631. elseif requestType == ChangePlayerInfo.PHONE_RESET then
  632. --忘记密码,数据没必要缓存。缓存反而会导致,用户用ID登录输入错误密码,也会用缓存的数据登录
  633. --[[self.password = stUserInfo.password
  634. self:updateUserInfo()
  635. -- 更新本地保存的数据
  636. local tt = {}
  637. tt.userInfo = response.userInfo
  638. tt.forceCover = true
  639. app.user:dispatchEvent({name = "updateWxUserInfo",response = tt})--]]
  640. end
  641. else
  642. logE("User:onBindResponse() stUserInfo is nil")
  643. end
  644. end
  645. self:dispatchEvent({name = "onChangePlayerInfo", response = response});
  646. end
  647. --////////////////////////////////////////////////////////////////////////////
  648. -- 聊天内容
  649. ChatMessageRequest = defClass("ChatMessageRequest"
  650. -- //类型 1: 表情 2: 语音 3:聊天
  651. , defVar("type", VT_Short, 0)
  652. -- //内容,客户端自定义格式
  653. , defVar("content", VT_String, "")
  654. )
  655. -- 聊天内容
  656. ChatMessageResponse = defClass("ChatMessageResponse"
  657. -- 用户uid
  658. , defVar("nUserId", VT_Int, 0)
  659. -- //类型 1: 表情 2: 语音 3:聊天
  660. , defVar("type", VT_Short, 0)
  661. -- //内容,客户端自定义格式
  662. , defVar("content", VT_String, "")
  663. )
  664. function User:onChatMessageResponse(status, response)
  665. logD("User:onChatMessageResponse(), ", table.tostring(response))
  666. --[[
  667. -- 只管发送消息,而不必关注是否有人处理
  668. if not app.room or not app.room.roomInfo then
  669. print("self.roomInfo no exist,容错处理!")
  670. return
  671. end
  672. --]]
  673. self:dispatchEvent({name = "onChatMessageResponse", response = response});
  674. end
  675. -- 是否可以发送聊天消息
  676. function User:canSendChatMessage(chat_type)
  677. if not self.lastChatMessage then
  678. self.lastChatMessage = {}
  679. end
  680. if not self.lastChatMessage[chat_type] then
  681. self.lastChatMessage[chat_type] = 0
  682. end
  683. local voicePermission = tonumber(loadUserInfo("voicePermission")) or 0
  684. voicePermission = voicePermission + 1
  685. logD("User:canSendChatMessage voicePermission out:"..voicePermission)
  686. if voicePermission <= 3 then
  687. logD("User:canSendChatMessage voicePermission:"..tostring(voicePermission))
  688. saveUserInfo("voicePermission",voicePermission)
  689. return true
  690. end
  691. local ChatDef = require("luaScript.GameChatDefine")
  692. local timeNow = os.time()
  693. local timeLast = self.lastChatMessage[chat_type] or 10
  694. local timeInterval = ChatDef.TimeInterval[chat_type] or 10
  695. if timeNow - timeLast >= timeInterval then
  696. return true
  697. else
  698. return false
  699. end
  700. end
  701. function User:sendChatMessage(chat_type, content)
  702. if not self:canSendChatMessage(chat_type) then
  703. return false;
  704. end
  705. self.lastChatMessage[chat_type] = os.time()
  706. local request = ChatMessageRequest:new()
  707. request.type = tonumber(chat_type);
  708. request.content = tostring(content)
  709. log("User:sendChatMessage() request = ", table.tostring(request));
  710. self:sendResponse{cmd = UserCmd.GAME_COMMAND_CHAT_MESSAGE , sender = request};
  711. return true;
  712. end
  713. function User:getLastChatTime(chat_type)
  714. return self.lastChatMessage[chat_type] or os.time()
  715. end
  716. function User:onServerKickOutResponse(status, response)
  717. uploadLogs(GAME_ERROR_TYPE.DING_HAO_ERROR)
  718. self.kickOut = true
  719. self:dispatchEvent({name = "onServerKickOutResponse", response = response})
  720. end
  721. function User:getGameConfig(gameId)
  722. return app.serverConfigs.configs[gameId]
  723. end
  724. function User:ctor(net)
  725. User.super.ctor(self , net, ModuleId);
  726. -- 玩家的个人信息
  727. self.userInfo = "";
  728. self.openid = ""
  729. self.unionid = ""
  730. self.nickname = ""
  731. self.sex = 1
  732. self.headimgurl = ""
  733. self.areano = 0
  734. self.gpsInfo = {gpsStatus = 0, x = 0, y = 0, addr = ""}
  735. self.access_token = ""
  736. self.refresh_token = ""
  737. self.loginType = 0
  738. --是否使用md5密码登录
  739. self.isMd5PassWord = "0"
  740. --是否正在绑定微信
  741. self.bindWeiXinIng = false
  742. --是否被顶号
  743. self.kickOut = false
  744. local sysVersion= getSystemVersion();
  745. local sysName = getSystemName();
  746. self.userDeviceInfo =
  747. {
  748. device = getDeviceID(), --设备唯一标识,如有则传,无则无需传输
  749. machine_type = getLocalizedModel(), --设备型号, "iPhone84"\ "OPPOR11"\ "ASK-AL00x"
  750. os = string.format(sysName..sysVersion) --设备操作系统, "android7.1.2" or "iOS13.3"
  751. }
  752. self.strUserDeviceInfo = json.encode(self.userDeviceInfo)
  753. self.lastLoginTime = 0; -- todo lwq 超时使用,5秒内不能重复登录,限制服务器叫我们客户端登录
  754. -- 登录返回的数据
  755. self.loginInfo = LoginResponse:new()
  756. -- 监听获取配置的消息,成功获取配置后请求登录
  757. app.serverConfigs:addEventListener("initSuccessed", handler(self, self.userLoginEx))
  758. -- 请求登陆
  759. self:defMsgFunc{name = "loginRequest" , cmd = UserCmd.LoginRequest, sender = LoginRequest};
  760. --请求改变玩家信息(忘记密码,修改手机号)
  761. self:defMsgFunc{name = "playerInfoChange" , cmd = UserCmd.GAME_COMMAND_CHANGE_PLAYER_INFO, sender = changePlayerInfoRequest};
  762. -- 推送登陆结果
  763. self:defPushMsg{cmd = UserCmd.LoginResponse , reader = LoginResponse, func = handler(self , self.onLoginResponse)};
  764. -- 使用手机号码登录失败
  765. self:defPushMsg{cmd = UserCmd.PhoneLoginErrResponse , reader = PhoneLoginErrResponse, func = handler(self , self.onPhoneLoginErrResponse)};
  766. -- 请求绑定手机号
  767. self:defMsgFunc{name = "requestBindPhoneNum" , cmd = UserCmd.PhoneBindRequest, sender = PhoneBindRequest};
  768. -- 绑定手机号码的结果
  769. self:defPushMsg{cmd = UserCmd.PhoneBindResponse , reader = PhoneBindResponse, func = handler(self , self.onBindResponse)};
  770. -- 聊天信息 GAME_COMMAND_CHAT_MESSAGE
  771. self:defPushMsg{cmd = UserCmd.GAME_COMMAND_CHAT_MESSAGE, reader = ChatMessageResponse, func = handler(self , self.onChatMessageResponse)};
  772. -- 账号在其他地方登陆
  773. self:defPushMsg{cmd = UserCmd.GAME_COMMAND_CLIENT_KICT_MULTI_LOGIN, reader = IntPacket, func = handler(self , self.onServerKickOutResponse)};
  774. --改变玩家信息的结果
  775. self:defPushMsg{cmd = UserCmd.GAME_COMMAND_CHANGE_INFO_RESPONSE, reader = changePlayerInfoResponse, func = handler(self , self.onChangePlayerInfoResponse)};
  776. end
  777. return User;