<template>
	<view class="lf-p-40 lf-w-100 lf-h-100 lf-row-center" style="height: 1200rpx;">
		<!-- <view class="lf-row-center lf-font-28">确认核销权限...</view> -->
		<button class="lf-m-t-30 cu-btn" v-if="haveVerfiy==false" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
			<text class="lf-font-32">确认核销</text>
		</button>
		<button class="lf-m-t-30 cu-btn1" v-else>
			<text class="lf-font-32">核销完成</text>
		</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: '',
				loadOptions: '',
				haveVerfiy: false,
				ifProduct: false,
				strVerfiy: ''
			}
		},
		onLoad(options) {
			this.wxlogin();
			this.userInfo = uni.getStorageSync('userinfo');
			this.loadOptions = options.scene;
			this.strVerfiy = this.loadOptions.slice(2,this.loadOptions.length);
			var search_0$ = this.loadOptions.slice(0,2);
			if(search_0$ == 'hy') {
				this.ifProduct = true
			}else {
				this.ifProduct = false;
			}
		},
		methods: {
			wxlogin(){
				return new Promise((resolve, rejecj) => {
					uni.login({
						complete: result => {
							if(result.errMsg == 'login:ok'){
								let code = result.code;
								this.$http(this.API.API_WXLOGIN, { code }).then(res => {
									console.log("获得用户信息", res);
									uni.setStorageSync('userinfo', res.data);
									resolve(res.data);
								})
							}
						}
					})
				})
			},
			// 普通产品核销
			getPhoneNumber(event){
				if(this.userInfo.mobile == '') {
					if(event.detail.errMsg == 'getPhoneNumber:ok'){
						let encryptedData = event.detail.encryptedData;
						let iv = event.detail.iv;
						this.$http(this.API.API_WECHAT_SETPHONE, {
							encryptedData,
							iv,
							// token: userInfo.token // 已在公共参数传
						}).then(res => {
							this.$msg('更新成功', {icon: 'success'});
							uni.setStorageSync('userinfo', res.data);
							if(this.ifProduct == true) {
								this.$http(this.API.API_CONFIRMPRODUCT_CODE, {verify_code: this.strVerfiy}).then(res => {
									this.$msg('核销成功!');
									this.haveVerfiy = true
								}).catch(err => {
									this.$msg(JSON.stringify(err.msg))
								});
							}else {
								this.$http(this.API.API_CONFIRM_CODE, {verify_code: this.strVerfiy}).then(res => {
									this.$msg('核销成功!');
									this.haveVerfiy = true
								}).catch(err => {
									this.$msg(JSON.stringify(err.msg))
								});
							}
						})
					}
				}else {
					if(this.ifProduct == true) {
						this.$http(this.API.API_CONFIRMPRODUCT_CODE, {verify_code: this.strVerfiy}).then(res => {
							this.$msg('核销成功!');
							this.haveVerfiy = true
						}).catch(err => {
							this.$msg(JSON.stringify(err.msg))
						});
					}else {
						this.$http(this.API.API_CONFIRM_CODE, {verify_code: this.strVerfiy}).then(res => {
							this.$msg('核销成功!');
							this.haveVerfiy = true
						}).catch(err => {
							this.$msg(JSON.stringify(err.msg))
						});
					}
				}
			},
		}
	}
</script>

<style scoped>
	.cu-btn {
		width: 260rpx;
		height: 260rpx;
		border-radius: 50%;
		background-color: #1998FE;
		color: white;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	.cu-btn1 {
		width: 260rpx;
		height: 260rpx;
		border-radius: 50%;
		background-color: #777;
		color: white;
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>
