forget-pass.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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" :style="{background: $config('primaryColors')}" :disabled="isDisable" @click="getCodeValidate">{{nowVal}}</button>
  11. </view>
  12. <view class="form-btn-con">
  13. <button class="form-btn" :style="{background: $config('buttonColors')}" @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" :style="{background: $config('buttonColors')}" 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/tools.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: '',
  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. onLoad(opts) {
  64. if(opts.username){
  65. this.form.phone = opts.username
  66. }
  67. },
  68. methods: {
  69. // 下一步
  70. goStep(){
  71. const _this = this
  72. // 表单校验
  73. if(this.form.phone == ''){
  74. uni.showToast({icon: 'none',title: '请输入登录手机号'})
  75. return
  76. }
  77. if(!isvalidPhone(this.form.phone)){
  78. uni.showToast({icon: 'none',title: '请输入正确的手机号码'})
  79. return
  80. }
  81. if(this.form.verifyCode == ''){
  82. uni.showToast({icon: 'none',title: '请输入验证码'})
  83. return
  84. }
  85. // 校验短信验证码是否正确
  86. const obj = {}
  87. obj.phone = _this.form.phone,
  88. obj.random = _this.randomCode, // 当前随机码
  89. obj.verifyCode = _this.form.verifyCode
  90. memberValidateVerifyCode(obj).then(res => {
  91. if(res.status == 200){
  92. _this.state = 1
  93. }else{
  94. uni.showToast({icon: 'none',title: res.message})
  95. }
  96. })
  97. },
  98. // 表单提交
  99. formSubmit(){
  100. const _this = this
  101. if(this.form.password == ''){
  102. uni.showToast({icon: 'none',title: '请输入新密码'})
  103. return
  104. }
  105. if(this.form.password.length < 6 || this.form.password.length > 12){
  106. uni.showToast({icon: 'none',title: '请输入6-12位新密码'})
  107. return
  108. }
  109. if(this.form.passwords == ''){
  110. uni.showToast({icon: 'none',title: '请再次输入新密码'})
  111. return
  112. }
  113. if(this.form.password != this.form.passwords){
  114. uni.showToast({icon: 'none',title: '两次密码输入不一致'})
  115. return
  116. }
  117. const obj = {}
  118. obj.phone = _this.form.phone,
  119. obj.random = _this.randomCode, // 当前随机码
  120. obj.verifyCode = _this.form.verifyCode,
  121. obj.password = _this.form.password
  122. memberChangePwd(obj).then(res => {
  123. if(res.status == 200){
  124. // 将修改后的账号密码 重新存储
  125. _this.$u.vuex('vuex_user.account', _this.form.phone)
  126. _this.$u.vuex('vuex_user.password', '')
  127. clzConfirm({
  128. title: '提示',
  129. content: '修改成功',
  130. confirmText: '去登录',
  131. showCancel: false,
  132. success: function (ret) {
  133. if (ret.confirm||ret.index==0) {
  134. uni.reLaunch({
  135. url: '/pages/login/login'
  136. })
  137. }
  138. }
  139. })
  140. }
  141. })
  142. },
  143. // 获取验证码
  144. getCodeValidate(){
  145. if(this.form.phone){
  146. if(!isvalidPhone(this.form.phone)){
  147. uni.showToast({icon: 'none',title: '请输入正确的手机号码'})
  148. return
  149. }else{
  150. // 校验手机号是否注册过
  151. memberGetByMobile({phone: this.form.phone}).then(res => {
  152. if(res.status == 200){
  153. // 图文验证码
  154. this.$refs.imageTxtPopup.open()
  155. }else{
  156. uni.showToast({icon: 'none',title: res.message})
  157. }
  158. })
  159. }
  160. }else{
  161. uni.showToast({icon: 'none',title: '请先输入手机号'})
  162. }
  163. },
  164. // 开始倒计时
  165. getCodeFun(){
  166. const TIME_COUNT = 60
  167. if (!this.timer) {
  168. this.count = TIME_COUNT
  169. this.timer = setInterval(() => {
  170. if (this.count > 0 && this.count <= TIME_COUNT) {
  171. this.count--
  172. this.nowVal = this.count + 's后重发'
  173. this.isDisable = true
  174. } else {
  175. this.nowVal = '重新获取验证码'
  176. clearInterval(this.timer)
  177. this.timer = null
  178. this.isDisable = false
  179. }
  180. }, 1000)
  181. }
  182. },
  183. // 图文验证码
  184. captchaImg(code, nowRandomCode){
  185. this.captchaVerify(code, nowRandomCode)
  186. },
  187. // 验证图片验证码
  188. captchaVerify(code, nowRandomCode){
  189. const _this = this
  190. let obj = {}
  191. obj.phone = this.form.phone
  192. obj.random = nowRandomCode
  193. obj.captcha = code
  194. // 短信验证码
  195. memberVerifyCode(obj).then(res => {
  196. console.log(JSON.stringify(res))
  197. if(res.status == 200){ // 验证码输入正确
  198. _this.randomCode = nowRandomCode // 图片验证码随机码
  199. // 关闭 图形验证码 弹框
  200. _this.$refs.imageTxtPopup.close()
  201. // 开启倒计时
  202. this.getCodeFun()
  203. uni.showToast({icon: 'none',title: '验证码发送成功'})
  204. }else { // 验证码输入错误
  205. _this.randomCode = ''
  206. // 切换验证码重新校验
  207. _this.changeImg = false
  208. _this.$nextTick(function(){
  209. _this.changeImg = true
  210. })
  211. uni.showToast({icon: 'none',title: res.message})
  212. }
  213. })
  214. },
  215. }
  216. }
  217. </script>
  218. <style scoped lang="scss">
  219. @import './login.scss';
  220. .forget-wrap .login-form{
  221. padding-top: 300upx;
  222. }
  223. .getcode-btn {
  224. color: #fff !important;
  225. border: none !important;
  226. }
  227. </style>