<template>
  <!-- 登录 -->
  <view
    style="
      padding: 0 44rpx;
      background-color: #fff;
      height: 100vh;
      overflow: hidden;
    "
  >
    <view class="" style="text-align: center; margin-top: 156rpx">
      <image
        src="../../static/images/logo.png"
        style="width: 350rpx; height: 350rpx"
        mode=""
      ></image>
    </view>
    <button
      open-type="getPhoneNumber"
      class="login-btn"
      @getphonenumber="tologin"
    >
      微信授权登录
    </button>


		<view>
			<u--input placeholder="请输入内容" border="surround" v-model="mobile"></u--input>
			<u--input placeholder="请输入内容" border="surround" v-model="code"></u--input>
		</view>
		<button @click="submit">登录</button>
		<button @click="submita">注册</button>
		<view class="" style="margin-top: 42rpx; display: flex">
			<u-checkbox-group v-model="checkboxValue1" placement="column" @change="checkboxChange">
				<u-checkbox shape="circle" :customStyle="{ marginBottom: '8px' }" :name="1">
				</u-checkbox>
			</u-checkbox-group>
			<text class="fontYin">我已阅读并同意</text>
			<text class="fontYin" style="color: #f83224">《用户协议》</text>
			<text class="fontYin">和</text>
			<text class="fontYin" style="color: #f83224">《隐私协议》</text>
		</view>
	</view>
</template>

<script>
	// import {conn} from '@/utils/WebIM.js'
	import {conn} from '../../utils/WebIM.js'
	import {
		login
	} from "../../network/api/homeApi.js";
	export default {
		data() {
			return {
				mobile: "",
				code: "",
				checkboxValue1: false,
				changea: [],
			};
		},
		methods: {
			//微信登录
			tologin(e) {
				if (this.changea.length > 0) {
					if (e.detail.code) {
						var phoneCode = e.detail.code;
						uni.login({
							success: (code) => {
								uni.$u.http
									.post("/api/wxmini/login", {
										code: code.code,
									})  
									.then((res) => {
										console.log(res);
										if (res == 10011) {
											uni.$u.http
												.post("/api/wxmini/mobile", {
													code: phoneCode,
												})
												.then((res) => {
													uni.$u.http
														.post("/api/register", {
															wx_code: code
																.code, //		是	String	微信登录时候的code
															mobile: res.mobile, //	是	String	手机号码
														})
														.then((res) => {
															console.log(res);
															uni.setStorageSync('user_no',res.easemob_username)
															uni.setStorageSync('pwd',res.easemob_password)
															var options = {
															  user: res.easemob_username,
															  pwd: res.easemob_password,
															  appKey: conn.appkey,
															  success: function (res2) {
																								
															  },
															  error: function(){
															  }
															};
															conn.open(options);
															uni.setStorageSync("token", res.token);
															uni.setStorageSync("openid", res
																.openid);
															uni.setStorageSync("user_info", res);
															setTimeout(() => {
																uni.navigateTo({
																	url: "/pageA/changelange",
																});
															}, 300);
														})
														.catch(() => {});
												})
												.catch(() => {});
										} else {
											uni.setStorageSync("token", res.token);
											uni.setStorageSync("openid", res.openid);
											uni.setStorageSync("user_info", res);
											setTimeout(() => {
												uni.navigateTo({
													url: "/pageA/changelange",
												});
											}, 300);
										}
									})
									.catch(() => {});
							},
						});
					}
				} else {
					this.$u.toast("请勾选隐私协议");
				}
			},
			checkboxChange(n) {
				console.log("change", n);
				this.changea = n;
			},
			submit() {
				// login({ mobile: "13000000000", password: "123456" }).then((res) => {
				//   console.log(res);
				//   if (res.data.code == 10000) {
				//     uni.setStorage({
				//       key: "token",
				//       data: res.data.data.token,
				//       success: function () {
				//         uni.switchTab({
				//           url: "../index/index",
				//         });
				//       },
				//     });
				//   }
				// });
				uni.$u.http
					.post("/api/login", {
						mobile: this.mobile,
						password: '123456',
					})
					.then((res) => {
						uni.setStorageSync("token", res.token);
						uni.setStorageSync("user_info", res);
						uni.setStorageSync('user_no',res.easemob_username)
						uni.setStorageSync('pwd',res.easemob_password)
						var options = {
						  user: res.easemob_username,
						  pwd: res.easemob_password,
						  appKey: conn.appkey,
						  success: function (res2) {
								console.log(res2);							
						  },
						  error: function(){
						  }
						};
						conn.open(options);
						uni.switchTab({
							url: "/pages/index/index",
						});
					})
					.catch((error) => {
						console.log(error);
					});
			},
			submita() {
				uni.$u.http
					.post("/api//mobile-register", {
						mobile: this.mobile,
						password: '123456',
					})
					.then((res) => {
						uni.$u.toast('注册成功')
					})
					.catch((error) => {
						console.log(error);
					});
			}
		},
	};

</script>

<style lang="scss" scoped>
page {
  background-color: #ffffff !important;
  height: 100vh;
}

.login-btn {
  width: 662rpx;
  height: 96rpx;
  background: #f83224;
  box-shadow: 0rpx 16rpx 40rpx -12rpx rgba(255, 21, 21, 0.5);
  border-radius: 48rpx;
  font-family: PingFangSC, PingFang SC;
  font-weight: 500;
  font-size: 32rpx;
  color: #ffffff;
  line-height: 96rpx;
  text-align: center;
  font-style: normal;
}

.fontYin {
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  font-size: 24rpx;
  color: #666666;
  line-height: 34rpx;
  text-align: left;
  font-style: normal;
}
</style>
