{"id":255,"date":"2024-01-08T17:00:08","date_gmt":"2024-01-08T09:00:08","guid":{"rendered":"https:\/\/www.hyw.life\/?p=255"},"modified":"2025-10-21T17:08:35","modified_gmt":"2025-10-21T09:08:35","slug":"uniapp%e7%99%bb%e5%bd%95%e6%8b%a6%e6%88%aa%e4%b9%8buni-addinterceptor","status":"publish","type":"post","link":"https:\/\/www.hyw.life\/?p=255","title":{"rendered":"uniapp\u767b\u5f55\u62e6\u622a\u4e4buni.addInterceptor"},"content":{"rendered":"\n<p><strong>1.\u521b\u5efa\u4e00\u4e2apermission.js\u6587\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u8bbe\u7f6e\u767d\u540d\u5355\uff0c\u767d\u540d\u5355\u4e2d\u7684\u8def\u5f84\u4e0d\u8fdb\u884c\u62e6\u622a\nconst whiteList = &#91;'pages\/login\/index','pages\/home\/index']\n\n\/\/ \u68c0\u67e5\u5730\u5740\u767d\u540d\u5355\nfunction checkWhite(url) {\n\tconst path = url.split('?')&#91;0]\n\treturn whiteList.indexOf(path) !== -1\n}\n\/\/ \u767b\u5f55\u9875\u9762\u8def\u5f84\nconst loginPage = \"\/pages\/login\/index\"\n\n\/\/\u8981\u62e6\u622a\u7684\u8df3\u8f6c\u65b9\u5f0f\nconst list = &#91;'navigateTo', 'redirectTo', 'reLaunch','switchTab']\nlist.forEach(item =&gt; {\n\tuni.addInterceptor(item, {\n\t\tinvoke(to) {\n\t\t\t\/\/\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u5b9e\u9645\u9700\u6c42\u5199\n\t\t\tif (uni.getStorageSync('token')) {\n\t\t\t\tif (to.url === loginPage) {\n\t\t\t\t\tuni.reLaunch({\n\t\t\t\t\t\turl: \"\/\"\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t\treturn true\n\t\t\t} else {\n\t\t\t\tif (checkWhite(to.url)) {\n\t\t\t\t\treturn true\n\t\t\t\t}\n\t\t\t\tuni.reLaunch({\n\t\t\t\t\turl: loginPage\n\t\t\t\t})\n\t\t\t\treturn false\n\t\t\t}\n\t\t},\n\t\tfail(err) {\n\t\t\tconsole.log(err)\n\t\t}\n\t})\n})\n\n<\/code><\/pre>\n\n\n\n<p><strong>2.\u5c06permission.js\u6587\u4ef6\u5f15\u5165\u5230main.js\u4e2d<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/main.js\nimport Vue from 'vue'\nimport App from '.\/App'\nimport '.\/permission'\n\nconst app = new Vue({\n  ...App\n})\n<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/doc.dcloud.net.cn\/uni-app-x\/api\/interceptor.html\">\u5b98\u65b9\u6587\u6863\u5730\u5740<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u521b\u5efa\u4e00\u4e2apermission.js\u6587\u4ef6 2.\u5c06permission.js\u6587\u4ef6\u5f15\u5165\u5230main.js\u4e2d \u5b98\u65b9\u6587 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":484,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,5],"tags":[15],"class_list":["post-255","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uniapp","category-qd","tag-uniapp"],"_links":{"self":[{"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/posts\/255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hyw.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=255"}],"version-history":[{"count":2,"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/posts\/255\/revisions"}],"predecessor-version":[{"id":257,"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/posts\/255\/revisions\/257"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/media\/484"}],"wp:attachment":[{"href":"https:\/\/www.hyw.life\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hyw.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hyw.life\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}