phoneLogin.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <view class="forget-wrap">
  3. <view class="logo">
  4. <u-image src="/static/logo.png" width="120" height="120" ></u-image>
  5. </view>
  6. <form class="login-form" @submit="formSubmit">
  7. <view class="form-item">
  8. <input v-model="form.phone" class="item-inp" name="phone" type="number" maxlength="11" placeholder="请输入登录手机号" />
  9. </view>
  10. <view class="form-item">
  11. <input v-model="form.verifyCode" class="item-inp" name="verifyCode" type="number" maxlength="6" placeholder="请输入短信验证码" />
  12. <button class="getcode-btn" :disabled="isDisable" @click="getCodeValidate">{{nowVal}}</button>
  13. </view>
  14. <view class="form-btn-con">
  15. <button class="form-btn" @click="submitFn">登录</button>
  16. </view>
  17. </form>
  18. <!-- 图文验证码 -->
  19. <uni-popup ref="imageTxtPopup" type="center">
  20. <popup-con type="forgetImageTxt" title="图文验证码" :popBtn="popBtn" :changeImg="changeImg" @captchaImg="captchaImg"></popup-con>
  21. </uni-popup>
  22. </view>
  23. </template>
  24. <script>
  25. import { isvalidPhone } from '@/libs/validate.js'
  26. import uniPopup from '@/components/uni-popup/uni-popup.vue'
  27. import popupCon from '@/components/uni-popup/popup-con.vue'
  28. import { memberVerifyCode, memberValidateVerifyCode, memberGetByMobile } from '@/api/user.js'
  29. export default{
  30. components: {
  31. uniPopup,
  32. popupCon
  33. },
  34. data(){
  35. return{
  36. state: 0, // 忘记密码当前状态
  37. form: {
  38. phone: '',
  39. verifyCode: '',
  40. password: '',
  41. passwords: ''
  42. },
  43. nowVal: '获取验证码', // 按钮显示内容
  44. count: '', // 当前秒数
  45. timer: null, // 倒计时
  46. isDisable: false, // 是否禁止获取验证码
  47. popBtn: [], // name为操作按钮名称,color为操作按钮颜色值
  48. changeImg: false, // 是否重新更换图形验证码
  49. randomCode: '', // 图片验证码随机码
  50. }
  51. },
  52. onLoad(opts) {
  53. if(opts.username){
  54. this.form.phone = opts.username
  55. }
  56. },
  57. methods: {
  58. // 登录
  59. submitFn(){
  60. const _this = this
  61. // 表单校验
  62. if(this.form.phone == ''){
  63. uni.showToast({icon: 'none',title: '请输入登录手机号'})
  64. return
  65. }
  66. if(!isvalidPhone(this.form.phone)){
  67. uni.showToast({icon: 'none',title: '请输入正确的手机号码'})
  68. return
  69. }
  70. if(this.form.verifyCode == ''){
  71. uni.showToast({icon: 'none',title: '请输入验证码'})
  72. return
  73. }
  74. // 校验短信验证码是否正确
  75. const obj = {}
  76. obj.phone = _this.form.phone,
  77. obj.random = _this.randomCode, // 当前随机码
  78. obj.verifyCode = _this.form.verifyCode
  79. memberValidateVerifyCode(obj).then(res => {
  80. if(res.status == 200){
  81. _this.state = 1
  82. }else{
  83. uni.showToast({icon: 'none',title: res.message})
  84. }
  85. })
  86. },
  87. // 获取验证码
  88. getCodeValidate(){
  89. if(this.form.phone){
  90. if(!isvalidPhone(this.form.phone)){
  91. uni.showToast({icon: 'none',title: '请输入正确的手机号码'})
  92. return
  93. }else{
  94. // 校验手机号是否注册过
  95. memberGetByMobile({phone: this.form.phone}).then(res => {
  96. if(res.status == 200){
  97. // 图文验证码
  98. this.$refs.imageTxtPopup.open()
  99. }else{
  100. uni.showToast({icon: 'none',title: res.message})
  101. }
  102. })
  103. }
  104. }else{
  105. uni.showToast({icon: 'none',title: '请先输入手机号'})
  106. }
  107. },
  108. // 开始倒计时
  109. getCodeFun(){
  110. const TIME_COUNT = 60
  111. if (!this.timer) {
  112. this.count = TIME_COUNT
  113. this.timer = setInterval(() => {
  114. if (this.count > 0 && this.count <= TIME_COUNT) {
  115. this.count--
  116. this.nowVal = this.count + 's后重发'
  117. this.isDisable = true
  118. } else {
  119. this.nowVal = '重新获取验证码'
  120. clearInterval(this.timer)
  121. this.timer = null
  122. this.isDisable = false
  123. }
  124. }, 1000)
  125. }
  126. },
  127. // 图文验证码
  128. captchaImg(code, nowRandomCode){
  129. this.captchaVerify(code, nowRandomCode)
  130. },
  131. // 验证图片验证码
  132. captchaVerify(code, nowRandomCode){
  133. const _this = this
  134. let obj = {}
  135. obj.phone = this.form.phone
  136. obj.random = nowRandomCode
  137. obj.captcha = code
  138. // 短信验证码
  139. memberVerifyCode(obj).then(res => {
  140. console.log(JSON.stringify(res))
  141. if(res.status == 200){ // 验证码输入正确
  142. _this.randomCode = nowRandomCode // 图片验证码随机码
  143. // 关闭 图形验证码 弹框
  144. _this.$refs.imageTxtPopup.close()
  145. // 开启倒计时
  146. this.getCodeFun()
  147. uni.showToast({icon: 'none',title: '验证码发送成功'})
  148. }else { // 验证码输入错误
  149. _this.randomCode = ''
  150. // 切换验证码重新校验
  151. _this.changeImg = false
  152. _this.$nextTick(function(){
  153. _this.changeImg = true
  154. })
  155. uni.showToast({icon: 'none',title: res.message})
  156. }
  157. })
  158. },
  159. }
  160. }
  161. </script>
  162. <style scoped lang="scss">
  163. @import './login.scss';
  164. .getcode-btn {
  165. background-color: #298bf2 !important;
  166. color: #fff !important;
  167. border: none !important;
  168. }
  169. </style>