useCodeLogin.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <script>
  2. import { loginPhone, sendMsg } from '@/api/newLogin.js'
  3. export default {
  4. data() {
  5. return {
  6. phone: '', code: '',
  7. countdown: 60,
  8. }
  9. },
  10. computed: {
  11. canGetCode() {
  12. return !this.isSendingCode && this.countdown === 60
  13. },
  14. canBind() {
  15. return this.code && this.code.length === 6 && !this.isBinding
  16. }
  17. },
  18. onLoad(options) {
  19. if (options.phone) {
  20. this.phone = options.phone;
  21. clearInterval(this.timer)
  22. this.timer = null
  23. this.startCountdown()
  24. }
  25. },
  26. methods: {
  27. validatePhone(phone) {
  28. const reg = /^1[3-9]\d{9}$/
  29. return reg.test(phone)
  30. },
  31. getCode() {
  32. if (this.isSendingCode || this.countdown < 60) return
  33. this.isSendingCode = true
  34. sendMsg({ phone: this.phone }).then(res => {
  35. console.log(res)
  36. uni.showToast({
  37. title: '验证码已发送',
  38. icon: 'success'
  39. })
  40. this.startCountdown()
  41. this.isSendingCode = false
  42. })
  43. },
  44. startCountdown() {
  45. this.countdown = 60
  46. this.timer = setInterval(() => {
  47. this.countdown--
  48. if (this.countdown <= 0) {
  49. clearInterval(this.timer)
  50. this.countdown = 60
  51. this.timer = null
  52. }
  53. }, 1000)
  54. },
  55. // 满6位自动校验
  56. autoCheckCode(val) {
  57. // 输入刚好6位时自动提交
  58. if (val.length === 6) {
  59. loginPhone({
  60. //openId: uni.getStorageSync('wx_copenid'),
  61. userType: '2',
  62. phone: this.phone,
  63. codeSwitch: 'true',//是否开启验证码登录 true-是 false-否
  64. phoneMsg: this.code
  65. }).then(res => {
  66. if (res.data.code == 200) {
  67. // uni.showToast({
  68. // title: '登录成功',
  69. // icon: 'success'
  70. // })
  71. uni.setStorageSync('wx_phone', this.phone)
  72. this.back()
  73. }
  74. })
  75. }
  76. },
  77. back() {
  78. let pages = getCurrentPages();
  79. if (pages.length >= 2) {
  80. let prevPage = pages[pages.length - 3];
  81. let prevRoute = prevPage.route;
  82. if (prevRoute && prevRoute !== 'pages/index/index') {
  83. if (prevRoute.startsWith('pages/')) {
  84. uni.switchTab({
  85. url: '/' + prevRoute
  86. })
  87. } else {
  88. uni.navigateBack({
  89. delta: 1,
  90. fail: () => {
  91. uni.switchTab({
  92. url: '/pages/index/index'
  93. })
  94. }
  95. })
  96. }
  97. } else {
  98. uni.switchTab({
  99. url: '/pages/index/index'
  100. })
  101. }
  102. } else {
  103. uni.switchTab({
  104. url: '/pages/index/index'
  105. })
  106. }
  107. },
  108. },
  109. beforeDestroy() {
  110. if (this.timer) {
  111. clearInterval(this.timer)
  112. this.timer = null
  113. }
  114. }
  115. }
  116. </script>
  117. <template>
  118. <view class="sett-box">
  119. <view class="greeting-area">
  120. <view class="bubble-box">
  121. <view class="bubble-text">请输入验证码</view>
  122. <view class="sendTag">验证码已发送至:+86 {{ phone }}</view>
  123. </view>
  124. </view>
  125. <view class="greeting-area1"></view>
  126. <view style="display: flex;justify-content: center;padding: 0 64rpx;">
  127. <view class="input-box">
  128. <u-input v-model="code" type="number" border="none" maxlength="6" placeholder="请输入手机验证码"
  129. placeholder-style="font-weight: 400;font-size: 30rpx;color: #C9CDD4;" class="input-item code-input"
  130. @input="autoCheckCode"></u-input>
  131. <view class="code-btn" :class="{ disabled: !canGetCode }" @click="getCode">
  132. <text v-if="countdown === 60">获取验证码</text>
  133. <text v-else>{{ countdown }}s 后重新获取</text>
  134. </view>
  135. </view>
  136. </view>
  137. </view>
  138. </template>
  139. <style scoped lang="scss">
  140. .sett-box {
  141. background: #ffffff;
  142. width: 100%;
  143. min-height: 100vh;
  144. line-height: 1;
  145. position: relative;
  146. .sendTag {
  147. margin-top: 25rpx;
  148. font-weight: 400;
  149. font-size: 28rpx;
  150. color: #86909C;
  151. text-align: left;
  152. }
  153. //padding: 36rpx 40rpx;
  154. .greeting-area1 {
  155. height: 438rpx;
  156. }
  157. .greeting-area {
  158. background-image: url('/static/login/loginBg.png');
  159. background-size: 100%;
  160. background-repeat: no-repeat;
  161. padding-left: 64rpx;
  162. padding-top: 256rpx;
  163. top: 0;
  164. z-index: 4;
  165. height: 438rpx;
  166. position: absolute;
  167. overflow: hidden;
  168. width: 100%;
  169. .bubble-text {
  170. font-weight: 500;
  171. font-size: 52rpx;
  172. color: #1D2129
  173. }
  174. }
  175. .code-btn{
  176. font-weight: 400;
  177. font-size: 30rpx;
  178. color: #19D29B;
  179. text-align: center;
  180. font-style: normal;
  181. text-transform: none;
  182. }
  183. }
  184. .input-box {
  185. width: 622rpx;
  186. height: 110rpx;
  187. background: #FFFFFF;
  188. display: flex;
  189. align-items: center;
  190. border-bottom: 1rpx solid #E7E7E7;
  191. .label {
  192. display: flex;
  193. }
  194. .input-item {
  195. flex: 1;
  196. }
  197. ::v-deep .uni-input-input {
  198. font-weight: 500;
  199. font-size: 30rpx;
  200. color: #1D2129;
  201. }
  202. }
  203. </style>