login.vue 3.8 KB

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