小程序分享二维码思路:
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 }) } } })},