239 lines
7.4 KiB
Vue
239 lines
7.4 KiB
Vue
<script>
|
||
let uri = require('urijs');
|
||
/**
|
||
* 用来处理从小程序拿到的参数
|
||
* */
|
||
function getUrlParams(url) {
|
||
try {
|
||
// 通过 ? 分割获取后面的参数字符串
|
||
let urlStr = url.split('?')[1]
|
||
// 创建空对象存储参数
|
||
let obj = {};
|
||
// 再通过 & 将每一个参数单独分割出来
|
||
let paramsArr = urlStr.split('&')
|
||
for (let i = 0, len = paramsArr.length; i < len; i++) {
|
||
// 再通过 = 将每一个参数分割为 key:value 的形式
|
||
let arr = paramsArr[i].split('=')
|
||
obj[arr[0]] = arr[1];
|
||
}
|
||
return obj
|
||
} catch {
|
||
return false
|
||
}
|
||
}
|
||
import store from '@/store/index';
|
||
export default {
|
||
globalData: {
|
||
// FPT上传地址
|
||
urlfileName:'https://jc.agrimedia.cn/discountt',
|
||
// parameters: {
|
||
// isWechat: true,
|
||
// token: "123456",
|
||
// cityInfor:{
|
||
// city: "天津市",
|
||
// lat: '39.093471137152775',
|
||
// lon: '117.12767279730903',
|
||
// }
|
||
// },
|
||
parameters: {}
|
||
},
|
||
onLaunch: function(parameters = {}) {
|
||
|
||
console.log(navigator.userAgent,'userAgent');
|
||
// 动态构建script标签,用于引入wxjs文件
|
||
var script = document.createElement('script');
|
||
script.src = "https://res.wx.qq.com/open/js/jweixin-1.6.0.js";
|
||
document.body.appendChild(script);
|
||
// 为适配旧版本项目代码中的取参逻辑此处为迭代后新版本传参并整合为旧版本参数格式
|
||
let basurl = uri(decodeURIComponent(window.location.href))
|
||
let querys = basurl.query(true);
|
||
console.log(querys,'querys');
|
||
if(Object.keys(querys)['length'] > 0){
|
||
if(querys?.para){
|
||
// 获取URL参数
|
||
var _para = getUrlParams(window.location.href);
|
||
console.log(_para,'_para');
|
||
// 小程序首页会直接进入店铺首页,首页无赋值店铺信息逻辑所再次处进入之后赋值
|
||
if(_para?.brand_id && typeof _para['brand_id'] != 'undefined' && typeof _para['brand_id'] != undefined && _para?.brand_name && typeof _para['brand_name'] != 'undefined' && typeof _para['brand_name'] != undefined){
|
||
store.commit('shopping/SETBRANDINFOR', { brand_id: _para['brand_id'], brand_name:decodeURIComponent(_para['brand_name'])});
|
||
};
|
||
|
||
// 参数初始化处理
|
||
if (typeof _para == 'object' && _para['para'] != undefined) {
|
||
console.log('存在参数');
|
||
// URL参数
|
||
var URLpara = decodeURIComponent(getUrlParams(window.location.href)['para']);
|
||
console.log(JSON.parse(URLpara),'*****');
|
||
try{
|
||
// 转换过来的URL参数为一个JSON字符串其中存在一个#/字符,直接使用JSON.parse会报错,所以将#/截取出来
|
||
var _r = URLpara.substr(0, URLpara.lastIndexOf("#/"));
|
||
this['globalData']['parameters'] = JSON.parse(`${_r}`);
|
||
}catch(e){
|
||
this['globalData']['parameters'] = JSON.parse(URLpara);
|
||
//TODO handle the exception
|
||
}
|
||
// 全局变量设置参数
|
||
console.log(this['globalData']['parameters'],'----');
|
||
// 合并其余参数
|
||
const other = JSON.parse(JSON.stringify(_para,(key,val)=>{
|
||
if(key === 'para') return undefined;
|
||
return val;
|
||
}))
|
||
this['globalData']['parameters'] = {...this['globalData']['parameters'],...other};
|
||
|
||
} else {
|
||
console.log('没有参数')
|
||
}
|
||
|
||
console.log(JSON.stringify(this['globalData']['parameters'],null,10), 'App Launch');
|
||
|
||
}else{
|
||
for(let key in querys){
|
||
console.log(querys);
|
||
querys[key] = JSON.parse(querys[key])
|
||
};
|
||
this['globalData']['parameters'] = { ...querys['userinfor'], ...querys['query'], timestamp:querys['timestamp']}
|
||
// 小程序首页会直接进入店铺首页,首页无赋值店铺信息逻辑所再次处进入之后赋值
|
||
if(this['globalData']['parameters']?.brand_id && typeof this['globalData']['parameters']['brand_id'] != 'undefined' && typeof this['globalData']['parameters']['brand_id'] != undefined && this['globalData']['parameters']?.brand_name && typeof this['globalData']['parameters']['brand_name'] != 'undefined' && typeof this['globalData']['parameters']['brand_name'] != undefined){
|
||
store.commit('shopping/SETBRANDINFOR', { brand_id: this['globalData']['parameters']['brand_id'], brand_name:decodeURIComponent(this['globalData']['parameters']['brand_name'])});
|
||
};
|
||
}
|
||
|
||
};
|
||
console.log(this['globalData']['parameters'],'测试取参');
|
||
return;
|
||
|
||
|
||
// 小程序进入H5逻辑
|
||
/**
|
||
* 小程序进入H5会携带从小程序中获取到的部分参数(利用encodeURIComponent函数编码,所以接收是需要decodeURIComponent函数解码)
|
||
* {
|
||
* token
|
||
* isWechat
|
||
* lat
|
||
* lon
|
||
* city
|
||
* }
|
||
*
|
||
* */
|
||
// 解码
|
||
// if (parameters['query']?.para)
|
||
// this['globalData']['parameters'] = JSON.parse(decodeURIComponent(parameters['query']['para']));
|
||
// console.log(this['globalData']['parameters'], 'globalData');
|
||
|
||
|
||
},
|
||
onShow: function() {
|
||
console.log('App Show');
|
||
// 全局初始化参数中存在isWechat属性则代表就是从小程序跳入
|
||
if (this['globalData']['parameters']?.isWechat) {
|
||
store.commit('SetcityInfor', this['globalData']['parameters']['cityInfor']);
|
||
|
||
} else {
|
||
// 反之则代表从APP进入H5
|
||
/**
|
||
* @获取用户位置信息
|
||
*/
|
||
// APP是由第三方开发,第三方开发文档中API-getLocation方法获取用户位置
|
||
// 因为IOS端会跳进Safari浏览器,而Safari浏览器会提示getLocation方法不存在所以此处进行判断打断执行
|
||
let isSafari = (/Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent));
|
||
console.log(isSafari, 'isSafari');
|
||
if (isSafari) return;
|
||
if (process.env.NODE_ENV !== 'development' && !this['globalData']['parameters']['isWechat']) {
|
||
getLocation().then(res => {
|
||
console.log(res, 'res');
|
||
store.commit('SetcityInfor', res);
|
||
});
|
||
}
|
||
}
|
||
},
|
||
onHide: function() {
|
||
console.log('App Hide')
|
||
},
|
||
}
|
||
</script>
|
||
|
||
<style lang="scss">
|
||
@import 'uview-ui/theme.scss';
|
||
@import "@/static/css/flex.css";
|
||
@import "@/static/css/themeColor.scss";
|
||
@import "@/static/css/icon/iconfont.css";
|
||
*::-webkit-scrollbar {
|
||
display: none;
|
||
}
|
||
*::-webkit-scrollbar-horizontal {
|
||
display: none;
|
||
}
|
||
/*每个页面公共css */
|
||
page {
|
||
width: 100%;
|
||
height: auto;
|
||
min-height: 100%;
|
||
position: relative;
|
||
|
||
}
|
||
|
||
* {
|
||
padding: 0;
|
||
margin: 0;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
image {
|
||
width: 100%;
|
||
height: 100%;
|
||
}
|
||
|
||
uni-button:after {
|
||
border: none !important;
|
||
}
|
||
|
||
// 点餐缺省样式
|
||
.default {
|
||
margin: auto;
|
||
color: #999999;
|
||
font-size: $FONTSIZE28;
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
margin-top: 150rpx;
|
||
|
||
.img {
|
||
width: 160rpx;
|
||
height: 132rpx;
|
||
margin-bottom: 15rpx;
|
||
}
|
||
}
|
||
@keyframes move {
|
||
|
||
/* 开始状态 */
|
||
0% {
|
||
background-color: #f1f2f4;
|
||
};
|
||
|
||
50%{
|
||
background-color: #E0E0E0;
|
||
}
|
||
|
||
/* 结束状态 */
|
||
100% {
|
||
background-color: #f1f2f4;
|
||
}
|
||
}
|
||
|
||
.skeleton {
|
||
width: 200rpx;
|
||
height: 200rpx;
|
||
animation-name: move;
|
||
animation-duration: 0.5s;
|
||
}
|
||
/* 适配 iPhoneX */
|
||
@supports (bottom: constant(safe-area-inset-bottom)) or (bottom: env(safe-area-inset-bottom)) {
|
||
.mescroll-safearea {
|
||
padding-bottom: constant(safe-area-inset-bottom);
|
||
padding-bottom: env(safe-area-inset-bottom);
|
||
}
|
||
}
|
||
|
||
</style>
|