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"
						}
					}
				]
				
		}
		]
相关推荐
人生导师yxc6 小时前
微信小程序接入支付宝沙箱支付(http请求)
微信小程序·小程序
蓝莓味的口香糖6 小时前
【vue3】组件的批量全局注册
前端·javascript·vue.js
云起SAAS6 小时前
日历黄历八字排盘紫微斗数奇门遁甲姓名分析号码吉凶命理抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·日历黄历八字排盘紫微斗数
独泪了无痕7 小时前
自动导入 AutoImport:告别手动引入依赖,优化Vue3开发体验
前端·vue.js·typescript
全栈小510 小时前
【前端】Vue 组件开发中的枚举值验证:从一个Type属性错误说起
前端·javascript·vue.js
꧁꫞꯭零꯭点꯭꫞꧂10 小时前
G6绘制机柜 以及机柜设备的demo
前端·javascript·vue.js
wuhen_n12 小时前
5年前端,我为什么要all in AI Agent?
前端·vue.js·ai编程
optimistic_chen12 小时前
【Vue入门】创建Vue工程环境和响应式函数
前端·javascript·vue.js·前端框架·html
前端Hardy12 小时前
用 uni-app x 重构我们的 App:一套代码跑通 iOS、Android、鸿蒙!人力成本直降 60%
前端·ios·uni-app
林恒smileZAZ12 小时前
告别满屏 v-if:用一个自定义指令搞定 Vue 前端权限控制
前端·javascript·vue.js