purchase-let/pages/login/login.vue

268 lines
7.7 KiB
Vue
Raw Normal View History

2024-04-25 18:02:30 +08:00
<template>
2024-04-28 18:08:57 +08:00
<view>
2024-05-06 17:58:41 +08:00
<up-navbar title="登录" :autoBack="false" placeholder bgColor="rgba(0,0,0,0)">
<template #left>
<up-icon v-if="!showWeixin" name="arrow-left" @click="navBack" size="20"></up-icon>
<view v-else></view>
</template>
</up-navbar>
2024-04-28 18:08:57 +08:00
<view class="login-box">
2024-04-27 18:02:43 +08:00
<image class="logo" src="https://lihai001.oss-cn-chengdu.aliyuncs.com/def/35adb202404271727457954.png"></image>
2024-04-29 18:14:58 +08:00
<view class="tips">欢迎登录惠农批发</view>
2024-04-28 18:08:57 +08:00
<block v-if="showWeixin">
<up-transition :show="showWeixin">
<view class="btn">
2024-05-06 17:58:41 +08:00
<up-button @click="weixinLogin" color="#20B128" size="large"><up-icon name="weixin-fill" color="#fff"
size="28"></up-icon></up-button>
2024-04-28 18:08:57 +08:00
</view>
<view class="btn">
2024-05-06 17:58:41 +08:00
<up-button color="#ECFFEE" @click="showWeixin=false" size="large"><text
style="color: #20B128;">使用短信验证登录</text></up-button>
2024-04-28 18:08:57 +08:00
</view>
</up-transition>
</block>
<block v-else>
<up-transition :show="!showWeixin">
<view class="form">
<view class="input">
2024-05-06 17:58:41 +08:00
<up-input :customStyle="{height: '100%'}" v-model="loginForm.phone" placeholderClass="place" border="none"
placeholder="请输入手机号" type="number">
2024-04-28 18:08:57 +08:00
<template #prefix>
<image style="height: 40rpx;width: 40rpx;margin-top: 6rpx;"
src="https://lihai001.oss-cn-chengdu.aliyuncs.com/def/48491202404281006484208.png"></image>
</template>
</up-input>
</view>
<view class="input">
2024-05-06 17:58:41 +08:00
<up-input :customStyle="{height: '100%'}" v-model="loginForm.code" :maxlength="4" placeholderClass="place"
border="none" placeholder="请输入验证码" type="number">
2024-04-28 18:08:57 +08:00
<template #prefix>
<image style="height: 40rpx;width: 40rpx;margin-top: 6rpx;"
src="https://lihai001.oss-cn-chengdu.aliyuncs.com/def/3a42f202404281007454918.png"></image>
</template>
<template #suffix>
2024-05-06 17:58:41 +08:00
<up-code :seconds="seconds" ref="uCodeRef" @change="codeChange"></up-code>
2024-04-28 18:08:57 +08:00
<view style="color: #20B128;" @click="getCode">{{tips}}</view>
</template>
</up-input>
</view>
</view>
<view class="btn">
<up-button color="#20B128" size="large" @click="codeLogin">登录</up-button>
</view>
</up-transition>
2024-04-27 18:02:43 +08:00
</block>
<view class="agreement">
2024-04-28 18:08:57 +08:00
<image v-if="!isAgree" @click="isAgree=true" src="@/static/icon/n-check.png"></image>
<image v-else @click="isAgree=false" src="@/static/icon/check.png"></image>
2024-04-27 18:02:43 +08:00
<view>
我已同意<text>用户协议</text><text>隐私政策</text>
</view>
</view>
</view>
2024-05-06 17:58:41 +08:00
<bindPhone :show="showBind" @close="showBind = false" @change="getPhoneNumber" />
2024-04-28 18:08:57 +08:00
</view>
2024-04-25 18:02:30 +08:00
</template>
<script setup>
2024-04-28 18:08:57 +08:00
import { onBackPress } from "@dcloudio/uni-app"
import { ref } from "vue"
2024-05-06 11:51:21 +08:00
import { userLoginApi, userLoginWeixinApi, getMobileByMnpApi } from "@/api/user.js";
2024-04-28 18:08:57 +08:00
import useUserStore from "@/store/user.js"
2024-05-06 11:51:21 +08:00
import bindPhone from "@/components/bindPhone.vue"
2024-05-06 17:58:41 +08:00
const navToIndex = () => {
if (!userStore.userInfo.supplier) uni.reLaunch({
2024-05-06 11:51:21 +08:00
url: '/pages/index/index'
})
else uni.$u.toast('功能开发中')
}
2024-04-25 18:02:30 +08:00
2024-04-28 18:08:57 +08:00
const userStore = useUserStore(); //使用pinia进行状态管理
2024-05-06 17:58:41 +08:00
if (userStore.userInfo && userStore.userInfo.mobile) {
2024-05-06 11:51:21 +08:00
navToIndex();
2024-05-06 17:58:41 +08:00
} else {
2024-05-06 11:51:21 +08:00
userStore.setToken('');
userStore.setUserInfo({});
}
2024-04-25 18:02:30 +08:00
2024-04-28 18:08:57 +08:00
const showWeixin = ref(true); //是否显示微信登录
const isAgree = ref(false); //是否同意协议
2024-04-25 18:02:30 +08:00
2024-04-28 18:08:57 +08:00
const weixinLogin = () => {
2024-04-30 18:08:36 +08:00
if (!isAgree.value) return uni.$u.toast('请先阅读并同意协议');
uni.showLoading({
title: '登录中'
})
2024-04-28 18:08:57 +08:00
uni.login({
provider: 'weixin',
success: (res) => {
2024-04-30 16:24:55 +08:00
userLoginWeixinApi({
code: res.code
2024-05-06 17:58:41 +08:00
}).then(res => {
2024-05-06 11:51:21 +08:00
uni.hideLoading();
2024-04-30 18:08:36 +08:00
userStore.setToken(res.data.token);
2024-05-06 11:51:21 +08:00
userStore.setUserInfo(res.data);
2024-05-06 17:58:41 +08:00
if (!res.data.mobile) { //未绑定手机号
2024-05-06 11:51:21 +08:00
return showBind.value = true;
2024-05-06 10:11:25 +08:00
}
2024-05-06 11:51:21 +08:00
navToIndex();
2024-04-30 16:24:55 +08:00
})
},
fail: (err) => {
console.log(err);
uni.$u.toast('登录失败');
2024-04-28 18:08:57 +08:00
}
})
}
2024-04-25 18:02:30 +08:00
2024-05-06 11:51:21 +08:00
const showBind = ref(false); //是否显示绑定手机号弹窗
2024-04-28 18:08:57 +08:00
const getPhoneNumber = (e) => {
2024-05-06 11:51:21 +08:00
console.log("===", e);
2024-05-06 17:58:41 +08:00
if (e.detail?.errMsg == 'getPhoneNumber:ok') {
2024-05-06 11:51:21 +08:00
getMobileByMnpApi({
code: e.detail.code
2024-05-06 17:58:41 +08:00
}).then(res => {
2024-05-06 11:51:21 +08:00
navToIndex();
})
2024-05-06 17:58:41 +08:00
} else {
2024-05-06 11:51:21 +08:00
console.log("用户拒绝授权");
return uni.$u.toast('您拒绝了授权');
}
2024-04-27 18:02:43 +08:00
}
2024-05-06 17:58:41 +08:00
2024-04-28 18:08:57 +08:00
const loginForm = ref({
phone: '15366662222',
code: ''
})
2024-05-06 17:58:41 +08:00
const isPhone = () => { //检验手机号是否正确
loginForm.value.phone = loginForm.value.phone.replace(/\s*/g, ""); //去除空格
2024-04-28 18:08:57 +08:00
return !/^1[3456789]\d{9}$/.test(loginForm.value.phone);
2024-04-27 18:02:43 +08:00
}
2024-05-06 17:58:41 +08:00
const codeLogin = () => {
2024-04-28 18:08:57 +08:00
if (!isAgree.value) return uni.$u.toast('请先阅读并同意协议');
// 验证码登录
2024-05-06 17:58:41 +08:00
if (isPhone()) return uni.$u.toast('请输入正确的手机号码');
2024-04-28 18:08:57 +08:00
console.log('登录');
userLoginApi({
account: '17811111111',
password: '1111',
terminal: 3,
scene: 1
2024-05-06 17:58:41 +08:00
}).then(res => {
2024-04-28 18:08:57 +08:00
console.log(res);
})
}
2024-05-06 17:58:41 +08:00
2024-04-28 18:08:57 +08:00
const tips = ref('');
const seconds = ref(60);
const uCodeRef = ref(null);
2024-05-06 17:58:41 +08:00
2024-04-28 18:08:57 +08:00
const codeChange = (text) => {
tips.value = text;
};
2024-05-06 17:58:41 +08:00
2024-04-28 18:08:57 +08:00
const getCode = () => {
if (!isAgree.value) return uni.$u.toast('请先阅读并同意协议');
if (uCodeRef.value.canGetCode) {
2024-05-06 17:58:41 +08:00
if (isPhone()) return uni.$u.toast('请输入正确的手机号码');
2024-04-28 18:08:57 +08:00
// 模拟向后端请求验证码
uni.showLoading({
title: '正在获取验证码',
});
setTimeout(() => {
uni.hideLoading();
// 这里此提示会被start()方法中的提示覆盖
uni.$u.toast('验证码已发送');
// 通知验证码组件内部开始倒计时
uCodeRef.value.start();
}, 2000);
} else {
uni.$u.toast('倒计时结束后再发送');
}
};
2024-05-06 17:58:41 +08:00
2024-04-28 18:08:57 +08:00
// 微信小程序无法拦截原生返回按钮,所以使用自定义导航拦截返回
2024-05-06 17:58:41 +08:00
const navBack = () => {
if (showWeixin.value == false) {
2024-04-28 18:08:57 +08:00
showWeixin.value = true;
return true;
2024-05-06 17:58:41 +08:00
} else uni.navigateBack();
2024-04-27 18:02:43 +08:00
}
2024-04-28 18:08:57 +08:00
</script>
<style lang="scss">
page {
background-color: #fff;
}
.login-box {
width: 700rpx;
2024-04-30 16:24:55 +08:00
height: 80vh;
2024-04-28 18:08:57 +08:00
margin: 0 auto;
box-sizing: border-box;
2024-04-27 18:02:43 +08:00
display: flex;
2024-04-28 18:08:57 +08:00
flex-direction: column;
align-items: center;
padding-top: 10vh;
position: relative;
.logo {
height: 152rpx;
width: 152rpx;
}
.tips {
color: #444444;
font-size: 28rpx;
margin: 30rpx 0;
}
.btn {
width: 600rpx;
margin-top: 40rpx;
font-size: 32rpx !important;
}
.form {
.input {
background-color: #ECFFEE;
width: 600rpx;
height: 90rpx;
box-sizing: border-box;
padding: 0 20rpx;
margin-bottom: 40rpx;
2024-05-06 17:58:41 +08:00
.place {
2024-04-28 18:08:57 +08:00
color: #333;
}
2024-05-06 17:58:41 +08:00
.customStyle {
2024-04-28 18:08:57 +08:00
color: red;
}
}
}
.agreement {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
display: flex;
justify-content: center;
color: #444;
image {
width: 40rpx;
height: 40rpx;
margin-right: 10rpx;
}
text {
color: #20B128;
}
2024-04-27 18:02:43 +08:00
}
}
2024-04-28 18:08:57 +08:00
</style>