Skip to content

Commit

Permalink
feat:更新登录
Browse files Browse the repository at this point in the history
  • Loading branch information
mason369 committed Jan 15, 2023
1 parent e2ad12e commit 1b3a397
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 26 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knock-ding-yanxuan",
"version": "1.2.0",
"version": "1.0.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
Expand Down
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title><%= htmlWebpackPlugin.options.title %></title>
<script src="https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
</head>
<body>
<noscript>
Expand Down
81 changes: 64 additions & 17 deletions src/components/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<li>|</li>
<li
:class="!isShowFrom ? 'login-active' : ''"
@click="isShowFrom = false"
@click="showWeinxinLogin"
>
微信扫码登录
</li>
Expand Down Expand Up @@ -59,29 +59,34 @@
登录
</button>
</div>
<div class="wx-qrcode" v-show="!isShowFrom">二维码</div>
<div class="wx-qrcode" id="weixin" v-show="!isShowFrom">
二维码
</div>
</div>
</div>
</div>
</template>

<script>
import { mapMutations } from "vuex";
import { reqGetSmsCode, reqLogin } from "@/request/api";
import { reqBindPhone, reqGetSmsCode, reqLogin } from "@/request/api";
export default {
data() {
return {
isShowFrom: true,
msg : "请按住滑块,拖动对应位置",
//手机号
phoneNum : "15918796216",
phoneNum : "19183879605",
//验证码
codeText : "获取验证码",
SMScode : "156"
};
},
methods: {
...mapMutations({setIsShowLoginModal: "isShowLoginModal/setIsShowLoginModal"}),
...mapMutations({
setIsShowLoginModal: "isShowLoginModal/setIsShowLoginModal",
setIsShowCartModal : "loginStatus/setLoginStatus"
}),
close() {
this.setIsShowLoginModal(false);
},
Expand All @@ -97,6 +102,21 @@ export default {
},
// 提交登录
async submitLogin() {
let uuid = sessionStorage.getItem("loginUuid");
let res = null;
if (uuid) {
res = await reqBindPhone({
phone : this.phoneNum.trim(),
verifyCode: this.SMScode.trim(),
uuid
});
}
else {
res = await reqLogin({
phone : this.phoneNum.trim(),
verifyCode: this.SMScode.trim()
});
}
if (!this.verify()) {
return;
}
Expand All @@ -105,19 +125,20 @@ export default {
this.$message.error("请输入验证码");
return;
}
let res = await reqLogin({
verifyCode: this.SMScode.trim(),
phone : this.phoneNum.trim()
});
console.log(res);
//登录成功
if (res.code === 0) {
this.$message.success("登录成功");
this.close();
sessionStorage.setItem("token", res["x-auth-token"]);
if (!res) {
return;
}
else {
this.$message.error(res.message);
console.log(res);
this.$message.success("登录成功");
this.close();
sessionStorage.setItem("token", res["x-auth-token"]);
this.setIsShowCartModal(true);
if (uuid) {
sessionStorage.removeItem("loginUuid");
//清除地址栏code
await this.$router.push("/home");
}
},
verify() {
Expand Down Expand Up @@ -149,18 +170,41 @@ export default {
},
// 获取验证码
async onGetCode() {
//防抖
if (!this.verify()) {
return;
}
//发送验证码
let res = await reqGetSmsCode({ phone: this.phoneNum.trim() });
if (res.code == 0 || res.code == 400) {
if (res.code === 0 || res.code === 400) {
this.countDown();
this.$message.success("验证码已发送");
}
else {
this.$message.error(res.msg);
}
},
// 微信登录
showWeinxinLogin() {
// 切换到展示二维码的界面
this.isShowFrom = false;
// 获取二维码
// 微信登录第一步:申请微信登录二维码
let _this = this;
new WxLogin({
id : "weixin",
appid : "wx67cfaf9e3ad31a0d", // 这个appid要填死
scope : "snsapi_login",
// 扫码成功后重定向的接口
redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
// state填写编码后的url
state : encodeURIComponent(
window.btoa("http://127.0.0.1:8080" + _this.$route.path)
),
// 调用样式文件
href:
"data:text/css;base64,Lyogd3hsb2dpbi5jc3MgKi8NCi5pbXBvd2VyQm94IC50aXRsZSwgLmltcG93ZXJCb3ggLmluZm97DQogIGRpc3BsYXk6IG5vbmU7DQp9DQoNCi5pbXBvd2VyQm94IC5xcmNvZGV7DQogIG1hcmdpbi10b3A6IDIwcHg7DQp9"
});
}
}
};
Expand Down Expand Up @@ -307,4 +351,7 @@ export default {
}
}
}
.wx-qrcode {
margin-top: 100px;
}
</style>
33 changes: 31 additions & 2 deletions src/components/TopBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,48 @@

<script>
import { mapMutations, mapState } from "vuex";
import { reqQrcodeLogin } from "@/request/api";
export default {
data() {
return { cartTotal: 0 };
},
name : "TopBar",
methods: {
...mapMutations({setIsShowLoginModal: "isShowLoginModal/setIsShowLoginModal"}),
...mapMutations({
setIsShowLoginModal: "isShowLoginModal/setIsShowLoginModal",
setLoginStatus : "loginStatus/setLoginStatus"
}),
login() {
// 触发登录弹窗
this.setIsShowLoginModal(true);
}
},
computed: {...mapState({isLogined: (state) => state.loginStatus.isLogined})}
computed: {...mapState({ isLogined: (state) => state.loginStatus.isLogined })},
created() {
setTimeout(async() => {
let loginCode = this.$route.query.code;
if (loginCode) {
const res = await reqQrcodeLogin({ code: loginCode });
console.log(res);
if (res.code === 0) {
this.$message.success("登录成功");
sessionStorage.setItem("token", res["x-auth-token"]);
this.setLoginStatus(true);
//清除地址栏code
await this.$router.push("/home");
}
else if (res.code === 400) {
this.$message.error(res.msg);
setIsShowLoginModal(true);
}
else if (res.code === 407) {
this.$message.warning("请使用手机号绑定登录微信");
sessionStorage.setItem("loginUuid", res.uuid);
setIsShowLoginModal(true);
}
}
}, 100);
}
};
</script>

Expand Down
6 changes: 6 additions & 0 deletions src/request/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ export const reqGetSmsCode = (params) => request.post("/sendSMS", qs.stringify(

//登录
export const reqLogin = (params) => request.post("/phoneRegin", qs.stringify( params ));

//扫码登录
export const reqQrcodeLogin = (params) => request.post("/wechatUsers/PCLogin", qs.stringify( params ));

//绑定手机号
export const reqBindPhone = (params) => request.post("/wechatUsers/binding", qs.stringify( params ));
2 changes: 1 addition & 1 deletion src/router/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default [
//重定向
{
path : "/",
redirect: "Home"
redirect: "home"
},
//首页组件
{
Expand Down
1 change: 0 additions & 1 deletion src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ export default {
comments: {},
async created() {
const { data: res } = await reqGetRecommended();
console.log(res);
}
};
</script>
Expand Down
3 changes: 1 addition & 2 deletions vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ module.exports = {
// devServer: {
// proxy: {
// "/api": {
// target : "http://gmall-h5-api.atguigu.cn", //代理转发路径
// target : process.env.VUE_APP_BASE_URL, //代理转发路径
// pathRewrites: { "^/api": "" }, //匹配字符串
// ws : true, //支持websocket
// changeOrigin: true //跨域
// }
// }
Expand Down

0 comments on commit 1b3a397

Please sign in to comment.