phoneLogin.vue 5.9 KB

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