Skip to content

Commit

Permalink
✨ feat(project): add alipay qq wechat login weiboShare
Browse files Browse the repository at this point in the history
  • Loading branch information
ishareme committed Jan 12, 2023
1 parent f669865 commit 05de120
Show file tree
Hide file tree
Showing 20 changed files with 481 additions and 15 deletions.
14 changes: 14 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@
rel="stylesheet"
href="https://at.alicdn.com/t/font_3042963_nv614canpao.css?spm=a313x.7781069.1998910419.47&file=font_3042963_nv614canpao.css"
/>
<!-- QQ 登录 -->
<script
type="text/javascript"
charset="utf-8"
src="https://connect.qq.com/qc_jssdk.js"
data-appid="101998494"
data-redirecturi="https://imooc-front.lgdsunday.club/login"
></script>
<!-- 微博分享 -->
<script
src="https://tjs.sjs.sinajs.cn/open/api/js/wb.js"
type="text/javascript"
charset="utf-8"
></script>
</head>
<body>
<div id="app"></div>
Expand Down
23 changes: 23 additions & 0 deletions src/api/pay.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,26 @@ export const getVipPayList = () => {
url: '/user/vip/pay/list',
});
};

// 支付宝下单
export const getAlipay = (subject, totalAmount, body, isMobile) => {
return request({
url: '/user/alipay',
params: {
subject,
totalAmount,
body,
isMobile,
},
});
};

// 获取支付结果
export const getPayResult = (out_trade_no) => {
return request({
url: '/sys/pay/result',
params: {
out_trade_no,
},
});
};
30 changes: 30 additions & 0 deletions src/api/sys.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,33 @@ export const getSts = () => {
url: '/user/sts',
});
};

// 微信登录前置数据获取
export const getWeixinLoginData = () => {
return request({
url: '/sys/wxlogin/data',
});
};

// 微信登录 获取access_token
export const getWeixinLoginToken = (appid, secret, code) => {
return request({
url: '/sys/wxlogin/access_token',
params: {
appid,
secret,
code,
},
});
};

// 微信登录 获取用户信息
export const getWeixinLoginUserInfo = (token, openid) => {
return request({
url: '/sys/wxlogin/userinfo',
params: {
token,
openid,
},
});
};
13 changes: 13 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ export const UNSPLASH_API_S_KEY = 'SzdWmSaX50iCXicSJlu4KhlYrM6cE5AoUa_ZndXs140';
export const OSS_REGION = 'oss-cn-beijing';
export const OSS_BUCKET = 'imooc-front';

// 没注册的code
export const LOGIN_TYPE_OAUTG_NO_REGISTER_CODE = 204;
export const LOGINTYPE_QQ = 'QQ';
export const LOGINTYPE_WX = 'WX';

// 兔小槽 反馈地址
export const FEEDBACK_URL = 'https://support.qq.com/product/491970';

// 微博APP Key
export const WEI_BO_APP_KEY = '3454329089';
// 微博用户的 UID
export const WEI_BO_UID = '5984245953';

// pc设备指定宽度
export const PC_DEVICE_WIDTH = 1280;

