phoneLogin.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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, checkCustomerState } 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. uni.showShareMenu({
  55. withShareTicket: true,
  56. menus: ['shareAppMessage', 'shareTimeline']
  57. })
  58. },
  59. onUnload() {
  60. uni.$off('getUserInfo')
  61. },
  62. methods: {
  63. // 登录
  64. submitFn(){
  65. const _this = this
  66. // 表单校验
  67. if(this.form.phone == ''){
  68. uni.showToast({icon: 'none',title: '请输入登录手机号'})
  69. return
  70. }
  71. if(!isvalidPhone(this.form.phone)){
  72. uni.showToast({icon: 'none',title: '请输入正确的手机号码'})
  73. return
  74. }
  75. if(this.form.verifyCode == ''){
  76. uni.showToast({icon: 'none',title: '请输入验证码'})
  77. return
  78. }
  79. // 校验短信验证码是否正确
  80. const obj = {}
  81. obj.phone = _this.form.phone
  82. obj.random = _this.randomCode // 当前随机码
  83. obj.verifiCode = _this.form.verifyCode
  84. uni.login({
  85. provider: 'weixin',
  86. success(res) {
  87. console.log(res)
  88. _this.wxcode = res.code
  89. obj.code = res.code
  90. loginPhone(obj).then(res => {
  91. if(res.status == 200){
  92. console.log(res,'login success')
  93. getApp().globalData.token = res.data
  94. _this.$u.vuex('vuex_token',res.data)
  95. uni.$emit("getUserInfo")
  96. uni.navigateBack({
  97. delta: 2
  98. })
  99. }else{
  100. uni.showToast({icon: 'none',title: res.message})
  101. }
  102. })
  103. }
  104. })
  105. },
  106. // 单击获取验证码
  107. getCodeValidate(){
  108. if(this.form.phone){
  109. if(!isvalidPhone(this.form.phone)){
  110. uni.showToast({icon: 'none',title: '请输入正确的手机号码'})
  111. return
  112. }else{
  113. // 先校验手机号用户是否禁用
  114. checkCustomerState({phone:this.form.phone}).then(res=>{
  115. if(res.status == 200) {
  116. this.retsetCode()
  117. // 图文验证码
  118. this.$refs.imageTxtPopup.open()
  119. } else {
  120. uni.showToast({
  121. title: res.message,
  122. icon: 'none'
  123. })
  124. }
  125. })
  126. }
  127. }else{
  128. uni.showToast({icon: 'none',title: '请先输入手机号'})
  129. }
  130. },
  131. // 开始倒计时
  132. getCodeFun(){
  133. const TIME_COUNT = 60
  134. if (!this.timer) {
  135. this.count = TIME_COUNT
  136. this.timer = setInterval(() => {
  137. if (this.count > 0 && this.count <= TIME_COUNT) {
  138. this.count--
  139. this.nowVal = this.count + 's后重发'
  140. this.isDisable = true
  141. } else {
  142. this.nowVal = '重新获取验证码'
  143. clearInterval(this.timer)
  144. this.timer = null
  145. this.isDisable = false
  146. }
  147. }, 1000)
  148. }
  149. },
  150. // 验证图片验证码
  151. captchaVerify(code, nowRandomCode){
  152. const _this = this
  153. let obj = {}
  154. obj.phone = this.form.phone
  155. obj.random = nowRandomCode
  156. obj.code = code
  157. // 发送短信验证码
  158. sendVerifyCode(obj).then(res => {
  159. console.log(JSON.stringify(res))
  160. if(res.status == 200){ // 验证码输入正确
  161. _this.randomCode = nowRandomCode // 图片验证码随机码
  162. // 关闭 图形验证码 弹框
  163. _this.$refs.imageTxtPopup.close()
  164. // 开启倒计时
  165. _this.getCodeFun()
  166. uni.showToast({icon: 'none',title: '验证码发送成功'})
  167. }else { // 验证码输入错误
  168. _this.retsetCode()
  169. uni.showToast({icon: 'none',title: res.message})
  170. }
  171. })
  172. },
  173. // 重新触发获取图片验证码
  174. retsetCode(){
  175. const _this = this
  176. _this.form.verifyCode = ''
  177. _this.randomCode = ''
  178. // 切换验证码重新校验
  179. _this.changeImg = false
  180. _this.$nextTick(function(){
  181. _this.changeImg = true
  182. })
  183. }
  184. }
  185. }
  186. </script>
  187. <style scoped lang="scss">
  188. @import './login.scss';
  189. </style>