<template>
    <view class="content">
        <view class="btn">
            <!-- <button type="default" @click="scan">扫描</button> -->
        	<input ref="inputs" v-model="code" type="text" placeholder="请扫码或输入活动码" :focus="isfocus">
        </view>
		
		<view class="admin">
			<button type="primary" @click="usecode" plain="true">核销</button>
		</view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
				code:"",
				isfocus:true
            }
        },
		onShow() {
			uni.getNetworkType({
			    success: function (res) {
			        console.log(res.networkType);//网络类型 wifi、2g、3g、4g、ethernet、unknown、none
			        if(res.networkType === "none" ){
			            console.log("当前无网络");
						uni.showToast({
							icon:"none",
							title:"当前无网络",
							duration:2000
						})
			        }else{
			            
			        }
			    }
			});
		},
        methods: {
			usecode(){
				var url = 'https://yunyubang.ali251.langye.net/api/member/code_use';
				let that = this;
				that.isfocus = false
				uni.request({
					url:url,
					method:"GET",
					data:{
						code:that.code
					},
					success(res) {
						if(res.statusCode==200){
							if(res.data.errcode){
								uni.showToast({
									icon:"none",
									title:res.data.errmsg,
									duration:2000,
									complete() {
										that.code = "";
										that.isfocus=true
									}
								})
								
							}else{
								uni.showToast({
									icon:"none",
									title:'核销成功',
									duration:2000,
									complete() {
										that.code = ""
										that.isfocus=true
									}
								})
								
							}
						}else{
							uni.showToast({
								icon:"none",
								title:'请求失败',
								duration:2000,
								complete() {
									that.code = "";
									that.isfocus=true
								}
							})
						}
						
						
					},
					fail() {
						
					}
				})
			}
			
        }
    }
</script>

<style>
	.content{
		
	}
	.btn input{
		border: 1px solid #ddd;
		height: 40px;
		padding: 15px 10px;
		margin: 10px;
		margin-top: 30px;
		font-size: 26px;
	}
	.admin{
		text-align: center;
	}
	.admin button{
		margin:10px;
		margin-top:20px;
		padding: 10px;
		font-size: 24px;
		
	}
</style>