uniapp微信小程序登錄,獲取手機號,支付,分享,消息訂閱功能


uniapp微信小程序登錄,獲取手機號,支付,分享,消息訂閱功能

 

二. 獲取手機號
在獲取完openid的前提下使用 getPhoneNumber獲取到加密的手機號,傳給后端解密即可。
// html
    <button v-if="theStep == 2" class="bottom" type="primary" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">手機號授權登錄</button>

// js 
// 獲取手機號
        getPhoneNumber(e) {
            const _that = this;
            if (e.detail.errMsg == 'getPhoneNumber:ok') {
                //  允許授權,獲取手機號
                // 傳給后端解密,包括基本信息傳給后端保存()
                this.$u
                    .post('/decodeUserInfo', {
                        encrypted: e.detail.encryptedData,
                        iv: e.detail.iv,
                        openid: _that.openId,
                        avatarUrl: _that.wxPublicInfo.avatarUrl, //頭像
                        city: _that.wxPublicInfo.city, //城市
                        country: _that.wxPublicInfo.country, //國家
                        nickName: _that.wxPublicInfo.nickName, //昵稱
                        province: _that.wxPublicInfo.province //
                    })
                    .then(res => {
                        if (res.code == 200) {
                            var phone = res.obj; //解析好的手機號
                            // 登錄- 獲取token
                            this.$u
                                .post('/memberLogin', {
                                    account: phone,
                                    password: '123456'
                                })
                                .then(memberLoginRes => {
                                    console.log(memberLoginRes);
                                    if (memberLoginRes.code == 200 && memberLoginRes.obj) {
                                        uni.setStorageSync('pk_token', memberLoginRes.obj.tokenHead + ' ' + memberLoginRes.obj.token);
                                        // 獲取基本信息
                                        this.$u.get('/info').then(suc => {
                                            if (suc.code == 200) {
                                                uni.setStorageSync('userInfo', suc.obj); //用戶基本信息
                                                uni.setStorageSync('openId', _that.openId); //保存openid
                                                uni.setStorageSync('firstAuthorization', true); //登錄狀態
                                                // 不是游客-刪除
                                                uni.removeStorageSync('touristWxPublicInfo');
                                                getApp().globalData.userInfo = suc.obj;
                                                // console.log(getApp().globalData)
                                                uni.redirectTo({
                                                    url: '/pagesHome/home/home'
                                                });
                                            }
                                        });
                                    } else if (memberLoginRes.code == 500) {
                                        // 未注冊會員
                                        uni.showModal({
                                            title: '溫馨提示',
                                            content: '您還不是名子瑜伽會員,可以選擇游客模式訪問',
                                            success: function(res) {
                                                if (res.confirm) {
                                                    uni.redirectTo({
                                                        url: '/pages/login/login'
                                                    });
                                                } else if (res.cancel) {
                                                    console.log('用戶點擊取消');
                                                }
                                            }
                                        });
                                    }
                                });
                        } else {
                            // 解析手機號錯誤
                            uni.showToast({
                                title: '手機號錯誤',
                                icon: 'none'
                            });
                        }
                    });
            } else {
                //拒絕授權后彈出一些提示
                uni.showToast({
                    title: '已拒絕手機號授權登錄',
                    icon: 'none'
                });
                setTimeout(function() {
                    uni.redirectTo({
                        url: '/pages/login/login'
                    });
                }, 1000);
            }
        },

三. 微信支付
前端將訂單信息(openid,商品信息,付款人等)傳給后,后端返回拉起支付所需要的參數,前端拉起支付,前端根據支付狀態做對應的操作,返回支付狀態給后端
var payData=JSON.parse(res.obj.signPackage);//拉起支付所需字段
                            var orderNumber=res.obj.merchantOrderSn;//訂單號
                            uni.requestPayment({
                                provider: 'wxpay',
                                _debug:1,
                                timeStamp: payData.timeStamp,
                                nonceStr: payData.nonceStr,
                                package: payData.package,
                                signType: payData.signType,
                                paySign: payData.paySign,
                                success: function (resPay) {
                                    console.log('success:' + JSON.stringify(resPay));
                                    // var resPay=JSON.parse(resPay).errMsg;
                                    _that.$u.post('/payState',{
                                        orderNumber:orderNumber,//訂單號
                                        payState:1,//訂單支付狀態:1為支付成功,0為預支付,2為支付失敗
                                    }).then(resPayState=>{
                                        if(resPayState.code==200){
                                            uni.showToast({
                                                title:'支付成功!',
                                            })
                                            setTimeout(function(){
                                                uni.redirectTo({
                                                    url:'/pages/exchangeRecord/exchangeRecord'
                                                });
                                            },1000)
                                        }
                                        
                                    })
                                    
                                },
                                fail: function (err) {
                                     console.log('fail:' + JSON.stringify(err));
                                    uni.showToast({
                                        title:'支付失敗!',
                                        icon:'none'
                                    })
                                    setTimeout(function(){
                                        uni.redirectTo({
                                           url:'/pages/exchangeRecord/exchangeRecord'
                                        });
                                    },1000)
                                    
                                    
                                   
                                }
                            });

 

四. 分享
使用onShareAppMessage() 和onShareTimeline() 就可以分享,這兩個函數和onLoad()同級
頁面中自定義按鈕分享需要加 open-type=“share”
//html 
<button open-type="share">分享</button>

// js
    onLoad(option) {},
    // 分享給朋友
    onShareAppMessage(res) {
        if (res.from === 'button') {
            // 來自頁面內分享按鈕
            console.log(res.target);
        }
        return {
            title: '標題',
            path: 'pages/exchangeShop/exchangeShop',
            imageUrl: '/static/common/mzlogo.png'
        };
    },
    // 分享到朋友圈
    onShareTimeline() {
        if (res.from === 'button') {
            // 來自頁面內分享按鈕
            console.log(res.target);
        }
        return {
            title: '標題',
            path: 'pages/exchangeShop/exchangeShop',
            imageUrl: '/static/common/mzlogo.png'
        };
    },

// css  清除 button 默認樣式,
button::after {
    border: none;
    background-color: none;
}

 

五. 消息訂閱
微信小程序的消息訂閱需要先在小程序后台添加消息模板
訂閱一次只能發送一次消息
// 彈出消息訂閱
        newsSubscription() {
            // 獲取用戶的當前設置,判斷是否點擊了“總是保持以上,不在詢問”
            uni.getSetting({
                withSubscriptions: true, //是否獲取用戶訂閱消息的訂閱狀態,默認false不返回
                success(res) {
                    if (res.authSetting['scope.subscribeMessage']) {
                        uni.openSetting({
                            // 打開設置頁
                            success(resSet) {
                                console.log(resSet.authSetting);
                            }
                        });
                    } else {
                        // 用戶沒有點擊“總是保持以上,不再詢問”則每次都會調起訂閱消息
                        uni.requestSubscribeMessage({
                            tmplIds: ['模板id'], //
                            success(res) {
                                console.log(res);
                                if (res['字段'] == 'accept') {
                                    // 字段就是tmplIds模板id
                                    // ....
                                }
                            }
                        });
                    }
                }
            });
        },

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM