uni-app写的微信小程序如何实现账号密码登录后获取token,并且每天的第一次登录后都会直接获取参数而不是耀重新登录(2)

接uni-app写的微信小程序如何实现账号密码登录后获取token,并且每天的第一次登录后都会直接获取参数而不是耀重新登录(1),

在main.js中

复制代码
import App from './App'

// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'

const app = new Vue({
  ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif

async function checkAndLogin() {
  const token = uni.getStorageSync('token');
  const username = uni.getStorageSync('userName');
  const password = uni.getStorageSync('userPassword');

  if (!token) {
    try {
      const response = await request({
        url: '/login',
        method: 'POST',
        data: { username, password }
      });

      if (response.token) {
        uni.setStorageSync('token', response.token);
      } else {
        throw new Error('用户名或密码不正确');
      }
    } catch (error) {
      uni.showModal({
        title: '登录失败',
        content: error.message || '用户名或密码不正确',
        showCancel: false,
      });
    }
  }
}

// 在 App.vue 或 main.js 中调用 checkAndLogin
async function onLaunch() {
  await checkAndLogin();
}

在pages.json

创建一个空的{

"path": "pages/login/login",

"style": {

"navigationBarTitleText": "登录"

}

},

让路径为pages/login/login里面什么内容都没有,这样保证每次进入的第一个页面都是空白页面

复制代码
"pages": [
		{
			"path": "pages/login/login",
			"style": {
				"navigationBarTitleText": "登录"
			}
		},
		{
			"path": "pages/mine/mine",
			"style": {
				"navigationBarTitleText": "我的"
			}
		},
		{
			"path": "pages/home/home",
			"style": {
				"navigationBarTitleText": "首页"
			}
		},
		{
			"path": "pages/Operation/Operation",
			"style": {
				"navigationBarTitleText": "运维"
			}
		},
		{
			"path": "pages/index/index",
			"style": {
				"navigationBarTitleText": "服务"
			}
		},
		{
			"path" : "pages/shebe/shebe",
			"style" : 
			{
				"navigationBarTitleText" : "设备"
			}
		}
	],
	"globalStyle": {
		"navigationBarTextStyle":"black",
		"navigationBarBackgroundColor": "#fff",
		"navigationBarTitleText": "uni-app",
		"backgroundColor": "#F8F8F8",
		"enablePullDownRefresh": true,
		"navigationStyle": "custom"

	},
	"uniIdRouter": {},
	"tabBar": {
		"custom": true,
		"color": "#7A7E83",
		"selectedColor": "#1296db",
		"borderStyle": "black",
		"backgroundColor": "#F8F8F8",
		"list": [
			{
				"pagePath": "pages/home/home",
				"text": "首页",
				"iconPath": "static/imgs/imgs55.png",
				"selectedIconPath": "static/imgs/imgs5.png"
			},
			{
				"pagePath": "pages/Operation/Operation",
				"text": "运维",
				"iconPath": "static/imgs/imgs66.png",
				"selectedIconPath": "static/imgs/imgs6.png"
			},
			{
				"pagePath": "pages/shebe/shebe",
				"text": "设备",
				"iconPath": "static/imgs/imgs66.png",
				"selectedIconPath": "static/imgs/imgs6.png"
			},
			{
				"pagePath": "pages/index/index",
				"text": "服务",
				"iconPath": "static/imgs/imgs11.png",
				"selectedIconPath": "static/imgs/imgs1.png"
			},
			{
				"pagePath": "pages/mine/mine",
				"text": "我的",
				"iconPath": "static/imgs/imgs44.png",
				"selectedIconPath": "static/imgs/imgs4.png"
			}
		]
	},"subPackages": true,
	"noParse": [],
	"subPackages": [
		{
				"root": "pagesA/",
				"pages":[
					{
						"path": "login/login",
						"style": {
							"navigationBarTitleText": "登录",
							// 启⽤⾃定义导航栏
							"navigationStyle": "custom",
							// 将导航栏⽂字的颜⾊设置为⽩⾊
							"navigationBarTextStyle": "white"
						}
					}
				]
				
		}
		]
相关推荐
河北清兮网络科技10 小时前
短剧 APP 产品说明
小程序·uni-app·短剧
killerbasd10 小时前
牧苏苏传 我不装了 4/7
前端·javascript·vue.js
大家的林语冰12 小时前
《前端周刊》尤大开源 Vite+ 全家桶,前端工业革命启动;尤大爆料 Void 云服务新产品,Vite 进军全栈开发;ECMA 源码映射规范......
前端·javascript·vue.js
M ? A14 小时前
Vue 迁移 React 实战:VuReact 一键自动化转换方案
前端·vue.js·经验分享·react.js·开源·自动化·vureact
Burt14 小时前
我的 2026 全栈选型:Vue3 + Elysia + Bun + AlovaJS
vue.js·全栈·bun
小锋java123414 小时前
SpringBoot 4 + Spring Security 7 + Vue3 前后端分离项目设计最佳实践
java·vue.js·spring boot
一 乐14 小时前
校园线上招聘|基于springboot + vue校园线上招聘系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·校园线上招聘系统
LanceJiang14 小时前
从输入 URL 到页面:一个 Vue 项目的“奇幻漂流”
vue.js
码喽7号15 小时前
vue学习四:Axios网络请求
前端·vue.js·学习
宠友信息15 小时前
一套基于uniapp+springboot完整社区系统是如何实现的?友猫社区源码级功能解析
java·spring boot·后端·微服务·微信·uni-app