webview.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <!-- /pages/webview/webview.vue -->
  2. <template>
  3. <view>
  4. <web-view
  5. style="height: calc(100% - 30px); margin-top: 30px"
  6. :src="src"
  7. ref="webview"
  8. ></web-view>
  9. <view
  10. class="zai-title text-shadow"
  11. style="position: fixed; top: 5px; z-index: 3"
  12. >返回</view
  13. >
  14. </view>
  15. </template>
  16. <script>
  17. import configService from "@/common/service/config.service.js";
  18. import { ACCESS_TOKEN, USER_NAME, USER_INFO } from "@/common/util/constants";
  19. import api from "@/api/api";
  20. export default {
  21. data() {
  22. return {
  23. timer: "",
  24. src: "",
  25. };
  26. },
  27. methods: {
  28. onLoaded() {
  29. this.$tip.toast("onLoaded");
  30. // 在webview加载完成后,注入JavaScript代码
  31. this.$refs.webview.evalJs(`
  32. (function() {
  33. // 监听URL变化事件
  34. window.addEventListener('hashchange', function() {
  35. // 将URL变化通过postMessage传递给uniapp
  36. window.postMessage(JSON.stringify({
  37. type: 'urlChange',
  38. url: window.location.href
  39. }));
  40. });
  41. // 监听popstate事件(用于处理浏览器历史记录的变化)
  42. window.addEventListener('popstate', function() {
  43. window.postMessage(JSON.stringify({
  44. type: 'urlChange',
  45. url: window.location.href
  46. }));
  47. });
  48. // 初始化时发送一次当前URL
  49. window.postMessage(JSON.stringify({
  50. type: 'initUrl',
  51. url: window.location.href
  52. }));
  53. })();
  54. `);
  55. // 监听来自webview的消息
  56. window.addEventListener("message", (event) => {
  57. const data = JSON.parse(event.data);
  58. if (data.type === "urlChange") {
  59. console.log("Webview URL changed:", data.url);
  60. // 在这里处理URL变化逻辑
  61. } else if (data.type === "initUrl") {
  62. console.log("Initial Webview URL:", data.url);
  63. // 处理初始化时的URL
  64. }
  65. });
  66. },
  67. startListening() {
  68. this.timer = setInterval(() => {
  69. var pages = getCurrentPages();
  70. var page = pages[pages.length - 1];
  71. var currentWebview = page.$getAppWebview();
  72. var urls = currentWebview.children()[0].getURL();
  73. console.log("=======当前地址======", urls);
  74. console.log(
  75. "WebView加载新页面------------------------------------------------------------------------" +
  76. this.src
  77. );
  78. if (urls.indexOf("ticket") != -1) {
  79. this.stopListening();
  80. var tourl = urls;
  81. tourl = tourl.substring(tourl.indexOf("ticket") + 7, tourl.length);
  82. var pos = tourl.indexOf("&");
  83. if (pos != -1) tourl = tourl.substring(0, pos);
  84. uni.navigateTo({
  85. url: "/pages/login/login",
  86. });
  87. this.loginCas(tourl);
  88. }
  89. }, 1000); // 间隔3秒检查一次
  90. },
  91. stopListening() {
  92. if (this.timer) {
  93. clearInterval(this.timer);
  94. this.timer = null;
  95. }
  96. },
  97. loginCas(value) {
  98. var than = this;
  99. var ser = this.src;
  100. new Promise((resolve, reject) => {
  101. api
  102. .validateCasLogin("", "?ticket=" + value + "&service=" + ser)
  103. .then((response) => {
  104. debugger;
  105. // console.log("response===>response", JSON.stringify(response));
  106. // if (response.data.code == 200) {
  107. // uni.setStorageSync(ACCESS_TOKEN, response.data.result.token);
  108. // } else {
  109. // }
  110. const result = response.data.result;
  111. const userInfo = result.userInfo;
  112. uni.setStorageSync(ACCESS_TOKEN, result.token);
  113. uni.setStorageSync(USER_INFO, userInfo);
  114. console.log("userInfo=" + userInfo);
  115. this.$store.commit("SET_TOKEN", result.token);
  116. console.log("result.token=" + result.token);
  117. this.$store.commit("SET_DICT", result.sysAllDictItems);
  118. this.$store.commit("SET_AVATAR", userInfo.avatar);
  119. this.$store.commit("SET_NAME", {
  120. username: userInfo.username,
  121. realname: userInfo.realname,
  122. });
  123. console.log("getPermissionList-----------");
  124. than.getPermissionList();
  125. })
  126. .catch((error) => {
  127. debugger;
  128. // console.log("catch===>response", error);
  129. // uni.navigateTo({
  130. // url: "/pages/home/home",
  131. // });
  132. });
  133. });
  134. },
  135. //获取权限菜单
  136. getPermissionList() {
  137. console.log("getPermissionList=========================");
  138. new Promise((resolve, reject) => {
  139. api
  140. .getPermission({})
  141. .then((response) => {
  142. console.log(response, "权限菜单----------------");
  143. if (response.statusCode == 200) {
  144. let data = response.data.result.menuApp;
  145. let dataBtn = response.data.result.appauth;
  146. uni.setStorageSync("menuPermission", data);
  147. uni.setStorageSync("btnPermission", dataBtn);
  148. this.$tip.success("登录成功!");
  149. uni.navigateTo({
  150. url: "/pages/index/index",
  151. });
  152. } else {
  153. reject(response);
  154. }
  155. })
  156. .catch((error) => {
  157. console.log("catch===>response", response);
  158. reject(error);
  159. });
  160. });
  161. },
  162. saveClientId() {
  163. console.log("saveClientId=========================");
  164. var info = plus.push.getClientInfo();
  165. var cid = info.clientid;
  166. this.$http
  167. .get("/sys/user/saveClientId", {
  168. params: {
  169. clientId: cid,
  170. },
  171. })
  172. .then((res) => {
  173. console.log("res::saveClientId>", res);
  174. //获取权限菜单
  175. this.getPermissionList();
  176. });
  177. },
  178. },
  179. onShow() {
  180. this.startListening();
  181. },
  182. onHide() {
  183. this.stopListening();
  184. },
  185. watch: {},
  186. onUnload() {
  187. plus.runtime.quit();
  188. },
  189. onLoad(option) {
  190. // 获取传递过来的url参数
  191. let optionurl = option.url;
  192. console.log("onLoad 当前URL:", option.url);
  193. // setTimeout(()=>{
  194. // console.log('onLoad setNavigationBarTitle', option.url);
  195. // uni.setNavigationBarTitle({
  196. // title: '单点登录'
  197. // });
  198. // },2000)
  199. console.log("option.url======" + option.url);
  200. this.src = decodeURIComponent(optionurl);
  201. },
  202. };
  203. </script>
  204. <style scoped>
  205. .zai-title {
  206. margin-top: 20upx;
  207. font-size: 58upx;
  208. color: #000000;
  209. text-align: center;
  210. }
  211. </style>