App.vue 4.5 KB

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