security.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. import {
  2. passWord,
  3. emailCode,
  4. checkEmail,
  5. email,
  6. tellCode,
  7. tell,
  8. verifypaypwdcode,
  9. modifypaypassword,
  10. paypwdcode,
  11. pwdmobliecode
  12. } from "@/api/member/security"
  13. import {
  14. info
  15. } from "@/api/member/info"
  16. import {
  17. captcha
  18. } from "@/api/website"
  19. export default {
  20. name: "security",
  21. components: {},
  22. data() {
  23. var validatePass = (rule, value, callback) => {
  24. if (value === "") {
  25. callback(new Error("请输入新密码"))
  26. } else if (value == this.passWordForm.oldPass) {
  27. callback(new Error("新密码不能与原密码相同!"))
  28. } else {
  29. if (this.passWordForm.checkPass !== "") {
  30. this.$refs.passWordRef.validateField("checkPass")
  31. }
  32. callback()
  33. }
  34. }
  35. var validatePass2 = (rule, value, callback) => {
  36. if (value === "") {
  37. callback(new Error("请再次输入密码"))
  38. } else if (value !== this.passWordForm.pass) {
  39. callback(new Error("两次输入密码不一致!"))
  40. } else {
  41. callback()
  42. }
  43. }
  44. var validateTellPass = (rule, value, callback) => {
  45. if (value === "") {
  46. callback(new Error("请输入新密码"))
  47. } else if (value == this.tellPassForm.oldPass) {
  48. callback(new Error("新密码不能与原密码相同!"))
  49. } else {
  50. if (this.tellPassForm.checkPass !== "") {
  51. this.$refs.tellPassRef.validateField("checkPass")
  52. }
  53. callback()
  54. }
  55. }
  56. var validateTellPass2 = (rule, value, callback) => {
  57. if (value === "") {
  58. callback(new Error("请再次输入密码"))
  59. } else if (value !== this.tellPassForm.pass) {
  60. callback(new Error("两次输入密码不一致!"))
  61. } else {
  62. callback()
  63. }
  64. }
  65. var checkemail = (rules, value, callback) => {
  66. const regEmail = /^\w+@\w+(\.\w+)+$/
  67. if (regEmail.test(value)) {
  68. return callback()
  69. }
  70. callback(new Error("请输入正确的的邮箱"))
  71. }
  72. var checkTell = (rules, value, callback) => {
  73. const regMobile = /^1[3|4|5|6|7|8|9][0-9]{9}$/
  74. if (regMobile.test(value)) {
  75. return callback()
  76. }
  77. callback(new Error("请输入正确的手机号"))
  78. }
  79. return {
  80. type: "all",
  81. passWordForm: {
  82. oldPass: "",
  83. pass: "",
  84. checkPass: ""
  85. },
  86. emailForm: {
  87. email: "",
  88. code: "", //邮箱验证码
  89. emailDynacode: "", //邮箱动态验证码
  90. emailCodeText: "",
  91. key: "",
  92. currEmail: ""
  93. },
  94. passWordRules: {
  95. oldPass: [{
  96. required: true,
  97. message: "请输入原密码",
  98. trigger: "blur"
  99. }],
  100. pass: [{
  101. required: true,
  102. validator: validatePass,
  103. trigger: "blur"
  104. }],
  105. checkPass: [{
  106. required: true,
  107. validator: validatePass2,
  108. trigger: "blur"
  109. }]
  110. },
  111. emailRules: {
  112. email: [{
  113. required: true,
  114. message: "请输入正确的邮箱",
  115. trigger: "blur"
  116. },
  117. {
  118. validator: checkemail,
  119. trigger: "blur"
  120. }
  121. ],
  122. code: [{
  123. required: true,
  124. message: "请输入验证码",
  125. trigger: "blur"
  126. }],
  127. emailDynacode: [{
  128. required: true,
  129. message: "请输入动态验证码",
  130. trigger: "blur"
  131. }]
  132. },
  133. captcha: {
  134. id: "",
  135. img: ""
  136. },
  137. seconds: 120,
  138. timer: null,
  139. isSend: false,
  140. isMobileSend: false,
  141. tellForm: {
  142. tell: "",
  143. code: "", //邮箱验证码
  144. tellDynacode: "", //邮箱动态验证码
  145. tellCodeText: "",
  146. key: "",
  147. currTell: ""
  148. },
  149. tellRules: {
  150. tell: [{
  151. required: true,
  152. message: "请输入正确的手机号",
  153. trigger: "blur"
  154. },
  155. {
  156. validator: checkTell,
  157. trigger: "blur"
  158. }
  159. ],
  160. code: [{
  161. required: true,
  162. message: "请输入验证码",
  163. trigger: "blur"
  164. }],
  165. tellDynacode: [{
  166. required: true,
  167. message: "请输入动态验证码",
  168. trigger: "blur"
  169. }]
  170. },
  171. isClick: true,
  172. payCodeText: '获取验证码',
  173. step: 0,
  174. payCode: '', // 动态码
  175. payPassword: '', // 支付密码
  176. payRepassword: '', // 重复支付密码
  177. payKey: '', // 短信key
  178. payInput: '',
  179. palceText: '输入短信验证码',
  180. memberInfo: {},
  181. tellPassForm: {
  182. code: "",
  183. tellPassCodeText: "",
  184. key: "",
  185. tellPassDynacode: "",
  186. pass: '',
  187. checkPass: ''
  188. },
  189. tellPassRules: {
  190. code: [{
  191. required: true,
  192. message: "请输入验证码",
  193. trigger: "blur"
  194. }],
  195. tellPassDynacode: [{
  196. required: true,
  197. message: "请输入动态验证码",
  198. trigger: "blur"
  199. }],
  200. pass: [{
  201. required: true,
  202. validator: validateTellPass,
  203. trigger: "blur"
  204. }],
  205. checkPass: [{
  206. required: true,
  207. validator: validateTellPass2,
  208. trigger: "blur"
  209. }]
  210. },
  211. loading: true,
  212. yes: true
  213. }
  214. },
  215. created() {
  216. this.getcaptcha()
  217. this.seconds = 120
  218. this.tellForm.tellCodeText = "获取动态码"
  219. this.emailForm.emailCodeText = "获取动态码"
  220. this.tellPassForm.tellPassCodeText = "获取动态码"
  221. this.isSend = false
  222. this.isMobileSend = false
  223. clearInterval(this.timer)
  224. this.getInfo()
  225. },
  226. mounted() {
  227. let self = this;
  228. setTimeout(function() {
  229. self.yes = false
  230. }, 300)
  231. },
  232. methods: {
  233. //获取个人信息
  234. async getInfo() {
  235. await info()
  236. .then(res => {
  237. if (res.code == 0) {
  238. this.memberInfo = res.data
  239. this.emailForm.currEmail = res.data.email
  240. this.tellForm.currTell = res.data.mobile
  241. }
  242. this.loading = false
  243. })
  244. .catch(err => {
  245. this.loading = false
  246. this.$message.error(err.message)
  247. })
  248. },
  249. async edit(type) {
  250. await this.getInfo()
  251. if (type == 'payPassWord') {
  252. if (!this.tellForm.currTell) {
  253. this.$confirm("你还未绑定手机号,请先绑定手机号?", "提示信息", {
  254. confirmButtonText: "确定",
  255. cancelButtonText: "取消",
  256. type: "warning"
  257. }).then(res => {
  258. if (res = 'confirm') {
  259. this.type = 'tell'
  260. } else {
  261. this.type = 'all'
  262. }
  263. })
  264. } else {
  265. this.type = type
  266. }
  267. } else {
  268. this.type = type
  269. }
  270. },
  271. //获取验证码
  272. getcaptcha() {
  273. captcha({
  274. captcha_id: this.captcha.id
  275. })
  276. .then(res => {
  277. if (res.code >= 0) {
  278. this.captcha = res.data
  279. this.captcha.img = this.captcha.img.replace(/\r\n/g, "")
  280. }
  281. })
  282. .catch(err => {
  283. this.$message.error(err.message)
  284. })
  285. },
  286. //修改密码
  287. save() {
  288. this.$refs.passWordRef.validate(valid => {
  289. if (valid) {
  290. passWord({
  291. new_password: this.passWordForm.pass,
  292. old_password: this.passWordForm.oldPass
  293. })
  294. .then(res => {
  295. this.$message({
  296. message: "修改密码成功",
  297. type: "success"
  298. })
  299. this.type = "all"
  300. this.$store.dispatch("member/member_detail", {
  301. refresh: 1
  302. })
  303. this.passWordForm.pass = ""
  304. this.passWordForm.oldPass = ""
  305. this.passWordForm.checkPass = ""
  306. })
  307. .catch(err => {
  308. this.$message.error(err.message)
  309. })
  310. } else {
  311. return false
  312. }
  313. })
  314. },
  315. // 检测邮箱是否存在
  316. async getCheckEmail() {
  317. let result = await checkEmail({
  318. email: this.emailForm.email
  319. })
  320. .then(res => {
  321. if (res.code != 0) {
  322. this.$message({
  323. message: res.message,
  324. type: "success"
  325. })
  326. return false
  327. }
  328. return true
  329. })
  330. .catch(err => {
  331. this.$message.error(err.message)
  332. })
  333. return result
  334. },
  335. //绑定邮箱
  336. async bindEmail() {
  337. this.$refs.emailRef.validate(valid => {
  338. if (valid) {
  339. email({
  340. email: this.emailForm.email,
  341. captcha_id: this.captcha.id,
  342. captcha_code: this.emailForm.code,
  343. code: this.emailForm.emailDynacode,
  344. key: this.emailForm.key
  345. })
  346. .then(res => {
  347. if (res.code == 0) {
  348. this.$message({
  349. message: "邮箱绑定成功",
  350. type: "success"
  351. })
  352. this.type = "all"
  353. this.emailForm.email = ""
  354. this.emailForm.code = ""
  355. this.emailForm.emailDynacode = ""
  356. clearInterval(this.timer)
  357. this.getcaptcha()
  358. }
  359. })
  360. .catch(err => {
  361. this.getcaptcha()
  362. this.$message.error(err.message)
  363. })
  364. } else {
  365. return false
  366. }
  367. })
  368. },
  369. //获取手机验证码
  370. async gettellCode() {
  371. if (!this.isMobileSend) {
  372. this.isMobileSend = true
  373. await tellCode({
  374. mobile: this.tellForm.tell,
  375. captcha_id: this.captcha.id,
  376. captcha_code: this.tellForm.code
  377. })
  378. .then(res => {
  379. let data = res.data
  380. if (data.key) {
  381. if (this.seconds == 120 && this.timer == null) {
  382. this.timer = setInterval(() => {
  383. this.seconds--
  384. this.tellForm.tellCodeText = "已发送(" + this.seconds + "s)"
  385. }, 1000)
  386. }
  387. this.tellForm.key = data.key
  388. } else {
  389. this.$message({
  390. message: res.message,
  391. type: "warning"
  392. })
  393. this.isMobileSend = false
  394. }
  395. })
  396. .catch(err => {
  397. this.getcaptcha()
  398. this.$message.error(err.message)
  399. if(err.message == '当前手机号已存在'){
  400. this.isMobileSend = false
  401. }
  402. })
  403. } else {
  404. this.$message({
  405. message: "请勿重复点击",
  406. type: "warning"
  407. })
  408. }
  409. },
  410. //绑定手机号
  411. bindtell() {
  412. this.$refs.tellRef.validate(valid => {
  413. if (valid) {
  414. tell({
  415. mobile: this.tellForm.tell,
  416. captcha_id: this.captcha.id,
  417. captcha_code: this.tellForm.code,
  418. code: this.tellForm.tellDynacode,
  419. key: this.tellForm.key
  420. })
  421. .then(res => {
  422. if (res.code == 0) {
  423. this.$message({
  424. message: "手机号绑定成功",
  425. type: "success"
  426. })
  427. this.type = "all"
  428. this.tellForm.email = ""
  429. this.tellForm.code = ""
  430. this.tellForm.emailDynacode = ""
  431. clearInterval(this.timer)
  432. this.getcaptcha()
  433. }
  434. })
  435. .catch(err => {
  436. this.getcaptcha()
  437. this.$message.error(err.message)
  438. })
  439. } else {
  440. return false
  441. }
  442. })
  443. },
  444. //获取输入框数据
  445. input(val) {
  446. this.isClick = false
  447. if (this.step == 0 && val.length == 4) {
  448. this.payCode = val;
  449. } else if (this.step == 1 && val.length == 6) {
  450. this.payPassword = val;
  451. } else if (val.length == 6) {
  452. this.payRepassword = val;
  453. }
  454. },
  455. //获取支付验证码
  456. sendMobileCode() {
  457. if (!this.isSend) {
  458. paypwdcode().then(res => {
  459. let data = res.data;
  460. if (data.key) {
  461. if (this.seconds == 120 && this.timer == null) {
  462. this.timer = setInterval(() => {
  463. this.seconds--;
  464. this.payCodeText =
  465. "已发送(" + this.seconds + "s)";
  466. }, 1000);
  467. }
  468. this.payKey = data.key;
  469. } else {
  470. this.$message({
  471. message: res.message,
  472. type: 'warning'
  473. });
  474. this.isSend = false;
  475. }
  476. })
  477. .catch(err => {
  478. this.$message.error(err.message);
  479. });
  480. }
  481. },
  482. //点击确定
  483. bindPayPwd() {
  484. clearInterval(this.timer)
  485. const reg = /^[0-9]*$/
  486. if (this.step == 0) {
  487. verifypaypwdcode({
  488. code: this.payCode,
  489. key: this.payKey
  490. }).then(res => {
  491. if (res.code == 0) {
  492. this.$refs.input.clear();
  493. this.step = 1;
  494. this.palceText = '请设置支付密码'
  495. }
  496. }).catch(err => {
  497. this.$message.error(err.message);
  498. })
  499. } else if (this.step == 1) {
  500. if (reg.test(this.$refs.input.value)) {
  501. this.$refs.input.clear();
  502. this.step = 2;
  503. this.palceText = '请再次输入'
  504. } else {
  505. this.$message.error('请输入数字')
  506. this.step = 1;
  507. this.$refs.input.clear();
  508. }
  509. } else {
  510. if (this.payPassword == this.payRepassword) {
  511. if (this.isSub) return;
  512. this.isSub = true;
  513. modifypaypassword({
  514. key: this.payKey,
  515. code: this.payCode,
  516. password: this.payPassword
  517. }).then(res => {
  518. if (res.code >= 0) {
  519. this.$message({
  520. message: "修改支付密码成功",
  521. type: "success"
  522. })
  523. this.type = 'all'
  524. this.step = 0,
  525. this.$refs.input.clear();
  526. clearInterval(this.timer)
  527. }
  528. }).catch(err => {
  529. this.$message.error(err.message);
  530. })
  531. } else {
  532. this.$message.error('两次密码输入不一样');
  533. this.initInfo();
  534. }
  535. }
  536. },
  537. //初始化信息
  538. initInfo() {
  539. this.step = 1;
  540. this.palceText = '请设置支付密码'
  541. this.password = '';
  542. this.repassword = '';
  543. this.oldpassword = '';
  544. this.isSub = false;
  545. this.$refs.input.clear();
  546. },
  547. //获取动态码
  548. getTellPassCode() {
  549. if (!this.isSend) {
  550. this.isSend = true
  551. pwdmobliecode({
  552. captcha_id: this.captcha.id,
  553. captcha_code: this.tellPassForm.code,
  554. })
  555. .then(res => {
  556. let data = res.data
  557. if (data.key) {
  558. if (this.seconds == 120 && this.timer == null) {
  559. this.timer = setInterval(() => {
  560. this.seconds--
  561. this.tellPassForm.tellPassCodeText = "已发送(" + this.seconds + "s)"
  562. }, 1000)
  563. }
  564. this.tellPassForm.key = data.key
  565. } else {
  566. this.$message({
  567. message: res.message,
  568. type: "warning"
  569. })
  570. this.isSend = false
  571. }
  572. })
  573. .catch(err => {
  574. this.getcaptcha()
  575. this.$message.error(err.message)
  576. })
  577. } else {
  578. this.$message({
  579. message: "请勿重复点击",
  580. type: "warning"
  581. })
  582. }
  583. },
  584. //修改密码
  585. tellPassSave() {
  586. this.$refs.tellPassRef.validate(valid => {
  587. if (valid) {
  588. passWord({
  589. new_password: this.tellPassForm.pass,
  590. code: this.tellPassForm.tellPassDynacode,
  591. key: this.tellPassForm.key,
  592. })
  593. .then(res => {
  594. this.$message({
  595. message: "修改密码成功",
  596. type: "success"
  597. })
  598. this.type = "all"
  599. this.$store.dispatch("member/member_detail", {
  600. refresh: 1
  601. })
  602. this.tellPassForm.pass = ""
  603. this.tellPassForm.checkPass = ""
  604. this.tellPassForm.key = ""
  605. this.tellPassForm.tellPassDynacode = ""
  606. })
  607. .catch(err => {
  608. this.$message.error(err.message)
  609. })
  610. } else {
  611. return false
  612. }
  613. })
  614. },
  615. },
  616. filters: {
  617. mobile(mobile) {
  618. return mobile.substring(0, 4 - 1) + '****' + mobile.substring(6 + 1);
  619. }
  620. },
  621. }