<style lang="scss" scoped>
	.IDidentification{
		width: 100%;
		height: 100vh;
		.box{
			width: 100%;
			padding: 15px;
			
		}
		.box-c{
			margin-top: 20px;
		}
		.box-c-item{
			width: 100%;
			height: 50px;
			display: flex;
			align-items: center;
			border-bottom: 1px solid #e2e2e2;
			
		}
		.box-c-item-l{
			width: 140rpx;
			font-size: 14px;
			color: #111111;
			font-weight: bold;
			
		}
		.box-c-item-r{
			width: 1px;
			flex: 1;
		}
		.btn{
			width: 100%;
			margin-top: 50px;
			display: flex;
			align-items: center;
			justify-content: center;
		}
		.btn-box{
			width: 400rpx;
			height: 88rpx;
			border-radius: 44rpx;
			color: #FFFFFF;
			font-size: 16px;
			display: flex;
			align-items: center;
			justify-content: center;
		}
		
	}
</style>
<template>
	<view class="IDidentification">
		<view class="box">
			<view style="font-size: 18px;font-weight: bold;">
				实名认证
			</view>
			<view style="font-size: 12px;color: #999999;">确保用户信息真实,承诺保障信息安全。</view>
			<view class="box-c">
				<view class="box-c-item">
					<view class="box-c-item-l">真实姓名</view>
					<view class="box-c-item-r">
						<input type="text" v-model="data.RealName" placeholder="请输入真实姓名">
					</view>
				</view>
				<view class="box-c-item">
					<view class="box-c-item-l">身份证号</view>
					<view class="box-c-item-r">
						<input type="idcard" v-model="data.IDCardNo" placeholder="请输入身份证号">
					</view>
				</view>
			</view>
			<view class="btn">
				<view class="btn-box" :style="{background:mainColor}" @click="gorenzheng">
					立即认证
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				pageTitle: '身份认证',
				mainColor:'',
				mall_UserInfo:null,
			
				Jumptype:0,
				data:{},
				UserId:0,
			}
		},
		created() {
			this.mainColor = this.$uiConfig.mainColor;
			this.mall_UserInfo = uni.getStorageSync("mall_UserInfo")?uni.getStorageSync("mall_UserInfo"):null;
			uni.setNavigationBarTitle({
			    title: this.pageTitle
			});
		},
		onLoad(options) {
			if(options && options.Jumptype){
				this.Jumptype = options.Jumptype
			}
			this.UserId = uni.getStorageSync("mall_UserInfo")?uni.getStorageSync("mall_UserInfo").UserId:null;
			this.getInfo()
		},
		methods:{
			getInfo(){
				this.request2(
					{
						url: '/api/AppletMiai/GetMiAiBaseIfo',
						data: { UserId:this.UserId}
					},
					res => {
						if(res.resultCode==1){
							this.data=res.data
						}
					}
				);
			},
			gorenzheng(){
				let regName =/^[\u4e00-\u9fa5]{2,4}$/;
				if(!regName.test(this.data.RealName)){ 
				  uni.showToast({
				  	title:'真实姓名填写有误',
					icon:'none'
				  })
				   return false; 
				 }
				let isIdCard = this.isIdCard(this.data.IDCardNo)
				if(isIdCard==false){
					uni.showToast({
					  	title:'身份证号填写有误',
						icon:'none'
					  })
				}else{
					this.data.birthday = this.getBirthdayFromIdCard(this.data.IDCardNo)
					this.getCallinterface()
				}
				 
			},
			getCallinterface(){
				let pages = getCurrentPages(); // 当前页面
				let beforePage = pages[pages.length - 2]; // 前一个页面
				
				this.request2({
						url: '/api/AppletMiai/GetSetMiAiBaseInfo',
						data: this.data
					},
					res => {
						if (res.resultCode == 1) {
							uni.showToast({
							    title: res.message,
							    duration: 2000,
								icon:"success",
								success:()=>{
									setTimeout(()=>{
										if(this.Jumptype == 0){
											uni.navigateBack({
											    delta: 1
											});
										}else if(this.Jumptype == 2){
											uni.navigateBack({
												 success: function() {
													 beforePage.$vm.getisAttestation(); // 执行前一个页面的方法
												 }
											});
										}
									},1000)
									
								}
							});
							
						}
					}
				);
			},
			getBirthdayFromIdCard(idCard) {  //获取出生日期
				var birthday = "";  
				if(idCard != null && idCard != ""){  
					if(idCard.length == 15){  
						birthday = "19"+idCard.substr(6,6);  
					} else if(idCard.length == 18){  
						birthday = idCard.substr(6,8);  
					}  
				  
					birthday = birthday.replace(/(.{4})(.{2})/,"$1-$2-");  
				}  
				  
				return birthday;  
			},
			isIdCard(idCard) {//身份证号码的验证
				// 15位和18位身份证号码的正则表达式
				var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
			
				// 如果通过该验证，说明身份证格式正确，但准确性还需计算
				if (regIdCard.test(idCard)) {
					if (idCard.length == 18) {
						var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10,
								5, 8, 4, 2); // 将前17位加权因子保存在数组里
						var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); // 这是除以11后，可能产生的11位余数、验证码，也保存成数组
						var idCardWiSum = 0; // 用来保存前17位各自乖以加权因子后的总和
						for (var i = 0; i < 17; i++) {
							idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
						}
			
						var idCardMod = idCardWiSum % 11;// 计算出校验码所在数组的位置
						var idCardLast = idCard.substring(17);// 得到最后一位身份证号码
			
						// 如果等于2，则说明校验码是10，身份证号码最后一位应该是X
						if (idCardMod == 2) {
							if (idCardLast == "X" || idCardLast == "x") {
								//alert("恭喜通过验证啦！");
								return true;
							} else {
								//alert("身份证号码错误！");
								return false;
							}
						} else {
							// 用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
							if (idCardLast == idCardY[idCardMod]) {
								//alert("恭喜通过验证啦！");
								return true;
							} else {
								//alert("身份证号码错误！");
								return false;
							}
						}
					}else{
						return true;
					}
				} else {
					//alert("身份证格式不正确!");
					return false;
				}
			}
		
		}
	
	}
</script>

