webview.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. var than = this
  69. this.timer = setInterval(() => {
  70. var pages = getCurrentPages();
  71. var page = pages[pages.length - 1];
  72. var currentWebview = page.$getAppWebview();
  73. var urls = currentWebview.children()[0].getURL();
  74. console.log("=======当前地址======", urls);
  75. console.log(
  76. "WebView加载新页面------------------------------------------------------------------------" +
  77. this.src
  78. );
  79. if (urls.indexOf("ticket") != -1) {
  80. this.stopListening();
  81. var tourl = urls;
  82. tourl = tourl.substring(tourl.indexOf("ticket") + 7, tourl.length);
  83. var pos = tourl.indexOf("&");
  84. if (pos != -1) tourl = tourl.substring(0, pos);
  85. console.log("=======ticket======", tourl);
  86. uni.navigateTo({
  87. url: "/pages/login/login?ticket="+tourl,
  88. });
  89. // setTimeout(()=>{
  90. // uni.navigateTo({
  91. // url: "/pages/index/index",
  92. // });
  93. // },10000)
  94. // than.loginCas(tourl);
  95. }
  96. }, 200); // 间隔3秒检查一次
  97. },
  98. stopListening() {
  99. if (this.timer) {
  100. clearInterval(this.timer);
  101. this.timer = null;
  102. }
  103. },
  104. loginCas(value) {
  105. var than = this;
  106. this.$tip.success(configService.apiUrl);
  107. var ser = configService.apiUrl;
  108. ser = tourl.replace(9999, 8092);
  109. this.$tip.success(ser);
  110. new Promise((resolve, reject) => {
  111. api
  112. .validateCasLogin("", "?ticket=" + value + "&service=" + ser)
  113. .then((response) => {
  114. than.$tip.success(response.message);
  115. // console.log("response===>response", JSON.stringify(response));
  116. // if (response.data.code == 200) {
  117. // uni.setStorageSync(ACCESS_TOKEN, response.data.result.token);
  118. // } else {
  119. // }
  120. const result = response.data.result;
  121. const userInfo = result.userInfo;
  122. uni.setStorageSync(ACCESS_TOKEN, result.token);
  123. uni.setStorageSync(USER_INFO, userInfo);
  124. console.log("userInfo=" + userInfo);
  125. than.$store.commit("SET_TOKEN", result.token);
  126. console.log("result.token=" + result.token);
  127. than.$store.commit("SET_DICT", result.sysAllDictItems);
  128. than.$store.commit("SET_AVATAR", userInfo.avatar);
  129. than.$store.commit("SET_NAME", {
  130. username: userInfo.username,
  131. realname: userInfo.realname,
  132. });
  133. than.$tip.success("loginCas=");
  134. than.getPermissionList();
  135. })
  136. .catch((error) => {
  137. debugger;
  138. // console.log("catch===>response", error);
  139. // uni.navigateTo({
  140. // url: "/pages/home/home",
  141. // });
  142. });
  143. });
  144. },
  145. //获取权限菜单
  146. getPermissionList() {
  147. var than = this;
  148. this.$tip.success("getPermissionList");
  149. console.log("getPermissionList=========================");
  150. new Promise((resolve, reject) => {
  151. api
  152. .getPermission({})
  153. .then((response) => {
  154. console.log(response, "权限菜单----------------");
  155. if (response.statusCode == 200) {
  156. let data = response.data.result.menuApp;
  157. let dataBtn = response.data.result.appauth;
  158. uni.setStorageSync("menuPermission", data);
  159. uni.setStorageSync("btnPermission", dataBtn);
  160. than.$tip.success("登录成功!");
  161. than.$tip.success("navigateTo="+pages/index/index);
  162. uni.navigateTo({
  163. url: "/pages/index/index",
  164. });
  165. } else {
  166. reject(response);
  167. }
  168. })
  169. .catch((error) => {
  170. console.log("catch===>response", response);
  171. reject(error);
  172. });
  173. });
  174. },
  175. saveClientId() {
  176. console.log("saveClientId=========================");
  177. var info = plus.push.getClientInfo();
  178. var cid = info.clientid;
  179. this.$http
  180. .get("/sys/user/saveClientId", {
  181. params: {
  182. clientId: cid,
  183. },
  184. })
  185. .then((res) => {
  186. console.log("res::saveClientId>", res);
  187. //获取权限菜单
  188. this.getPermissionList();
  189. });
  190. },
  191. },
  192. onShow() {
  193. this.startListening();
  194. },
  195. onHide() {
  196. this.stopListening();
  197. },
  198. watch: {},
  199. onUnload() {
  200. plus.runtime.quit();
  201. },
  202. onLoad(option) {
  203. // 获取传递过来的url参数
  204. let optionurl = option.url;
  205. console.log("onLoad 当前URL:", option.url);
  206. // setTimeout(()=>{
  207. // console.log('onLoad setNavigationBarTitle', option.url);
  208. // uni.setNavigationBarTitle({
  209. // title: '单点登录'
  210. // });
  211. // },2000)
  212. console.log("option.url======" + option.url);
  213. this.src = decodeURIComponent(optionurl);
  214. },
  215. };
  216. </script>
  217. <style scoped>
  218. .zai-title {
  219. margin-top: 20upx;
  220. font-size: 58upx;
  221. color: #000000;
  222. text-align: center;
  223. }
  224. </style>