App.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. let ret = await this.$myRequest({
  44. url: "/regist",
  45. data: {
  46. "code": code,
  47. "shareToken":this.globalData.shareToken||"",
  48. "projectId":this.globalData.projectId||"",
  49. }
  50. });
  51. if (ret.data.success) {
  52. let userId = ret.data.single.userId;
  53. this.globalData.userId = userId;
  54. if (ret.data.single.authed == 1) {
  55. let token = ret.data.single.token;
  56. this.globalData.token = token
  57. }
  58. uni.$emit('request')
  59. }else{
  60. //注册失败,去掉老的code,会进入onLaunch,重新获取新的code
  61. let href = location.href;
  62. let hrefs = href.split('#');
  63. let shareToken = this.getQueryString('shareToken')
  64. let projectId = this.getQueryString('projectId')
  65. let state = this.getQueryString('state');
  66. let url = href.origin+href.pathname+"?shareToken="+shareToken+"&projectId="+projectId+"&state="+state+"&pathName="+hrefs[1];
  67. window.location.href = url;
  68. }
  69. },
  70. },
  71. globalData: {
  72. token: "",
  73. userId:"",
  74. projectId:"",
  75. shareToken:"",
  76. pathName:"",
  77. color1: "#F07423",
  78. color2: "#FD8F3C",
  79. color3: "#FFC444",
  80. color4: "#F5F5F7",
  81. color5: "#F7A98E",
  82. color6: "#FFF4EB",
  83. color7:"#F8BA91",
  84. color8:"#FFF4EB",
  85. color9:"#8B654D",
  86. color10:"#FCF6F1",
  87. BASE_URL:"https://dm-api.elab-plus.cn",//默认环境
  88. }
  89. }
  90. </script>
  91. <style>
  92. </style>