| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- import {
- mobileCode,
- registerConfig,
- wechatMobileCode
- } from "@/api/auth/login"
- import {
- adList,
- captcha
- } from "@/api/website"
- export default {
- data: () => {
- var isMobile = (rule, value, callback) => {
- if (!value) {
- return callback(new Error("手机号不能为空"))
- } else {
- if (/^\d{11}$/.test(value)) {
- callback()
- } else {
- callback(new Error("请输入正确的手机号"))
- }
- }
- }
- return {
- qrcodeData: {
- time: 0,
- timer: 0,
- },
- wx_key: '',
- expire_time: '',
- ischecked: false,
- ischecked1: false,
- activeName: "first", // tab切换
- formData: {
- account: "",
- password: "",
- vercode: "",
- mobile: "",
- dynacode: "",
- key: "",
- checked: false,
- autoLoginRange: 7
- }, // 表单数据
- captcha: {
- id: "",
- img: ""
- }, // 验证码
- dynacodeData: {
- seconds: 120,
- timer: null,
- codeText: "获取动态码",
- isSend: false
- }, // 动态码
- isSub: false, // 提交防重复
- registerConfig: {
- is_enable: 1,
- register: '',
- login: ''
- },
- accountRules: {
- account: [{
- required: true,
- message: "请输入登录账号",
- trigger: "blur"
- }],
- password: [{
- required: true,
- message: "请输入登录密码",
- trigger: "blur"
- }],
- vercode: [{
- required: true,
- message: "请输入验证码",
- trigger: "blur"
- }]
- },
- mobileRules: {
- mobile: [{
- required: true,
- validator: isMobile,
- trigger: "blur"
- }],
- vercode: [{
- required: true,
- message: "请输入验证码",
- trigger: "blur"
- }],
- dynacode: [{
- required: true,
- message: "请输入短信动态码",
- trigger: "blur"
- }]
- },
- wechatRules: {
- mobile: [{
- required: true,
- validator: isMobile,
- trigger: "blur"
- }],
- vercode: [{
- required: true,
- message: "请输入验证码",
- trigger: "blur"
- }],
- dynacode: [{
- required: true,
- message: "请输入短信动态码",
- trigger: "blur"
- }]
- },
- codeRules: {
- mobile: [{
- required: true,
- validator: isMobile,
- trigger: "blur"
- }],
- vercode: [{
- required: true,
- message: "请输入验证码",
- trigger: "blur"
- }]
- },
- loadingAd: true,
- adList: [],
- backgroundColor: '',
- img: '',
- third_party: 0
- }
- },
- created() {
- this.ischecked = this.$route.params.third_party;
- if (this.ischecked) {
- this.weixinLogin()
- }
- this.getAdList()
- this.getCaptcha()
- this.getRegisterConfig()
- },
- head() {
- return {
- title: '登录-' + this.$store.state.site.siteInfo.site_name
- }
- },
- watch: {
- "dynacodeData.seconds": {
- handler(newValue, oldValue) {
- if (newValue == 0) {
- clearInterval(this.dynacodeData.timer)
- this.dynacodeData = {
- seconds: 120,
- timer: null,
- codeText: "获取动态码",
- isSend: false
- }
- }
- },
- immediate: true,
- deep: true
- }
- },
- methods: {
- getAdList() {
- adList({
- keyword: "NS_PC_LOGIN"
- })
- .then(res => {
- if (res.code == 0 && res.data.adv_list) {
- this.adList = res.data.adv_list
- for (let i = 0; i < this.adList.length; i++) {
- if (this.adList[i].adv_url) this.adList[i].adv_url = JSON.parse(this.adList[i].adv_url)
- }
- this.backgroundColor = this.adList[0].background
- }
- this.loadingAd = false
- })
- .catch(err => {
- this.loadingAd = false
- })
- },
- handleClick(tab, event) {},
- handleChange(curr, pre) {
- this.backgroundColor = this.adList[curr].background
- },
- /**
- * 账号登录
- */
- accountLogin(formName) {
- this.$refs[formName].validate(valid => {
- if (valid) {
- var data = {
- username: this.formData.account,
- password: this.formData.password
- }
- if (this.captcha.id != "") {
- data.captcha_id = this.captcha.id
- data.captcha_code = this.formData.vercode
- }
- if (this.formData.checked) {
- data.autoLoginRange = this.formData.autoLoginRange
- }
- if (this.isSub) return
- this.isSub = true
- this.$store
- .dispatch("member/login", data)
- .then(res => {
- if (res.code >= 0) {
- this.$message({
- message: "登录成功!",
- type: "success"
- })
- if (this.$route.query.redirect) {
- const a = this.$route.query.redirect
- const b = this.$route.query
- this.$router.push(this.$route.query.redirect)
- } else {
- this.$router.push({
- name: "member"
- })
- }
- } else {
- this.isSub = false
- this.getCaptcha()
- this.$message({
- message: res.message,
- type: "warning"
- })
- }
- })
- .catch(err => {
- this.isSub = false
- this.$message.error(err.message)
- this.getCaptcha()
- })
- } else {
- return false
- }
- })
- },
- /**
- * 手机号登录
- */
- mobileLogin(formName) {
- this.$refs[formName].validate(valid => {
- if (valid) {
- var data = {
- mobile: this.formData.mobile,
- key: this.formData.key,
- code: this.formData.dynacode
- }
- if (this.captcha.id != "") {
- data.captcha_id = this.captcha.id
- data.captcha_code = this.formData.vercode
- }
- if (this.isSub) return
- this.isSub = true
- this.$store
- .dispatch("member/mobile_login", data)
- .then(res => {
- if (res.code >= 0) {
- this.$message({
- message: "登录成功!",
- type: "success"
- })
- if (this.$route.query.redirect) {
- this.$router.push(this.$route.query.redirect)
- } else {
- this.$router.push({
- name: "member"
- })
- }
- } else {
- this.isSub = false
- this.getCaptcha()
- this.$message({
- message: res.message,
- type: "warning"
- })
- }
- })
- .catch(err => {
- this.isSub = false
- this.$message.error(err.message)
- this.getCaptcha()
- })
- } else {
- return false
- }
- })
- },
- /**
- * 微信登录
- */
- wechatLogin(formName) {
- this.$refs[formName].validate(valid => {
- if (valid) {
- var data = {
- mobile: this.formData.mobile,
- key: this.formData.key,
- code: this.formData.dynacode
- }
- if (this.captcha.id != "") {
- data.captcha_id = this.captcha.id
- data.captcha_code = this.formData.vercode
- }
- if (this.isSub) return
- this.isSub = true
- this.$store
- .dispatch("wechat/wechatLogin", data)
- .then(res => {
- if (res.code >= 0) {
- this.$message({
- message: "登录成功!",
- type: "success"
- })
- if (this.$route.query.redirect) {
- this.$router.push(this.$route.query.redirect)
- } else {
- this.$router.push({
- name: "member"
- })
- }
- } else {
- this.isSub = false
- this.getCaptcha()
- this.$message({
- message: res.message,
- type: "warning"
- })
- }
- })
- .catch(err => {
- this.isSub = false
- this.$message.error(err.message)
- this.getCaptcha()
- })
- } else {
- return false
- }
- })
- },
- weixinLogin() {
- this.ischecked = true;
- this.$store
- .dispatch("wechat/logincode")
- .then(res => {
- if (res.code >= 0) {
- this.img = res.data.qrcode;
- this.wx_key = res.data.key;
- this.expire_time = res.data.expire_time;
- this.qrcodeData.timer = setInterval(() => {
- this.checkLogin()
- }, 2000);
- }
- })
- },
- // 检测是否扫码
- checkLogin() {
- this.qrcodeData.time += 2;
- if (this.qrcodeData.time > this.expire_time) {
- clearInterval(this.qrcodeData.timer);
- return;
- };
- var data = {
- key: this.wx_key
- };
- this.$store
- .dispatch("wechat/checklogin", data)
- .then(res => {
- if (res.code >= 0) {
- if (res.data.token != undefined) {
- this.$message({
- message: "登录成功!",
- type: "success"
- })
- if (this.$route.query.redirect) {
- this.$router.push(this.$route.query.redirect)
- } else {
- this.$router.push({
- name: "member"
- })
- }
- } else {
- this.ischecked1 = true;
- }
- clearInterval(this.qrcodeData.timer);
- }
- })
- .catch(err => {
- console.log(err.message)
- })
- },
- closeWx() {
- this.ischecked = false;
- },
- closeWx1() {
- this.ischecked = false;
- this.ischecked1 = false;
- },
- /**
- * 获取注册配置
- */
- getRegisterConfig() {
- registerConfig()
- .then(res => {
- if (res.code >= 0) {
- this.registerConfig = res.data.value
- if (this.registerConfig.login.indexOf('username') != -1) {
- this.activeName = 'first';
- } else {
- this.activeName = 'second';
- }
- }
- })
- .catch(err => {
- console.log(err.message)
- })
- },
- /**
- * 获取验证码
- */
- getCaptcha() {
- captcha({
- captcha_id: this.captcha.id
- })
- .then(res => {
- if (res.code >= 0) {
- this.captcha.id = res.data.id
- this.captcha.img = res.data.img
- this.captcha.img = this.captcha.img.replace(/\r\n/g, "")
- }
- })
- .catch(err => {
- this.$message.error(err.message)
- })
- },
- /**
- * 发送手机动态码
- */
- sendMobileCode(formName) {
- if (this.dynacodeData.seconds != 120) return
- this.$refs[formName].clearValidate("dynacode")
- this.$refs[formName].validateField("mobile", valid => {
- if (valid) {
- return false
- }
- })
- this.$refs[formName].validateField("vercode", valid => {
- if (!valid) {
- mobileCode({
- mobile: this.formData.mobile,
- captcha_id: this.captcha.id,
- captcha_code: this.formData.vercode
- })
- .then(res => {
- if (res.code >= 0) {
- this.formData.key = res.data.key
- if (this.dynacodeData.seconds == 120 && this.dynacodeData.timer ==
- null) {
- this.dynacodeData.timer = setInterval(() => {
- this.dynacodeData.seconds--
- this.dynacodeData.codeText = this.dynacodeData.seconds +
- "s后可重新获取"
- }, 1000)
- }
- }
- })
- .catch(err => {
- this.$message.error(err.message)
- })
- } else {
- return false
- }
- })
- },
- /**
- * 发送微信绑定手机动态码
- */
- sendWechatMobileCode(formName) {
- if (this.dynacodeData.seconds != 120) return
- this.$refs[formName].clearValidate("dynacode")
- this.$refs[formName].validateField("mobile", valid => {
- if (valid) {
- return false
- }
- })
- this.$refs[formName].validateField("vercode", valid => {
- if (!valid) {
- wechatMobileCode({
- mobile: this.formData.mobile,
- captcha_id: this.captcha.id,
- captcha_code: this.formData.vercode
- })
- .then(res => {
- if (res.code >= 0) {
- this.formData.key = res.data.key
- if (this.dynacodeData.seconds == 120 && this.dynacodeData.timer ==
- null) {
- this.dynacodeData.timer = setInterval(() => {
- this.dynacodeData.seconds--
- this.dynacodeData.codeText = this.dynacodeData.seconds +
- "s后可重新获取"
- }, 1000)
- }
- }
- })
- .catch(err => {
- this.$message.error(err.message)
- })
- } else {
- return false
- }
- })
- },
- keypress(e) {
- let that = this;
- var keycode = e.all ? e.keyCode : e.which;
- if (keycode == 13) {
- if (that.activeName == "first") {
- that.accountLogin('ruleForm'); // 登录方法名
- } else {
- that.mobileLogin('mobileRuleForm'); // 登录方法名
- }
- return false;
- }
- }
- }
- }
|