webview.vue 6.0 KB

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