• <ul id="cgeq2"></ul>
  • 歡迎您光臨深圳塔燈網(wǎng)絡(luò)科技有限公司!
    電話圖標 余先生:13699882642

    網(wǎng)站百科

    為您解碼網(wǎng)站建設(shè)的點點滴滴

    解析并截取小程序二維碼上的參數(shù)(轉(zhuǎn)載)

    發(fā)表日期:2019-11 文章編輯:小燈 瀏覽次數(shù):12371

    小程序分享二維碼思路:
    a分享二維碼給b,二維碼上帶有a的標識ppid
    b接收a的二維碼打開頁面,將接收到的ppid傳遞給后臺,后臺就可以知道b是通過a的二維碼打開的頁面

    1.在后臺設(shè)置識別二維碼進入的頁面,這里用index頁面。
    2.在index頁面是接收二維碼里面的參數(shù),解析并截取獲取。二維碼的參數(shù)格式:scene=ppid:12

    export default {
        data() {
            return {
                ppid: "",
            }
        },
        onLoad(option) {
            // 解析二維碼里面的參數(shù)獲得ppid
            this.ppid = this.scene_decode(decodeURIComponent(option.scene)).ppid //封裝的scene_decode() 方法
            if(this.ppid) {
            //我這里是把ppid存進vuex,然后在b注冊時,把ppid傳給后臺
                this.saveppid(this.ppid)// ppid 存進vuex
            }
        },
        methods: {
        // 截取ppid的方法
            scene_decode(scene) {
                var _str = scene + "";
                var _str_list = _str.split(",");
                var res = {};
                for (var i in _str_list) {
                    var _tmp_str = _str_list[i];
                    var _tmp_str_list = _tmp_str.split(":");
                    if (_tmp_str_list.length > 0&&_tmp_str_list[0]) {
                        res[_tmp_str_list[0]] = _tmp_str_list[1] || null;
                    }
                }
                return res;
            }
        }
    }

    vuex里面存ppid的方法

    import Vue from 'vue'
    import Vuex from 'vuex'
    
    Vue.use(Vuex)
    
    const store = new Vuex.Store({
        state: {
            ppid: '',
        },
        mutations: {
            // 二維碼ppid
            saveppid(state,ppid) {
                state.ppid = ppid
                uni.setStorage({
                    key: 'ppid',
                    data: ppid
                })
                // console.log(state.ppid)
            },
        },
    })
    
    export default store

    3.b登陸注冊時,從vuex里面取出ppid傳給后臺

    _getuserinfo(res) {
        console.log(store.state.ppid)
        var that = this
        var userinfoDetails = {}
        userinfoDetails = res.detail.userInfo
        uni.getUserInfo({
          provider: 'weixin',
          success: function () {
            uni.login({
                success:function(res){
                    uni.showLoading({
                        title: '登陸中...',
                        mask: false
                    });
                    uni.request({
                        url: that.apiUrl + 'small/index/GetOpenid?code=' + res.code,
                        success: (res) => {
                            console.log(res)
                            if (res.data.openid) {
                                uni.setStorageSync('openid', res.data.openid)
                                userinfoDetails.openid = res.data.openid
                                //store.state.ppid取ppid,然后賦值給userinfoDetails.ppid
                                userinfoDetails.ppid = store.state.ppid || ''
                            }
                            if(res.data.status == 0) {
                                that.sendInfo(userinfoDetails) // 用戶還沒注冊過需調(diào)用此方法
                                console.log('我還沒有注冊')
                            } else if (res.data.status == 1) {
                                uni.showToast({
                                    title: '登錄成功',
                                    icon: 'success',
                                    duration: 2000
                                })
                                that.getUserData() // 調(diào)用獲取用戶信息的接口
                            } else {
                                uni.hideLoading()
                                uni.showToast({
                                    title: '登錄失敗',
                                    duration: 2000,
                                    icon:'none'
                                })
                            }
                        }
                    })
                }
            })
          }
        });
    },
    sendInfo(userinfoDetails) {
        var that = this
        uni.request({
            url: this.apiUrl + 'small/index/insertvip', //注冊接口
            data: userinfoDetails,
            method: 'POST',
            success: (res) => {
                if(res.data.userinfo == 1) {
                    uni.hideLoading()
                    uni.showToast({
                        title: '注冊成功',
                        icon: 'success',
                        duration: 2000
                    })
                    that.getUserData() // 調(diào)用獲取用戶信息的接口
                } else {
                    uni.hideLoading()
                    uni.showToast({
                        title: res.data.msg,
                        duration: 2000
                    })
                }
            }
        })
    },

    本頁內(nèi)容由塔燈網(wǎng)絡(luò)科技有限公司通過網(wǎng)絡(luò)收集編輯所得,所有資料僅供用戶學(xué)習(xí)參考,本站不擁有所有權(quán),如您認為本網(wǎng)頁中由涉嫌抄襲的內(nèi)容,請及時與我們聯(lián)系,并提供相關(guān)證據(jù),工作人員會在5工作日內(nèi)聯(lián)系您,一經(jīng)查實,本站立刻刪除侵權(quán)內(nèi)容。本文鏈接:http://www.juherenli.com/25262.html
    相關(guān)小程序