123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- <template>
- <view class="page-box">
- <!--
- <uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="button"
- activeColor="#007aff">
- </uni-segmented-control> -->
- <view style="margin-top: 30px;">
- <uni-forms v-if="current==0" :modelValue="postForm" ref="postForm" :rules="rules" :label-width="100">
- <uni-forms-item name="username" label="登录账号" required>
- <uni-easyinput type="text" v-model="postForm.username" placeholder="用户名/手机号" />
- </uni-forms-item>
- <uni-forms-item name="password" label="登录密码" required>
- <uni-easyinput type="password" v-model="postForm.password" placeholder="请输入密码" />
- </uni-forms-item>
- <uni-forms-item name="captchaValue" label="图形验证" required>
- <view class="ca-box">
- <uni-easyinput class="input" type="text" v-model="postForm.captchaValue"
- placeholder="输入验证码" />
- <image class="img" @click="refreshCaptcha" :src="captchaUrl"></image>
- </view>
- </uni-forms-item>
- </uni-forms>
- <uni-forms v-if="current==1" :modelValue="postForm" ref="postForm" :rules="rules">
- <sms-box v-model="postForm" ref="smsBox" :type="2"></sms-box>
- </uni-forms>
- <view>
- <button type="primary" @tap="handleLogin">登录</button>
- </view>
- <view class="login-opt">
- <a @tap="$navs.toReg">立即注册</a>
- <a @tap="$navs.toForgot">忘记密码?</a>
- </view>
- </view>
- <view class="other-login">
- <!-- <view class="tt">其它登录方式</view> -->
- <!-- <view style="display: flex; align-items: center;"> -->
- <!-- #ifdef MP-WEIXIN -->
- <!-- <view class="types">
- <image src="../../static/images/we-chat.png" style="width: 35px; height: 35px;" @click="appLoginWx">
- </image>
- <view>微信登录</view>
- </view> -->
- <!-- #endif -->
- <!-- <view class="types">
- <image src="../../static/images/face-id.png" style="width: 35px; height: 35px;"
- @click="appLoginFace"></image>
- <view>人脸登录</view>
- </view> -->
- <!-- </view> -->
- </view>
- </view>
- </template>
- <script>
- import SmsBox from './components/SmsBox.vue'
- import {
- login,
- mobileLogin
- } from '@/api/user.js'
- import {
- wxLogin,
- apiGetWechatUrl
- } from '@/api/ability/login/wx.js'
- export default {
- components: {
- SmsBox
- },
- data() {
- return {
- captchaUrl: '',
- current: 0,
- items: ['账号登录', '手机登录'],
- rules: {
- username: {
- rules: [{
- required: true,
- errorMessage: '用户名不能为空!'
- }]
- },
- password: {
- rules: [{
- required: true,
- errorMessage: '密码不能为空!'
- }]
- },
- captchaValue: {
- rules: [{
- required: true,
- errorMessage: '图形验证码不能为空!'
- }]
- }
- },
- postForm: {
- captchaKey: '',
- captchaValue: '',
- smsCode: '',
- smsType: 2,
- username: '',
- password: ''
- }
- }
- },
-
- onBackPress(e) {
- this.backIndex()
- return true;
- },
- created() {
- this.refreshCaptcha()
- },
- methods: {
-
- backIndex(){
- uni.switchTab({
- url: '/pages/index/index'
- })
- },
- onClickItem(e) {
- if (this.current != e.currentIndex) {
- this.current = e.currentIndex;
- }
- },
- handleWxLogin(code, nickName, avatar) {
- uni.showLoading({
- title: '登录中...'
- });
- wxLogin({
- code: code,
- nickName: nickName,
- avatar: avatar
- }).then(data => {
- // 保存本地token
- this.loginBack(data)
- uni.hideLoading();
- })
- },
- appLoginWx() {
- let that = this
- // #ifdef MP-WEIXIN
- uni.showLoading({
- title: '请稍后...'
- });
- let code = '';
- // 获取微信code
- uni.login({
- provider: 'weixin',
- success: (res) => {
- code = res.code
- }
- })
- // 获取用户信息-新版
- if (uni.getUserProfile) {
- uni.getUserProfile({
- desc: "获取你的昵称,头像,地区以及姓名",
- success: (profile) => {
- if (profile.errMsg === 'getUserProfile:ok') {
- const data = JSON.parse(profile.rawData);
- that.handleWxLogin(code, data.nickName, data.avatarUrl);
- }
- }
- })
- }
- // #endif
- // #ifdef H5
- apiGetWechatUrl({
- state: 'h5'
- }).then(data => {
- window.location = data.url
- })
- //#endif
- },
- thirdLogin() {
- uni.login({
- success: function(loginRes) {
- console.log(loginRes.authResult);
- }
- });
- },
- refreshCaptcha() {
- this.postForm.captchaKey = new Date().getTime()
- this.captchaUrl = `${this.$urls.api}/api/common/captcha/gen?key=${this.postForm.captchaKey}`
- },
- quickLogin() {
- this.postForm.username = 'student'
- this.postForm.password = 'student'
- this.doLogin()
- },
- loginBack(data) {
- // 关闭加载
- uni.hideLoading()
- // 保存本地token
- uni.setStorageSync('token', data.token);
-
- if(!data.idCard){
- //打印请求返回的数据
- uni.redirectTo({
- url: '/pages/user/info/info'
- });
- }else{
- uni.setStorageSync('idCard', data.idCard);
- //打印请求返回的数据
- uni.switchTab({
- url: '/pages/index/index'
- });
- }
- },
- async mobileLogin() {
- // 消息框
- const smsCheck = await this.$refs.smsBox.validate().then(res => {
- return res
- });
- console.log('smsCheck', smsCheck)
- if (!smsCheck) {
- return;
- }
- // 对整个表单进行校验,返回一个 Promise
- this.$refs.postForm.validate().then((res) => {
- uni.showLoading({
- title: '登录中...'
- });
- // 手机登录
- mobileLogin(this.postForm).then(data => {
- this.loginBack(data)
- })
- }).catch(() => {
- })
- },
- accountLogin() {
- this.$refs.postForm.validate().then((res) => {
- uni.showLoading({
- title: '登录中...'
- });
- // 快速注册后开始考试
- login(this.postForm).then(data => {
- this.loginBack(data)
- });
- }).catch(() => {
- })
- },
- // 登录入口
- handleLogin() {
- if (this.current === 0) {
- this.accountLogin()
- } else {
- this.mobileLogin()
- }
- },
- // 人脸登录
- appLoginFace() {
- uni.redirectTo({
- url: '/pages/login/face'
- })
- }
- }
- }
- </script>
- <style>
- .other-login {
- flex-grow: 1;
- display: flex;
- align-items: center;
- justify-content: center;
- flex-direction: column;
- }
- .other-login .tt {
- text-align: center;
- border-bottom: #eee 1px solid;
- padding-bottom: 10px;
- width: 100%;
- color: #666;
- }
- .other-login .types {
- display: flex;
- align-items: center;
- flex-direction: column;
- justify-content: center;
- margin-top: 20px;
- font-size: 12px;
- color: #333;
- padding: 0px 10px 0px 10px;
- }
- .other-login .types uni-view,
- view {
- font-size: 12px;
- color: #666;
- }
- </style>
|