UNIAPP實現(xiàn)微信小程序登錄授權(quán)和手機號授權(quán)功能(uniapp做微信小程序)
UNIAPP實現(xiàn)微信小程序登錄授權(quán)和手機號授權(quán)(uniapp做微信小程序)
描述:uniapp開發(fā)小程序,先授權(quán)用戶信息后再出現(xiàn)手機號授權(quán)的頁面進行手機號授權(quán)。完成后返回上一頁面并把信息存入后臺以及前臺緩存中,方便使用。
1.在uniapp的manifest.json進行微信小程序配置

2.封裝request請求api.js(如果已封裝可跳過)
const BASE_URL = 'xxxxxxxxxxxxxxxxxxxxx';
import func from '@/config/func.js';
export const myRequest = (url, method, data = {}, header = {}) => {
func.loading('正在加載中...')
return new Promise((resolve, reject) => {
uni.request({
url: BASE_URL + url,
method: method || 'GET',
header: {
'content-type': 'application/x-www-form-urlencoded'
} || header,
data: data || {},
success: (res) => {
uni.hideLoading();
let code = res.data.code;
if (code == 1) {
resolve(res.data.data)
} else {
func.alert(res.data.msg)
}
},
fail: (err) => {
uni.showToast({
title: '請求接口失敗',
icon: 'none'
})
reject(err)
}
})
})
}3.封裝微信授權(quán)登錄以及獲取手機號,之后把用戶信息數(shù)據(jù)傳入后臺。
import {
myRequest
} from '@/config/api.js';
// uni.login()封裝
const wxLogin = function(openid) {
return new Promise((resolve, reject) => {
uni.login({
success(res) {
if (res.code) {
resolve(res.code)
} else {
reject(res.errMsg);
}
}
})
})
}
/*微信小程序登錄*/
const wechatAppLogin = function() {
/*登錄提示*/
loading("正在授權(quán)")
uni.getUserProfile({
desc: '獲取用戶授權(quán)',
success: res => {
let userInfo = res.userInfo;
wxLogin().then(code => { // 引用uni.login()封裝
myRequest('getOpenid', 'POST', {
code: code
}) //獲取openid
.then(function(v) {
uni.hideLoading();
uni.setStorageSync("useInfo", res.userInfo);
uni.setStorageSync("openid", v.openid);
wx.navigateTo({
url: '/pages/login/index'
})
}, function(error) {
reject(error);
})
})
}
})
}
// 獲取手機號授權(quán)
const getPhoneNumber = function(event) {
let that = this;
let code = event.detail.code; //獲取手機code
var promise = new Promise(function(resolve, reject) {
uni.checkSession({
success: (res) => {
myRequest('getPhone', 'POST', {
code: code
}) //獲取手機號
.then(function(v) {
let phone = v.data;
let useInfo = uni.getStorageSync('useInfo')
wx.setStorageSync('mobile', mobile)
resolve(phone);
myRequest('login', 'POST', {
openid: uni.getStorageSync('openid'),
nickname: useInfo.nickName,
img: useInfo.avatarUrl,
phone: phone
}) //傳入后臺數(shù)據(jù)
.then(function(v) {
uni.navigateBack({
delta: 1
})
}, function(error) {
reject(error);
})
}, function(error) {
reject(error);
})
},
fail(err) {
login()
}
})
})
return promise;
}
module.exports = {
wechatAppLogin,
getPhoneNumber
}4.在頁面中引用,登錄頁面login.vue中:
<template>
<view class="">
<f-navbar title="登錄" navbarType='3'></f-navbar>
<view class="arvImg marCenter marT100 ">
<image class="imgz " src="/static/images/arv.png" alt=""></image>
</view>
<view class="FontCenter fontSize18 marT40 fontBold">
健身房
</view>
<view class="fontSize16 FontCenter marT80">
申請獲取以下權(quán)限
</view>
<button class="btnBig marT140" @click="getUserInfo" v-if="useInfo == ''">
微信賬號快捷登錄
</button>
<button class="btnBig marT140" v-else open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber" >
點擊獲取手機號
</button>
</view>
</template>
<script>
import func from '@/config/func.js';
export default {
data() {
return {
useInfo:wx.getStorageSync('useInfo')||''
}
},
methods:{
// 授權(quán)登錄
getUserInfo(){
func.wechatAppLogin()
},
// 手機號授權(quán)
onGetPhoneNumber(e){
func.getPhoneNumber(e)
}
}
}
</script>
<style>
page {
background-color: #fff;
}
</style>示例圖:


到此這篇關(guān)于UNIAPP實現(xiàn)微信小程序登錄授權(quán)和手機號授權(quán)(uniapp做微信小程序)的文章就介紹到這了,更多相關(guān)UNIAPP微信小程序登錄授權(quán)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
自適應(yīng)方案postcss-pxtorem使用步驟
這篇文章主要介紹了如何使用postcss-pxtorem插件將px單位轉(zhuǎn)換為rem單位,包括安裝插件、創(chuàng)建配置文件和引入腳本的步驟,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-11-11
JavaScript實現(xiàn)網(wǎng)頁加載進度條代碼超簡單
網(wǎng)頁進度條能夠更好的反應(yīng)當(dāng)前網(wǎng)頁的加載進度情況,loading進度條可用動畫的形式從開始0%到100%完成網(wǎng)頁加載這一過程。代碼簡單易懂,效果非常好,需要的一起學(xué)習(xí)學(xué)習(xí)吧2015-09-09

