code.vue 5.7 KB

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