LoginHandler.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // LoginHandler.ts
  2. import { oops } from 'db://oops-framework/core/Oops';
  3. import { smc } from '../SingletonModuleComp';
  4. import { ServerHandler } from './ServerHandler';
  5. import { CocosHandler } from './CocosHandler';
  6. export class LoginHandler {
  7. private static _inst: LoginHandler;
  8. public static get inst(): LoginHandler {
  9. if (!this._inst) {
  10. this._inst = new LoginHandler();
  11. }
  12. return this._inst;
  13. }
  14. async getPrivacyStatus() {
  15. const data = { method: 'privacy.grant.get' };
  16. const result = await CocosHandler.inst.sendMessageToAndroid(data, '获取隐私授权状态');
  17. return JSON.parse(result);
  18. }
  19. async savePrivacyStatus(status: boolean) {
  20. const data = {
  21. method: 'privacy.grant.set',
  22. param: JSON.stringify({ granted: status })
  23. };
  24. const result = await CocosHandler.inst.sendMessageToAndroid(data, '保存隐私授权状态');
  25. return JSON.parse(result);
  26. }
  27. openAgreement() {
  28. const type = smc.game.GameModel.protocolType;
  29. const url = type === 1 ? oops.config.game.gamePrivacyUrl : oops.config.game.gameProtocolUrl;
  30. const data = {
  31. method: 'system.browser.open',
  32. param: JSON.stringify({ url })
  33. };
  34. CocosHandler.inst.sendMessageToAndroid(data, '打开协议链接');
  35. }
  36. async wechatLogin() {
  37. const param = {
  38. callback: {
  39. onSuccess: 'LoginHandler.inst.wechatLoginSuccess',
  40. onFaile: 'LoginHandler.inst.wechatLoginFail'
  41. }
  42. };
  43. const data = {
  44. method: 'auth.wechat',
  45. param: JSON.stringify(param)
  46. };
  47. await CocosHandler.inst.sendMessageToAndroid(data, '微信登录');
  48. }
  49. wechatLoginSuccess(str: string) {
  50. console.log('[微信] 登录成功', str);
  51. this.wxLogin(str);
  52. }
  53. wechatLoginFail(str: string) {
  54. console.log('[微信] 登录失败', str);
  55. oops.gui.toast('微信登录失败');
  56. }
  57. async wxLogin(code: string) {
  58. const param = {
  59. method: 'request.post',
  60. param: JSON.stringify({ code, url: '/api/wxlogin' }) // 修改为实际接口
  61. };
  62. const result = await CocosHandler.inst.sendMessageToAndroid(param, '请求服务器微信登录');
  63. if (result?.data) {
  64. ServerHandler.inst.getAccountInfo();
  65. }
  66. }
  67. }
  68. window["LoginHandler"] = LoginHandler;