<template>
	<view>
		<view class="content">
			<view class="item">
				<view>姓名:</view>
				<input placeholder="请输入姓名" type="text" v-model="uName" placeholder-class="input-placeholder" />
			</view>
			<view class="divider-line"></view>
			<view class="item">
				<view>电话:</view>
				<input placeholder="请输入联系电话" type="text" v-model="uPhone" placeholder-class="input-placeholder" />
			</view>
			<view class="divider-line"></view>
			<view class="item">
				<view>邮箱:</view>
				<input placeholder="请输入邮箱" type="text" v-model="uEmail" placeholder-class="input-placeholder" />
			</view>
			<view class="divider-line"></view>
		</view>
		<view class="uni-btn-v"><button class="save-btn" @tap="changInfo">保存</button></view>
	</view>
</template>

<script>
	import common from '../../common/common.js'
	import md5 from '../../common/md5.js';
	import CryptoJS from '../../common/crypto/crypto-js.js';
	var _self;
	export default {
		data() {
			const token = '';
			return {
				token: '',
				uName: '',
				uPhone: '',
				uEmail: '',
				uAvatar: '',
				uUserName: '',
				uUserPwd: '',
				appInfo: {
					deviceNo: '',
					appId: '240fbf7a-aa32-4cde-9674-5147a7d5eb2f',
					appVersion: 3
				}
			};
		},
		methods: {
			changInfo() {
				if (_self.checkParams()) {
					uni.showLoading({
						title: '修改中...'
					})
					var urlPath = common.userCenterBaseUrl + '/app/user/updateuserinfo';
					uni.request({
						method: 'PUT',
						url: urlPath,
						data: {
							'email': _self.uEmail,
							'name': _self.uName,
							'phone': _self.uPhone,
							'avatar': _self.uAvatar
						},
						header: {
							'token': _self.token,
						},
						success(res) {
							_self.doLogin()
						},
						fail(error) {
							uni.hideLoading()
							uni.showToast({
								title: '修改失败,请重试',
								duration: 2000
							})
							console.log(error);
						}
					})
				}
			},
			decrypt(word) {
				var str = common.decode(word.data);
				var key = CryptoJS.enc.Utf8.parse("CMXX_TOKEN_INFOS"); //十六位十六进制数作为密钥
				var iv = CryptoJS.enc.Utf8.parse("16-Bytes--String");
				var decryptedData = CryptoJS.AES.decrypt(str, key, {
					iv: iv,
					mode: CryptoJS.mode.CBC,
					padding: CryptoJS.pad.Pkcs7
				});
				var decryptedStr = CryptoJS.enc.Utf8.stringify(decryptedData);
				return decryptedStr;
			},
			doLogin() {
				uni.request({
					url: common.path('{baseUrl}/app/sign/login', [common.userCenterBaseUrl]),
					method: 'POST',
					data: {
						username: _self.uUserName,
						password: md5.md5(md5.md5(md5.md5(_self.uUserPwd))),
						deviceNo: _self.appInfo.deviceNo,
						appId: _self.appInfo.appId,
						appVersion: _self.appInfo.appVersion
					},
					success(response) {
						uni.hideLoading()
						if (response.statusCode === 200) {
							var data = response.data;
							var loginStr = _self.decrypt(data);
							try {
								uni.setStorageSync('appToken', data.data);
								uni.setStorageSync('userInfo', loginStr);
							} catch (e) {}
							uni.showToast({
								title: '修改成功',
								duration: 1000
							})
						} else if (response.statusCode === 400) {
							var data = response.data;
							uni.showToast({
								title: data.msg,
								duration: 1000,
								icon: 'none'
							})
						}

					},
					fail(response) {
						uni.showToast({
							title: '请求异常',
							duration: 1000,
							icon: 'none'
						})
					}
				});
			},
			checkParams() {
				if (_self.uName == '') {
					uni.showToast({
						title: '请输入姓名'
					})
					return false;
				}
				if (_self.uPhone == '') {
					uni.showToast({
						title: '请输入电话'
					})
					return false;
				}
				if (_self.uEmail == '') {
					uni.showToast({
						title: '请输入邮箱'
					})
					return false;
				}
				return true;
			}
		},
		onLoad() {
			_self = this;
			uni.getStorage({
				key: 'appToken',
				success: res => {
					_self.token = res.data;
				}
			});
			uni.getStorage({
				key: 'userInfo',
				success: res => {
					var userInfo = JSON.parse(res.data);
					_self.uName = userInfo.name
					_self.uPhone = userInfo.phone
					_self.uEmail = userInfo.email
					_self.uAvatar = userInfo.avatar
					_self.uUserName = userInfo.username
				}
			})
			uni.getStorage({
				key: 'deviceNo',
				success: res => {
					_self.appInfo.deviceNo = res.data
				}
			})
			uni.getStorage({
				key: 'pwd',
				success: res => {
					_self.uUserPwd = res.data
				}
			})
			_self.initDeviceNo()
		},
		onUnload() {},
		onShow() {},
		components: {}
	};
</script>

<style>
	.content {
		padding: 15rpx;
		margin-top: 50rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.uni-btn-v {
		width: 100%;
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.item {
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		border-width: 1rpx;
		border-bottom: #555555;
		width: 100%;
		padding: 15rpx;
	}

	.content .app-title {
		z-index: 99;
		font-size: 60rpx;
		color: #FFFFFF;
		border-bottom: 1rpx solid #FFFFFF;
	}

	.divider-line {
		height: 1px;
		margin-top: 5px;
		margin-bottom: 5px;
		width: 100%;
		background: #D8D8D8;
		align-self: center;
	}

	.body {
		display: flex;
		flex-direction: column;
		justify-content: center;
		text-align: center;
		align-items: center;
	}

	.content .uni-form-item {
		margin-bottom: 20rpx;
	}

	.title {
		text-align: left;
		font-size: 30rpx;
		color: #FFFFFF;
	}

	.uni-input {
		border: 1rpx solid #FFFFFF;
		background-color: #FFFFFF;
		height: 50rpx;
		padding: 10rpx;
		border-radius: 10rpx;
	}

	.save-btn {
		display: inline-block;
		width: 80%;
		align-self: center;
		height: 80rpx;
		line-height: 70rpx;
		background: #008B8B;
		color: #fff;
		font-size: 28rpx;
		border-radius: 40rpx;
		margin-top: 75rpx;
	}
</style>
