login.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 '../../common/api'
  44. export default {
  45. data() {
  46. return {
  47. //租户ID
  48. agent: 1,
  49. currentModeIndex: 0,
  50. loginModeList: ['密码登录', '验证码登录'],
  51. inputType: 'password',
  52. codeDisabled: false,
  53. codeTips: '',
  54. formData: {
  55. mobile: '15601691234',
  56. password: '',
  57. code: ''
  58. },
  59. rules: {
  60. mobile: [
  61. {
  62. type: 'integer',
  63. required: true,
  64. message: '请填写手机号',
  65. trigger: ['blur', 'change']
  66. },
  67. {
  68. // 自定义验证函数,见上说明
  69. validator: (rule, value, callback) => {
  70. // 上面有说,返回true表示校验通过,返回false表示不通过
  71. // uni.$u.test.mobile()就是返回true或者false的
  72. return uni.$u.test.mobile(value)
  73. },
  74. message: '手机号码不正确',
  75. // 触发器可以同时用blur和change
  76. trigger: ['change', 'blur']
  77. }
  78. ],
  79. password: {
  80. type: 'string',
  81. min: 4,
  82. max: 16,
  83. required: true,
  84. message: '密码长度4-16位密码',
  85. trigger: ['blur', 'change']
  86. },
  87. code: {
  88. type: 'integer',
  89. len: 4,
  90. required: true,
  91. message: '请填写4位验证码',
  92. trigger: ['blur', 'change']
  93. }
  94. }
  95. }
  96. },
  97. onLoad() {},
  98. onReady() {
  99. // 如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则
  100. this.$refs.form.setRules(this.rules)
  101. },
  102. methods: {
  103. handleModeChange(index) {
  104. if (index !== this.currentModeIndex) {
  105. this.currentModeIndex = index
  106. this.$refs.form.clearValidate()
  107. }
  108. },
  109. codeChange(text) {
  110. this.codeTips = text
  111. },
  112. getCode() {
  113. const mobile = this.formData.mobile
  114. if (!mobile) {
  115. uni.$u.toast('请填写手机号')
  116. } else if (!uni.$u.test.mobile(mobile)) {
  117. uni.$u.toast('手机号格式不正确')
  118. } else if (this.$refs.uCode.canGetCode) {
  119. // 模拟向后端请求验证码
  120. uni.showLoading({
  121. title: '正在获取验证码'
  122. })
  123. //scene:1登陆获取验证码场景
  124. sendSmsCode({ agent: 1, mobile: mobile, scene: 1 })
  125. .then(res => {
  126. //console.log(res)
  127. uni.hideLoading()
  128. if (res.data.code === 0) {
  129. // 这里此提示会被this.start()方法中的提示覆盖
  130. uni.$u.toast('验证码已发送')
  131. // 通知验证码组件内部开始倒计时
  132. this.$refs.uCode.start()
  133. } else {
  134. uni.$u.toast(res.data.msg)
  135. }
  136. })
  137. .catch(err => {
  138. uni.$u.toast('服务器接口请求异常')
  139. })
  140. } else {
  141. uni.$u.toast('倒计时结束后再发送')
  142. }
  143. },
  144. handleSubmit() {
  145. this.$refs.form.validate().then(res => {
  146. uni.$u.toast('登录')
  147. if (this.currentModeIndex === 0) {
  148. passwordLogin({ agent: 1, mobile: this.formData.mobile, password: this.formData.password })
  149. .then(res => {
  150. if (res.data.code === 0) {
  151. uni.$u.toast('登录成功')
  152. // TODO 登录成功,保存toke
  153. } else {
  154. uni.$u.toast(res.data.msg)
  155. // TODO 登录失败
  156. }
  157. })
  158. .catch(err => {
  159. uni.$u.toast('服务器接口请求异常')
  160. })
  161. } else if (this.currentModeIndex === 1) {
  162. smsLogin({ agent: 1, mobile: this.formData.mobile, code: this.formData.code })
  163. }
  164. })
  165. },
  166. navigateBack() {
  167. uni.navigateBack()
  168. }
  169. }
  170. }
  171. </script>
  172. <style lang="scss" scoped>
  173. .unp-header {
  174. height: 400rpx;
  175. @include flex-center;
  176. .unp-logo {
  177. @include flex-center(column);
  178. }
  179. }
  180. .unp-box {
  181. @include flex-center(column);
  182. .mode-section {
  183. width: 560rpx;
  184. }
  185. .btn-group {
  186. width: 560rpx;
  187. }
  188. }
  189. .lk-group {
  190. height: 40rpx;
  191. margin-top: 40rpx;
  192. @include flex-space-between;
  193. font-size: 12rpx;
  194. color: $u-primary;
  195. text-decoration: $u-primary;
  196. }
  197. </style>