123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- // app.ts
- import { Login } from './api/login'
- import Toast from 'tdesign-miniprogram/toast/index';
- const voiceCbs: any = []
- const voiceProps: VoiceProps = {
- noMute: false,
- pullUrls: [],
- pushUrl: false,
- }
- const loginCbs = []
- const loginProps = {
- isLogin: true
- };
- const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
- App<IAppOption>({
- sleep: sleep,
- globalData: {
- voiceProps,
- userInfo: {
- phoneNumber: '',
- nickName: '',
- country: '',
- avatarUrl: '',
- gender: 0,
- city: '',
- province: '',
- language: 'zh_CN'
- },
- token: '',
- isLogin: false,
- roomId: ''
- },
- addVoicePropsListener(cb: Function) {
- voiceCbs.push(cb)
- },
- removeVoicePropsListener(cb: Function) {
- voiceCbs.splice(voiceCbs.indexOf(cb), 1)
- },
- setVoiceProps(props: VoiceProps) {
- Object.keys(props).forEach((k: string) => {
- (voiceProps as any)[k] = (props as any)[k]
- })
- voiceCbs.forEach((cb: Function) => cb(props))
- },
- async setLogin(status: boolean): Promise<boolean> {
- this.globalData.isLogin = status
- return Promise.resolve(this.globalData.isLogin)
- },
- unwatch(key: string, method: Function) {
- },
- watch(key: string, method: Function) {
- var obj: GlobalDataProps = this.globalData;
- type Keys = keyof typeof obj;
- //加个前缀生成隐藏变量,防止死循环发生
- let ori = obj[key as Keys]; //obj[key]这个不能放在Object.defineProperty里
- if (ori) { //处理已经声明的变量,绑定处理
- method(ori);
- }
- Object.defineProperty(obj, key, {
- configurable: true,
- enumerable: true,
- set: function (value) {
- this['_' + key] = value;
- console.log('是否会被执行2', key)
- method(value);
- },
- get: function () {
- // 在其他界面调用key值的时候,这里就会执行。
- if (typeof this['_' + key] == 'undefined') {
- if (ori) {
- //这里读取数据的时候隐藏变量和 globalData设置不一样,所以要做同步处理
- this['_' + key] = ori;
- return ori;
- } else {
- return undefined;
- }
- } else {
- return this['_' + key];
- }
- }
- })
- },
- resetUserInfo() {
- this.globalData.userInfo = {
- nickName: "",
- gender: 0,
- phoneNumber: "",
- avatarUrl: '',
- city: '',
- country: '',
- language: 'zh_CN',
- province: ''
- }
- },
- async login(slient: boolean = false) {
- let returnValue: undefined | GlobalUserInfo;
- const app = getApp<IAppOption>();
- !slient && wx.showLoading({
- title: "登录中..."
- })
- wx.login({
- success: async function (res) {
- if (res.code) {
- const code = res.code
- const result = await Login(code)
- if (result && result.token) {
- wx.hideLoading();
- wx.setStorageSync('token', result.token)
- wx.setStorageSync('isLogin', true)
- wx.setStorageSync('wxUserId', result.wxUserId)
- await app.setLogin(true)
- app.globalData.userInfo = Object.assign({}, app.globalData.userInfo, result)
- returnValue = app.globalData.userInfo
- } else {
- wx.hideLoading();
- Toast({
- selector: '#t-toast',
- message: '登录失败 !',
- });
- wx.setStorageSync('isLogin', false)
- await app.setLogin(false)
- console.error('登录失败')
- }
- }
- }
- });
- await sleep(1500)
- return Promise.resolve(returnValue)
- },
- async onLaunch() {
- },
- })
|