app.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // app.ts
  2. import { Login } from './api/login'
  3. import Toast from 'tdesign-miniprogram/toast/index';
  4. const voiceCbs: any = []
  5. const voiceProps: VoiceProps = {
  6. noMute: false,
  7. pullUrls: [],
  8. pushUrl: false,
  9. }
  10. const loginCbs = []
  11. const loginProps = {
  12. isLogin: true
  13. };
  14. const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
  15. App<IAppOption>({
  16. sleep: sleep,
  17. globalData: {
  18. voiceProps,
  19. userInfo: {
  20. phoneNumber: '',
  21. nickName: '',
  22. country: '',
  23. avatarUrl: '',
  24. gender: 0,
  25. city: '',
  26. province: '',
  27. language: 'zh_CN'
  28. },
  29. token: '',
  30. isLogin: false,
  31. roomId: ''
  32. },
  33. addVoicePropsListener(cb: Function) {
  34. voiceCbs.push(cb)
  35. },
  36. removeVoicePropsListener(cb: Function) {
  37. voiceCbs.splice(voiceCbs.indexOf(cb), 1)
  38. },
  39. setVoiceProps(props: VoiceProps) {
  40. Object.keys(props).forEach((k: string) => {
  41. (voiceProps as any)[k] = (props as any)[k]
  42. })
  43. voiceCbs.forEach((cb: Function) => cb(props))
  44. },
  45. async setLogin(status: boolean): Promise<boolean> {
  46. this.globalData.isLogin = status
  47. return Promise.resolve(this.globalData.isLogin)
  48. },
  49. unwatch(key: string, method: Function) {
  50. },
  51. watch(key: string, method: Function) {
  52. var obj: GlobalDataProps = this.globalData;
  53. type Keys = keyof typeof obj;
  54. //加个前缀生成隐藏变量,防止死循环发生
  55. let ori = obj[key as Keys]; //obj[key]这个不能放在Object.defineProperty里
  56. if (ori) { //处理已经声明的变量,绑定处理
  57. method(ori);
  58. }
  59. Object.defineProperty(obj, key, {
  60. configurable: true,
  61. enumerable: true,
  62. set: function (value) {
  63. this['_' + key] = value;
  64. console.log('是否会被执行2', key)
  65. method(value);
  66. },
  67. get: function () {
  68. // 在其他界面调用key值的时候,这里就会执行。
  69. if (typeof this['_' + key] == 'undefined') {
  70. if (ori) {
  71. //这里读取数据的时候隐藏变量和 globalData设置不一样,所以要做同步处理
  72. this['_' + key] = ori;
  73. return ori;
  74. } else {
  75. return undefined;
  76. }
  77. } else {
  78. return this['_' + key];
  79. }
  80. }
  81. })
  82. },
  83. resetUserInfo() {
  84. this.globalData.userInfo = {
  85. nickName: "",
  86. gender: 0,
  87. phoneNumber: "",
  88. avatarUrl: '',
  89. city: '',
  90. country: '',
  91. language: 'zh_CN',
  92. province: ''
  93. }
  94. },
  95. async login(slient: boolean = false) {
  96. let returnValue: undefined | GlobalUserInfo;
  97. const app = getApp<IAppOption>();
  98. !slient && wx.showLoading({
  99. title: "登录中..."
  100. })
  101. wx.login({
  102. success: async function (res) {
  103. if (res.code) {
  104. const code = res.code
  105. const result = await Login(code)
  106. if (result && result.token) {
  107. wx.hideLoading();
  108. wx.setStorageSync('token', result.token)
  109. wx.setStorageSync('isLogin', true)
  110. wx.setStorageSync('wxUserId', result.wxUserId)
  111. await app.setLogin(true)
  112. app.globalData.userInfo = Object.assign({}, app.globalData.userInfo, result)
  113. returnValue = app.globalData.userInfo
  114. } else {
  115. wx.hideLoading();
  116. Toast({
  117. selector: '#t-toast',
  118. message: '登录失败 !',
  119. });
  120. wx.setStorageSync('isLogin', false)
  121. await app.setLogin(false)
  122. console.error('登录失败')
  123. }
  124. }
  125. }
  126. });
  127. await sleep(1500)
  128. return Promise.resolve(returnValue)
  129. },
  130. async onLaunch() {
  131. },
  132. })