123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <template>
- <view class="container login">
- <!-- logo区域 -->
- <view class="login-main">
- <image class="logo" :src="StaticUrl + 'icon-logo@2x.png'" mode="widthFix"></image>
- </view>
- <!-- 邀请码 -->
- <view class="login-input">
- <input
- class="input"
- type="number"
- v-model="params.mobile"
- @blur="handBlurInput"
- placeholder="请输入手机号"
- maxlength="11"
- />
- </view>
- <view class="login-input">
- <input
- class="input_code"
- type="number"
- v-model="params.verificationCode"
- placeholder="请输入短信验证码"
- maxlength="6"
- />
- <view class="code-btn" :class="[isMobileDisabled ? 'disabled' : '']">
- <button type="button" @click.stop="GetMobileCodeFn" :disabled="isMobileDisabled" class="button">
- {{ mobileCodeText }}
- </button>
- </view>
- </view>
- <!-- 提示信息 -->
- <view class="logo-message"
- ><text>{{ loginMessage }}</text></view
- >
- <button
- class="login-btn"
- :class="[isLoginStatus ? 'disabled' : '']"
- type="primary"
- open-type="getUserInfo"
- size="small"
- @click="SubMitLogins"
- lang="zh_CN"
- >
- 登录
- </button>
- <cm-loading text="登录中..." :visible="isLogin"></cm-loading>
- </view>
- </template>
- <script>
- import authorize from '@/common/authorize.js'
- import CmLoading from '@/components/cm-module/cm-loading/cm-loading.vue'
- import { mapGetters, mapActions } from 'vuex'
- export default {
- components: {
- CmLoading
- },
- data() {
- return {
- invitationCode: '', //获取用户登录的邀请码
- loginMessage: '未注册的手机号验证后自动创建呵呵商城账户', //登录信息反馈
- StaticUrl: this.$Static,
- disabled: false,
- params: {
- mobile: '',
- headImgUrl: '', //微信头像
- nickName: '', //微信昵称
- openId: '', //微信openId
- verificationCode: '' //短信验证码
- },
- count: '', //倒计时
- isMobileDisabled: true, //获取手机短信按钮
- mobileCodeText: '获取验证码',
- codeTime: null,
- isLogin: false,
- hasProfile: false
- }
- },
- onLoad() {
- if (this.hasLogin) return this.$api.switchTabTo('/pages/tabBar/index/index')
- },
- created() {
- this.loginInit()
- },
- computed: {
- ...mapGetters(['hasLogin'])
- },
- methods: {
- ...mapActions('user', ['customLogin']),
- // 初始化登录状态
- loginInit() {
- const userInfo = uni.getStorageSync('userInfo')
- if (userInfo) {
- this.hasProfile = true
- this.params.nickName = userInfo.nickName
- this.params.headImgUrl = userInfo.avatarUrl
- } else {
- this.hasProfile = false
- }
- },
- // 登录
- SubMitLogins() {
- if (!this.params.mobile) {
- this.$util.msg('请输入手机号', 2000)
- return
- }
- if (!this.$reg.isMobile(this.params.mobile)) {
- this.$util.msg('请输入正确的手机号', 2000)
- return
- }
- if (this.params.verificationCode == '') {
- this.$util.msg('请输入短信验证码', 2000)
- return
- }
- this.params.openId = uni.getStorageSync('openId')
- // 直接登录 / 授权再登录
- this.hasProfile ? this.LoginGetUser() : this.ProfileAndLogin()
- },
- // 获取用户信息
- GetUserProfile() {
- return new Promise((resolve, reject) => {
- wx.getUserProfile({
- desc: '呵呵商城小程序获取您的信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
- success: res => {
- this.params.nickName = res.userInfo.nickName
- this.params.headImgUrl = res.userInfo.avatarUrl
- this.hasProfile = true
- // 缓存用户信息
- uni.setStorageSync('userInfo', res.userInfo)
- resolve()
- },
- fail: () => {
- this.$util.msg('授权失败', 2000)
- this.hasProfile = false
- reject()
- }
- })
- })
- },
- // 授权并且登录
- ProfileAndLogin() {
- this.GetUserProfile().then(() => {
- this.LoginGetUser()
- })
- },
- // 登录
- async LoginGetUser() {
- try {
- this.isLogin = true
- await this.customLogin(this.params)
- this.isLogin = false
- } catch (e) {
- this.isLogin = false
- }
- },
- //获取手机验证码
- GetMobileCodeFn() {
- if (this.params.mobile == '') {
- this.$util.msg('请输入手机号', 2000)
- return
- }
- if (!this.$reg.isMobile(this.params.mobile)) {
- this.$util.msg('请输入正确的手机号', 2000)
- return
- }
- this.isMobileDisabled = true
- this.PublicService.GetheHeSend({
- mobile: this.params.mobile
- })
- .then(res => {
- const TIME_COUNT = 60
- this.$util.msg('验证短信已发送', 2000)
- if (!this.codeTime) {
- this.count = TIME_COUNT
- this.isMobileDisabled = true
- this.codeTime = setInterval(() => {
- if (this.count > 1 && this.count <= TIME_COUNT) {
- this.count--
- this.mobileCodeText = this.count + 's重新发送'
- } else {
- this.isMobileDisabled = false
- clearInterval(this.codeTime)
- this.codeTime = null
- this.mobileCodeText = '获取验证码'
- }
- }, 1000)
- }
- })
- .catch(error => {
- this.$util.msg(error.msg, 2000)
- this.isMobileDisabled = false
- })
- },
- handBlurInput(e) {
- console.log(e)
- if (e.detail.value) {
- this.isMobileDisabled = false
- } else {
- this.isMobileDisabled = true
- }
- }
- }
- }
- </script>
- <style lang="scss">
- .login {
- display: flex;
- align-items: center;
- flex-direction: column;
- .login-main,
- .login-title,
- .logo-message {
- width: 590rpx;
- }
- .login-main {
- display: flex;
- justify-content: center;
- margin: 180rpx 0 60rpx;
- .logo {
- width: 200rpx;
- height: 200rpx;
- display: block;
- }
- }
- .login-title {
- display: flex;
- justify-content: flex-start;
- image {
- width: 40rpx;
- height: 40rpx;
- margin-right: 6rpx;
- }
- }
- .login-btn {
- width: 600rpx;
- height: 90rpx;
- text-align: center;
- line-height: 90rpx;
- background: $btn-confirm;
- border-radius: 45rpx;
- color: #fff !important;
- margin-top: 40rpx;
- &.disabled {
- background: #e1e1e1;
- color: #999999;
- }
- }
- .login-input {
- margin: 10rpx 0;
- width: 600rpx;
- height: 90rpx;
- box-sizing: border-box;
- padding: 25rpx 0;
- border-bottom: 1px solid #e1e1e1;
- .input {
- width: 100%;
- height: 40rpx;
- left: 40rpx;
- font-size: $font-size-28;
- color: #333333;
- }
- .input_code {
- width: 420rpx;
- height: 40rpx;
- left: 40rpx;
- font-size: $font-size-28;
- color: #333333;
- float: left;
- }
- .code-btn {
- width: 180rpx;
- height: 40rpx;
- line-height: 40rpx;
- font-size: $font-size-28;
- color: #333333;
- float: right;
- text-align: center;
- .button {
- width: 180rpx;
- height: 40rpx;
- line-height: 40rpx;
- padding: 0;
- color: $color-system;
- }
- &.disabled {
- .button {
- color: #b2b2b2;
- }
- }
- }
- }
- .logo-message {
- font-size: 24rpx;
- line-height: 44rpx;
- color: #fc464c;
- text-align: left;
- }
- }
- </style>
|