code.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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></u--input>
  9. </view>
  10. <view class="login-input">
  11. <u-input placeholder="请输入短信验证码" type="number" v-model="code" clearable>
  12. <template slot="suffix">
  13. <u-code ref="uCode" @change="codeChange" seconds="60" changeText="Xs"></u-code>
  14. <u-button @tap="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. // 模拟向后端请求验证码
  57. uni.showLoading({
  58. title: '正在获取验证码'
  59. })
  60. // 这里此提示会被this.start()方法中的提示覆盖
  61. uni.$u.toast('验证码已发送');
  62. // 通知验证码组件内部开始倒计时
  63. this.$refs.uCode.start();
  64. $http.post('/api/v1/mp/sms/sendSmsCode', {
  65. mobile: this.mobile,
  66. noToken: true
  67. }).then(res => {
  68. if (res.code === 0 && res.data) {
  69. this.codeId = res.data
  70. }
  71. })
  72. } else {
  73. uni.$u.toast('倒计时结束后再发送');
  74. }
  75. },
  76. login() {
  77. let _this = this
  78. if (!_this.mobile) {
  79. uni.$u.toast('请输入手机号');
  80. return
  81. }
  82. const rule = /^[1][0-9][0-9]{9}$/
  83. if (!rule.test(_this.mobile)) {
  84. uni.$u.toast('请输入正确的手机号');
  85. return
  86. }
  87. if (!_this.code) {
  88. uni.$u.toast('请输入验证码');
  89. return
  90. }
  91. _this.loading = true
  92. $http.post('/auth/mobile', {
  93. mobile: _this.mobile,
  94. messageId: _this.codeId,
  95. code: _this.code,
  96. identity: 1,
  97. noToken: true
  98. }).then(res => {
  99. _this.loading = false
  100. if (res.code === 0 && res.token) {
  101. // #ifdef MP-WEIXIN
  102. uni.setStorageSync('token', res.token)
  103. _this.getBaseInfo()
  104. // #endif
  105. // #ifdef H5
  106. uni.$u.toast('登录成功');
  107. uni.setStorageSync('token', res.token)
  108. setTimeout(() => {
  109. uni.navigateBack({
  110. delta: 2
  111. })
  112. }, 500)
  113. // #endif
  114. }
  115. }).catch(() => {
  116. _this.loading = false
  117. })
  118. },
  119. getBaseInfo() {
  120. $http.post('/api/v1/mp/user/getLoginUserinfo', {}).then(res => {
  121. uni.hideLoading();
  122. if (res.code == 0) {
  123. uni.setStorageSync('userInfo', res.data)
  124. if (res.data.openId) {
  125. uni.$u.toast('登录成功');
  126. setTimeout(() => {
  127. uni.navigateBack({
  128. delta: 2
  129. })
  130. }, 500)
  131. } else {
  132. this.authShow = true
  133. }
  134. }
  135. }).catch(() => {
  136. uni.hideLoading();
  137. })
  138. },
  139. authClose() {
  140. this.authShow = false
  141. setTimeout(() => {
  142. uni.navigateBack({
  143. delta: 2
  144. })
  145. }, 500)
  146. },
  147. }
  148. }
  149. </script>
  150. <style lang="scss" scoped>
  151. .container {
  152. height: 100vh;
  153. width: 100%;
  154. overflow: hidden;
  155. }
  156. .login {
  157. padding: 70rpx 70rpx 0;
  158. &-title {
  159. font-size: 56rpx;
  160. color: #333;
  161. line-height: 80rpx;
  162. margin-bottom: 60rpx;
  163. }
  164. &-input {
  165. display: flex;
  166. align-items: center;
  167. margin-bottom: 50rpx;
  168. height: 80rpx;
  169. background-color: #EDEDED !important;
  170. border-radius: 100rpx;
  171. padding: 10rpx 48rpx;
  172. /deep/ .u-input__content__field-wrapper__field {
  173. background: none;
  174. }
  175. /deep/ .u-border {
  176. border: none;
  177. }
  178. }
  179. .pat{
  180. padding: 10rpx 66rpx;
  181. }
  182. /deep/ .u-button {
  183. width: 100%;
  184. background-color: $uni-bg-color;
  185. color: #fff;
  186. border-radius: 32px;
  187. }
  188. /deep/ .u-button--success{
  189. background-color: none;
  190. }
  191. }
  192. </style>