phoneLogin.vue 5.9 KB

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