123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- <template>
- <view class="ss-p-30 head-box">
- <template v-if="changeType === 'login'">
- <view class="head-title ss-m-b-30 MiSans-Semibold">欢迎来到门墩儿</view>
- <uni-segmented-control
- class="ss-m-t-30 MiSans-Medium"
- :current="current"
- :values="items"
- style-type="text"
- active-color="#00B760"
- @clickItem="onClickItem"
- />
- <!-- <view class="head-subtitle ss-m-t-10">未注册的手机号,验证后自动注册账号</view> -->
- <view class="ss-m-t-30">
- <!-- 手机号快捷登录 -->
- <view v-if="current === 0">
- <button v-if="!protocol" class="send-button MiSans-Medium" @click="showProtocolToast">手机号快捷登录</button>
- <button v-else class="send-button MiSans-Medium" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">手机号快捷登录</button>
- </view>
- <!-- 短信验证码登录 -->
- <uni-forms
- v-if="current === 1"
- ref="smsLoginRef"
- v-model="state.sms"
- :rules="state.smsRules"
- validateTrigger="bind"
- labelWidth="140"
- labelAlign="center"
- >
- <uni-forms-item name="phone" label="手机号">
- <uni-easyinput placeholder="请输入手机号" v-model="state.sms.phone" :inputBorder="false" type="number">
-
- </uni-easyinput>
- </uni-forms-item>
- <uni-forms-item name="code" label="验证码">
- <uni-easyinput placeholder="请输入验证码" v-model="state.sms.code" :inputBorder="false" type="number" maxlength="6">
- <template v-slot:right>
- <button class="login-code MiSans-Normal" :disabled="state.isMobileEnd" :class="{ 'code-btn-end': state.isMobileEnd }" @tap="handleCode">
- {{ getSmsTimer('smsLogin') }}
- </button>
- </template>
- </uni-easyinput>
- </uni-forms-item>
- </uni-forms>
- <!-- 账号密码登录 -->
- <uni-forms
- v-if="current === 2"
- ref="accountLoginRef"
- v-model="state.account"
- :rules="state.rules"
- validateTrigger="bind"
- labelWidth="140"
- labelAlign="center"
- >
- <uni-forms-item name="phone" label="账号">
- <uni-easyinput placeholder="请输入账号" v-model="state.account.phone" :inputBorder="false"></uni-easyinput>
- </uni-forms-item>
- <uni-forms-item name="password" label="密码">
- <uni-easyinput type="password" placeholder="请输入密码" v-model="state.account.password" :inputBorder="false"></uni-easyinput>
- </uni-forms-item>
- </uni-forms>
- <button v-if="current !== 0" class="send-button MiSans-Medium" @tap="handleLogin"> 登 录 </button>
- <view class="agreement-box ss-flex ss-row-center">
- <uni-icons size="20" :type="protocol ? 'checkbox-filled' : 'circle'" :color="protocol ? '#00B760' : '#ccc'" @tap="protocol = !protocol"></uni-icons>
- <view class="color-999 ss-flex ss-col-center ss-m-l-8 font-size-13">
- <span class="MiSans-Normal">我已阅读并遵守</span>
- <view class="color-primary MiSans-Medium" @tap.stop="handleToDetail('user')">
- 《用户协议》
- </view>
- <view class="agreement-text MiSans-Normal">和</view>
- <view class="color-primary MiSans-Medium" @tap.stop="handleToDetail('privacy')">
- 《隐私协议》
- </view>
- </view>
- </view>
- <view class="register MiSans-Normal ss-m-t-30" style="margin-bottom: 30px;" @tap="handleChangeRegister">还没有登录账户?去注册</view>
- </view>
- </template>
- <template v-if="changeType === 'register'">
- <view class="head-title pb-20 MiSans-Semibold">手机号注册</view>
- <uni-forms
- ref="registerForm"
- v-model="state.register"
- :rules="state.smsRules"
- validateTrigger="bind"
- labelWidth="140"
- labelAlign="center"
- >
- <uni-forms-item name="phone" label="手机号">
- <uni-easyinput placeholder="请输入手机号" v-model="state.register.phone" :inputBorder="false" type="number">
- </uni-easyinput>
- </uni-forms-item>
- <uni-forms-item name="code" label="验证码">
- <uni-easyinput
- placeholder="请输入验证码"
- v-model="state.register.code"
- :inputBorder="false"
- type="number"
- maxlength="6"
- >
- <template v-slot:right>
- <button
- class="login-code MiSans-Normal"
- :disabled="state.isMobileEnd"
- :class="{ 'code-btn-end': state.isMobileEnd }"
- @tap="handleRegisterCode"
- >
- {{ getSmsTimer('smsRegister') }}
- </button>
- </template>
- </uni-easyinput>
- </uni-forms-item>
- </uni-forms>
- <view class="register login color-primary ss-p-b-5 MiSans-Normal" style="text-align: end;" @tap="handleChangeLogin">已有账户?去登陆</view>
- <view>
- <button class="send-button" @tap="handleRegister"> 注 册 </button>
- </view>
- <view class="color-999 ss-flex ss-col-center ss-row-center ss-m-l-8 font-size-13" style="margin-bottom: 30px;">
- <span class="MiSans-Normal">点击注册即代表您同意</span>
- <view class="color-primary" @tap.stop="handleToDetail('user')">
- 《用户协议》
- </view>
- <view class="agreement-text">和</view>
- <view class="color-primary" @tap.stop="handleToDetail('privacy')">
- 《隐私协议》
- </view>
- </view>
- </template>
- </view>
- </template>
- <script setup>
- import { ref, unref } from 'vue'
- import { mobile, password, code } from '@/utils/validate'
- import { getSmsCode, getSmsTimer } from '@/utils/code'
- import { userStore } from '@/store/user'
- const useUserStore = userStore()
- const items = ['手机号快捷登录', '短信登录', '账号登录']
- const current = ref(0)
- const accountLoginRef = ref()
- const smsLoginRef = ref()
- const registerForm = ref()
- const protocol = ref(false)
- const state = ref({
- isMobileEnd: false, // 手机号输入完毕
- codeText: '获取验证码',
- sms: {
- phone: '',
- code: ''
- },
- register: {
- phone: '',
- code: ''
- },
- account: {
- phone: '',
- password: ''
- },
- rules: {
- phone: mobile,
- password,
- },
- smsRules: {
- code,
- phone: mobile
- }
- })
- // 设置默认账号密码便于开发快捷登录
- if (window && window.location && window.location.hostname && window.location.hostname === 'localhost') {
- state.value.account.phone = '13229740092'
- state.value.account.password = 'Citu123456'
- }
- const changeType = ref('login')
- // 登录成功后的返回页面
- // const backUrl = getCurrentPages().length ? getCurrentPages()[getCurrentPages().length - 2].route : ''
- const onClickItem = (e) => {
- current.value = e.currentIndex
- }
- // 获取验证码
- const handleCode = () => {
- if (!state.value.sms.phone) {
- uni.showToast({
- title: '请输入手机号',
- icon: 'none',
- duration: 2000
- })
- return
- }
- getSmsCode('smsLogin', state.value.sms.phone)
- }
- // 获取验证码
- const handleRegisterCode = () => {
- if (!state.value.register.phone) {
- uni.showToast({
- title: '请输入手机号',
- icon: 'none',
- duration: 2000
- })
- return
- }
- getSmsCode('smsRegister', state.value.register.phone)
- }
- // 查看协议详情
- const handleToDetail = (type) => {
- const url = type === 'user' ? '/pagesB/agreement/user' : '/pagesB/agreement/privacy'
- uni.navigateTo({
- url
- })
- }
- // 注册
- function handleChangeRegister () {
- changeType.value = 'register'
- }
- // 登录
- function handleChangeLogin () {
- changeType.value = 'login'
- }
- const showProtocolToast = () => {
- uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
- }
- // 微信登录
- const getPhoneNumber = async (e) => {
- if (e?.detail?.errMsg !== 'getPhoneNumber:ok') {
- uni.showToast({ title: '微信登录失败', icon: 'none' })
- return
- }
- changeType.value = 'login'
- wx.login({
- success: async (result) => {
- const wxLoginCode = result?.code || ''
- const query = {
- loginCode: wxLoginCode,
- phoneCode: e.detail.code,
- state: e.detail.encryptedData,
- }
- await useUserStore.handleSmsLogin(query, current.value)
- },
- fail:(res)=> { console.log("获取登录凭证code失败!", res) }
- })
- }
- async function handleRegister () {
- const validate = await unref(registerForm).validate()
- if (!validate) return
- try {
- await useUserStore.handleRegister(state.value.register)
- changeType.value = 'login'
- } finally {
- }
- }
- // 登录
- const handleLogin = async () => {
- if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
- const validate = await unref(current.value === 1 ? smsLoginRef : accountLoginRef).validate()
- if (!validate) return
- const query = current.value === 1 ? state.value.sms : state.value.account
- Object.assign(query, {
- account: query.phone
- })
-
- await useUserStore.handleSmsLogin(query, current.value)
- }
- </script>
- <style scoped lang="scss">
- .login-code {
- width: 73px;
- min-width: 73px;
- color: #00B760;
- text-align: center;
- font-size: 12px;
- cursor: pointer;
- // border: 1px dashed #00B760;
- // border-radius: 26px;
- padding: 0;
- }
- .head-title {
- font-size: 40rpx;
- text-align: center;
- color: #00B760;
- margin-top: 30rpx;
- }
- .quickLogon {
- display: flex;
- justify-content: space-between;
- padding: 0 20rpx;
- align-items: center;
- }
- .wxLogon {
- font-size: .85em;
- color: #00B760;
- border: none;
- margin: 0;
- padding: 0;
- }
- .register {
- widows: 100%;
- font-size: .85em;
- text-align: center;
- color: #00B760;
- padding-bottom: 2px;
- text-decoration: underline;
- &.login {
- color: #00B760;
- }
- }
- .pb-20 {
- padding-bottom: 40rpx;
- }
- </style>
|