<script>
var _this;
export default {
data() {
return {
phone: '', //用户/电话
password: '', //密码
code: '' //微信登陆的code
};
},
onLoad(params) {
let code = this.getCode('code');
if (code) {
this.showLoading('正在登陆');
this.code = code;
this.wxLoginByCode();
}
// if (params.hasOwnProperty('code')) {
// this.showLoading('正在登陆');
// this.code = params.code;
// this.wxLoginByCode();
// }
},
methods: {
back() {
this.navigateBack();
},
getCode(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
var r = window.location.search.substr(1).match(reg);
if (r != null) return decodeURIComponent(r[2]);
return null;
},
// 点击操作,过去微信授权登录
wxLogin() {
var params = window.location.href;
const backUrl = window.encodeURIComponent(params);
const url = `https://www.c3w.com.cn/authorize/baixiao/get_code?url=${backUrl}`;
window.location.href = `https://www.c3w.com.cn/authorize/baixiao/get_code?url=${backUrl}`;
},
// wxLoginByCode
async wxLoginByCode() {
const { data } = await this.loginHttp.reqWxLogin({ code: this.code });
if (data) {
this.setData('token', data.token);
if (this.getData('back_url')) {
let back_url = this.getData('back_url');
let origin = window.location.origin;
window.location.href = origin + back_url
} else {
this.switchTab('/pages/index/index');
}
}
},
async startLogin() {
const { phone, password } = this;
if (!this.testPhone()) {
return;
}
if (!password) {
return this.uniToast('密码不能为空');
}
wx.showLoading({
title: '正在登录'
});
const { data } = await this.loginHttp.reqLogin({
phone,
password
});
wx.hideLoading();
if (data) {
this.setData('token', data.token);
if (this.getData('back_url')) {
let back_url = this.getData('back_url');
let origin = window.location.origin;
window.location.href = origin + back_url
} else {
this.switchTab('/pages/index/index');
}
// const back_url = this.getData('back_url');
// const back_tab = this.getData('back_tab');
// if (back_url) {
// if (back_tab) {
// this.removeData('back_url');
// this.removeData('back_tab');
// this.switchTab(back_url);
// } else {
// this.removeData('back_url');
// this.redirectTo(back_url);
// }
// } else {
// this.switchTab('/pages/index/index');
// }
}
},
testPhone() {
const phone = this.phone;
if (!phone) {
this.uniToast('手机号不能为空');
return false;
}
if (!/^1[3456789]\d{9}$/.test(phone)) {
this.uniToast('手机号格式错误');
return false;
}
return true;
}
}
};
</script>