uniapp 实现微信小程序手机号一键登录

app 和 h5 手机号一键登录,参考文档:uni-app官网

以下是uniapp 实现微信小程序手机号一键登录

1、布局

XML 复制代码
<template>
    <view class="mainContent">
        <image class="closeImg" @click="onCloseClick"
               src="quicklogin_close.png"></image>
        <view class="centerLayout">
            <image class="logoImg" src="quicklogin_logo.png"
                   mode="scaleToFill"/>
            <view class="phoneTxt">188****8888</view>
            <view class="serviceTxt">天翼账号提供认证服务</view>
            <button class="btnLogin" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
                本机号码一键登录
            </button>
            <view class="other" @click="onOtherLoginClick">其他登录方式</view>
        </view>
        <view class="agreementLayout">
            <u-checkbox-group v-model="checked" placement="column" @change="onCheckboxChange">
                <u-checkbox size="30rpx" activeColor="#ff1b1b" :name="true">
                </u-checkbox>
            </u-checkbox-group>
            <view class="agreementTxt">
                我已阅读并同意外卖狮
                <text style="color: #F72124;font-weight: bold"
                      @click="NavTo('/packageB/pages/Agreement/userAgreement')">用户协议
                </text>
                和
                <text style="color: #F72124;font-weight: bold"
                      @click="NavTo('/packageB/pages/Agreement/concealAgreement')">隐私政策
                </text>
                并使用本机号码登录
            </view>
        </view>
    </view>
</template>

<script setup>
import {Back, NavTo, SwcTo} from "@/utlis/uni_api";
import {getCurrentInstance, ref} from "vue";
import {wxGetPhoneNumber, wxPhoneNumberLogin} from "@/utlis/innerface/userLogin";

const checked = ref("")
let code = ""
let {proxy} = getCurrentInstance();

//微信获取手机号信息
const getPhoneNumber = (datas) => {
    console.log(datas)//微信小程序返回的相关数据
    uniLogin(datas)
}

const uniLogin = (datas) => {
    uni.login({
        provider: 'weixin',
        success: res => {
            console.log(res)
            code = res.code

            let params = {
                code: code,
                iv: datas.detail.iv,
                encryptedData: datas.detail.encryptedData
            }

            //请求后台,获取手机号和code
            wxGetPhoneNumber(params, proxy).then(res => {
                userLogin(res.code,res.phoneNumber)
            }).catch((err) => {
                console.log("wxGetPhoneNumber err = ", err)
            })
        }
    });
}

//关闭
const onCloseClick = () => {
    Back()
}

//一键号码登录
const userLogin = (sCode,phoneNumber) => {
    let params = {
        code: sCode,
        phoneNumber: phoneNumber
    }

    //后台登录
    wxPhoneNumberLogin(params, proxy).then(res => {
        uni.setStorageSync('token', res.data.accessToken)
        uni.setStorageSync('activityShowType', true)
        setTimeout(() => {
            SwcTo('/pages/Home/home')
        }, 200)
    }).catch((err) => {
        console.log("wxPhoneNumberLogin err = ", err)
    })
}

//其他号码登录
const onOtherLoginClick = () => {
    NavTo('/packageB/pages/Login/Login')
}

//复选框选中监听
const onCheckboxChange = (e) => {
    console.log(e)
}
</script>

<style lang="scss">
page {
    background-color: #F6F6F6;
}
</style>

<style scoped lang="scss">
.mainContent {
    width: 100%;
    height: 100%;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.closeImg {
    width: 50rpx;
    height: 50rpx;
    margin: 80rpx 0 0 40rpx;
}

.centerLayout {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin-top: 200rpx;
}

.logoImg {
    width: 130rpx;
    height: 130rpx;
    border-radius: 30rpx;
}

.phoneTxt {
    font-weight: bold;
    font-size: 34rpx;
    letter-spacing: 1rpx;
    color: #464646;
    margin-top: 14rpx;
}

.serviceTxt {
    font-size: 24rpx;
    letter-spacing: 1rpx;
    color: #D5D5D5;
    margin-top: 5rpx;
}

.btnLogin {
    width: 75%;
    background-color: #FF1A1D;
    color: #FFFCF7;
    font-size: 28rpx;
    text-align: center;
    border-radius: 40rpx;
    padding: 5rpx 0;
    letter-spacing: 1rpx;
    margin-top: 60rpx;
}

.other {
    font-size: 28rpx;
    font-weight: bold;
    color: #787878;
    letter-spacing: 1rpx;
    margin-top: 50rpx;
}

.agreementLayout {
    position: absolute;
    bottom: 5%;
    left: 50%;
    transform: translateX(-50%);
    width: 90%;
    color: #767676;
    font-size: 26rpx;
    display: flex;
    letter-spacing: 1rpx;
}

.agreementTxt {
    padding-top: 7rpx;
}
</style>
相关推荐
AI_AGENT_DEV_AI9 小时前
原生 APP 开发的开发优点
uni-app
weixin_4692738120 小时前
Notepad link:一款极简、免注册的在线记事本工具
notepad++
JackieDYH21 小时前
uniapp-通知滚动组件+轮播图组件封装-案例
uni-app·轮播图·通知
2501_915921431 天前
appuploader-cli 命令行上传 IPA 到 App Store Connect upload CI 集成
android·ci/cd·小程序·https·uni-app·iphone·webview
结网的兔子2 天前
【前端开发】UniApp 项目地图选型与Web-APP跨端迁移方案
前端·uni-app
java1234_小锋2 天前
【免费】基于Python的微信小程序网约车(打车,在线叫车,移动出行)系统(FastAPI+Vue3) 锋哥原创出品,必属精品
微信小程序·小程序·fastapi·网约车系统·在线叫车系统
2501_916007473 天前
Bundle ID 注册与管理 App ID 创建指南 命名规则 权限开关与删除注意事项
android·ios·小程序·https·uni-app·iphone·webview
PedroQue993 天前
uni-app路由插件化:解锁高效开发新姿势
前端·uni-app
2501_915909063 天前
iOS 证书创建与管理 类型限制 p12 密码与云备份实操
android·ios·小程序·https·uni-app·iphone·webview
anyup3 天前
【2026年8月】uView Pro 千星,还拿到了 GVP
前端·uni-app·github