App.vue 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <script>
  2. const config = require('./static/config.js');
  3. export default {
  4. onLaunch: function() {
  5. let href = location.href;
  6. let code = this.getQueryString('code');
  7. /**
  8. * 解题思路,由于不可抗因素,导致必须想办法,现有如下想法
  9. * (一)如是不带code,则为自然进入,给他用微信加code;
  10. * (二)如果带了code,则有可能是分享(非图片分享)或者复制链接进来的
  11. * bug: 如果之前去掉code,则会进入(一)进入了死循环;
  12. * 想法:先去让他带着code去regist,成功则说明是新的code,失败则去掉code进入(一)
  13. */
  14. if(!code){
  15. let url = href.split('#')[0];
  16. url = encodeURIComponent(url);
  17. url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+config.appid+"&redirect_uri="+url+"&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect"
  18. window.location.href = url;
  19. }
  20. this.globalData.BASE_URL = config.BASE_URL;
  21. },
  22. onShow: function() {
  23. let shareToken = this.getQueryString('shareToken');
  24. let projectId = this.getQueryString('projectId');
  25. let pathName = this.getQueryString('pathName');
  26. this.globalData.projectId = projectId||'';
  27. this.globalData.shareToken = shareToken||'';
  28. this.globalData.pathName = pathName||'';
  29. this.regist();
  30. document.body.style.setProperty("background-color",this.globalData.color4);
  31. },
  32. onHide: function() {
  33. },
  34. methods: {
  35. getThemeInfo() {
  36. },
  37. getQueryString(name){
  38. var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s|&|$)", "i");
  39. return reg.test(location.href) ? unescape(RegExp.$2.replace(/\+/g, " ")) : ""
  40. },
  41. async regist() {
  42. let code = this.getQueryString('code');
  43. if(code){
  44. let ret = await this.$myRequest({
  45. url: "/regist",
  46. data: {
  47. "code": code,
  48. "shareToken":this.globalData.shareToken||"",
  49. "projectId":this.globalData.projectId||"",
  50. }
  51. });
  52. if (ret.data.success) {
  53. let userId = ret.data.single.userId;
  54. this.globalData.userId = userId;
  55. if (ret.data.single.authed == 1) {
  56. let token = ret.data.single.token;
  57. this.globalData.token = token
  58. }
  59. uni.$emit('request')
  60. }else{
  61. //注册失败,去掉老的code,会进入onLaunch,重新获取新的code
  62. let href = location.href;
  63. let hrefs = href.split('#');
  64. let shareToken = this.getQueryString('shareToken')
  65. let projectId = this.getQueryString('projectId')
  66. let state = this.getQueryString('state');
  67. let url = href.origin+href.pathname+"?shareToken="+shareToken+"&projectId="+projectId+"&state="+state+"&pathName="+hrefs[1];
  68. window.location.href = url;
  69. }
  70. }
  71. },
  72. },
  73. globalData: {
  74. token: "",
  75. userId:"",
  76. projectId:"",
  77. shareToken:"",
  78. pathName:"",
  79. color1: "#F07423",
  80. color2: "#FD8F3C",
  81. color3: "#FFC444",
  82. color4: "#F5F5F7",
  83. color5: "#F7A98E",
  84. color6: "#FFF4EB",
  85. color7:"#F8BA91",
  86. color8:"#FFF4EB",
  87. color9:"#8B654D",
  88. color10:"#FCF6F1",
  89. BASE_URL:"https://dm-api.elab-plus.cn",//默认环境
  90. }
  91. }
  92. </script>
  93. <style>
  94. </style>