app.js 12.3 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, checkUidAndSessionKey, getGlobalData } from './libs/login/login.js';
import { app_report } from './libs/services/appReport';
import {WeToast} from './vendors/toast/wetoast';
import {getYHStorageSync} from './utils/util'
import Yas from './utils/yas'

const store = createStore();

let yas;

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

class App extends Component {

  constructor(props) {
    super(props)
    this.WeToast = WeToast
    this.hasAwaken = false;
   // this.globalData = {
   //    userInfo: null,
   // }
   //  this.getUserInfo()

  }
	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',
      'pages/select-country/select-country',
      /**零元购*/
      // "pages/bindPhoneNumber/bindPhoneNumber",
      "pages/zeroSell/index",
      "pages/zeroSell/detail",
      "pages/zeroSell/myList",
      "pages/zeroSell/snapshootShare",
      "pages/zeroSell/h5Page"
    ],
		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'
			}]
		},
		"navigateToMiniProgramAppIdList": [
			"wx084ab813d88c594b"
		]
	};

  componentWillMount () {
    yas = new Yas(this);
    wx.setStorageSync('show_bind', false);
		let {setSystemInfo, wechatLogin, setSessionkey} = this.props;
    let options = this.$router.params;
		// 获取系统信息
		let systemInfo = Taro.getSystemInfoSync();
		let sid = new Date().getTime() + '';
		this.globalData = {
			systemInfo: systemInfo,
			sid,
			ch: options.scene
    }

    this.props.globalData.systemInfo = systemInfo;
    this.props.globalData.sid = sid;
    this.props.globalData.ch = options.scene;

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

		setSystemInfo(systemInfo);
		this.props.setUdid(udid.get());
		setSessionkey();
    app_report('start', 'LIFECYCLE', 'START', {}, this);
    let userInfo = Taro.getStorageSync('userInfo');
    if (userInfo && userInfo.uid) {
      checkUidAndSessionKey(userInfo.uid, userInfo.session_key).then(result => {
        if (result.statusCode === 200) {
          console.log('session_key 未过期');
        } else if (result.statusCode === 401) {
          console.log('session_key 已过期,请重新获取');
          Taro.setStorage({key: 'session_key', data: ''});
          Taro.setStorage({key: 'userInfo', data: ''});
          Taro.setStorage({key: 'unionID', data: ''});
          Taro.setStorage({ key: 'WXThird_session', data: '' });
          Taro.setStorage({ key: 'unionid', data: '' });
          Taro.setStorage({ key: 'user_union_type', data: '' });
        }
      }).catch(error => {
        console.log(error);
      });
    }
		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) {
        checkUidAndSessionKey(userInfo.uid, userInfo.session_key).then(result => {
          if (result.statusCode === 200) {
            console.log('session_key 未过期');
            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'
              });
            }
          } else if (result.statusCode === 401) {
            console.log('session_key 已过期,请重新获取');
            Taro.setStorageSync('session_key', '');
            Taro.setStorageSync('userInfo', '');
            Taro.setStorageSync('unionID', '');
            Taro.setStorageSync('WXThird_session', '');
            Taro.setStorageSync('unionid', '');
            Taro.setStorageSync('user_union_type', '');
            getGlobalData().userInfo = {};
            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'
              });
            }
          } else {
            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'
              });
            }
          }
        }).catch(error => {
          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'
            });
          }
        });
      } 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) {
        checkUidAndSessionKey(userInfo.uid, userInfo.session_key).then(result => {
          if (result.statusCode === 200) {
            console.log('session_key 未过期');
            if (userInfo && userInfo.uid && userInfo.session_key) {
              if (isLogined) {
                isLogined(true);
              }
              if (isLoginCallback) {
                isLoginCallback();
              }
            } else {
              if (isLogined) {
                isLogined(false);
              }
            }
          } else if (result.statusCode === 401) {
            Taro.setStorageSync('session_key', '');
            Taro.setStorageSync('userInfo', '');
            Taro.setStorageSync('unionID', '');
            Taro.setStorageSync('WXThird_session', '');
            Taro.setStorageSync('unionid', '');
            Taro.setStorageSync('user_union_type', '');
            let userInfo = Taro.getStorageSync('userInfo');
            getGlobalData().userInfo = {};
            if (userInfo && userInfo.uid && userInfo.session_key) {
              if (isLogined) {
                isLogined(true);
              }
              if (isLoginCallback) {
                isLoginCallback();
              }
            } else {
              if (isLogined) {
                isLogined(false);
              }
            }
          } else {
            let userInfo = Taro.getStorageSync('userInfo');
            if (userInfo && userInfo.uid && userInfo.session_key) {
              if (isLogined) {
                isLogined(true);
              }
              if (isLoginCallback) {
                isLoginCallback();
              }
            } else {
              if (isLogined) {
                isLogined(false);
              }
            }
          }
        });
      } else {
        if (isLogined) {
          isLogined(false);
        }
      }
    });
	}

	componentDidHide () {
		app_report('start', "LIFECYCLE", "STOP", {}, this);
        yas.report('YB_ENTER_BACKGROUND');
  }

  componentDidShow() {
    let options = this.$router.params;
    let activeWay = options && options.scene;
    let par = activeWay ? {activeWay} : {}
    app_report('start', 'LIFECYCLE', 'RESUME', par, this);
    yas.report('YB_ENTER_FOREGROUND');

    if (!this.hasAwaken) {
      let pathParam = '';
      if (options && options.query) {
        for (let Key in options.query) {
          if (!pathParam.length) {
            pathParam = Key + '=' + options.query[Key] + '';
          } else {
            pathParam = pathParam + '&' + '' + Key + '=' + options.query[Key] + '';
          }
        }
      }
      let path = '';

      if (options && options.path) {
        path = options.path;
      }
      if (pathParam !== '') {
        path = path + '?' + pathParam;
      }
      const params = {
        PAGE_PATH: path
      };

      this.hasAwaken = true;
      yas.report('YB_AWAKE_MP', params);
    }
  }

	componentCatchError () {}

  getUserInfo() {
    let value = getYHStorageSync('userInfo','app')
    if (value) {
      this.globalData.userInfo = value;
    }
    return value;
  }

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

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