register.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <view class="register">
  3. <view class="title">欢迎注册</view>
  4. <uni-forms class="form" :modelValue="dataForm" :rules="rules" validate-trigger="bind" label-width="80px">
  5. <uni-forms-item name="userName" label="用户名">
  6. <uni-easyinput type="text" v-model="dataForm.userName" placeholder="用户名" />
  7. </uni-forms-item>
  8. <uni-forms-item name="nickName" label="昵称">
  9. <uni-easyinput type="text" v-model="dataForm.nickName" placeholder="昵称" />
  10. </uni-forms-item>
  11. <uni-forms-item name="password" label="密码">
  12. <uni-easyinput type="password" v-model="dataForm.password" placeholder="密码" />
  13. </uni-forms-item>
  14. <uni-forms-item name="corfirmPassword" label="确认密码">
  15. <uni-easyinput type="password" v-model="dataForm.corfirmPassword" placeholder="确认密码" />
  16. </uni-forms-item>
  17. <button class="btn-submit" @click="submit" type="warn">注册并登陆</button>
  18. </uni-forms>
  19. <navigator class="nav-login" url="/pages/login/login" >
  20. 返回登陆页面
  21. </navigator>
  22. </view>
  23. </template>
  24. <script>
  25. export default {
  26. data() {
  27. return {
  28. dataForm: {
  29. userName: '',
  30. nickName: '',
  31. password: '',
  32. corfirmPassword: ''
  33. },
  34. rules: {
  35. userName: {
  36. rules: [{
  37. required: true,
  38. errorMessage: '请输入用户名',
  39. }]
  40. },
  41. nickName: {
  42. rules: [{
  43. required: true,
  44. errorMessage: '请输入昵称',
  45. }]
  46. },
  47. password: {
  48. rules: [{
  49. required: true,
  50. errorMessage: '请输入密码',
  51. }]
  52. },
  53. corfirmPassword: {
  54. rules: [{
  55. required: true,
  56. errorMessage: '请输入确认密码',
  57. }, {
  58. validateFunction: (rule, value, data, callback) => {
  59. console.log("validateFunction")
  60. if (data.password != value) {
  61. callback('两次密码输入不一致')
  62. }
  63. return true;
  64. }
  65. }]
  66. }
  67. }
  68. }
  69. },
  70. methods: {
  71. submit() {
  72. this.$http({
  73. url: '/register',
  74. data: this.dataForm,
  75. method: 'POST'
  76. }).then(() => {
  77. uni.showToast({
  78. title: "注册成功,您已成为盒子IM的用户",
  79. icon: 'none'
  80. })
  81. this.login();
  82. })
  83. },
  84. login() {
  85. const loginForm = {
  86. terminal: this.$enums.TERMINAL_TYPE.APP,
  87. userName: this.dataForm.userName,
  88. password: this.dataForm.password
  89. }
  90. this.$http({
  91. url: '/login',
  92. data: loginForm,
  93. method: 'POST'
  94. }).then((loginInfo) => {
  95. console.log("登录成功,自动跳转到聊天页面...")
  96. uni.setStorageSync("userName", loginForm.userName);
  97. uni.setStorageSync("password", loginForm.password);
  98. loginInfo.expireTime = new Date().getTime() + loginInfo.refreshTokenExpiresIn * 1000;
  99. uni.setStorageSync("loginInfo", loginInfo);
  100. // 调用App.vue的初始化方法
  101. getApp().init()
  102. // 跳转到聊天页面
  103. uni.switchTab({
  104. url: "/pages/chat/chat"
  105. })
  106. })
  107. }
  108. }
  109. }
  110. </script>
  111. <style lang="scss" scoped>
  112. .register {
  113. .title {
  114. padding-top: 150rpx;
  115. padding-bottom: 50rpx;
  116. color: royalblue;
  117. text-align: center;
  118. font-size: 60rpx;
  119. font-weight: 600;
  120. }
  121. .form {
  122. padding: 50rpx;
  123. .btn-submit {
  124. margin-top: 80rpx;
  125. border-radius: 50rpx;
  126. }
  127. }
  128. .nav-login {
  129. position: fixed;
  130. width: 100%;
  131. bottom: 100rpx;
  132. color: royalblue;
  133. text-align: center;
  134. font-size: 32rpx;
  135. }
  136. }
  137. </style>