login.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <view class="u-page">
  3. <u--form labelPosition="left" :model="model1" ref="form1">
  4. <u-form-item label="电话" prop="phone" borderBottom ref="item1">
  5. <u--input v-model="model1.phone" border="none" placeholder="电话"></u--input>
  6. </u-form-item>
  7. <u-form-item label="验证码" prop="code" labelWidth="80" borderBottom>
  8. <u--input v-model="model1.code" border="none" placeholder="请填写验证码"></u--input>
  9. <u-button slot="right" @tap="getCode" :text="tips" type="success" size="mini" :disabled="disabled1">
  10. </u-button>
  11. </u-form-item>
  12. <u-code ref="uCode" @change="codeChange" seconds="20" @start="disabled1 = true" @end="disabled1 = false">
  13. </u-code>
  14. </u--form>
  15. <u-button type="primary" text="提交" customStyle="margin-top: 50px" @click="submit"></u-button>
  16. <u-loading-page :loading="isLoading" bg-color="#e8e8e8"></u-loading-page>
  17. </view>
  18. </template>
  19. <script>
  20. import helper from '@/common/helper.js';
  21. export default {
  22. data() {
  23. return {
  24. isLoading: false,
  25. disabled1: false,
  26. tips: '',
  27. showCalendar: false,
  28. showBirthday: false,
  29. model1: {
  30. phone: '13333333333',
  31. code: '123456'
  32. },
  33. rules: {
  34. code: {
  35. type: 'string',
  36. required: true,
  37. len: 6,
  38. message: '请填写4位验证码',
  39. trigger: ['blur']
  40. },
  41. }
  42. }
  43. },
  44. onReady() {
  45. // 如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则
  46. this.$refs.form1.setRules(this.rules)
  47. },
  48. methods: {
  49. codeChange(text) {
  50. this.tips = text;
  51. },
  52. getCode() {
  53. if (this.$refs.uCode.canGetCode) {
  54. // 模拟向后端请求验证码
  55. uni.showLoading({
  56. title: '正在获取验证码'
  57. })
  58. setTimeout(() => {
  59. uni.hideLoading();
  60. // 这里此提示会被this.start()方法中的提示覆盖
  61. uni.$u.toast('验证码已发送');
  62. // 通知验证码组件内部开始倒计时
  63. this.$refs.uCode.start();
  64. }, 2000);
  65. } else {
  66. uni.$u.toast('倒计时结束后再发送');
  67. }
  68. },
  69. submit() {
  70. let that = this
  71. // 如果有错误,会在catch中返回报错信息数组,校验通过则在then中返回true
  72. this.$refs.form1.validate().then(res => {
  73. uni.$u.toast('校验通过')
  74. this.isLoading = true
  75. that.$request.baseRequest('get', '/commonUser/loginVerifyCode', {
  76. phone: that.model1.phone,
  77. verifyCode: that.model1.code
  78. }).then(res => {
  79. that.$request.TokenRequest('post', '/commonUser/api/loginQuickly', {
  80. mobilePhone: that.model1.phone,
  81. veriCode: that.model1.code
  82. }).then(res1 => {
  83. uni.setStorageSync('pcUserInfo', res1.data)
  84. uni.setStorageSync('userInfo', res.data)
  85. helper.getListByUserId()
  86. that.$store.commit('login', res.data)
  87. // that.liangxinLogin()
  88. uni.switchTab({
  89. url: '/pages/index/index'
  90. });
  91. this.isLoading = false
  92. })
  93. })
  94. .catch(res => {
  95. uni.showToast({
  96. title: res.message,
  97. icon: 'none',
  98. duration: 2000
  99. })
  100. });
  101. }).catch(errors => {
  102. uni.$u.toast('校验失败')
  103. })
  104. },
  105. reset() {
  106. const validateList = ['userInfo.name', 'userInfo.sex', 'radiovalue1', 'checkboxValue1', 'intro',
  107. 'hotel', 'code', 'userInfo.birthday'
  108. ]
  109. this.$refs.form1.resetFields()
  110. this.$refs.form1.clearValidate()
  111. setTimeout(() => {
  112. this.$refs.form1.clearValidate(validateList)
  113. // 或者使用 this.$refs.form1.clearValidate()
  114. }, 10)
  115. },
  116. hideKeyboard() {
  117. uni.hideKeyboard()
  118. }
  119. },
  120. }
  121. </script>
  122. <style lang="scss">
  123. </style>