<template>

	<view class="login u-flex-col u-col-center">
		<image src="../../static/logo..png" class="logo" mode=""></image>
		<text class="text">房屋底价超市</text>
		<!-- #ifdef  MP-WEIXIN-->
		<button open-type="getPhoneNumber" class="login-btn" @getphonenumber="tologin">
			用户一键登录
		</button>
		<!-- #endif -->
		<!-- #ifdef  MP-TOUTIAO -->
		<button open-type="getPhoneNumber" class="login-btn" @getphonenumber="tologin1">
			抖音授权登录
		</button>
		<!-- #endif -->
	</view>

</template>

<script>
	import {
		mapState
	} from "vuex"
	export default {
		data() {
			return {
				phone: '',
				pwd: ''
			}
		},
		onLoad() {

		},
		computed: {
			...mapState(['config', 'defaultcity'])
		},
		methods: {
			setcity() {
				this.$u.post('/api/Index/location', {
					longitude: this.defaultcity.userLocation.split(',')[0],
					latitude: this.defaultcity.userLocation.split(',')[1],
					city_name: this.defaultcity.city
				})
			},
			tologin(e) {
				if (e.detail.code) {
					var phoneCode = e.detail.code
					uni.login({
						success: (code) => {
							this.$u.post('/api/Login/login', {
								code: code.code,
								program_num:2
							}).then(res => {
								if (res.code == 1) {
									uni.setStorageSync("token", res.data.token)
									if (!res.data.phone || 1) {
										this.$u.post('/api/Member/bind_Phone', {
											code: phoneCode,
											program_num:2
										}).then(res => {
											if (res.code == 1) {
												this.$u.toast("登录成功")
												this.$u.post('/api/Member/member_info').then(
													res => {
														uni.setStorageSync("hx_username",
															res.data.hx_username)
														this.$WebIM.conn.open({
															user: res.data.hx_username,
															pwd: "888888",
														}).then(() => {
															console.log(
																"login success"
															);
														}).catch((reason) => {
															console.log(
																"login fail",
																reason);
														});
													})
												this.setcity()
												setTimeout(() => {
													uni.navigateBack()
												}, 800)
											} else {
												this.$u.toast(res.msg)
												uni.removeStorageSync('token')
											}
										})
									} else {
										this.$u.toast("登录成功")
										this.setcity()
										setTimeout(() => {
											uni.navigateBack()
										}, 800)
									}
								} else {
									this.$u.toast(res.msg)
								}
							})
						}
					})
				}
			},
			tologin1() {
				tt.login({
					success: (code) => {
						this.$u.post('/api/Login/tou_login', {
							code: code.code
						}).then(res => {
							if (res.code == 1) {
								uni.setStorageSync("token", res.data.token)
								this.$u.toast("登录成功")
								this.$u.post('/api/Member/member_info').then(
									res => {
										uni.setStorageSync("hx_username", res.data.hx_username)
										this.$WebIM.conn.open({
											user: res.data.hx_username,
											pwd: "888888",
										}).then(() => {
											console.log(
												"login success");
										}).catch((reason) => {
											console.log("login fail",
												reason);
										});
									})
								this.setcity()
								setTimeout(() => {
									uni.navigateBack()
								}, 800)
								// }
							} else {
								this.$u.toast(res.msg)
							}
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.login {
		.login-btn {
			width: 470rpx;
			line-height: 88rpx;
			background: #1F7EFF;
			border-radius: 20rpx;
			text-align: center;
			font-size: 28rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			margin: 0 0 20rpx 0;
			padding: 0;
		}

		.logo {
			width: 200rpx;
			height: 200rpx;
			border-radius: 100rpx;
			margin-top: 280rpx;
			margin-bottom: 54rpx;
		}

		.text {
			color: #1F7EFF;
			font-size: 60rpx;
			letter-spacing: 10rpx;
			font-weight: 500;
			margin-bottom: 184rpx;
		}

		.login-btn::after {
			border: none;
		}
	}
</style>