Skip to content
This repository has been archived by the owner on Nov 15, 2018. It is now read-only.

Commit

Permalink
fix(share): (#579) 在iOS的内置微信浏览器中分享出去的url地址不正确的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
mutoe committed Oct 23, 2018
1 parent ac7dd0e commit dd40151
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/mixin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import reload from "@/util/wechatShareForIOS.js";

export default {
data() {
return {
Expand All @@ -9,6 +11,14 @@ export default {
computed: {
currencyUnit() {
return this.$store.state.currency.unit;
},
isIosWechat() {
const ua = navigator.userAgent;
const wechatUA = ua.toLowerCase().match(/MicroMessenger/i);
const isIos = ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
const isWechat =
wechatUA !== null && wechatUA.toString() === "micromessenger";
return isIos && isWechat && this.$route.query.jxytime === undefined;
}
},
watch: {
Expand All @@ -27,9 +37,11 @@ export default {
},
methods: {
goBack(num = -1) {
window.history.length <= 1
? this.$router.push("/")
const fallIndex = this.isIosWechat ? 2 : 1;
window.history.length <= fallIndex
? this.$router.replace("/")
: this.$router.back(num);
}
},
reload: reload
}
};
6 changes: 5 additions & 1 deletion src/page/UserHome.vue
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,11 @@ export default {
val && this.updateData();
}
},
beforeMount() {
if (this.isIosWechat) {
this.reload(this.$router);
}
},
mounted() {
this.typeFilter = this.$refs.typeFilter;
this.bannerHeight = this.$refs.banner.getBoundingClientRect().height;
Expand Down
5 changes: 5 additions & 0 deletions src/page/feed/FeedDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ export default {
}
}
},
beforeMount() {
if (this.isIosWechat) {
this.reload(this.$router);
}
},
activated() {
if (this.feedID) {
this.comments = [];
Expand Down
5 changes: 5 additions & 0 deletions src/page/group/detail/GroupPostDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ export default {
created() {
this.fetchFeed();
},
beforeMount() {
if (this.isIosWechat) {
this.reload(this.$router);
}
},
activated() {
if (this.postId) {
if (this.postId !== this.oldID) {
Expand Down
6 changes: 6 additions & 0 deletions src/page/news/NewsDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ export default {
}
}
},
beforeMount() {
if (this.isIosWechat) {
this.$Message.info("reload");
this.reload(this.$router);
}
},
activated() {
if (this.newsID) {
if (this.newsID !== this.oldID) {
Expand Down
62 changes: 62 additions & 0 deletions src/util/wechatShareForIOS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { parse, stringify } from "querystring";

/**
* 为 ios 下的微信准备的刷新方法
* 因为在 ios 的微信下页面跳转后分享出去的链接地址还是原来落地页的地址
* 所以在每次地址跳转后使用 reload 方法重新加载地址
* refer https://github.com/slimkit/plus-small-screen-client/issues/579
*
* !! 如果微信端修复了这个问题请移除此文件并全局搜索调用此文件的地方删除之 !!
*
* @author mutoe <[email protected]>
*/

/**
* 浏览器标准刷新方法
* @param num 值为0,为了兼容vue-router的API
*/
const go = num => {
if (num === 0) {
location.reload(true);
}
};

/**
* 判断是否在IOS中
*/
const isIOS = () => /(iPhone|iPad|iPod)/i.test(navigator.userAgent);

/**
* 获取时间戳
*/
const getNowTimeStamp = () => `${Date.now()}`;

/**
* 处理url,获得更新时间戳之后的url
*/
const getUrl = () => {
let { origin, pathname, search, hash } = location;
if (search) {
const searchObj = parse(search.slice(1, search.length));
searchObj.jxytime = getNowTimeStamp();
search = `?${stringify(searchObj)}`;
} else {
search = `?jxytime=${getNowTimeStamp()}`;
}
return `${origin}${pathname}${search}${hash}`;
};

/**
* 防止缓存,强制刷新
* @param router vue-router实例
*/
function reload(router = { go: go }) {
if (isIOS()) {
// 为了兼容IOS
location.replace(getUrl());
} else {
router.go(0);
}
}

export default reload;

0 comments on commit dd40151

Please sign in to comment.