register-from.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <div>
  3. <v-row justify="center">
  4. <v-col cols="10">
  5. <v-text-field
  6. v-model="userRegistry.username"
  7. placeholder="请输入用户名"
  8. label="用户名"
  9. :rules="[() => userRegistry.username != null || '用户名不能为空']"
  10. clearable
  11. @input="checkUsername"
  12. @blur="selectUsername"
  13. />
  14. </v-col>
  15. </v-row>
  16. <v-row justify="center">
  17. <v-col cols="10">
  18. <v-text-field
  19. v-model="userRegistry.email"
  20. placeholder="请输入邮箱"
  21. label="邮箱"
  22. :rules="[() => userRegistry.email != null || '邮箱不能为空']"
  23. type="email"
  24. clearable
  25. @input="checkEmail"
  26. @blur="selectEmail"
  27. />
  28. </v-col>
  29. </v-row>
  30. <v-row justify="center">
  31. <v-col cols="10">
  32. <v-row justify="center">
  33. <v-col cols="5">
  34. <v-text-field
  35. v-model="userRegistry.regVerifyCode"
  36. placeholder="请输入邮件验证码"
  37. label="邮件验证码"
  38. :rules="[() => userRegistry.regVerifyCode != null || '邮件验证码不能为空']"
  39. type="email"
  40. clearable
  41. />
  42. </v-col>
  43. <v-col cols="5">
  44. <v-btn color="primary" @click="getVerifyCode">获取邮件验证码</v-btn>
  45. </v-col>
  46. </v-row>
  47. </v-col>
  48. </v-row>
  49. <v-row justify="center">
  50. <v-col cols="10">
  51. <v-text-field
  52. v-model="userRegistry.password"
  53. placeholder="请输入密码"
  54. label="密码"
  55. :rules="[() => userRegistry.password != null || '密码不能为空']"
  56. clearable
  57. type="password"
  58. />
  59. </v-col>
  60. </v-row>
  61. <v-row justify="center">
  62. <v-col cols="5">
  63. <img :src="captchaUrl" alt="验证码" title="点击刷新" style="cursor:pointer;" @click="getCaptcha">
  64. <!--<img :src="captchaBase64" alt="验证码" title="点击刷新" style="cursor:pointer;" @click="getCaptcha">-->
  65. </v-col>
  66. <v-col cols="5">
  67. <v-text-field
  68. v-model="userRegistry.captcha"
  69. placeholder="验证码"
  70. label="验证码"
  71. :rules="[() => userRegistry.captcha != null || '验证码不能为空']"
  72. clearable
  73. />
  74. </v-col>
  75. </v-row>
  76. <v-row justify="center">
  77. <v-btn color="primary" @click="submitRegister">注册</v-btn>
  78. </v-row>
  79. </div>
  80. </template>
  81. <script>
  82. import { randomString, getCaptchaUrl } from '@/utils'
  83. import { getRegVerifyCode, isUsernameExist, selectUsername, isEmailRegistered, selectEmail } from '@/api/user/account'
  84. export default {
  85. name: 'Register',
  86. data() {
  87. return {
  88. captchaUrl: '',
  89. captchaBase64: '',
  90. r: '',
  91. showMessage: false,
  92. userRegistry: {
  93. regType: 1,
  94. username: '',
  95. email: '',
  96. password: '',
  97. regVerifyCode: '',
  98. captcha: '',
  99. r: ''
  100. },
  101. verifyCode: {
  102. type: '',
  103. receiver: ''
  104. }
  105. }
  106. },
  107. created() {
  108. this.getCaptcha()
  109. },
  110. methods: {
  111. checkUsername() {
  112. isUsernameExist(this.userRegistry.username)
  113. .then(res => {
  114. if (res.code === 0) {
  115. console.log(res.msg)
  116. } else {
  117. alert(res.data)
  118. }
  119. })
  120. .catch(error => {
  121. console.error(error.message)
  122. })
  123. },
  124. selectUsername() {
  125. selectUsername(this.userRegistry.username).then(res => {
  126. if (res.code === 0) {
  127. console.log(res.msg)
  128. } else {
  129. alert(res.data)
  130. }
  131. })
  132. .catch(error => {
  133. console.error(error.message)
  134. })
  135. },
  136. checkEmail() {
  137. console.log('检查邮箱')
  138. isEmailRegistered(this.userRegistry.email)
  139. .then(res => {
  140. if (res.code === 0) {
  141. console.log(res.msg)
  142. } else {
  143. alert(res.data)
  144. }
  145. })
  146. .catch(error => {
  147. console.error(error.message)
  148. })
  149. },
  150. selectEmail() {
  151. selectEmail(this.userRegistry.email).then(res => {
  152. if (res.code === 0) {
  153. console.log(res.msg)
  154. } else {
  155. alert(res.data)
  156. }
  157. }).catch(error => {
  158. console.error(error.message)
  159. })
  160. },
  161. submitRegister() {
  162. var re = /^(([^()[\]\\.,;:\s@\"]+(\.[^()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  163. if (!re.test(this.userRegistry.email)) {
  164. console.log('email 字段不符合要求')
  165. return
  166. } else if (this.userRegistry.password === '' || this.userRegistry.password.length < 6) {
  167. console.log('password 字段不符合要求')
  168. return
  169. } else if (this.userRegistry.captcha === '' || this.userRegistry.username === '') {
  170. alert('captcha 或 username 字段不符合要求')
  171. return
  172. }
  173. if (this.$store.state.webInfo.openInvitationRegister === 1 && this.userRegistry.invitationCode === '') {
  174. console.log('openInvitationRegister 或 invitationCode 字段不符合要求')
  175. return
  176. }
  177. // 返回到父组件
  178. this.$emit('register', this.userRegistry)
  179. },
  180. getCaptcha() {
  181. this.r = randomString(10)
  182. // 图片上发送点击事件时,captchaUrl 的值发生变化,然后才去请求后端
  183. this.captchaUrl = getCaptchaUrl(this.r)
  184. /* getBase64Captcha(this.captchaUrl)
  185. .then(res => {
  186. if (res.code === 0) {
  187. this.captchaBase64 = 'data:image/jpg;base64,' + res.data
  188. } else {
  189. alert(res.data)
  190. }
  191. })
  192. .catch(error => {
  193. console.error(error.message)
  194. })*/
  195. },
  196. getVerifyCode() {
  197. const email = this.userRegistry.email
  198. if (email == null || email === '') {
  199. alert('请输入邮箱地址')
  200. return
  201. }
  202. this.verifyCode.receiver = email
  203. this.verifyCode.type = 1
  204. getRegVerifyCode(this.verifyCode)
  205. .then(res => {
  206. if (res.code === 0) {
  207. alert(res.msg)
  208. } else {
  209. this.$notify({
  210. title: res.code,
  211. message: res.msg,
  212. type: 'warning',
  213. duration: 500
  214. })
  215. }
  216. })
  217. .catch(error => {
  218. console.error(error.message)
  219. })
  220. }
  221. }
  222. }
  223. </script>
  224. <style>
  225. </style>