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)文章
學(xué)習(xí)JavaScript設(shè)計模式(繼承)
這篇文章主要帶領(lǐng)大家學(xué)習(xí)JavaScript設(shè)計模式,其中重點介紹繼承,舉例說明為什么需要繼承,對繼承進行詳細剖析,感興趣的小伙伴們可以參考一下2015-11-11
基于JavaScript實現(xiàn) 網(wǎng)頁切出 網(wǎng)站title變化代碼
這篇文章主要介紹了基于JavaScript實現(xiàn) 網(wǎng)頁切出 網(wǎng)站title變化代碼的相關(guān)資料,需要的朋友可以參考下2016-04-04
js將long日期格式轉(zhuǎn)換為標準日期格式實現(xiàn)思路
本文為大家詳細介紹下js將long日期格式轉(zhuǎn)換為標準日期格式,感興趣的朋友可以參考下哈,希望可以幫助到你2013-04-04
JS賦值、淺拷貝和深拷貝(數(shù)組和對象的深淺拷貝)實例詳解
這篇文章主要介紹了JS賦值、淺拷貝和深拷貝,結(jié)合實例形式總結(jié)分析了JavaScript數(shù)組和對象的深淺拷貝相關(guān)概念、原理、操作技巧與使用注意事項,需要的朋友可以參考下2020-03-03

