login.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. import {
  2. mobileCode,
  3. registerConfig,
  4. wechatMobileCode
  5. } from "@/api/auth/login"
  6. import {
  7. adList,
  8. captcha
  9. } from "@/api/website"
  10. export default {
  11. data: () => {
  12. var isMobile = (rule, value, callback) => {
  13. if (!value) {
  14. return callback(new Error("手机号不能为空"))
  15. } else {
  16. if (/^\d{11}$/.test(value)) {
  17. callback()
  18. } else {
  19. callback(new Error("请输入正确的手机号"))
  20. }
  21. }
  22. }
  23. return {
  24. qrcodeData: {
  25. time: 0,
  26. timer: 0,
  27. },
  28. wx_key: '',
  29. expire_time: '',
  30. ischecked: false,
  31. ischecked1: false,
  32. activeName: "first", // tab切换
  33. formData: {
  34. account: "",
  35. password: "",
  36. vercode: "",
  37. mobile: "",
  38. dynacode: "",
  39. key: "",
  40. checked: false,
  41. autoLoginRange: 7
  42. }, // 表单数据
  43. captcha: {
  44. id: "",
  45. img: ""
  46. }, // 验证码
  47. dynacodeData: {
  48. seconds: 120,
  49. timer: null,
  50. codeText: "获取动态码",
  51. isSend: false
  52. }, // 动态码
  53. isSub: false, // 提交防重复
  54. registerConfig: {
  55. is_enable: 1,
  56. register: '',
  57. login: ''
  58. },
  59. accountRules: {
  60. account: [{
  61. required: true,
  62. message: "请输入登录账号",
  63. trigger: "blur"
  64. }],
  65. password: [{
  66. required: true,
  67. message: "请输入登录密码",
  68. trigger: "blur"
  69. }],
  70. vercode: [{
  71. required: true,
  72. message: "请输入验证码",
  73. trigger: "blur"
  74. }]
  75. },
  76. mobileRules: {
  77. mobile: [{
  78. required: true,
  79. validator: isMobile,
  80. trigger: "blur"
  81. }],
  82. vercode: [{
  83. required: true,
  84. message: "请输入验证码",
  85. trigger: "blur"
  86. }],
  87. dynacode: [{
  88. required: true,
  89. message: "请输入短信动态码",
  90. trigger: "blur"
  91. }]
  92. },
  93. wechatRules: {
  94. mobile: [{
  95. required: true,
  96. validator: isMobile,
  97. trigger: "blur"
  98. }],
  99. vercode: [{
  100. required: true,
  101. message: "请输入验证码",
  102. trigger: "blur"
  103. }],
  104. dynacode: [{
  105. required: true,
  106. message: "请输入短信动态码",
  107. trigger: "blur"
  108. }]
  109. },
  110. codeRules: {
  111. mobile: [{
  112. required: true,
  113. validator: isMobile,
  114. trigger: "blur"
  115. }],
  116. vercode: [{
  117. required: true,
  118. message: "请输入验证码",
  119. trigger: "blur"
  120. }]
  121. },
  122. loadingAd: true,
  123. adList: [],
  124. backgroundColor: '',
  125. img: '',
  126. third_party: 0
  127. }
  128. },
  129. created() {
  130. this.ischecked = this.$route.params.third_party;
  131. if (this.ischecked) {
  132. this.weixinLogin()
  133. }
  134. this.getAdList()
  135. this.getCaptcha()
  136. this.getRegisterConfig()
  137. },
  138. head() {
  139. return {
  140. title: '登录-' + this.$store.state.site.siteInfo.site_name
  141. }
  142. },
  143. watch: {
  144. "dynacodeData.seconds": {
  145. handler(newValue, oldValue) {
  146. if (newValue == 0) {
  147. clearInterval(this.dynacodeData.timer)
  148. this.dynacodeData = {
  149. seconds: 120,
  150. timer: null,
  151. codeText: "获取动态码",
  152. isSend: false
  153. }
  154. }
  155. },
  156. immediate: true,
  157. deep: true
  158. }
  159. },
  160. methods: {
  161. getAdList() {
  162. adList({
  163. keyword: "NS_PC_LOGIN"
  164. })
  165. .then(res => {
  166. if (res.code == 0 && res.data.adv_list) {
  167. this.adList = res.data.adv_list
  168. for (let i = 0; i < this.adList.length; i++) {
  169. if (this.adList[i].adv_url) this.adList[i].adv_url = JSON.parse(this.adList[i].adv_url)
  170. }
  171. this.backgroundColor = this.adList[0].background
  172. }
  173. this.loadingAd = false
  174. })
  175. .catch(err => {
  176. this.loadingAd = false
  177. })
  178. },
  179. handleClick(tab, event) {},
  180. handleChange(curr, pre) {
  181. this.backgroundColor = this.adList[curr].background
  182. },
  183. /**
  184. * 账号登录
  185. */
  186. accountLogin(formName) {
  187. this.$refs[formName].validate(valid => {
  188. if (valid) {
  189. var data = {
  190. username: this.formData.account,
  191. password: this.formData.password
  192. }
  193. if (this.captcha.id != "") {
  194. data.captcha_id = this.captcha.id
  195. data.captcha_code = this.formData.vercode
  196. }
  197. if (this.formData.checked) {
  198. data.autoLoginRange = this.formData.autoLoginRange
  199. }
  200. if (this.isSub) return
  201. this.isSub = true
  202. this.$store
  203. .dispatch("member/login", data)
  204. .then(res => {
  205. if (res.code >= 0) {
  206. this.$message({
  207. message: "登录成功!",
  208. type: "success"
  209. })
  210. if (this.$route.query.redirect) {
  211. const a = this.$route.query.redirect
  212. const b = this.$route.query
  213. this.$router.push(this.$route.query.redirect)
  214. } else {
  215. this.$router.push({
  216. name: "member"
  217. })
  218. }
  219. } else {
  220. this.isSub = false
  221. this.getCaptcha()
  222. this.$message({
  223. message: res.message,
  224. type: "warning"
  225. })
  226. }
  227. })
  228. .catch(err => {
  229. this.isSub = false
  230. this.$message.error(err.message)
  231. this.getCaptcha()
  232. })
  233. } else {
  234. return false
  235. }
  236. })
  237. },
  238. /**
  239. * 手机号登录
  240. */
  241. mobileLogin(formName) {
  242. this.$refs[formName].validate(valid => {
  243. if (valid) {
  244. var data = {
  245. mobile: this.formData.mobile,
  246. key: this.formData.key,
  247. code: this.formData.dynacode
  248. }
  249. if (this.captcha.id != "") {
  250. data.captcha_id = this.captcha.id
  251. data.captcha_code = this.formData.vercode
  252. }
  253. if (this.isSub) return
  254. this.isSub = true
  255. this.$store
  256. .dispatch("member/mobile_login", data)
  257. .then(res => {
  258. if (res.code >= 0) {
  259. this.$message({
  260. message: "登录成功!",
  261. type: "success"
  262. })
  263. if (this.$route.query.redirect) {
  264. this.$router.push(this.$route.query.redirect)
  265. } else {
  266. this.$router.push({
  267. name: "member"
  268. })
  269. }
  270. } else {
  271. this.isSub = false
  272. this.getCaptcha()
  273. this.$message({
  274. message: res.message,
  275. type: "warning"
  276. })
  277. }
  278. })
  279. .catch(err => {
  280. this.isSub = false
  281. this.$message.error(err.message)
  282. this.getCaptcha()
  283. })
  284. } else {
  285. return false
  286. }
  287. })
  288. },
  289. /**
  290. * 微信登录
  291. */
  292. wechatLogin(formName) {
  293. this.$refs[formName].validate(valid => {
  294. if (valid) {
  295. var data = {
  296. mobile: this.formData.mobile,
  297. key: this.formData.key,
  298. code: this.formData.dynacode
  299. }
  300. if (this.captcha.id != "") {
  301. data.captcha_id = this.captcha.id
  302. data.captcha_code = this.formData.vercode
  303. }
  304. if (this.isSub) return
  305. this.isSub = true
  306. this.$store
  307. .dispatch("wechat/wechatLogin", data)
  308. .then(res => {
  309. if (res.code >= 0) {
  310. this.$message({
  311. message: "登录成功!",
  312. type: "success"
  313. })
  314. if (this.$route.query.redirect) {
  315. this.$router.push(this.$route.query.redirect)
  316. } else {
  317. this.$router.push({
  318. name: "member"
  319. })
  320. }
  321. } else {
  322. this.isSub = false
  323. this.getCaptcha()
  324. this.$message({
  325. message: res.message,
  326. type: "warning"
  327. })
  328. }
  329. })
  330. .catch(err => {
  331. this.isSub = false
  332. this.$message.error(err.message)
  333. this.getCaptcha()
  334. })
  335. } else {
  336. return false
  337. }
  338. })
  339. },
  340. weixinLogin() {
  341. this.ischecked = true;
  342. this.$store
  343. .dispatch("wechat/logincode")
  344. .then(res => {
  345. if (res.code >= 0) {
  346. this.img = res.data.qrcode;
  347. this.wx_key = res.data.key;
  348. this.expire_time = res.data.expire_time;
  349. this.qrcodeData.timer = setInterval(() => {
  350. this.checkLogin()
  351. }, 2000);
  352. }
  353. })
  354. },
  355. // 检测是否扫码
  356. checkLogin() {
  357. this.qrcodeData.time += 2;
  358. if (this.qrcodeData.time > this.expire_time) {
  359. clearInterval(this.qrcodeData.timer);
  360. return;
  361. };
  362. var data = {
  363. key: this.wx_key
  364. };
  365. this.$store
  366. .dispatch("wechat/checklogin", data)
  367. .then(res => {
  368. if (res.code >= 0) {
  369. if (res.data.token != undefined) {
  370. this.$message({
  371. message: "登录成功!",
  372. type: "success"
  373. })
  374. if (this.$route.query.redirect) {
  375. this.$router.push(this.$route.query.redirect)
  376. } else {
  377. this.$router.push({
  378. name: "member"
  379. })
  380. }
  381. } else {
  382. this.ischecked1 = true;
  383. }
  384. clearInterval(this.qrcodeData.timer);
  385. }
  386. })
  387. .catch(err => {
  388. console.log(err.message)
  389. })
  390. },
  391. closeWx() {
  392. this.ischecked = false;
  393. },
  394. closeWx1() {
  395. this.ischecked = false;
  396. this.ischecked1 = false;
  397. },
  398. /**
  399. * 获取注册配置
  400. */
  401. getRegisterConfig() {
  402. registerConfig()
  403. .then(res => {
  404. if (res.code >= 0) {
  405. this.registerConfig = res.data.value
  406. if (this.registerConfig.login.indexOf('username') != -1) {
  407. this.activeName = 'first';
  408. } else {
  409. this.activeName = 'second';
  410. }
  411. }
  412. })
  413. .catch(err => {
  414. console.log(err.message)
  415. })
  416. },
  417. /**
  418. * 获取验证码
  419. */
  420. getCaptcha() {
  421. captcha({
  422. captcha_id: this.captcha.id
  423. })
  424. .then(res => {
  425. if (res.code >= 0) {
  426. this.captcha.id = res.data.id
  427. this.captcha.img = res.data.img
  428. this.captcha.img = this.captcha.img.replace(/\r\n/g, "")
  429. }
  430. })
  431. .catch(err => {
  432. this.$message.error(err.message)
  433. })
  434. },
  435. /**
  436. * 发送手机动态码
  437. */
  438. sendMobileCode(formName) {
  439. if (this.dynacodeData.seconds != 120) return
  440. this.$refs[formName].clearValidate("dynacode")
  441. this.$refs[formName].validateField("mobile", valid => {
  442. if (valid) {
  443. return false
  444. }
  445. })
  446. this.$refs[formName].validateField("vercode", valid => {
  447. if (!valid) {
  448. mobileCode({
  449. mobile: this.formData.mobile,
  450. captcha_id: this.captcha.id,
  451. captcha_code: this.formData.vercode
  452. })
  453. .then(res => {
  454. if (res.code >= 0) {
  455. this.formData.key = res.data.key
  456. if (this.dynacodeData.seconds == 120 && this.dynacodeData.timer ==
  457. null) {
  458. this.dynacodeData.timer = setInterval(() => {
  459. this.dynacodeData.seconds--
  460. this.dynacodeData.codeText = this.dynacodeData.seconds +
  461. "s后可重新获取"
  462. }, 1000)
  463. }
  464. }
  465. })
  466. .catch(err => {
  467. this.$message.error(err.message)
  468. })
  469. } else {
  470. return false
  471. }
  472. })
  473. },
  474. /**
  475. * 发送微信绑定手机动态码
  476. */
  477. sendWechatMobileCode(formName) {
  478. if (this.dynacodeData.seconds != 120) return
  479. this.$refs[formName].clearValidate("dynacode")
  480. this.$refs[formName].validateField("mobile", valid => {
  481. if (valid) {
  482. return false
  483. }
  484. })
  485. this.$refs[formName].validateField("vercode", valid => {
  486. if (!valid) {
  487. wechatMobileCode({
  488. mobile: this.formData.mobile,
  489. captcha_id: this.captcha.id,
  490. captcha_code: this.formData.vercode
  491. })
  492. .then(res => {
  493. if (res.code >= 0) {
  494. this.formData.key = res.data.key
  495. if (this.dynacodeData.seconds == 120 && this.dynacodeData.timer ==
  496. null) {
  497. this.dynacodeData.timer = setInterval(() => {
  498. this.dynacodeData.seconds--
  499. this.dynacodeData.codeText = this.dynacodeData.seconds +
  500. "s后可重新获取"
  501. }, 1000)
  502. }
  503. }
  504. })
  505. .catch(err => {
  506. this.$message.error(err.message)
  507. })
  508. } else {
  509. return false
  510. }
  511. })
  512. },
  513. keypress(e) {
  514. let that = this;
  515. var keycode = e.all ? e.keyCode : e.which;
  516. if (keycode == 13) {
  517. if (that.activeName == "first") {
  518. that.accountLogin('ruleForm'); // 登录方法名
  519. } else {
  520. that.mobileLogin('mobileRuleForm'); // 登录方法名
  521. }
  522. return false;
  523. }
  524. }
  525. }
  526. }