forget-pass.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <view class="forget-wrap">
  3. <form class="login-form" @submit="formSubmit">
  4. <view v-show="state == 0">
  5. <view class="form-item">
  6. <input v-model="form.phone" class="item-inp" name="phone" type="number" maxlength="11" placeholder="请输入登录手机号" />
  7. </view>
  8. <view class="form-item">
  9. <input v-model="form.verifyCode" class="item-inp" name="verifyCode" type="number" maxlength="6" placeholder="请输入短信验证码" />
  10. <button class="getcode-btn" :disabled="isDisable" @click="getCodeValidate">{{nowVal}}</button>
  11. </view>
  12. <view class="form-btn-con">
  13. <button class="form-btn" @click="goStep">下一步</button>
  14. </view>
  15. </view>
  16. <view v-show="state == 1">
  17. <view class="form-item">
  18. <input v-model="form.password" class="item-inp" name="password" password type="text" maxlength="12" placeholder="请输入新密码" />
  19. </view>
  20. <view class="form-item">
  21. <input v-model="form.passwords" class="item-inp" name="passwords" password type="text" maxlength="12" placeholder="请再次输入新密码" />
  22. </view>
  23. <view class="form-btn-con">
  24. <button class="form-btn" form-type="submit">提交</button>
  25. </view>
  26. </view>
  27. </form>
  28. <!-- 图文验证码 -->
  29. <uni-popup ref="imageTxtPopup" type="center">
  30. <popup-con type="forgetImageTxt" title="图文验证码" :popBtn="popBtn" :changeImg="changeImg" @captchaImg="captchaImg"></popup-con>
  31. </uni-popup>
  32. </view>
  33. </template>
  34. <script>
  35. import { isvalidPhone } from '@/libs/validate.js'
  36. import uniPopup from '@/components/uni-popup/uni-popup.vue'
  37. import popupCon from '@/components/uni-popup/popup-con.vue'
  38. import { memberVerifyCode, memberChangePwd, memberValidateVerifyCode, memberGetByMobile } from '@/api/user.js'
  39. import { clzConfirm } from '@/libs/tools.js'
  40. export default{
  41. components: {
  42. uniPopup,
  43. popupCon
  44. },
  45. data(){
  46. return{
  47. state: 0, // 忘记密码当前状态
  48. form: {
  49. phone: this.$store.state.vuex_user.account,
  50. verifyCode: '',
  51. password: '',
  52. passwords: ''
  53. },
  54. nowVal: '获取验证码', // 按钮显示内容
  55. count: '', // 当前秒数
  56. timer: null, // 倒计时
  57. isDisable: false, // 是否禁止获取验证码
  58. popBtn: [], // name为操作按钮名称,color为操作按钮颜色值
  59. changeImg: false, // 是否重新更换图形验证码
  60. randomCode: '', // 图片验证码随机码
  61. }
  62. },
  63. methods: {
  64. // 下一步
  65. goStep(){
  66. const _this = this
  67. // 表单校验
  68. if(this.form.phone == ''){
  69. uni.showToast({icon: 'none',title: '请输入登录手机号'})
  70. return
  71. }
  72. if(!isvalidPhone(this.form.phone)){
  73. uni.showToast({icon: 'none',title: '请输入正确的手机号码'})
  74. return
  75. }
  76. if(this.form.verifyCode == ''){
  77. uni.showToast({icon: 'none',title: '请输入验证码'})
  78. return
  79. }
  80. // 校验短信验证码是否正确
  81. const obj = {}
  82. obj.phone = _this.form.phone,
  83. obj.random = _this.randomCode, // 当前随机码
  84. obj.verifyCode = _this.form.verifyCode
  85. memberValidateVerifyCode(obj).then(res => {
  86. if(res.status == 200){
  87. _this.state = 1
  88. }else{
  89. uni.showToast({icon: 'none',title: res.message})
  90. }
  91. })
  92. },
  93. // 表单提交
  94. formSubmit(){
  95. const _this = this
  96. if(this.form.password == ''){
  97. uni.showToast({icon: 'none',title: '请输入新密码'})
  98. return
  99. }
  100. if(this.form.password.length < 6 || this.form.password.length > 12){
  101. uni.showToast({icon: 'none',title: '请输入6-12位新密码'})
  102. return
  103. }
  104. if(this.form.passwords == ''){
  105. uni.showToast({icon: 'none',title: '请再次输入新密码'})
  106. return
  107. }
  108. if(this.form.password != this.form.passwords){
  109. uni.showToast({icon: 'none',title: '两次密码输入不一致'})
  110. return
  111. }
  112. const obj = {}
  113. obj.phone = _this.form.phone,
  114. obj.random = _this.randomCode, // 当前随机码
  115. obj.verifyCode = _this.form.verifyCode,
  116. obj.password = _this.form.password
  117. memberChangePwd(obj).then(res => {
  118. if(res.status == 200){
  119. // 将修改后的账号密码 重新存储
  120. _this.$u.vuex('vuex_user.account', _this.form.phone)
  121. _this.$u.vuex('vuex_user.password', '')
  122. clzConfirm({
  123. title: '提示',
  124. content: '修改成功',
  125. confirmText: '去登录',
  126. showCancel: false,
  127. success: function (ret) {
  128. if (ret.confirm||ret.index==0) {
  129. uni.reLaunch({
  130. url: '/pages/login/login'
  131. })
  132. }
  133. }
  134. })
  135. }
  136. })
  137. },
  138. // 获取验证码
  139. getCodeValidate(){
  140. if(this.form.phone){
  141. if(!isvalidPhone(this.form.phone)){
  142. uni.showToast({icon: 'none',title: '请输入正确的手机号码'})
  143. return
  144. }else{
  145. // 校验手机号是否注册过
  146. memberGetByMobile({phone: this.form.phone}).then(res => {
  147. if(res.status == 200){
  148. // 图文验证码
  149. this.$refs.imageTxtPopup.open()
  150. }else{
  151. uni.showToast({icon: 'none',title: res.message})
  152. }
  153. })
  154. }
  155. }else{
  156. uni.showToast({icon: 'none',title: '请先输入手机号'})
  157. }
  158. },
  159. // 开始倒计时
  160. getCodeFun(){
  161. const TIME_COUNT = 60
  162. if (!this.timer) {
  163. this.count = TIME_COUNT
  164. this.timer = setInterval(() => {
  165. if (this.count > 0 && this.count <= TIME_COUNT) {
  166. this.count--
  167. this.nowVal = this.count + 's'
  168. this.isDisable = true
  169. } else {
  170. this.nowVal = '重新获取验证码'
  171. clearInterval(this.timer)
  172. this.timer = null
  173. this.isDisable = false
  174. }
  175. }, 1000)
  176. }
  177. },
  178. // 图文验证码
  179. captchaImg(code, nowRandomCode){
  180. this.captchaVerify(code, nowRandomCode)
  181. },
  182. // 验证图片验证码
  183. captchaVerify(code, nowRandomCode){
  184. const _this = this
  185. let obj = {}
  186. obj.phone = this.form.phone
  187. obj.random = nowRandomCode
  188. obj.captcha = code
  189. // 短信验证码
  190. memberVerifyCode(obj).then(res => {
  191. console.log(JSON.stringify(res))
  192. if(res.status == 200){ // 验证码输入正确
  193. _this.randomCode = nowRandomCode // 图片验证码随机码
  194. // 关闭 图形验证码 弹框
  195. _this.$refs.imageTxtPopup.close()
  196. // 开启倒计时
  197. this.getCodeFun()
  198. uni.showToast({icon: 'none',title: '验证码发送成功'})
  199. }else { // 验证码输入错误
  200. _this.randomCode = ''
  201. // 切换验证码重新校验
  202. _this.changeImg = false
  203. _this.$nextTick(function(){
  204. _this.changeImg = true
  205. })
  206. uni.showToast({icon: 'none',title: res.message})
  207. }
  208. })
  209. },
  210. }
  211. }
  212. </script>
  213. <style scoped lang="scss">
  214. @import './login.scss';
  215. .forget-wrap .login-form{
  216. padding-top: 300upx;
  217. }
  218. .getcode-btn {
  219. background-color: #298bf2 !important;
  220. color: #fff !important;
  221. border: none !important;
  222. }
  223. </style>