app.js 5.42 KB
import Taro, { Component } from '@tarojs/taro'
import { Provider, connect } from '@tarojs/redux'
import { bindActionCreators } from 'redux';
import Index from './pages/index'
import createStore from './store'
import userUtil from './utils/user';
import {common as commonModel} from  './models';

import globalData from './actions/globalData';
import * as user from './actions/user';

import './app.scss'

const store = createStore();

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

class App extends Component {
	config = {
		pages: [
			'pages/index/index',
			'pages/search/index',
			'pages/searchList/index',
			'pages/filter/index',
			// 'pages/brand/index',
			'pages/productDetail/index',
			'pages/classify/index',
			'pages/message/index',
			'pages/userCenter/index',
			'pages/login/index'
		],
		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/index',
				text: '消息',
				iconPath: './static/images/tab-icon-03.png',
				selectedIconPath: './static/images/tab-icon-03-focus.png'
			}, {
				pagePath: 'pages/index/index',
				text: '我的',
				iconPath: './static/images/tab-icon-04.png',
				selectedIconPath: './static/images/tab-icon-04-focus.png'
			}]
		}
	};

	// componentDidMount () {
	// 	let {setSystemInfo} = this.props;
	// 	console.log(this.props)
	// 	// 获取系统信息
	// 	let systemInfo = Taro.getSystemInfoSync();
	// 	console.log('系统信息')
	// 	console.log(systemInfo)
	// 	systemInfo.screenHeight = !systemInfo.screenHeight ? systemInfo.windowHeight : systemInfo.screenHeight;
	// 	systemInfo.screenWidth = !systemInfo.screenWidth ? systemInfo.windowWidth : systemInfo.screenWidth;

	// 	setSystemInfo(systemInfo);
	// 	// this.checkUDID();

	// 	// that.getSimplePise()
	// 	// this.checkNetworkType()

	// 	// if (options && options.scene){
	// 	// this.globalData.ch = options.scene
	// 	// }

	// 	// if (options && options.query && options.query.union_type){
	// 	// this.updateUnionType(options.query.union_type)
	// 	// }

	// 	Taro.checkSession({
	// 		success() {
	// 			//登录态未过期
	// 			this.getWechatThirdSession();
	// 			this.getUserInfo();
	// 			this.getUnionID();
	// 			this.getUnionTypeWithUid(this.getUid());
	// 		},
	// 		fail() {
	// 			//登录态过期
	// 			Taro.setStorage({
	// 				key: 'WXThird_session',
	// 				data: ''
	// 			});
	// 			Taro.setStorage({
	// 				key: 'userInfo',
	// 				data: {}
	// 			});

	// 			Taro.setStorage({
	// 				key: 'unionID',
	// 				data: ''
	// 			});

	// 			Taro.setStorage({
	// 				key: 'user_union_type',
	// 				data: ''
	// 			});
	// 		}
	// 	})
	// 	// this.getSessionkey();
	// 	// let timestamp = new Date().getTime() + ''
	// 	// this.globalData.sid = md5(timestamp);

	// 	// if (options){
	// 	// let channel = options.query.channel;
	// 	// let channelType = options.query.type;
	// 	// let params = { 'CHANNEL': channel, 'TYPE': channelType };
	// 	// logEvent(YB_LAUNCH_APP, params, this); 
	// 	// }
	// 	// setTimeout(function () {
	// 	// 	wechatLoginAction(function (response) {
	// 	// 		that.getShareInfo();
	// 	// 	});
	// 	// }, 1000);
	// 	// this.mtainit(options);
	// }

	async componentDidShow () {
		const a = await this.props.user.wechatLogin()

		console.log('-----------', this.props.user.openID)
	}

	componentDidHide () {}

	componentCatchError () {}

	isLogin() {
		let {globalData} = this.props;
	
		return globalData.userInfo.uid > 0 ? true : false;
	}

	getUid() {
		let uid = this.props.globalData.userInfo.uid;

		if (!uid) {
			let userInfo = this.getUserInfo();

			uid = userInfo && userInfo.uid;
		}
		return uid;
	}

	getWechatThirdSession() {
		let value = this.globalData.WXThird_session;

		return !value ? userUtil.getYHStorageSync('WXThird_session','app') : value;
	}

	getUserInfo() {
		let value = userUtil.getYHStorageSync('userInfo','app');

		if (value) {
			this.props.userInfo(value);
		}
		return value;
	}

	getUnionID() {
		let value = userUtil.getYHStorageSync('unionID','app');

		if (value) {
			this.props.setWxUnionId(value);
		}
	}
	
	/*
    获取用户相关union_type
  	*/
	getUnionTypeWithUid(uid) {
		commonModel.queryUnionTypeByUid({uid: uid}).then(ret => {
			if (ret && ret.code == 200) {
				this.props.setUnionInfo({
					userUnionType: ret.data.unionType,
					userUnionTypeImageUrl: ret.data.imageUrl,
					shareId: ret.data.shareId,
					userNounionTypeImageUrl: ret.data.applyImageUrl
				});
			}
		})
	}

	getSessionkey () {
		if (this.isLogin) {
		  var value = userUtil.getYHStorageSync('sessionkey','app')
		  if (value) {
			// console.log(value)
			this.globalData.sessionkey = value;
		  }
		}
		return undefined;
	}

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

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