register.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <!-- 账号注册页 -->
  2. <template>
  3. <view class="uni-content">
  4. <match-media :min-width="690">
  5. <view class="login-logo">
  6. <image :src="logo"></image>
  7. </view>
  8. <!-- 顶部文字 -->
  9. <text class="title title-box">用户名密码注册</text>
  10. </match-media>
  11. <uni-forms ref="form" :value="formData" :rules="rules" validate-trigger="submit" err-show-type="toast">
  12. <uni-forms-item name="username" required>
  13. <uni-easyinput :inputBorder="false" :focus="focusUsername" @blur="focusUsername = false"
  14. class="input-box" placeholder="请输入用户名" v-model="formData.username" trim="both" />
  15. </uni-forms-item>
  16. <uni-forms-item name="nickname">
  17. <uni-easyinput :inputBorder="false" :focus="focusNickname" @blur="focusNickname = false"
  18. class="input-box" placeholder="请输入用户昵称" v-model="formData.nickname" trim="both" />
  19. </uni-forms-item>
  20. <uni-forms-item name="password" v-model="formData.password" required>
  21. <uni-easyinput :inputBorder="false" :focus="focusPassword" @blur="focusPassword = false"
  22. class="input-box" maxlength="20"
  23. :placeholder="'请输入' + (config.passwordStrength == 'weak'?'6':'8') + '-16位密码'" type="password"
  24. v-model="formData.password" trim="both" />
  25. </uni-forms-item>
  26. <uni-forms-item name="password2" v-model="formData.password2" required>
  27. <uni-easyinput :inputBorder="false" :focus="focusPassword2" @blur="focusPassword2 =false"
  28. class="input-box" placeholder="再次输入密码" maxlength="20" type="password" v-model="formData.password2"
  29. trim="both" />
  30. </uni-forms-item>
  31. <uni-forms-item>
  32. <uni-captcha ref="captcha" scene="register" v-model="formData.captcha" />
  33. </uni-forms-item>
  34. <uni-id-pages-agreements scope="register" ref="agreements"></uni-id-pages-agreements>
  35. <button class="uni-btn" type="primary" @click="submit">注册</button>
  36. <button @click="navigateBack" class="register-back">返回</button>
  37. <match-media :min-width="690">
  38. <view class="link-box">
  39. <text class="link" @click="registerByEmail">邮箱验证码注册</text>
  40. <text class="link" @click="toLogin">已有账号?点此登录</text>
  41. </view>
  42. </match-media>
  43. </uni-forms>
  44. </view>
  45. </template>
  46. <script>
  47. import rules from './validator.js';
  48. import mixin from '@/uni_modules/uni-id-pages/common/login-page.mixin.js';
  49. import config from '@/uni_modules/uni-id-pages/config.js'
  50. import {
  51. store,
  52. mutations
  53. } from '@/uni_modules/uni-id-pages/common/store.js'
  54. const uniIdCo = uniCloud.importObject("uni-id-co")
  55. export default {
  56. mixins: [mixin],
  57. data() {
  58. return {
  59. formData: {
  60. username: "",
  61. nickname: "",
  62. password: "",
  63. password2: "",
  64. captcha: ""
  65. },
  66. rules,
  67. focusUsername: false,
  68. focusNickname: false,
  69. focusPassword: false,
  70. focusPassword2: false,
  71. logo: "/static/logo.png"
  72. }
  73. },
  74. onReady() {
  75. this.$refs.form.setRules(this.rules)
  76. },
  77. onShow() {
  78. // #ifdef H5
  79. document.onkeydown = event => {
  80. let e = event || window.event;
  81. if (e && e.keyCode == 13) { //回车键的键值为13
  82. this.submit()
  83. }
  84. };
  85. // #endif
  86. },
  87. methods: {
  88. /**
  89. * 触发表单提交
  90. */
  91. submit() {
  92. this.$refs.form.validate().then((res) => {
  93. if (this.formData.captcha.length != 4) {
  94. this.$refs.captcha.focusCaptchaInput = true
  95. return uni.showToast({
  96. title: '请输入验证码',
  97. icon: 'none',
  98. duration: 3000
  99. });
  100. }
  101. if (this.needAgreements && !this.agree) {
  102. return this.$refs.agreements.popup(() => {
  103. this.submitForm(res)
  104. })
  105. }
  106. this.submitForm(res)
  107. }).catch((errors) => {
  108. let key = errors[0].key
  109. key = key.replace(key[0], key[0].toUpperCase())
  110. this['focus' + key] = true
  111. })
  112. },
  113. submitForm(params) {
  114. uniIdCo.registerUser(this.formData).then(e => {
  115. this.loginSuccess(e)
  116. })
  117. .catch(e => {
  118. console.log(e.message);
  119. //更好的体验:登录错误,直接刷新验证码
  120. this.$refs.captcha.getImageCaptcha()
  121. })
  122. },
  123. navigateBack() {
  124. uni.navigateBack()
  125. },
  126. toLogin() {
  127. uni.navigateTo({
  128. url: '/uni_modules/uni-id-pages/pages/login/login-withpwd'
  129. })
  130. },
  131. registerByEmail() {
  132. uni.navigateTo({
  133. url: '/uni_modules/uni-id-pages/pages/register/register-by-email'
  134. })
  135. }
  136. }
  137. }
  138. </script>
  139. <style lang="scss">
  140. @import "@/uni_modules/uni-id-pages/common/login-page.scss";
  141. @media screen and (max-width: 690px) {
  142. .uni-content {
  143. margin-top: 15px;
  144. height: 100%;
  145. background-color: #fff;
  146. }
  147. }
  148. @media screen and (min-width: 690px) {
  149. .uni-content {
  150. padding: 30px 40px 60px;
  151. max-height: 530px;
  152. }
  153. .link-box {
  154. /* #ifndef APP-NVUE */
  155. display: flex;
  156. /* #endif */
  157. flex-direction: row;
  158. justify-content: space-between;
  159. margin-top: 10px;
  160. }
  161. .link {
  162. font-size: 12px;
  163. }
  164. }
  165. .uni-content ::v-deep .uni-forms-item__label {
  166. position: absolute;
  167. left: -15px;
  168. }
  169. button {
  170. margin-top: 15px;
  171. }
  172. </style>