find.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import {
  2. rePass,
  3. nextStep,
  4. smsCode,
  5. registerConfig
  6. } from "@/api/auth/login"
  7. import {
  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. let self = this
  24. var setPass = function(rule, value, callback) {
  25. let regConfig = self.registerConfig
  26. if (!value) {
  27. return callback(new Error("请输入新的登录密码"))
  28. } else {
  29. if (regConfig.pwd_len > 0) {
  30. if (value.length < regConfig.pwd_len) {
  31. return callback(new Error("密码长度不能小于" + regConfig.pwd_len + "位"))
  32. } else {
  33. callback()
  34. }
  35. } else {
  36. if (regConfig.pwd_complexity != "") {
  37. let passwordErrorMsg = "密码需包含",
  38. reg = ""
  39. if (regConfig.pwd_complexity.indexOf("number") != -1) {
  40. reg += "(?=.*?[0-9])"
  41. passwordErrorMsg += "数字"
  42. } else if (regConfig.pwd_complexity.indexOf("letter") != -1) {
  43. reg += "(?=.*?[a-z])"
  44. passwordErrorMsg += "、小写字母"
  45. } else if (regConfig.pwd_complexity.indexOf("upper_case") != -1) {
  46. reg += "(?=.*?[A-Z])"
  47. passwordErrorMsg += "、大写字母"
  48. } else if (regConfig.pwd_complexity.indexOf("symbol") != -1) {
  49. reg += "(?=.*?[#?!@$%^&*-])"
  50. passwordErrorMsg += "、特殊字符"
  51. } else {
  52. reg += ""
  53. passwordErrorMsg += ""
  54. }
  55. if (reg.test(value)) {
  56. return callback(new Error(passwordErrorMsg))
  57. } else {
  58. callback()
  59. }
  60. }
  61. }
  62. }
  63. }
  64. var checkPass = function(rule, value, callback) {
  65. if (!value) {
  66. return callback(new Error("请输入确认密码"))
  67. } else {
  68. if (value !== self.formData.pass) {
  69. callback(new Error("两次密码输入不一致"))
  70. } else {
  71. callback()
  72. }
  73. }
  74. }
  75. return {
  76. formData: {
  77. mobile: "",
  78. vercode: "",
  79. dynacode: "",
  80. pass: "",
  81. repass: "",
  82. key: ""
  83. },
  84. step: 1,
  85. activeName: "first",
  86. isShowPhone: "",
  87. captcha: {
  88. id: "",
  89. img: ""
  90. }, // 验证码
  91. dynacodeData: {
  92. seconds: 120,
  93. timer: null,
  94. codeText: "获取动态码",
  95. isSend: false
  96. }, // 动态码
  97. registerConfig: {},
  98. rules: {
  99. mobile: [{
  100. required: true,
  101. validator: isMobile,
  102. trigger: "blur"
  103. }],
  104. vercode: [{
  105. required: true,
  106. message: "请输入验证码",
  107. trigger: "blur"
  108. }],
  109. dynacode: [{
  110. required: true,
  111. message: "请输入短信动态码",
  112. trigger: "blur"
  113. }],
  114. pass: [{
  115. required: true,
  116. validator: setPass,
  117. trigger: "blur"
  118. }],
  119. repass: [{
  120. required: true,
  121. validator: checkPass,
  122. trigger: "blur"
  123. }]
  124. }
  125. }
  126. },
  127. created() {
  128. this.getCaptcha()
  129. this.getRegisterConfig()
  130. },
  131. head() {
  132. return {
  133. title: '找回密码-' +this.$store.state.site.siteInfo.site_name
  134. }
  135. },
  136. watch: {
  137. "dynacodeData.seconds": {
  138. handler(newValue, oldValue) {
  139. if (newValue == 0) {
  140. clearInterval(this.dynacodeData.timer)
  141. this.dynacodeData = {
  142. seconds: 120,
  143. timer: null,
  144. codeText: "获取动态码",
  145. isSend: false
  146. }
  147. }
  148. },
  149. immediate: true,
  150. deep: true
  151. }
  152. },
  153. methods: {
  154. /**
  155. * 下一步
  156. */
  157. nextStep(formName) {
  158. this.$refs[formName].validate(valid => {
  159. if (valid) {
  160. nextStep({
  161. mobile: this.formData.mobile
  162. })
  163. .then(res => {
  164. if (res.code == -1) {
  165. this.step = 2
  166. } else {
  167. this.$message({
  168. message: res.message,
  169. type: "warning"
  170. })
  171. }
  172. })
  173. .catch(err => {
  174. if (err.code == 0) {
  175. this.$message({
  176. message: "该手机号未注册",
  177. type: "warning"
  178. })
  179. } else {
  180. this.$message.error(err.message)
  181. }
  182. })
  183. } else {
  184. return false
  185. }
  186. })
  187. },
  188. /**
  189. * 获取动态验证码 下一步
  190. */
  191. nextStepToSetPass(formName) {
  192. this.$refs[formName].validate(valid => {
  193. if (valid) {
  194. this.step = 3
  195. } else {
  196. return false
  197. }
  198. })
  199. },
  200. /**
  201. * 重置密码
  202. */
  203. resetPass(formName) {
  204. this.$refs[formName].validate(valid => {
  205. if (valid) {
  206. rePass({
  207. password: this.formData.pass,
  208. code: this.formData.dynacode,
  209. key: this.formData.key,
  210. mobile: this.formData.mobile
  211. })
  212. .then(res => {
  213. if (res.code >= 0) {
  214. this.step = 4
  215. this.$message({
  216. message: res.message,
  217. type: "success"
  218. })
  219. }
  220. })
  221. .catch(err => {
  222. this.$message.error(err.message)
  223. })
  224. } else {
  225. return false
  226. }
  227. })
  228. },
  229. /**
  230. * 获取验证码
  231. */
  232. getCaptcha() {
  233. captcha({
  234. captcha_id: this.captcha.id
  235. })
  236. .then(res => {
  237. if (res.code >= 0) {
  238. this.captcha.id = res.data.id
  239. this.captcha.img = res.data.img
  240. this.captcha.img = this.captcha.img.replace(/\r\n/g, "")
  241. }
  242. })
  243. .catch(err => {
  244. this.$message.error(err.message)
  245. })
  246. },
  247. /**
  248. * 发送手机动态码
  249. */
  250. sendMobileCode(formName) {
  251. if (this.dynacodeData.seconds != 120) return
  252. this.$refs[formName].clearValidate("dynacode")
  253. this.$refs[formName].validateField("vercode", valid => {
  254. if (!valid) {
  255. if (this.isSend) return
  256. this.isSend = true
  257. smsCode({
  258. captcha_id: this.captcha.id,
  259. captcha_code: this.formData.vercode,
  260. mobile: this.formData.mobile
  261. })
  262. .then(res => {
  263. if (res.code >= 0) {
  264. this.formData.key = res.data.key
  265. if (this.dynacodeData.seconds == 120 && this.dynacodeData.timer == null) {
  266. this.dynacodeData.timer = setInterval(() => {
  267. this.dynacodeData.seconds--
  268. this.dynacodeData.codeText = this.dynacodeData.seconds + "s后可重新获取"
  269. }, 1000)
  270. } else {
  271. this.$message({
  272. message: res.message,
  273. type: "warning"
  274. })
  275. this.isSend = false
  276. this.getCaptcha()
  277. }
  278. }
  279. })
  280. .catch(err => {
  281. this.isSend = false
  282. this.getCaptcha()
  283. this.$message.error(err.message)
  284. })
  285. } else {
  286. return false
  287. }
  288. })
  289. },
  290. /**
  291. * 获取注册配置
  292. */
  293. getRegisterConfig() {
  294. registerConfig()
  295. .then(res => {
  296. if (res.code >= 0) {
  297. this.registerConfig = res.data.value
  298. }
  299. })
  300. .catch(err => {
  301. console.log(err.message)
  302. })
  303. }
  304. }
  305. }