login.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <view class="container">
  3. <view class="unp-header">
  4. <view class="unp-logo">
  5. <u-avatar size="80" icon="github-circle-fill" fontSize="80"></u-avatar>
  6. </view>
  7. </view>
  8. <view class="unp-box">
  9. <!-- 登录方式选择 -->
  10. <view class="mode-section">
  11. <u-subsection mode="subsection" fontSize="15" :list="loginModeList" :current="currentModeIndex" @change="handleModeChange"></u-subsection>
  12. </view>
  13. <u-gap height="40"></u-gap>
  14. <!-- 登录表单 -->
  15. <u--form labelPosition="left" :model="formData" :rules="rules" ref="form">
  16. <u-form-item label="手机号" prop="mobile" labelWidth="60" borderBottom ref="item-mobile">
  17. <u-input type="number" maxlength="11" v-model="formData.mobile" clearable placeholder="请填写手机号" border="none"></u-input>
  18. </u-form-item>
  19. <u-gap height="20"></u-gap>
  20. <u-form-item v-if="currentModeIndex === 0" label="密码" prop="password" labelWidth="60" borderBottom ref="item-password">
  21. <u-input :type="inputType" maxlength="16" v-model="formData.password" placeholder="请填写密码" border="none">
  22. <template slot="suffix">
  23. <u-icon v-if="inputType === 'password'" size="20" color="#666666" name="eye-fill" @click="inputType = 'text'"></u-icon>
  24. <u-icon v-if="inputType === 'text'" size="20" color="#666666" name="eye-off" @click="inputType = 'password'"></u-icon>
  25. </template>
  26. </u-input>
  27. </u-form-item>
  28. <u-form-item v-else label="验证码" prop="code" labelWidth="60" borderBottom>
  29. <u--input type="number" maxlength="4" v-model="formData.code" border="none" placeholder="请填写验证码"></u--input>
  30. <u-button slot="right" @tap="getCode" :text="codeTips" type="success" size="mini" :disabled="codeDisabled"></u-button>
  31. <u-code ref="uCode" @change="codeChange" seconds="60" @start="codeDisabled = true" @end="codeDisabled = false"></u-code>
  32. </u-form-item>
  33. <view class="btn-group">
  34. <u-button type="primary" text="登录" customStyle="margin-top: 50px" @click="handleSubmit"></u-button>
  35. <u-gap height="20"></u-gap>
  36. <u-button type="info" text="返回" @click="navigateBack()"></u-button>
  37. </view>
  38. </u--form>
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. import { passwordLogin, sendSmsCode, smsLogin } from '../../api/auth'
  44. export default {
  45. data() {
  46. return {
  47. currentModeIndex: 0,
  48. loginModeList: ['密码登录', '验证码登录'],
  49. inputType: 'password',
  50. codeDisabled: false,
  51. codeTips: '',
  52. formData: {
  53. mobile: '',
  54. password: '',
  55. code: ''
  56. },
  57. rules: {
  58. mobile: [
  59. {
  60. type: 'integer',
  61. required: true,
  62. message: '请填写手机号',
  63. trigger: ['blur', 'change']
  64. },
  65. {
  66. // 自定义验证函数,见上说明
  67. validator: (rule, value, callback) => {
  68. // 上面有说,返回true表示校验通过,返回false表示不通过
  69. // uni.$u.test.mobile()就是返回true或者false的
  70. return uni.$u.test.mobile(value)
  71. },
  72. message: '手机号码不正确',
  73. // 触发器可以同时用blur和change
  74. trigger: ['change', 'blur']
  75. }
  76. ],
  77. password: {
  78. type: 'string',
  79. min: 4,
  80. max: 16,
  81. required: true,
  82. message: '密码长度4-16位密码',
  83. trigger: ['blur', 'change']
  84. },
  85. code: {
  86. type: 'integer',
  87. len: 4,
  88. required: true,
  89. message: '请填写4位验证码',
  90. trigger: ['blur', 'change']
  91. }
  92. }
  93. }
  94. },
  95. onLoad() {},
  96. onReady() {
  97. // 如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则
  98. this.$refs.form.setRules(this.rules)
  99. },
  100. methods: {
  101. handleModeChange(index) {
  102. if (index !== this.currentModeIndex) {
  103. this.currentModeIndex = index
  104. this.$refs.form.clearValidate()
  105. }
  106. },
  107. codeChange(text) {
  108. this.codeTips = text
  109. },
  110. getCode() {
  111. const mobile = this.formData.mobile
  112. if (!mobile) {
  113. uni.$u.toast('请填写手机号')
  114. } else if (!uni.$u.test.mobile(mobile)) {
  115. uni.$u.toast('手机号格式不正确')
  116. } else if (this.$refs.uCode.canGetCode) {
  117. // 模拟向后端请求验证码
  118. uni.showLoading({
  119. title: '正在获取验证码'
  120. })
  121. //scene:1登陆获取验证码场景
  122. sendSmsCode({ mobile: mobile, scene: 1 }).then(res => {
  123. //console.log(res)
  124. uni.hideLoading()
  125. uni.$u.toast('验证码已发送')
  126. // 通知验证码组件内部开始倒计时
  127. this.$refs.uCode.start()
  128. })
  129. } else {
  130. uni.$u.toast('倒计时结束后再发送')
  131. }
  132. },
  133. handleSubmit() {
  134. this.$refs.form.validate().then(res => {
  135. if (this.currentModeIndex === 0) {
  136. this.handleLoginPromise(passwordLogin({ mobile: this.formData.mobile, password: this.formData.password }))
  137. } else if (this.currentModeIndex === 1) {
  138. this.handleLoginPromise(smsLogin({ mobile: this.formData.mobile, code: this.formData.code }))
  139. }
  140. })
  141. },
  142. handleLoginPromise(promise) {
  143. promise.then(res => {
  144. this.$store.commit('setToken', res.data)
  145. uni.$u.toast('登录成功')
  146. setTimeout(() => {
  147. this.navigateBack()
  148. }, 1000)
  149. })
  150. },
  151. navigateBack() {
  152. uni.navigateBack()
  153. }
  154. }
  155. }
  156. </script>
  157. <style lang="scss" scoped>
  158. .unp-header {
  159. height: 400rpx;
  160. @include flex-center;
  161. .unp-logo {
  162. @include flex-center(column);
  163. }
  164. }
  165. .unp-box {
  166. @include flex-center(column);
  167. .mode-section {
  168. width: 560rpx;
  169. }
  170. .btn-group {
  171. width: 560rpx;
  172. }
  173. }
  174. .lk-group {
  175. height: 40rpx;
  176. margin-top: 40rpx;
  177. @include flex-space-between;
  178. font-size: 12rpx;
  179. color: $u-primary;
  180. text-decoration: $u-primary;
  181. }
  182. </style>