code.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <template>
  2. <view class="container">
  3. <u-navbar title="手机验证码登录" :border="true" :placeholder="true" :autoBack="true" />
  4. <view class="container">
  5. <view class="login">
  6. <view class="login-title">验证码登录</view>
  7. <view class="login-input pat">
  8. <u-input placeholder="请输入手机号" type="number" v-model="mobile" border="none" clearable @blur="changeNumber"/>
  9. </view>
  10. <view class="login-input">
  11. <u-input placeholder="请输入短信验证码" type="number" v-model="code" clearable @blur="changeCode">
  12. <template slot="suffix">
  13. <u-code ref="uCode" @change="codeChange" seconds="60" changeText="Xs"></u-code>
  14. <u-button @click="getCode" :text="tips" type="default" size="mini"></u-button>
  15. </template>
  16. </u-input>
  17. </view>
  18. <u-button @click="login" text="登录" shape="circle" :loading="loading"></u-button>
  19. </view>
  20. </view>
  21. <auth :auth-show="authShow" @close="authClose" />
  22. </view>
  23. </template>
  24. <script>
  25. import $http from '@/utils/request.js'
  26. import Auth from '../../components/auth/auth.vue'
  27. export default {
  28. components: {
  29. Auth
  30. },
  31. data() {
  32. return {
  33. loading: false,
  34. mobile: '',
  35. codeId: '',
  36. code: '',
  37. tips: '',
  38. authShow: false,
  39. };
  40. },
  41. methods: {
  42. codeChange(text) {
  43. this.tips = text;
  44. },
  45. getCode() {
  46. const rule = /^[1][0-9][0-9]{9}$/
  47. if (!this.mobile) {
  48. uni.$u.toast('请输入手机号');
  49. return
  50. }
  51. if (!rule.test(this.mobile)) {
  52. uni.$u.toast('请输入正确的手机号');
  53. return
  54. }
  55. if (this.$refs.uCode.canGetCode) {
  56. // 这里此提示会被this.start()方法中的提示覆盖
  57. uni.$u.toast('验证码已发送');
  58. // 通知验证码组件内部开始倒计时
  59. this.$refs.uCode.start();
  60. $http.post('/api/v1/mp/sms/sendSmsCode', {
  61. mobile: this.mobile,
  62. noToken: true
  63. }).then(res => {
  64. if (res.code === 0 && res.data) {
  65. this.codeId = res.data
  66. }
  67. })
  68. } else {
  69. uni.$u.toast('倒计时结束后再发送');
  70. }
  71. },
  72. changeNumber(e) {
  73. this.mobile = e
  74. },
  75. changeCode(e) {
  76. this.code = e
  77. },
  78. login() {
  79. let _this = this
  80. if (!_this.mobile) {
  81. uni.$u.toast('请输入手机号');
  82. return
  83. }
  84. const rule = /^[1][0-9][0-9]{9}$/
  85. if (!rule.test(_this.mobile)) {
  86. uni.$u.toast('请输入正确的手机号');
  87. return
  88. }
  89. if (!_this.code) {
  90. uni.$u.toast('请输入验证码');
  91. return
  92. }
  93. _this.loading = true
  94. $http.post('/auth/mobile', {
  95. mobile: _this.mobile,
  96. messageId: _this.codeId,
  97. code: _this.code,
  98. identity: 1,
  99. noToken: true
  100. }).then(res => {
  101. _this.loading = false
  102. if (res.code === 0 && res.token) {
  103. // #ifdef MP-WEIXIN
  104. uni.setStorageSync('token', res.token)
  105. _this.getBaseInfo()
  106. // #endif
  107. // #ifdef H5 || MP-ALIPAY
  108. uni.$u.toast('登录成功');
  109. uni.setStorageSync('token', res.token)
  110. setTimeout(() => {
  111. uni.navigateBack({
  112. delta: 2
  113. })
  114. }, 500)
  115. // #endif
  116. }
  117. }).catch(() => {
  118. _this.loading = false
  119. })
  120. },
  121. getBaseInfo() {
  122. $http.post('/api/v1/mp/user/getLoginUserinfo', {}).then(res => {
  123. uni.hideLoading();
  124. if (res.code == 0) {
  125. uni.setStorageSync('userInfo', res.data)
  126. if (res.data.openId) {
  127. uni.$u.toast('登录成功');
  128. setTimeout(() => {
  129. uni.navigateBack({
  130. delta: 2
  131. })
  132. }, 500)
  133. } else {
  134. this.authShow = true
  135. }
  136. }
  137. }).catch(() => {
  138. uni.hideLoading();
  139. })
  140. },
  141. authClose() {
  142. this.authShow = false
  143. setTimeout(() => {
  144. uni.navigateBack({
  145. delta: 2
  146. })
  147. }, 500)
  148. },
  149. }
  150. }
  151. </script>
  152. <style lang="scss" scoped>
  153. .container {
  154. height: 100vh;
  155. width: 100%;
  156. overflow: hidden;
  157. }
  158. .login {
  159. padding: 70rpx 70rpx 0;
  160. &-title {
  161. font-size: 56rpx;
  162. color: #333;
  163. line-height: 80rpx;
  164. margin-bottom: 60rpx;
  165. }
  166. &-input {
  167. display: flex;
  168. align-items: center;
  169. margin-bottom: 50rpx;
  170. height: 80rpx;
  171. background-color: #EDEDED !important;
  172. border-radius: 100rpx;
  173. padding: 10rpx 48rpx;
  174. ::v-deep .u-input__content__field-wrapper__field {
  175. background: none;
  176. }
  177. ::v-deep .u-border {
  178. border: none;
  179. }
  180. }
  181. .pat{
  182. padding: 10rpx 66rpx;
  183. }
  184. ::v-deep .u-button {
  185. width: 100%;
  186. background-color: $uni-bg-color;
  187. color: #fff;
  188. border-radius: 32px;
  189. }
  190. ::v-deep .u-button--success{
  191. background-color: none;
  192. }
  193. }
  194. </style>