Newer
Older
smartKitchenMiniProgram / app.js

require('regenerator-runtime')
import wxMqtt from './utils/mqtt/wxMqtt'
import { Provider } from './libs/wechat-weapp-redux.min';
import { configStore } from './utils/store/store';
import { judgeMyApi } from './utils/util/index'
const store = configStore();
let count = 0;

App(Provider(store)({

	onConnect : function(){
    let timer = setTimeout(() => {
      wxMqtt.connectMqtt();
      clearTimeout(timer);
    }, 500);
	},
	onLoad: function() {
		this.wxLogin();
		
	},
	
	onHide: function() {
		wxMqtt.destroyMqtt();
	},

	onUnload: function() {
		wxMqtt.destroyMqtt();
	},

  onShow: async function() {
		let access_token = await wx.getStorageSync('access_token')
		if(!access_token) return;
		await this.onConnect();

    this.closeInstance = wxMqtt.on('close', (errorMsg) => {
			if(count>5) return;
			count++;
      wxMqtt.destroyMqtt() && wxMqtt.reconnectMqtt();
      console.log('errorMsg: mqttClose', errorMsg);
    })

    this.errorInstance = wxMqtt.on('error', (errorMsg) => {
			if(count>5) return;
			count++;
      wxMqtt.destroyMqtt() && wxMqtt.reconnectMqtt();
      console.log('errorMsg: mqttError', errorMsg);
    })
	},
	wxLogin: async function () {
		let canIuse = await judgeMyApi({myApi:'getUserProfile',myVersion:'2.24.0'});
		if(!canIuse) return false;
		wx.showLoading({
			title: '正在登陆',
		})
		const res = await login();
		wx.hideLoading();
		if (res) {
			wx.showToast({
				title: '登录成功',
				duration: 1000
			})
			this.setData({
				isLogin: true
			})
			return true;
		}
		let timer = setTimeout(() => {
			wx.showToast({
				title: '登录失败',
				icon: 'error',
				duration: 1000
			})
			clearTimeout(timer);
			return false;
		}, 1800);
	},
}))