App.vue 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. this.globalData.projectId = projectId||'';
  26. this.globalData.shareToken = shareToken||'';
  27. this.regist();
  28. document.body.style.setProperty("background-color",this.globalData.color4);
  29. },
  30. onHide: function() {
  31. },
  32. methods: {
  33. getThemeInfo() {
  34. },
  35. getQueryString(name){
  36. var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s|&|$)", "i");
  37. return reg.test(location.href) ? unescape(RegExp.$2.replace(/\+/g, " ")) : ""
  38. },
  39. async regist() {
  40. let code = this.getQueryString('code');
  41. if(code){
  42. let ret = await this.$myRequest({
  43. url: "/regist",
  44. data: {
  45. "code": code,
  46. "shareToken":this.globalData.shareToken||"",
  47. "projectId":this.globalData.projectId||"",
  48. }
  49. });
  50. if (ret.data.success) {
  51. let userId = ret.data.single.userId;
  52. this.globalData.userId = userId;
  53. if (ret.data.single.authed == 1) {
  54. let token = ret.data.single.token;
  55. this.globalData.token = token
  56. }
  57. uni.$emit('request')
  58. }else{
  59. //注册失败,去掉老的code,会进入onLaunch,重新获取新的code
  60. let url = location.origin+location.pathname;
  61. window.location.href = url;
  62. }
  63. }
  64. },
  65. },
  66. globalData: {
  67. token: "",
  68. userId:"",
  69. projectId:"",
  70. shareToken:"",
  71. pathName:"",
  72. color1: "#F07423",
  73. color2: "#FD8F3C",
  74. color3: "#FFC444",
  75. color4: "#F5F5F7",
  76. color5: "#F7A98E",
  77. color6: "#FFF4EB",
  78. color7:"#F8BA91",
  79. color8:"#FFF4EB",
  80. color9:"#8B654D",
  81. color10:"#FCF6F1",
  82. BASE_URL:"https://dm-api.elab-plus.cn",//默认环境
  83. }
  84. }
  85. </script>
  86. <style>
  87. </style>