Expand Down
5 changes: 5 additions & 0 deletions src/router/modules/mobile-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ export default [
user: true,
},
},
{
path: '/pay/result',
name: 'payResult',
component: () => import('@/views/pay'),
},
];
5 changes: 5 additions & 0 deletions src/router/modules/pc-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export default [
user: true,
},
},
{
path: '/pay/result',
name: 'payResult',
component: () => import('@/views/pay'),
},
],
},
{
Expand Down
8 changes: 8 additions & 0 deletions src/store/modules/user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import md5 from 'md5';
import { login, getProfile, register } from '@/api/sys';
import { Message } from '@/libs';
import { LOGIN_TYPE_OAUTG_NO_REGISTER_CODE } from '@/constants';

export default {
namespaced: true,
state: () => ({
Expand All @@ -23,6 +25,9 @@ export default {
...payload,
password: password ? md5(password) : '',
});
if (data.code === LOGIN_TYPE_OAUTG_NO_REGISTER_CODE) {
return data.code;
}
context.commit('setToken', data.token);
// 获取用户信息
context.dispatch('profile');
Expand All @@ -31,6 +36,9 @@ export default {
code: 204, // 该用户尚未注册,用于第一次扫码登录时
token: '63776a8e-dc47-49e6-b63d-e53c51966943',
};
// if (data.code === LOGIN_TYPE_OAUTG_NO_REGISTER_CODE) {
// return data.code;
// }
context.commit('setToken', data.token);
context.dispatch('profile');
}
Expand Down
54 changes: 54 additions & 0 deletions src/utils/brodacast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 同源不同页通讯 window open
* window.BroadcastChannel 不可用的时候降级处理 localStorage
*/

// 频道关键key值
const LOGIN_SUCCESS_CHANNEL = 'LOGIN_SUCCESS_CHANNEL';
let brodacastChannel = null;
if (window.BroadcastChannel) {
brodacastChannel = new BroadcastChannel(LOGIN_SUCCESS_CHANNEL);
}

// 发送消息的方法
const send = (data) => {
if (brodacastChannel) {
brodacastChannel.postMessage(data);
} else {
localStorage.setItem(LOGIN_SUCCESS_CHANNEL, JSON.stringify(data));
}
};

// 等待回调的方法
const wait = () => {
return new Promise((resolve) => {
if (brodacastChannel) {
brodacastChannel.onmessage = (event) => {
resolve(event.data);
};
} else {
localStorage.onstorage = (event) => {
if (event.key === LOGIN_SUCCESS_CHANNEL) {
resolve(JSON.parse(event.newValue));
}
};
}
});
};

// 销毁

const clear = () => {
if (brodacastChannel) {
brodacastChannel.close();
brodacastChannel = null;
} else {
localStorage.removeItem(LOGIN_SUCCESS_CHANNEL);
}
};

export default {
send,
wait,
clear,
};
37 changes: 37 additions & 0 deletions src/utils/oauth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import store from '@/store';
import { LOGIN_TYPE_OAUTG_NO_REGISTER_CODE } from '@/constants';
import { Message } from '@/libs';
import router from '@/router';

/**
* 第三方登录统一处理方法
* @param {*} oauthType 登录方式
* @param {*} oauthData 第三方数据
*/
export const oauthLogin = async (oauthType, oauthData) => {
// 1.登录
// 2. 是否需要注册
const code = await store.dispatch('user/login', {
loginType: oauthType,
...oauthData,
});
// 判断扫码是否需要注册
if (code === LOGIN_TYPE_OAUTG_NO_REGISTER_CODE) {
Message(
'success',
`欢迎你 ${oauthData.nickname}, 请创建你的账号`,
6000
);
// 进入注册页面,同时携带当前的第三方数据和注册标记
router.push({
path: '/register',
query: {
reqType: oauthType,
...oauthData,
},
});
return;
}
// 否则表示用户已注册,直接进入首页
router.push('/');
};
22 changes: 22 additions & 0 deletions src/utils/pay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getAlipay } from '@/api/pay';
import { isMobileTerminal } from '@/utils/flexible';

/**
* 触发支付宝支付
* @param {*} title 支付标题
* @param {*} desc 支付描述
*/
export const alipay = async (title, desc) => {
try {
const { encodeURI } = await getAlipay(
title,
'0.01',
desc,
isMobileTerminal.value
);
const url = decodeURIComponent(encodeURI);
window.location.href = url;
} catch (error) {
console.log('[ 1212 ]');
}
};
12 changes: 12 additions & 0 deletions src/utils/share.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { WEI_BO_APP_KEY, WEI_BO_UID } from '@/constants';

/**
*
* @param {*} imgUrl 分享图片
* @param {*} path 网页链接
*/
export const weiboShare = (imgUrl, path) => {
window.open(
`https://service.weibo.com/share/share.php?appkey=${WEI_BO_APP_KEY}&ralateUid=${WEI_BO_UID}&pic=${imgUrl}&title=这张图不错哦,给大家分享一下 ${path}`
);
};
9 changes: 8 additions & 1 deletion src/views/layout/components/floating/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
</template>
<div
class="flex items-center py-1 px-1.5 cursor-pointer rounded hover:bg-zinc-100/60 dark:hover:bg-zinc-800"
@click="onFeedbackClick"
>
<SvgIcon
name="feedback"
class="w-1.5 h-1.5 mr-1"
fillClass="fill-zinc-900 dark:fill-zinc-300"
/>
<span class="text-zinc-800 dark:text-zinc-300 text-sm"
>前往博客</span
>前往吐槽</span
>
</div>
</Popover>
Expand All @@ -45,6 +46,8 @@ import Driver from 'driver.js';
import 'driver.js/dist/driver.min.css';
import { onMounted } from 'vue';
import steps from './steps';
import { FEEDBACK_URL } from '@/constants';
let driver = null;
onMounted(() => {
driver = new Driver({
Expand All @@ -61,6 +64,10 @@ const onGuideClick = () => {
// 开始
driver.start();
};
const onFeedbackClick = () => {
window.open(FEEDBACK_URL, '__blank');
};
</script>

<style lang="scss" scoped></style>
73 changes: 73 additions & 0 deletions src/views/loginRegister/login/QQLogin.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<template>
<div>
<span id="qqLoginBtn" v-show="false"> </span>
<SvgIcon class="w-4 cursor-pointer" name="qq" @click="onQQLogin" />
</div>
</template>

<script>
// QQ 登录的 URL
const QQ_LOGIN_URL =
'https://graph.qq.com/oauth2.0/authorize?client_id=101998494&response_type=token&scope=all&redirect_uri=https%3A%2F%2Fimooc-front.lgdsunday.club%2Flogin';
</script>
<script setup>
import { onMounted } from 'vue';
import Brodacast from '@/utils/brodacast.js';
import { oauthLogin } from '@/utils/oauth.js';
import { LOGINTYPE_QQ } from '@/constants';
const onQQLogin = () => {
openQQWindow();
};
// 处理qq登录视窗 移动端显示为满屏的状态
const openQQWindow = () => {
window.open(
QQ_LOGIN_URL,
'oauth2Login_10609',
'height=525,width=585, toolbar=no, menubar=no, scrollbars=no, status=no, location=yes, resizable=yes'
);
// 打开视窗之后开始等待
Brodacast.wait().then((oauthObj) => {
Brodacast.clear();
// 执行登录操作
oauthLogin(LOGINTYPE_QQ, oauthObj);
});
};
onMounted(() => {
/* eslint-disable */
QC.Login(
{
btnId: 'qqLoginBtn', //插入按钮的节点id
},
// 登录成功之后的回调 会在 登录回调页面中被执行 小窗口中
// qq登录存在缓存 登录成功之后 下次进入页面 会自动重新登录 刷新页面也会自动执行
(data, opts) => {
console.log('qq登录成功', data, opts);
// 注销登录 防止下次打开页面直接展示上一次用户信息
QC.Login.signOut();
// 拿到qq用户的唯一标识 通过这个标识来判断当前用户是否已经注册我们app中
const accessToken = /access_token=((.*))&expires_in/.exec(
window.location.hash
)[1];
// 拼接获取到的对象
const { nickname, figureurl_qq_2 } = data;
const oauthObj = {
nickname,
figureurl_qq_2,
accessToken,
};
Brodacast.send(oauthObj);
// 针对于 移动端而言:通过移动端触发 QQ 登录会展示三个页面,原页面、QQ 吊起页面、回调页面。并且移动端一个页面展示整屏内容,且无法直接通过 window.close() 关闭,所以在移动端中,我们需要在当前页面继续进行后续操作。
oauthLogin(LOGINTYPE_QQ, oauthObj);
// 移动端下不行 移动端下没有窗口的概念
window.close();
}
);
});
</script>

<style lang="scss" scoped></style>
Loading

0 comments on commit 05de120

Please sign in to comment.