From ee96a2b2f5127dadea09a65c72ee7068035ff0d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=BD=AA?= Date: Fri, 18 Apr 2025 11:55:50 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E9=99=86=E6=A0=B9=E6=8D=AEtoken?= =?UTF-8?q?=E5=8C=BA=E5=88=86=E8=BA=AB=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.vue | 106 +++++++++----- node_modules/jwt-decode/LICENSE | 21 +++ node_modules/jwt-decode/README.md | 134 ++++++++++++++++++ node_modules/jwt-decode/build/cjs/index.d.ts | 23 +++ node_modules/jwt-decode/build/cjs/index.js | 62 ++++++++ .../jwt-decode/build/cjs/package.json | 1 + node_modules/jwt-decode/build/esm/index.d.ts | 23 +++ node_modules/jwt-decode/build/esm/index.js | 57 ++++++++ node_modules/jwt-decode/package.json | 81 +++++++++++ pages/adminHome/index.vue | 30 +++- pages/index/cleanPlan.vue | 19 +-- pages/index/index.vue | 7 +- pages/login/login.vue | 82 ++++++++--- store/index.js | 4 +- 14 files changed, 584 insertions(+), 66 deletions(-) create mode 100644 node_modules/jwt-decode/LICENSE create mode 100644 node_modules/jwt-decode/README.md create mode 100644 node_modules/jwt-decode/build/cjs/index.d.ts create mode 100644 node_modules/jwt-decode/build/cjs/index.js create mode 100644 node_modules/jwt-decode/build/cjs/package.json create mode 100644 node_modules/jwt-decode/build/esm/index.d.ts create mode 100644 node_modules/jwt-decode/build/esm/index.js create mode 100644 node_modules/jwt-decode/package.json diff --git a/App.vue b/App.vue index 20cabd9..ce387d2 100644 --- a/App.vue +++ b/App.vue @@ -2,7 +2,7 @@ // #ifdef APP-PLUS import appUpdate from "@/components/appUpdate/appUpdate.js"; // #endif - +import { jwtDecode } from "jwt-decode"; export default { onLaunch: function () { console.log("%c%s", "color:red", "onLaunch---"); @@ -76,37 +76,79 @@ export default { // "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9zaWQiOiIwOGRkNzFiZC01N2MxLTQ1YmMtODBiYS0xMjkzYjU4MTViMmEiLCJ1bmlxdWVfbmFtZSI6IuS_nea0gSIsInJvbGUiOiJVc2VyIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbW9iaWxlcGhvbmUiOiIxODE3NDAxMDU2MiIsImdyb3Vwc2lkIjoiNGIyZDk2NjItYTc2OC0wOGYyLWEwYzgtNzYyYjhjNzZkYTJhIiwibmJmIjoxNzQ0MjYzMTAyLCJleHAiOjE3NDQyNjY3MDIsImlhdCI6MTc0NDI2MzEwMn0.ZJTwC7rWCcywoM7ncysw-JRIiwXz4y_6hlmqctN-gMM" let token = data.token; uni.setStorageSync("token", "Bearer " + token); - const normalTabBar = [ - { - pagePath: "/pages/index/planList", - iconPath: "/static/tab/planList.png", - selectedIconPath: "/static/tab/planList-active.png", - text: "计划", - }, - { - pagePath: "/pages/index/index", - iconPath: "/static/tab/admin-home.png", - selectedIconPath: "/static/tab/admin-home-acitve.png", - text: "首页", - }, - { - pagePath: "/pages/user/index", - iconPath: "/static/tab/admin-my.png", - selectedIconPath: "/static/tab/admin-my-active.png", - text: "设置", - }, - ]; - this.$u.vuex("vuex_tabbar", normalTabBar); - uni.switchTab({ - url: "/pages/index/index", - success() { - console.log("跳转成功"); - window.initFn = null; // 清除全局函数 - }, - fail(err) { - console.error("跳转失败", err); - }, - }); + console.log(jwtDecode(token), "解析的token信息"); + let resData = jwtDecode(token); + this.$u.vuex("vuex_userData", resData); + // User | Admin + let role = resData.role; + console.log(role, "role----"); + // return; + if (role === "User") { + const normalTabBar = [ + { + pagePath: "/pages/index/planList", + iconPath: "/static/tab/planList.png", + selectedIconPath: "/static/tab/planList-active.png", + text: "计划", + }, + { + pagePath: "/pages/index/index", + iconPath: "/static/tab/admin-home.png", + selectedIconPath: "/static/tab/admin-home-acitve.png", + text: "首页", + }, + { + pagePath: "/pages/user/index", + iconPath: "/static/tab/admin-my.png", + selectedIconPath: "/static/tab/admin-my-active.png", + text: "设置", + }, + ]; + this.$u.vuex("vuex_tabbar", normalTabBar); + uni.switchTab({ + url: "/pages/index/index", + success() { + console.log("跳转成功"); + window.initFn = null; // 清除全局函数 + }, + fail(err) { + console.error("跳转失败", err); + }, + }); + } + if (role === "Admin") { + const adminTabBar = [ + { + pagePath: "/pages/adminHome/index", + iconPath: "/static/tab/admin-home.png", + selectedIconPath: "/static/tab/admin-home-acitve.png", + text: "首页", + }, + { + pagePath: "/pages/adminNfc/index", + iconPath: "/static/tab/nfc.png", + selectedIconPath: "/static/tab/nfc-active.png", + text: "nfc写入", + }, + { + pagePath: "/pages/adminMy/index", + iconPath: "/static/tab/admin-my.png", + selectedIconPath: "/static/tab/admin-my-active.png", + text: "我的", + }, + ]; + this.$u.vuex("vuex_tabbar", adminTabBar); + uni.switchTab({ + url: "/pages/adminHome/index", + success() { + console.log("跳转成功"); + window.initFn = null; // 清除全局函数 + }, + fail(err) { + console.error("跳转失败", err); + }, + }); + } }, }, }; diff --git a/node_modules/jwt-decode/LICENSE b/node_modules/jwt-decode/LICENSE new file mode 100644 index 0000000..bcd1854 --- /dev/null +++ b/node_modules/jwt-decode/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Auth0, Inc. (http://auth0.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/node_modules/jwt-decode/README.md b/node_modules/jwt-decode/README.md new file mode 100644 index 0000000..dc4ce06 --- /dev/null +++ b/node_modules/jwt-decode/README.md @@ -0,0 +1,134 @@ +![Browser library that helps decoding JWT tokens which are Base64Url encoded](https://cdn.auth0.com/website/sdks/banners/jwt-decode-banner.png) + +**IMPORTANT:** This library doesn't validate the token, any well-formed JWT can be decoded. You should validate the token in your server-side logic by using something like [express-jwt](https://github.com/auth0/express-jwt), [koa-jwt](https://github.com/stiang/koa-jwt), [Microsoft.AspNetCore.Authentication.JwtBearer](https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.JwtBearer), etc. + +![Release](https://img.shields.io/npm/v/jwt-decode) +![Downloads](https://img.shields.io/npm/dw/jwt-decode) +[![License](https://img.shields.io/:license-MIT-blue.svg?style=flat)](https://opensource.org/licenses/MIT) +[![CircleCI](https://img.shields.io/circleci/build/github/auth0/jwt-decode)](https://circleci.com/gh/auth0/jwt-decode) + +:books: [Documentation](#documentation) - :rocket: [Getting Started](#getting-started) - :speech_balloon: [Feedback](#feedback) + +## Documentation + +- [Docs site](https://www.auth0.com/docs) - explore our docs site and learn more about Auth0. + +## Getting started + +### Installation + +Install with NPM or Yarn. + +Run `npm install jwt-decode` or `yarn add jwt-decode` to install the library. + +### Usage + +```js +import { jwtDecode } from "jwt-decode"; + +const token = "eyJ0eXAiO.../// jwt token"; +const decoded = jwtDecode(token); + +console.log(decoded); + +/* prints: + * { + * foo: "bar", + * exp: 1393286893, + * iat: 1393268893 + * } + */ + +// decode header by passing in options (useful for when you need `kid` to verify a JWT): +const decodedHeader = jwtDecode(token, { header: true }); +console.log(decodedHeader); + +/* prints: + * { + * typ: "JWT", + * alg: "HS256" + * } + */ +``` + +**Note:** A falsy or malformed token will throw an `InvalidTokenError` error; see below for more information on specific errors. + +## Errors + +This library works with valid JSON web tokens. The basic format of these token is +``` +[part1].[part2].[part3] +``` +All parts are supposed to be valid base64 (url) encoded json. +Depending on the `{ header: