uni-app手机验证码登录

1,024 阅读1分钟
<view class="login-in">
    <input type="text" value="" placeholder="手机号"  v-model="keyword"/>
    <input type="text" value="" placeholder="请输入验证码" v-model="msg"/>
    <button class="yanzheng" @click="validation()">获取验证码</button>
</view>	
<view class="login-btn">
    <button @click="loginer()">登录</button>
</view>
export default {
    data() {
        return {
            keyword:"",
            msg:"",
            show:true,
            time:60
        }
    }
}
methods:{
	//获取验证码
    validation(){
        uni.request({
            url:"http://ceshi2.dishait.cn/api/v1/user/sendcode",
            method:"POST",
            data:{phone:this.keyword},
            success: (res) => {
                console.log(res)
                this.msg = res.data.msg
            }
        })
    },
    // 登录
    loginer(){
        uni.request({
            url:"http://ceshi2.dishait.cn/api/v1/user/phonelogin",
            data:{phone:this.keyword,code:this.msg},
            method:"post",
            success:res=>{
                if(res.data.msg=="登录成功"){
                    uni.setStorageSync("token",res.data.data.token)
                    uni.setStorageSync("username",res.data.data.username)
                    uni.setStorageSync("userpic",res.data.data.userpic)
                    uni.switchTab({
                        url:"/pages/medetail/detail"
                    })
                }
            }
        })
    },
}