phoneLogin.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <view class="forget-wrap">
  3. <view class="logo">
  4. <u-image src="/static/logo.png" width="140" height="140" ></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 title="图文验证码" :popBtn="popBtn" :changeImg="changeImg" @captchaImg="captchaVerify"></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 { loginPhone, sendVerifyCode } from '@/api/user.js'
  29. export default{
  30. components: {
  31. uniPopup,
  32. popupCon
  33. },
  34. data(){
  35. return{
  36. form: {
  37. phone: '',
  38. verifyCode: '',
  39. password: '',
  40. passwords: ''
  41. },
  42. nowVal: '获取验证码', // 按钮显示内容
  43. count: '', // 当前秒数
  44. timer: null, // 倒计时
  45. isDisable: false, // 是否禁止获取验证码
  46. popBtn: [], // name为操作按钮名称,color为操作按钮颜色值
  47. changeImg: false, // 是否重新更换图形验证码
  48. randomCode: '', // 图片验证码随机码
  49. wxcode: ''
  50. }
  51. },
  52. onLoad(opts) {
  53. },
  54. methods: {
  55. // 登录
  56. submitFn(){
  57. const _this = this
  58. // 表单校验
  59. if(this.form.phone == ''){
  60. uni.showToast({icon: 'none',title: '请输入登录手机号'})
  61. return
  62. }
  63. if(!isvalidPhone(this.form.phone)){
  64. uni.showToast({icon: 'none',title: '请输入正确的手机号码'})
  65. return
  66. }
  67. if(this.form.verifyCode == ''){
  68. uni.showToast({icon: 'none',title: '请输入验证码'})
  69. return
  70. }
  71. // 校验短信验证码是否正确
  72. const obj = {}
  73. obj.phone = _this.form.phone
  74. obj.random = _this.randomCode // 当前随机码
  75. obj.verifiCode = _this.form.verifyCode
  76. uni.login({
  77. provider: 'weixin',
  78. success(res) {
  79. console.log(res)
  80. _this.wxcode = res.code
  81. obj.code = res.code
  82. loginPhone(obj).then(res => {
  83. if(res.status == 200){
  84. console.log(res,'login success')
  85. getApp().globalData.token = res.data
  86. _this.$u.vuex('vuex_token',res.data)
  87. uni.$emit("getUserInfo")
  88. uni.navigateBack({
  89. delta: 2
  90. })
  91. }else{
  92. uni.showToast({icon: 'none',title: res.message})
  93. }
  94. })
  95. }
  96. })
  97. },
  98. // 单击获取验证码
  99. getCodeValidate(){
  100. if(this.form.phone){
  101. if(!isvalidPhone(this.form.phone)){
  102. uni.showToast({icon: 'none',title: '请输入正确的手机号码'})
  103. return
  104. }else{
  105. this.retsetCode()
  106. // 图文验证码
  107. this.$refs.imageTxtPopup.open()
  108. }
  109. }else{
  110. uni.showToast({icon: 'none',title: '请先输入手机号'})
  111. }
  112. },
  113. // 开始倒计时
  114. getCodeFun(){
  115. const TIME_COUNT = 60
  116. if (!this.timer) {
  117. this.count = TIME_COUNT
  118. this.timer = setInterval(() => {
  119. if (this.count > 0 && this.count <= TIME_COUNT) {
  120. this.count--
  121. this.nowVal = this.count + 's后重发'
  122. this.isDisable = true
  123. } else {
  124. this.nowVal = '重新获取验证码'
  125. clearInterval(this.timer)
  126. this.timer = null
  127. this.isDisable = false
  128. }
  129. }, 1000)
  130. }
  131. },
  132. // 验证图片验证码
  133. captchaVerify(code, nowRandomCode){
  134. const _this = this
  135. let obj = {}
  136. obj.phone = this.form.phone
  137. obj.random = nowRandomCode
  138. obj.code = code
  139. // 发送短信验证码
  140. sendVerifyCode(obj).then(res => {
  141. console.log(JSON.stringify(res))
  142. if(res.status == 200){ // 验证码输入正确
  143. _this.randomCode = nowRandomCode // 图片验证码随机码
  144. // 关闭 图形验证码 弹框
  145. _this.$refs.imageTxtPopup.close()
  146. // 开启倒计时
  147. _this.getCodeFun()
  148. uni.showToast({icon: 'none',title: '验证码发送成功'})
  149. }else { // 验证码输入错误
  150. _this.retsetCode()
  151. uni.showToast({icon: 'none',title: res.message})
  152. }
  153. })
  154. },
  155. // 重新触发获取图片验证码
  156. retsetCode(){
  157. const _this = this
  158. _this.form.verifyCode = ''
  159. _this.randomCode = ''
  160. // 切换验证码重新校验
  161. _this.changeImg = false
  162. _this.$nextTick(function(){
  163. _this.changeImg = true
  164. })
  165. }
  166. }
  167. }
  168. </script>
  169. <style scoped lang="scss">
  170. @import './login.scss';
  171. </style>