测试了微信自动登录
This commit is contained in:
parent
e1ad909101
commit
a67fe5e375
31
src/api/oauth.js
Normal file
31
src/api/oauth.js
Normal 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',
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -1,8 +1,10 @@
|
|||||||
// 应用全局配置
|
// 应用全局配置
|
||||||
const config = {
|
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后台网关地址
|
//cloud后台网关地址
|
||||||
// baseUrl: 'http://192.168.10.3:8080',
|
// baseUrl: 'http://192.168.10.3:8080',
|
||||||
// 应用信息
|
// 应用信息
|
||||||
appInfo: {
|
appInfo: {
|
||||||
// 应用名称
|
// 应用名称
|
||||||
|
|||||||
@ -40,6 +40,9 @@ import { getCodeImg } from '@/api/login'
|
|||||||
import { ref } from "vue";
|
import { ref } from "vue";
|
||||||
import config from '@/config.js'
|
import config from '@/config.js'
|
||||||
import useUserStore from '@/store/modules/user'
|
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 userStore = useUserStore()
|
||||||
const codeUrl = ref("");
|
const codeUrl = ref("");
|
||||||
const captchaEnabled = ref(true);
|
const captchaEnabled = ref(true);
|
||||||
@ -50,6 +53,18 @@ const loginForm = ref({
|
|||||||
code: "",
|
code: "",
|
||||||
uuid: ''
|
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()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// 获取图形验证码
|
// 获取图形验证码
|
||||||
|
|||||||
@ -10,33 +10,40 @@
|
|||||||
<uni-list-item showExtraIcon="true" :extraIcon="{ type: 'calendar-filled' }" title="创建日期"
|
<uni-list-item showExtraIcon="true" :extraIcon="{ type: 'calendar-filled' }" title="创建日期"
|
||||||
:rightText="user.createTime" />
|
:rightText="user.createTime" />
|
||||||
</uni-list>
|
</uni-list>
|
||||||
|
|
||||||
|
<u-button @click="register()">绑定微信</u-button>
|
||||||
</view>
|
</view>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script setup>
|
||||||
import { getUserProfile } from "@/api/system/user"
|
import { getUserProfile } from "@/api/system/user"
|
||||||
|
import { ref } from "vue";
|
||||||
|
import modal from "@/plugins/modal"
|
||||||
|
|
||||||
export default {
|
const user = ref({})
|
||||||
data() {
|
const roleGroup = ref("")
|
||||||
return {
|
const postGroup = ref("")
|
||||||
user: {},
|
function getUser() {
|
||||||
roleGroup: "",
|
getUserProfile().then(response => {
|
||||||
postGroup: ""
|
user.value = response.data
|
||||||
}
|
roleGroup.value = response.roleGroup
|
||||||
},
|
postGroup.value = response.postGroup
|
||||||
onLoad() {
|
})
|
||||||
this.getUser()
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
getUser() {
|
|
||||||
getUserProfile().then(response => {
|
|
||||||
this.user = response.data
|
|
||||||
this.roleGroup = response.roleGroup
|
|
||||||
this.postGroup = 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>
|
</script>
|
||||||
|
|
||||||
<style lang="scss">
|
<style lang="scss">
|
||||||
|
|||||||
@ -81,10 +81,9 @@ export function generateUUID(): string {
|
|||||||
* 获取code
|
* 获取code
|
||||||
* @returns 生成的code字符串
|
* @returns 生成的code字符串
|
||||||
*/
|
*/
|
||||||
export async function getCode() {
|
export async function getWxCode(appid?: string,redirect_uri?:string) {
|
||||||
// #ifdef H5
|
// #ifdef H5
|
||||||
let appid = "";
|
if (appid == undefined || redirect_uri == undefined) return ""
|
||||||
let url = "";
|
|
||||||
let code = "";
|
let code = "";
|
||||||
|
|
||||||
// 截取url中的code方法
|
// 截取url中的code方法
|
||||||
@ -106,16 +105,19 @@ export async function getCode() {
|
|||||||
if (code == undefined || code == "" || code == null) {
|
if (code == undefined || code == "" || code == null) {
|
||||||
// 如果没有code,则去请求
|
// 如果没有code,则去请求
|
||||||
console.log("h5");
|
console.log("h5");
|
||||||
window.location.href =
|
let href= "https://open.weixin.qq.com/connect/oauth2/authorize?"+
|
||||||
"https://open.weixin.qq.com/connect/oauth2/authorize?" +
|
|
||||||
tansParams({
|
tansParams({
|
||||||
appid: appid,
|
appid: appid,
|
||||||
redirect_uri: url,
|
redirect_uri: redirect_uri,
|
||||||
response_type: "code",
|
response_type: "code",
|
||||||
scope: "snsapi_userinfo",
|
scope: "snsapi_userinfo",
|
||||||
state: "STATE",
|
state: "STATE",
|
||||||
}) +
|
}) +
|
||||||
"#wechat_redirect";
|
"#wechat_redirect";
|
||||||
|
console.log(href);
|
||||||
|
setTimeout(() => {
|
||||||
|
window.location.href = href;
|
||||||
|
}, 5000);
|
||||||
} else {
|
} else {
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,7 @@ export default defineConfig(() => {
|
|||||||
outDir: 'dist',
|
outDir: 'dist',
|
||||||
},
|
},
|
||||||
server: {
|
server: {
|
||||||
port: '3002'
|
port: '80'
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
uni()
|
uni()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user