测试了微信自动登录

This commit is contained in:
D 2024-01-18 02:45:14 +08:00
parent e1ad909101
commit a67fe5e375
6 changed files with 87 additions and 30 deletions

31
src/api/oauth.js Normal file
View File

@ -0,0 +1,31 @@
import request from '@/utils/request'
/**
* 微信登录
* @param {*} source pub miniapp
* @param {*} code
* @returns
*/
export function wxLogin(source,code) {
return request({
url: `/wx/login/${source}/${code}`,
headers: {
isToken: false
},
method: 'post',
})
}
/**
* 微信绑定
* @param {*} source 微信绑定
* @param {*} code
* @returns
*/
export function wxRegister(source,code) {
return request({
url: `/wx/register/${source}/${code}`,
headers: {
isToken: true
},
method: 'post',
})
}

View File

@ -1,6 +1,8 @@
// 应用全局配置
const config = {
baseUrl: 'https://vue.ruoyi.vip/prod-api',
// baseUrl: 'https://vue.ruoyi.vip/prod-api',
// baseUrl: 'http://localhost/prod-api',
baseUrl: 'http://localhost:8080',
//cloud后台网关地址
// baseUrl: 'http://192.168.10.3:8080',
// 应用信息

View File

@ -40,6 +40,9 @@ import { getCodeImg } from '@/api/login'
import { ref } from "vue";
import config from '@/config.js'
import useUserStore from '@/store/modules/user'
import { getWxCode } from '@/utils/geek';
import { wxLogin } from '@/api/oauth';
import { setToken } from '@/utils/auth';
const userStore = useUserStore()
const codeUrl = ref("");
const captchaEnabled = ref(true);
@ -50,6 +53,18 @@ const loginForm = ref({
code: "",
uuid: ''
});
const useWxLogin = ref(true)
if (useWxLogin.value) {
getWxCode().then(res => {
console.log(res);
wxLogin('miniapp',res).then(res => {
if(res.token != null){
setToken(res.token);
loginSuccess()
}
});
})
}
//

View File

@ -10,33 +10,40 @@
<uni-list-item showExtraIcon="true" :extraIcon="{ type: 'calendar-filled' }" title="创建日期"
:rightText="user.createTime" />
</uni-list>
<u-button @click="register()">绑定微信</u-button>
</view>
</template>
<script>
<script setup>
import { getUserProfile } from "@/api/system/user"
import { ref } from "vue";
import modal from "@/plugins/modal"
export default {
data() {
return {
user: {},
roleGroup: "",
postGroup: ""
}
},
onLoad() {
this.getUser()
},
methods: {
getUser() {
const user = ref({})
const roleGroup = ref("")
const postGroup = ref("")
function getUser() {
getUserProfile().then(response => {
this.user = response.data
this.roleGroup = response.roleGroup
this.postGroup = response.postGroup
user.value = response.data
roleGroup.value = response.roleGroup
postGroup.value = response.postGroup
})
}
}
}
getUser()
import { wxRegister } from "@/api/oauth"
import { getWxCode } from "@/utils/geek"
function register(){
modal.loading('绑定微信中...')
getWxCode().then(res=>{
wxRegister('miniapp',res).then(res=>{
modal.closeLoading()
})
})
}
</script>
<style lang="scss">

View File

@ -81,10 +81,9 @@ export function generateUUID(): string {
* code
* @returns code字符串
*/
export async function getCode() {
export async function getWxCode(appid?: string,redirect_uri?:string) {
// #ifdef H5
let appid = "";
let url = "";
if (appid == undefined || redirect_uri == undefined) return ""
let code = "";
// 截取url中的code方法
@ -106,16 +105,19 @@ export async function getCode() {
if (code == undefined || code == "" || code == null) {
// 如果没有code则去请求
console.log("h5");
window.location.href =
"https://open.weixin.qq.com/connect/oauth2/authorize?" +
let href= "https://open.weixin.qq.com/connect/oauth2/authorize?"+
tansParams({
appid: appid,
redirect_uri: url,
redirect_uri: redirect_uri,
response_type: "code",
scope: "snsapi_userinfo",
state: "STATE",
}) +
"#wechat_redirect";
console.log(href);
setTimeout(() => {
window.location.href = href;
}, 5000);
} else {
return code;
}

View File

@ -9,7 +9,7 @@ export default defineConfig(() => {
outDir: 'dist',
},
server: {
port: '3002'
port: '80'
},
plugins: [
uni()