This commit is contained in:
weiwei committed 2025-04-24 19:23:11 +08:00
1 parent e44cf79082
commit 3287ba950f
3 files changed
+112 -114

No files matched your search

+55 -57
View File
@@ -1,6 +1,6 @@
<template>
<crud-layout title="登录" bg="/static/page/blue-bg.png">
<view class="card p-x-30rpx p-y-40rpx mt-5">
<view class="card p-x-30rpx p-y-40rpx my-5">
<tm-form ref="applyFormRef" v-model="applyForm" :rules="applyFormRules" direction="vertical">
<tm-form-item name="username" label="用户名" :showBottom="false">
<tm-input v-model="applyForm.username" placeholder="请输入用户名..." />
@@ -14,7 +14,7 @@
<tm-button block @tap="doSubmit">登录</tm-button>
<button
style="background-color: #0779fd; border-radius: 20rpx; color: white; font-size: 32rpx; margin: 10rpx 0"
style="background-color: #0779fd; border-radius: 24rpx; color: white; font-size: 32rpx; margin: 20rpx 0; padding: 4rpx 0"
open-type="getUserInfo"
@click="getUserInfo"
>
@@ -66,17 +66,15 @@ const doSubmit = async () => {
router.back()
}
// import { useUserStore } from "@/store/user.js"
// 使用store仓库
// const userStore = useUserStore()
// 获取用户信息
const getUserInfo = () => {
uni.getUserInfo({
success: (res) => {
// 存储用户信息
// userStore.updateUserInfo(res.userInfo)
console.log(res)
// 存储用户信息
uni.setStorageSync("user_info", res.userInfo)
login()
},
fail: (res) => {
@@ -85,7 +83,7 @@ const getUserInfo = () => {
})
}
// 请求获取token
// 登录
const login = async () => {
const result = await uni.login().catch((error) => error)
if (result.errMsg === "login:ok") {
@@ -94,6 +92,8 @@ const login = async () => {
code: result.code,
}
console.log("code", result.code)
const res: any = await request({
url: "/auth/oauth2/token?grant_type=mobile",
method: "post",
@@ -103,64 +103,62 @@ const login = async () => {
"Content-Type": "application/x-www-form-urlencoded",
},
})
console.log("xx", res)
// if (res == 0) {
// // const token = "Bearer 123456789"
// // 更新获取的token
// // userStore.updateToken(token)
console.log("res", res)
// router.back()
// return uni.showToast({ title: "登录成功", icon: "none" })
// } else {
// return uni.showToast({ title: "获取token失败,登录失败", icon: "error" })
// }
if (res.code == 0) {
uni.setStorageSync("token", res.data.access_token)
router.back()
return uni.showToast({ title: "登录成功", icon: "none" })
} else {
return uni.showToast({ title: "获取token失败,登录失败", icon: "error" })
}
} else {
uni.showToast({ title: "获取登录code失败", icon: "error" })
}
}
// 绑定
const bind = async (mobile?: string, code?: string) => {
// 1. 使用验证码登录获取token
const params = {
mobile: "SMS@" + "17319791971",
code: "0e3XvSll2ptntf4bg3ol2BdVFH1XvSlM",
grant_type: "mobile",
}
// const bind = async (mobile?: string, code?: string) => {
// // 1. 使用验证码登录获取token
// const params = {
// mobile: "SMS@" + "17319791971",
// code: "0e3XvSll2ptntf4bg3ol2BdVFH1XvSlM",
// grant_type: "mobile",
// }
const res = await request({
url: "/auth/oauth2/token",
method: "post",
data: params,
header: {
Authorization: "Basic bWluaTptaW5p",
"Content-Type": "application/x-www-form-urlencoded",
},
})
console.log(res)
// const res = await request({
// url: "/auth/oauth2/token",
// method: "post",
// data: params,
// header: {
// Authorization: "Basic bWluaTptaW5p",
// "Content-Type": "application/x-www-form-urlencoded",
// },
// })
// console.log(res)
const token = res.data.access_token
// const token = res.data.access_token
// 2. 获取新的小程序code并完成绑定
// uni.login({
// success(loginRes) {
// wx.request({
// url: "${BASE_URL}/admin/social/bind?state=MINI&code=" + loginRes.code,
// method: "post",
// header: {
// Authorization: "Bearer " + token,
// },
// success() {
// wx.showToast({
// title: "绑定成功",
// success() {
// wx.navigateBack()
// },
// })
// },
// })
// },
// })
}
// // 2. 获取新的小程序code并完成绑定
// // uni.login({
// // success(loginRes) {
// // wx.request({
// // url: "${BASE_URL}/admin/social/bind?state=MINI&code=" + loginRes.code,
// // method: "post",
// // header: {
// // Authorization: "Bearer " + token,
// // },
// // success() {
// // wx.showToast({
// // title: "绑定成功",
// // success() {
// // wx.navigateBack()
// // },
// // })
// // },
// // })
// // },
// // })
// }
</script>
+56 -56
View File
@@ -1,82 +1,82 @@
import { throttle } from "radash";
import { throttle } from "radash"
function paramsToQuery(params: { [key: string]: string }) {
let query = [];
for (let key in params) {
query.push(key + "=" + params[key]);
}
return query.join("&");
let query = []
for (let key in params) {
query.push(key + "=" + params[key])
}
return query.join("&")
}
// 路由跳转
function _jump(path = "", params = {}, type = 1) {
if (!path) {
console.error("router::jump>>path 不能为空");
}
if (hasTabbar(path)) {
uni.switchTab({ url: path });
return;
}
let url: string = "";
if (Object.keys(params)) {
url = `${path}?${paramsToQuery(params)}`;
}
if (type == 1) {
uni.navigateTo({ url: url });
return;
}
if (type == 2) {
uni.redirectTo({ url: url });
return;
}
if (type == 3) {
uni.reLaunch({ url: url });
return;
}
if (!path) {
console.error("router::jump>>path 不能为空")
}
if (hasTabbar(path)) {
uni.switchTab({ url: path })
return
}
let url: string = ""
if (Object.keys(params)) {
url = `${path}?${paramsToQuery(params)}`
}
if (type == 1) {
uni.navigateTo({ url: url })
return
}
if (type == 2) {
uni.redirectTo({ url: url })
return
}
if (type == 3) {
uni.reLaunch({ url: url })
return
}
}
// 路由返回
function _back(page = 1) {
if (hasHistory()) {
uni.navigateBack({
delta: page,
});
} else {
_jump(__TABBAR__[0], {});
}
if (hasHistory()) {
uni.navigateBack({
delta: page,
})
} else {
_jump(__TABBAR__[0], {})
}
}
export function jump(...args: any) {
throttle({ interval: 500 }, () => _jump(...args))();
throttle({ interval: 500 }, () => _jump(...args))()
}
export function back(...args: any) {
throttle({ interval: 500 }, () => _back(...args))();
throttle({ interval: 500 }, () => _back(...args))()
}
export function hasHistory() {
const pages = getCurrentPages();
if (pages.length > 1) {
return true;
}
return false;
const pages = getCurrentPages()
if (pages.length > 1) {
return true
}
return false
}
export function getCurrentPage() {
const pages = getCurrentPages();
return pages[pages.length - 1];
const pages = getCurrentPages()
return pages[pages.length - 1]
}
export function hasTabbar(str: string = "") {
if (!str) {
const { route } = getCurrentPage();
if (route) {
str = `/${route}`;
}
}
if (Array.isArray(__TABBAR__) && __TABBAR__.indexOf(str) !== -1) {
return true;
} else {
return false;
}
if (!str) {
const { route } = getCurrentPage()
if (route) {
str = `/${route}`
}
}
if (Array.isArray(__TABBAR__) && __TABBAR__.indexOf(str) !== -1) {
return true
} else {
return false
}
}
+1 -1
View File
@@ -177,7 +177,7 @@ http.interceptors.response.use(
uni.removeStorageSync(TOKEN_KEY)
uni.removeStorageSync(USER_INFO_KEY)
uni.showToast({ title: "登录已过期,请重新登录", icon: "none" })
router.jump("/pages/test/login")
router.jump("/pages/test/login", {}, 1)
return false
}