<template>
	<view>
		<view class="bg-img bg-mask flex-justify-center align-center" style="height: 300rpx;text-align: center;position: relative;">
			<image src="../../static/img/user-bg.png" mode="" style="position: absolute;top: 0;width: 100%;height: 100%;z-index: 0;"></image>
			<view class="padding-xl text-white" style="z-index: 9;">
				<view @tap="ChooseImage()" class="cu-avatar xl round" :style="avatar?'background-image:url('+avatar+');':'background-image:url(https://ossweb-img.qq.com/images/lol/web201310/skin/big99008.jpg);'"></view>
				<view style="text-align: center;margin-top: 15rpx;">
					{{account}}
				</view>
				<!-- <open-data type="userAvatarUrl"></open-data> -->
				<!-- <open-data type="userNickName"></open-data> -->
				<button v-if="loginBtn" class="padding-text text-lg" @tap="wxGetUserInfo">
					登录
				</button>
			</view>
		</view>
		<view class="cu-list menu sm-border card-menu margin-top box-sha">
			<view class="cu-item arrow">
				<navigator @tap="isLogin" class="content" hover-class="none" :url="login?'/workBenchPages/pages/personalInfo/personalInfo':''" open-type="navigate">
					<text class="margin-right-xs">个人信息</text>
				</navigator>
			</view>
			<view class="cu-item arrow">
				<navigator @tap="isLogin" class="content" hover-class="none" :url="login?'/workBenchPages/pages/bindPhone/bindPhone':''" open-type="navigate">
					<text class="margin-right-xs">绑定手机</text>
				</navigator>
			</view>
			<view class="cu-item arrow" v-if="skin">
				<navigator @tap="isLogin" class="content" hover-class="none" :url="login?'/workBenchPages/pages/messageCenter/messageCenter':''" open-type="navigate">
					<text class="margin-right-xs">消息中心</text>
					<!-- 消息提示-没说，先不加 -->
					<!-- <view style="top: 38rpx;left: auto;right: 57rpx;" class='cu-tag badge'>9</view> -->
				</navigator>
			</view>
			<view class="cu-item action">
				<view class="content padding-tb-sm">
					<text class="margin-right-xs">通知提醒</text>
				</view>
				<view class="action">
					<switch :disabled="!login" @tap="isLogin" @change="SwitchSex" :class="skin?'checked':''" :checked="skin?true:false"></switch>
				</view>
			</view>
		</view>
		
		<view class="cu-list menu sm-border card-menu margin-top box-sha">
			<!-- <view class="cu-item arrow">
				<view class="content">
					<text class="margin-right-xs">版本更新</text>
				</view>
				<view class="action">
					<text class="text-grey text-sm">已是最新版本</text>
				</view>
			</view> -->
			<view class="cu-item arrow" style="position: relative;">
				<navigator class="content" hover-class="none" url="/workBenchPages/pages/aboutUs/aboutUs" open-type="navigate">
					<text class="margin-right-xs">关于我们</text>
				</navigator>
				<view class="action" style="position: absolute;right: 90rpx;">
					<text class="text-grey text-sm">担保公司</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				skin: false,
				loginBtn:true,// 展示登录按钮
				login:false,// 判断登录否--
				avatar:'',// 头像
				account:'',// 昵称
				roleArr:[],// 保存权限
				time:false,// 防止连点
			}
		},
		onShow() {
			// 当token过期时
			if(!uni.getStorageSync('token')){
				this.loginBtn = true
				this.account = ''
			}
			// console.log(this.baseUrl,'base')
			
		},
		onLoad() {
		},
		methods: {
			SwitchSex(e) {// 开启消息提薪否
				if(!this.login){
					this.skin = false
					e.detail.value = false
					return uni.showToast({
						title:'请先登录后在进行操作',icon:'none'
					})
				}
				this.skin = e.detail.value
				// console.log(e.detail.value,'当前切换消息提示的接口');
				this.$http.get('/apiHrmsAuth/hrms/auth/userlogin/isRemind',{isRemind:e.detail.value?0:1}).then(res=>{
					// console.log(res,'触发提醒接口');
				})
				
				// if(e.detail.value){
					// 消息订阅接口,产品说改了,因为金融没法长期订阅,所以不要了// 避免后面又要了，暂留.
					// wx.requestSubscribeMessage({
					//   tmplIds: ['SDVYa12FdN1OjJCaB9uM-GmqepnZMr98xCpE0Uw4pPU'],
					//   success (res) {
					// 	  console.log(res,'调用消息提醒接口')
					//   }
					// })
					
				// }
			},
			wxGetUserInfo(){
				if(this.time) return
				// console.log('触发点击');
				this.time = true
				let _this = this,code = ''
				uni.login({
					success(res){
						// console.log(res,'login数据');
						code = res.code
					}
				})
				wx.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: infoRes => {
						// console.log(infoRes,'参数');
						_this.userInfo = infoRes.userInfo; //用户信息，微信头像，昵称等等
						let params = {
							code: code,
							encryptedData: infoRes.encryptedData,
							iv: infoRes.iv,
							identify: 0
						}
						_this.$http.post('/apiHrmsAuth/hrms/auth/userlogin/wxAuthLogin',params).then(res =>{
							_this.login = true
							_this.loginBtn = false
							_this.account = res.data.account
							_this.avatar = res.data.headPortaritUrl
							this.time = false
							uni.setStorageSync('account',res.data.account)
							uni.setStorageSync('headPortaritUrl',res.data.headPortaritUrl)
							uni.setStorageSync('token',res.data.token)
							_this.getNowRole()// 角色权限获取
							_this.getRouter()// 路由权限获取
							this.$http.get('/apiHrmsAuth/hrms/auth/userlogin/readIsRemind').then(res=>{
								// console.log(res,'取得是否获取提醒');
								this.skin = res.data==0?true:false
							})
							uni.showToast({
								title:'登录成功！'
							})
						}).catch((err)=>{
							// console.log(err.data,'抛出错误')
							this.time = false
							uni.showToast({
								title:err.data.message,icon:'none'
							})
						})
					},
					fail:(res)=> {
						this.time = false
						// uni.showToast({
						// 	title:'登录授权失败',
						// 	icon:'none',
						// })
					}
				});
			},
			isLogin(){
				if(!this.login) return uni.showToast({
					title:'请先登录后在进行操作',icon:'none'
				})
			},
			// 获取角色权限
			getNowRole(){
				return this.$http.get('/api-guarantee/dg-apply-amount-info/getRoles').then(res=>{
					uni.setStorageSync('roleArr',res.data)
					// console.log(uni.getStorageSync('roleArr'),'角色权限')
				}).catch(err=>{
				})
			},
			// 处理权限，把所有文字标识提取出来
			getName(data){
				if(!data||data.length==0) return
				data.map(e=>{
					this.roleArr.push(e.name)
					if(e.children){
						this.getName(e.children)
					}
				})
			},
			// 获取路由权限---仅做假判断
			getRouter(){
				return this.$http.get('/api-hrms/hrms/permission/getPermission').then(res=>{
					// console.log(res,'路由权限')
					let arr = [] 
					// 小程序端暂时仅需要这俩
					res.data.map(e=>{
						if(e.name==='客户资源管理系统'||e.name==="担保业务管理系统"||e.name==="保后业务管理系统"){
							arr.push(e)
						}
					}) 
					// 处理权限，提取name做标识
					this.getName(arr)
					// 储存路由权限做判断
					uni.setStorageSync('routerArr',this.roleArr)
					// console.log(this.roleArr,'获取所有的name')
				})
			},
			//上传图片--str区分是哪个上传图片
			ChooseImage() {
				if(!uni.getStorageSync('token')) return uni.showToast({
					title:'请先登录后在进行操作！',icon:'none'
				})
				let url = this.baseUrl+'/api-guarantee/dg-apply-amount-info/uploadFile'
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						uni.uploadFile({
							url: url,
							filePath: res.tempFilePaths[0],
							name: 'file',
							header: {
								"Content-Type": "multipart/form-data",
								"token": uni.getStorageSync('token')
							},
							success: (uploadFileRes) => {
								let res = JSON.parse(uploadFileRes.data),
								data = res.data
								this.$http.get('/api-hrms/hrms/user/update/url',{headPortraitUrl:res.data[0]}).then(res=>{
									this.avatar = data[0]
									uni.showToast({
										title:'更换头像成功！'
									})
								})
									
							}
						})
						
					}
				})
			},
			
		}
	}
</script>

<style  lang="scss" scoped>
page{
	background-color: #F5F5F5;
}
.box-sha{
	box-shadow: 0 0 16rpx #ccc;
}
.padding-text{
	text-align: center;
	height: 50rpx;
	
	margin-top: 15rpx;
	font-size: 25rpx;
	line-height: 52rpx;
}
/deep/ .cu-list.menu>.cu-item.arrow{
	padding-right: 0;
}
navigator{
	z-index: 9999;
}
/deep/ switch::before ,switch::after{
	display: none;
	content: '';
}
</style>
