//app.js var http = require('utils/util.js'); var md5 = require('utils/md5.js'); var commonJs = require('utils/common.js') let livePlayer = requirePlugin('live-player-plugin') App({ data:{ windowWidth:0,//屏幕的宽度 windowHeight:0,//屏幕的高度 share_open_id:'',//分享用户的opneid sale_id:'',//销售id systemInfo:{}, safeAreaTop:30 }, onLaunch: function () { var _this = this; wx.setStorageSync('is_guide_mask', false) this.checkSession() wx.getSystemInfo({ success (res) { _this.data.systemInfo = res if(res.platform == "ios"){ _this.data.safeAreaTop=res.statusBarHeight + 10 }else if(res.platform == "android"){ _this.data.safeAreaTop=res.statusBarHeight + 10 }else{ _this.data.safeAreaTop=res.statusBarHeight + 10 } } }) }, onShow(options) { console.log('onShow',options) if (options.scene == 1007 || options.scene == 1008 || options.scene == 1044) { livePlayer.getShareParams() .then(res => { // 房间号 console.log('get room id', res.room_id) // 用户openid console.log('get openid', res.openid) // 分享者openid,分享卡片进入场景才有 console.log('get share openid', res.share_openid) if(res.share_openid){ this.data.share_open_id = res.share_openid this.checkSession(()=>{ this.reportInviteInfo() }) } // 开发者在跳转进入直播间页面时,页面路径上携带的自定义参数,这里传回给开发者 console.log('get custom params', res.custom_params) }).catch(err => { console.log('get share params', err) }) } if(options.query.openid){//上报用户关系 this.data.share_open_id = options.query.openid this.checkSession(()=>{ this.reportInviteInfo() }) } if(options.query.scene){//记录销售用户关系 var obj = this.getQueryVariable(decodeURIComponent(options.query.scene)) console.log(obj) if(obj.sale_id){ this.data.sale_id = obj.sale_id this.checkSession(()=>{ this.salerShare() }) } if(obj.room_id){//去直播间 let customParams = encodeURIComponent(JSON.stringify({ path: 'pages/index/index', pid: 1 })) // 开发者在直播间页面路径上携带自定义参数(如示例中的path和pid参数),后续可以在分享卡片链接和跳转至商详页时获取,详见【获取自定义参数】、【直播间到商详页面携带参数】章节(上限600个字符,超过部分会被截断) wx.navigateTo({ url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${obj.room_id}&custom_params=${customParams}` })// 其中wx2b03c6e691cd7370是直播组件appid不能修改 } } }, checkSession (cb) {//判断微信登录是否过期 var _this = this; wx.checkSession({ success () { //session_key 未过期,并且在本生命周期一直有效 console.log(wx.getStorageSync('openid'),'openid') if(!wx.getStorageSync('openid')){ _this.login(cb) }else{//已在登录状态 console.log("已在登录状态") cb?cb():'' } }, fail() { console.log('过期,重新登录') // session_key 已经失效,需要重新执行登录流程 _this.login(cb) } }) }, login(cb){//获取openid var _this = this; wx.login({ success (res) { if (res.code) { // 发起网络请求 发送 res.code 到后台换取 openId, sessionKey, unionId wx.setStorageSync('isSpringBox', false) http.req('/api/wx/auth', 'get', { code: res.code, }, function (res) { if(res && res.errno == 200){ wx.setStorageSync('openid',res.rst.openid) wx.setStorageSync('session_key',res.rst.session_key) cb?cb():'' }else{ } },()=>{ }) } else { console.log('登录失败!' + res.errMsg) } } }) }, reportInviteInfo() {//上报分享关系 if(this.data.share_open_id == wx.getStorageSync('openid')){//排除自己邀请自己的情况 return; } http.req('/api/wxuser/share', 'post', { share_open_id: this.data.share_open_id, current_open_id: wx.getStorageSync('openid') }, (res) => { console.log('上报用户关系接口',res) if(res && res.errno == 200){ }else{ } },()=>{ }) }, salerShare(){//记录销售关系 http.req('/api/wxuser/salerShare', 'post', { saler_id: this.data.sale_id, openid: wx.getStorageSync('openid') }, (res) => { console.log('记录销售关系接口',res) if(res && res.errno == 200){ }else{ } },()=>{ }) }, shareEvent(res){//分享 var pages = getCurrentPages() //获取加载的页面 var currentPage = pages[pages.length-1] //获取当前页面的对象 var url = currentPage.route //当前页面url var options = currentPage.options options = Object.assign(options,{'openid':wx.getStorageSync('openid')}) console.log(url,options) var path = ''; for(var i in options){ path += '&' + i + '=' + options[i] } var path2 = url + "?" + path.substr(1) console.log(path2) console.log(res) if(res.from === 'button'){ //来自页面内转发按钮 } return{ title:'海鲜阿嫂', path: path2 } }, getQueryVariable(query){ var vars = query.split("&"); var arr = {} vars.forEach((item,index)=>{ var pair = item.split("=") Object.assign(arr,{[pair[0]]:pair[1]}) }) return arr; }, func: { req: http.req, md5: md5.hex_md5, getDay: commonJs.getDay, NumberHandle: commonJs.NumberHandle }, })