gzhGo.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. </template>
  3. <script>
  4. export default {
  5. data() {
  6. return {
  7. }
  8. },
  9. onLoad() {
  10. var self = this;
  11. //#ifdef H5
  12. var code = this.getQueryString("code");
  13. var backUrl = this.getQueryStringBack("backUrl");
  14. var openId = this.getQueryString("state");
  15. self.request({
  16. action : 'wx/getAccessToken',
  17. data : {
  18. code : code,
  19. openId : openId
  20. },
  21. success (res) {
  22. var domain = window.location.href;
  23. // 用户已绑定
  24. if (res.data.code == '200') {
  25. var userInfo = res.data.data.userInfo;
  26. uni.setStorageSync('userInfo', userInfo);
  27. uni.setStorage({
  28. key: 'Authorization',
  29. data: res.data.data.wxLoginToken,
  30. success: function () {
  31. // 是消息推送页 且 是本人微信打开
  32. if (!!backUrl && res.data.data.sameOpenId) {
  33. var checkingUrl = domain.split("?")[0] + "#" + backUrl;
  34. window.location.href = checkingUrl;
  35. // 跳转主页
  36. } else {
  37. var checkingUrl = domain.split("?")[0] + "#" + '/pages/home/task';
  38. window.location.href = checkingUrl;
  39. }
  40. }
  41. });
  42. // 获取到用户unionid,未绑定用户
  43. } else if (res.data.code == '300') {
  44. var checkingUrl = domain.split("?")[0] + "#" + '/pages/index/checking';
  45. window.location.href = checkingUrl + '?openId='+res.data.data.openid + '&unionid='+res.data.data.unionid
  46. }
  47. }
  48. })
  49. // #endif
  50. },
  51. methods:{
  52. getQueryString(name) {
  53.   var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  54.   var reg_rewrite = new RegExp("(^|/)" + name + "/([^/]*)(/|$)", "i");
  55.   var r = window.location.search.substr(1).match(reg);
  56.   var q = window.location.pathname.substr(1).match(reg_rewrite);
  57.   if(r != null){
  58.     return unescape(r[2]);
  59.   }else if(q != null){
  60.     return unescape(q[2]);
  61.   }else{
  62.     return null;
  63.   }
  64. },
  65. getQueryStringBack(name) {
  66. let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  67. if(window.location.hash.indexOf("?") < 0){
  68. return null;
  69. }
  70. let r = window.location.hash.split("?")[1].match(reg);   
  71. if (r != null) return decodeURIComponent(r[2]);
  72.    return null;
  73. }
  74. }
  75. }
  76. </script>
  77. <style>
  78. </style>