app.js 4.84 KB
import Taro, { Component } from '@tarojs/taro'
import '@tarojs/async-await'
import { Provider, connect } from '@tarojs/redux'
import { bindActionCreators } from 'redux';
import Index from './pages/index'
import createStore from './store'
import * as globalData from './actions/globalData';
import udid from './utils/udid';
import './app.scss'
import event from './utils/event';
import { loginAction } from './libs/login/login.js'
import getPrivateKey from './libs/request/getPrivateKey.js'

const store = createStore();

@connect(({ globalData, user }) => ({
		globalData
}), (dispatch) => {
    return bindActionCreators({
		...globalData
	}, dispatch)
})

class App extends Component {
	config = {
		pages: [
			'pages/index/index',
			'pages/search/index',
			'pages/searchList/index',
			'pages/filter/index',
			'pages/productDetail/index',
			'pages/sell/index',
			'pages/classify/index',
			'pages/message/message/message',
      'pages/message/messageDetail/messageDetail',
			'pages/userCenter/index',
			'pages/login/index',
			'pages/orderList/index',
			'pages/nativeTest/nativeTest',
			'pages/userCenter/collection/collection',
			'pages/userCenter/addressManager/addressManager',
			'pages/userCenter/addressManager/addressEditer/addressEditer',
			'pages/userCenter/addressManager/addressEditer/addressPicker',
			'pages/order/orderConfirm/orderConfirm',
      'pages/order/orderSuccess/orderSuccess',
			'pages/order/orderProtocol/orderProtocol',
			'pages/nativeLogin/nativeLogin',
      'pages/myassets/index',
      'pages/orderDetail/index',
      'pages/orderList/orderlistcell',
      'pages/orderDetail/logistics/logistics',
      'pages/webview/webview'
    ], 
		window: {
			backgroundTextStyle: 'light',
			navigationBarBackgroundColor: '#fff',
			// navigationBarTitleText: 'WeChat',
			navigationBarTextStyle: 'black'
		},
		tabBar: {
			color: '#bbb',
			selectedColor: '#08304B',
			backgroundColor: '#fff',
			borderStyle: '#eee',
			list: [{
				pagePath: 'pages/index/index',
				text: '首页',
				iconPath: './static/images/tab-icon-01.png',
				selectedIconPath: './static/images/tab-icon-01-focus.png'
			},{
				pagePath: 'pages/classify/index',
				text: '分类',
				iconPath: './static/images/tab-icon-02.png',
				selectedIconPath: './static/images/tab-icon-02-focus.png'
			}, {
        pagePath: 'pages/message/message/message',
				text: '消息',
				iconPath: './static/images/tab-icon-03.png',
				selectedIconPath: './static/images/tab-icon-03-focus.png'
			}, {
				pagePath: 'pages/userCenter/index',
				text: '我的',
				iconPath: './static/images/tab-icon-04.png',
				selectedIconPath: './static/images/tab-icon-04-focus.png'
			}]
		}
	};

  componentWillMount () {
		let {setSystemInfo, wechatLogin, setSessionkey} = this.props;

		// 获取系统信息
		let systemInfo = Taro.getSystemInfoSync();

		systemInfo.screenHeight = !systemInfo.screenHeight ? systemInfo.windowHeight : systemInfo.screenHeight;
		systemInfo.screenWidth = !systemInfo.screenWidth ? systemInfo.windowWidth : systemInfo.screenWidth;

		setSystemInfo(systemInfo);
		this.props.setUdid(udid.get());
		setSessionkey();

		Taro.checkSession({
			success: () =>{
				//登录态未过期
				console.log('登录态未过期');
				this.props.setWxThirdSession();
				this.props.setUserInfo();
				this.props.setWxUnionId();
				// this.props.getUnionTypeWithUid(this.props.globalData.userInfo.uid); // 有赚
			},
			fail: () => {
				console.log('登录已经过期')
				//登录态过期
				Taro.setStorage({key: 'WXThird_session', data: ''});
				Taro.setStorage({key: 'userInfo', data: ''});
				Taro.setStorage({key: 'unionID', data: ''});
				Taro.setStorage({key: 'user_union_type', data: ''});
        Taro.setStorage({key: 'session_key', data: ''});
			}
		}).catch(error => {
      console.log(error);
    });
    
		event.on('user-is-login', (loginedCallBack, loginSuccess) => {
      let userInfo = Taro.getStorageSync('userInfo');
      if (userInfo && userInfo.uid && userInfo.session_key) {
        if (loginedCallBack) {
          loginedCallBack();
				}
			} else {
        event.on('my-user-login-success', () => {
          event.off('my-user-login-success');
          if (loginSuccess) {
            loginSuccess();
          }
        })
				Taro.navigateTo({
					url: '/pages/nativeLogin/nativeLogin'
				});
			}
		});

    event.on('judge-user-is-login', (isLogined, isLoginCallback) => {
      let userInfo = Taro.getStorageSync('userInfo');
      if (userInfo && userInfo.uid && userInfo.session_key) {
        if (isLogined) {
          isLogined(true);
          isLoginCallback();
        }
      } else {
        if (isLogined) {
          isLogined(false);
        }
      }
    });
	}

	componentDidHide () {}

	componentCatchError () {}

	render () {
		return (
		<Provider store={store}>
			<Index />
		</Provider>
		)
	}
}

Taro.render(<App />, document.getElementById('app'));