App.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <script>
  2. import {appId} from './common/config'
  3. export default {
  4. onLaunch: function () {
  5. this.createUser();
  6. //判断是h5 然后是微信浏览器
  7. // #ifdef H5
  8. if (this.is_wx()) {
  9. if (!uni.getStorageSync("openId")) {
  10. if (this.getCode() == "") {
  11. // const currenturl = location.href.split("#")[0];
  12. uni.setStorageSync("currentUrl",window.location.href.split("#")[0])
  13. const currenturl = encodeURIComponent(window.location.href.split("#")[0]);
  14. //访问这个链接 redirect_uri 传入当前路径---执行后重新跳转到当前页面--但是路径上会带上code参数
  15. window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${currenturl}&response_type=code&scope=snsapi_base#wechat_redirect`;
  16. } else {
  17. let code = this.getCode();
  18. //拿着code 传输给后台
  19. this.$request
  20. .get({
  21. url: `mp/api/code2accesstoken/${code}`,
  22. // loadingTip: "加载中...",
  23. data: {},
  24. })
  25. .then((res) => {
  26. //将openId参数放进缓存
  27. uni.setStorageSync('openId',res.data.openid)
  28. });
  29. }
  30. }
  31. }
  32. // #endif
  33. },
  34. onShow: function () {
  35. console.log("App Show");
  36. },
  37. onHide: function () {
  38. console.log("App Hide");
  39. },
  40. methods: {
  41. //获取code
  42. getCode() {
  43. let code = "";
  44. let url = window.location.search;
  45. if (url.indexOf("?") !== -1) {
  46. var strings = url.substring(1).split("&");
  47. for (var i in strings) {
  48. if (strings[i].indexOf("code") === 0) {
  49. code = strings[i].split("=")[1];
  50. }
  51. }
  52. }
  53. return code;
  54. },
  55. //判断是否是微信浏览器i
  56. is_wx() {
  57. let en = window.navigator.userAgent.toLowerCase();
  58. // 匹配en中是否含有MicroMessenger字符串
  59. if (en.match(/MicroMessenger/i) == "micromessenger") {
  60. return true;
  61. } else {
  62. return false;
  63. }
  64. },
  65. //微信浏览器h5 调用登录
  66. //查询用户信息
  67. createUser() {
  68. let _this = this;
  69. // #ifdef H5
  70. this.$request
  71. .get({
  72. url: "system/temporaryUser",
  73. loadingTip: "加载中...",
  74. })
  75. .then((res) => {
  76. //当是微信浏览器的时候
  77. uni.setStorageSync("user", res.data.user);
  78. uni.setStorageSync("token", res.data.token);
  79. });
  80. // #endif
  81. // #ifdef MP-TOUTIAO
  82. tt.login({
  83. success(res) {
  84. let para = {
  85. code: res.code,
  86. petName: "",
  87. modelPhone: "",
  88. };
  89. _this.$request
  90. .post({
  91. url: "api/douyin/code2Session",
  92. data: para,
  93. })
  94. .then(
  95. (res) => {
  96. console.log(res);
  97. uni.setStorageSync("user", res.data.user);
  98. uni.setStorageSync("token", res.data.token);
  99. uni.setStorageSync("openId", res.data.openId);
  100. },
  101. (err) => {
  102. console.log("登录失败", err);
  103. }
  104. );
  105. },
  106. });
  107. // #endif
  108. },
  109. },
  110. };
  111. </script>
  112. <style>
  113. /*每个页面公共css */
  114. @font-face {
  115. font-family: "Alibaba PuHuiTi 2.0";
  116. src: url("https://test.jue-ming.com:8849/api/show?filePath=./webo/font/AlibabaPuHuiTi-2-55-Regular/AlibabaPuHuiTi-2-55-Regular.woff2")
  117. format("woff2"),
  118. url("https://test.jue-ming.com:8849/api/show?filePath=./webo/font/AlibabaPuHuiTi-2-55-Regular/AlibabaPuHuiTi-2-55-Regular.woff")
  119. format("woff"),
  120. url("https://test.jue-ming.com:8849/api/show?filePath=./webo/font/AlibabaPuHuiTi-2-55-Regular/AlibabaPuHuiTi-2-55-Regular.ttf")
  121. format("truetype");
  122. font-weight: normal;
  123. font-style: normal;
  124. font-display: swap;
  125. }
  126. @font-face {
  127. font-family: "ZhankuKuaiLeTi";
  128. src: url("https://test.jue-ming.com:8849/api/show?filePath=./webo/font/ZhankuKuaiLeTi.ttf")
  129. format("truetype");
  130. font-weight: normal;
  131. font-style: normal;
  132. font-display: swap;
  133. }
  134. @font-face {
  135. font-family: "YanShiXieHeiTi";
  136. src: url("https://test.jue-ming.com:8849/api/show?filePath=./webo/font/演示斜黑体.otf")
  137. format("truetype");
  138. font-weight: normal;
  139. font-style: normal;
  140. font-display: swap;
  141. }
  142. </style>