123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <template>
- <view class="wrap">
- <view class="content">
- <view class="title">手机号登录</view>
- <input class="u-border-bottom" type="number" maxlength="11" v-model="tel" placeholder="请输入手机号" />
- <view class="u-text-center u-type-error u-m-t-20" v-if="telError">手机号输入错误</view>
- <button @tap="submit" :style="[inputStyle]" class="getCaptcha">获取短信验证码</button>
- <u-message-input v-if="show" :focus="true" :value="messageCode" @change="change" @finish="finish" mode="bottomLine" :maxlength="codelength"></u-message-input>
- </view>
- <!-- <view class="u-text-center u-type-error" v-if="phoneError">手机号输入错误</view> -->
- <view class="captcha">
- <!-- <text v-if="show&&this.messageDisable==false" @tap="noCaptcha">收不到验证码点这里</text> -->
- <text v-if="messageShow">{{ second }}秒后可重新获取验证码</text>
- </view>
- <view class="buttom">
- <view class="hint u-text-center">
- 登录代表同意
- <text class="link">隐私政策,</text>
- 并授权使用您的账号信息(如昵称、头像、收货地址)以便您统一管理
- </view>
- </view>
- <u-toast ref="uToast" />
- </view>
- </template>
- <script>
- import getUrlParams from "./../../../utils/getUrlParams.js";
- import { config } from '@/common/config.js';
- export default {
- data() {
- return {
- tel: '',
- messageCode:'',
- messageShow: false,
- messageDisable: false,
- codelength: 4,
- show: false,
- second:60,
- toastMsg:'',
- toastUrl:'',
- toastType:'',
- accessToken:'',
- userId:'',
- telError:false,
- // messageError:false
- }
- },
- onLoad() {
-
- },
- computed: {
- inputStyle() {
- let style = {};
- if(this.tel.length==11&&this.messageDisable==false&&this.$u.test.mobile(this.tel)) {
- style.color = "#fff";
- style.backgroundColor = '#5295F5';
- this.telError = false;
- // style.backgroundColor = this.$u.color['warning'];
- }else if(this.tel.length==11&&!this.$u.test.mobile(this.tel)){
- this.telError = true;
- }
- return style;
- }
- },
- methods: {
- showToast() {
- this.$refs.uToast.show({
- title: this.toastMsg,
- type: this.toastType,
- url: this.toastUrl
- })
- },
- submit() {
- if(this.$u.test.mobile(this.tel)&&this.messageDisable==false) {
- let that = this;
- this.$u.api.getPhoneLoginCode({mobile:this.tel})
- .then(res =>{
- this.messageDisable = true;
- this.messageShow = true;
- this.show = true;
- let interval = setInterval(() => {
- that.second--;
- if (that.second <= 0) {
- that.messageDisable=false
- that.messageShow = false;
- if (that.messageCode.lenth != 4) {
- // this.messageError = true;
- }
- clearInterval(interval);
- that.second=60;
- }
- }, 1000);
- console.log('getcode res',res);
- this.accessToken = res.data.accessToken;
- this.$u.vuex('vuex_token', res.data.accessToken);
- this.userId = res.data.userId;
- }).catch(err=>{
- console.log('err',err)
- this.toastMsg = err.code + ":" + err.msg;
- this.showToast();
- console.log('getcode err',err)
- });
- }
- },
- // 收不到验证码选择时的选择
- // noCaptcha() {
- // uni.showActionSheet({
- // itemList: ['重新获取验证码', '接听语音验证码'],
- // success: function(res) {
-
- // },
- // fail: function(res) {
-
- // }
- // });
- // },
- // change事件侦听
- change(value) {
- // console.log('change', value);
- },
- // 输入完验证码最后一位执行
- finish(value) {
- let params = {
- accessToken:this.accessToken,
- userId:this.userId,
- code:value
- };
- this.$u.api.phoneLoginAuth(params)
- .then(res =>{
- if(res.code=='200'){
- // console.log('finish res',res);
- this.$u.vuex('vuex_user', res.data);
- this.$u.vuex('vuex_hasLogin', true);
- this.wechatLogin()
- // location.href = backUrl;
- // this.$refs.uToast.show({
- // title: '操作成功',
- // url: backUrl,
- // // isTab:true,
- // callback :function(err){
- // console.log('err',err)
- // }
- // });
-
- // this.$u.api.getuseinfo().then(res => {
- // console.log('getuseinfo',res.retBody);
- // this.$u.vuex('vuex_user', res.retBody);
- // this.toastMsg = '操作成功';
- // this.showToast();
- // setTimeout(() => {
- // this.$u.route({
- // url: 'pages/template/wxCenter/index'
- // })
- // }, 2000);
- // });
-
- }else{
- this.toastMsg = res.msg;
- this.showToast();
- }
-
- }).catch(err=>{
- console.log('finish err',err);
- this.toastMsg = err.msg;
- this.showToast();
- });
- },
- // 微信登录
- wechatLogin() {
- const openId = this.$store.state.vuex_wxinfo.openId
- if (openId) {
- this.jumpIndex()
- } else {
- this.getCode()
- }
- },
- // 微信已登录则跳转到首页
- jumpIndex() {
- let storage = window.sessionStorage;
- let backUrl = storage.getItem('backUrl');
- if (backUrl) {
- location.href = backUrl;
- } else {
- location.href = config.jumpHomeUrl
- }
- },
- // 获取code
- getCode () {
- var local = window.location.href // 获取页面url
- let locationLocaturl = window.location.search;
- this.code = getUrlParams(locationLocaturl,"code"); // 截取code
- if (this.code == null || this.code === '') { // 如果没有code,则去请求
- window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.config.wxAppid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&#wechat_redirect`
- } else {
- this.handleGetWXInfo(this.code) //把code传给后台获取用户信息
- }
- },
- // 通过code获取 openId等用户信息,/api/user/wechat/login 为后台接口
- handleGetWXInfo (code) {
- this.$u.api.getWXInfo(code).then((res) => {
- if (res.code === 200 ) {
- this.$u.vuex('vuex_wxinfo', res.data);
- this.jumpIndex()
- }
- }).catch((err) => {
- this.$refs.uToast.show({
- title: err.msg,
- type: 'error',
- });
- })
- },
- }
- };
- </script>
- <style lang="scss" scoped>
- .hide{display: none!important;}
- .wrap {
- font-size: 28rpx;
- .content {
- width: 600rpx;
- margin: 80rpx auto 0;
- .title {
- text-align: left;
- font-size: 60rpx;
- font-weight: 500;
- margin-bottom: 100rpx;
- }
- input {
- text-align: left;
- margin-bottom: 10rpx;
- padding-bottom: 20rpx;
- border-bottom: 1px solid #ddd;
- }
- .getCaptcha {
- margin: 45rpx auto 130rpx;
- background-color: #a8c6f1;
- color: $u-tips-color;
- border: none;
- font-size: 30rpx;
- padding: 12rpx 0;
-
- &::after {
- border: none;
- }
- }
- }
- .buttom {
- .hint {
- padding: 20rpx 40rpx;
- font-size: 20rpx;
- color: $u-tips-color;
-
- .link {
- color: $u-type-warning;
- }
- }
- }
- .captcha {
- color: $u-type-warning;
- font-size: 30rpx;
- margin-top: 40rpx;
- text-align: center;
- }
- }
- </style>
|