code.vue 4.8 KB

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