欢迎您光临深圳塔灯网络科技有限公司!
电话图标 余先生:13699882642

网站百科

为您解码网站建设的点点滴滴

解析并截取小程序二维码上的参数(转载)

发表日期:2019-11 文章编辑:小灯 浏览次数:11153

小程序分享二维码思路:
a分享二维码给b,二维码上带有a的标识ppid
b接收a的二维码打开页面,将接收到的ppid传递给后台,后台就可以知道b是通过a的二维码打开的页面

1.在后台设置识别二维码进入的页面,这里用index页面。
2.在index页面是接收二维码里面的参数,解析并截取获取。二维码的参数格式:scene=ppid:12

export default {
    data() {
        return {
            ppid: "",
        }
    },
    onLoad(option) {
        // 解析二维码里面的参数获得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) // 用户还没注册过需调用此方法
                            console.log('我还没有注册')
                        } else if (res.data.status == 1) {
                            uni.showToast({
                                title: '登录成功',
                                icon: 'success',
                                duration: 2000
                            })
                            that.getUserData() // 调用获取用户信息的接口
                        } 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() // 调用获取用户信息的接口
            } else {
                uni.hideLoading()
                uni.showToast({
                    title: res.data.msg,
                    duration: 2000
                })
            }
        }
    })
},

本页内容由塔灯网络科技有限公司通过网络收集编辑所得,所有资料仅供用户学习参考,本站不拥有所有权,如您认为本网页中由涉嫌抄袭的内容,请及时与我们联系,并提供相关证据,工作人员会在5工作日内联系您,一经查实,本站立刻删除侵权内容。本文链接:https://www.dengtar.com/25262.html
相关小程序
 八年  行业经验

多一份参考,总有益处

联系深圳网站公司塔灯网络,免费获得网站建设方案及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:余经理:13699882642

Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.