Register.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <template>
  2. <div class="main user-layout-register">
  3. <h3><span>注册</span></h3>
  4. <a-form ref="formRegister" :autoFormCreate="(form)=>{this.form = form}" id="formRegister">
  5. <a-form-item
  6. fieldDecoratorId="email"
  7. :fieldDecoratorOptions="{rules: [{ required: true, message: '请输入邮箱地址' }], validateTrigger: 'blur'}">
  8. <a-input size="large" type="text" placeholder="邮箱"></a-input>
  9. </a-form-item>
  10. <a-form-item
  11. fieldDecoratorId="password"
  12. :fieldDecoratorOptions="{rules: [{ required: true, message: '至少6位密码,区分大小写' }, { validator: this.handlePasswordLevel }], validateTrigger: ['change', 'blur']}">
  13. <a-popover placement="right" trigger="click" :visible="clicked" @visibleChange="clicked = true">
  14. <template slot="content">
  15. <div :style="{ width: '240px' }">
  16. <div :class="['user-register', passwordLevelClass]">强度:<span>{{ passwordLevelName }}</span></div>
  17. <a-progress :percent="state.percent" :showInfo="false" strokeColor="#FF0000" />
  18. <div style="margin-top: 10px;">
  19. <span>请至少输入 6 个字符。请不要使用容易被猜到的密码。</span>
  20. </div>
  21. </div>
  22. </template>
  23. <a-input size="large" type="password" placeholder="至少6位密码,区分大小写"></a-input>
  24. </a-popover>
  25. </a-form-item>
  26. <a-form-item
  27. fieldDecoratorId="password2"
  28. :fieldDecoratorOptions="{rules: [{ required: true, message: '至少6位密码,区分大小写' }, { validator: this.handlePasswordCheck }], validateTrigger: 'blur'}">
  29. <a-input size="large" type="password" placeholder="确认密码"></a-input>
  30. </a-form-item>
  31. <a-form-item
  32. fieldDecoratorId="mobile"
  33. :fieldDecoratorOptions="{rules: [{ required: true, message: '手机号' }], validateTrigger: 'blur'}">
  34. <a-input-group size="large" compact>
  35. <a-select style="width: 20%" size="large" defaultValue="+86">
  36. <a-select-option value="+86">+86</a-select-option>
  37. <a-select-option value="+87">+87</a-select-option>
  38. </a-select>
  39. <a-input style="width: 80%" placeholder="11 位手机号"></a-input>
  40. </a-input-group>
  41. </a-form-item>
  42. <a-row :gutter="16">
  43. <a-col class="gutter-row" :span="16">
  44. <a-form-item
  45. fieldDecoratorId="captcha"
  46. :fieldDecoratorOptions="{rules: [{ required: true, message: '请输入验证码' }], validateTrigger: 'blur'}">
  47. <a-input size="large" type="text" placeholder="验证码">
  48. <a-icon slot="prefix" type='mail' :style="{ color: 'rgba(0,0,0,.25)' }"/>
  49. </a-input>
  50. </a-form-item>
  51. </a-col>
  52. <a-col class="gutter-row" :span="8">
  53. <a-button
  54. class="getCaptcha"
  55. size="large"
  56. :disabled="state.smsSendBtn"
  57. @click.stop.prevent="getCaptcha"
  58. v-text="!state.smsSendBtn && '获取验证码'||(state.time+' s')"></a-button>
  59. </a-col>
  60. </a-row>
  61. <a-form-item>
  62. <a-button
  63. size="large"
  64. type="primary"
  65. htmlType="submit"
  66. class="register-button"
  67. :loading="registerBtn"
  68. @click.stop.prevent="handleSubmit"
  69. :disabled="registerBtn">注册
  70. </a-button>
  71. <router-link class="login" :to="{ name: 'login' }">使用已有账户登录</router-link>
  72. </a-form-item>
  73. </a-form>
  74. </div>
  75. </template>
  76. <script>
  77. import { getSmsCaptcha } from '@/api/login'
  78. const levelNames = {
  79. 0: '低',
  80. 1: '低',
  81. 2: '中',
  82. 3: '强'
  83. }
  84. const levelClass = {
  85. 0: 'error',
  86. 1: 'error',
  87. 2: 'warning',
  88. 3: 'success'
  89. }
  90. export default {
  91. name: "Register",
  92. components: {
  93. },
  94. data() {
  95. return {
  96. form: null,
  97. clicked: false,
  98. state: {
  99. time: 60,
  100. smsSendBtn: false,
  101. passwordLevel: 0,
  102. percent: 0,
  103. },
  104. registerBtn: false
  105. }
  106. },
  107. computed: {
  108. passwordLevelClass () {
  109. return levelClass[this.state.passwordLevel]
  110. },
  111. passwordLevelName () {
  112. return levelNames[this.state.passwordLevel]
  113. }
  114. },
  115. methods: {
  116. handlePasswordLevel (rule, value, callback) {
  117. let level = 0
  118. // 判断这个字符串中有没有数字
  119. if (/[0-9]/.test(value)) {
  120. level++
  121. }
  122. // 判断字符串中有没有字母
  123. if (/[a-zA-Z]/.test(value)) {
  124. level++
  125. }
  126. // 判断字符串中有没有特殊符号
  127. if (/[^0-9a-zA-Z_]/.test(value)) {
  128. level++
  129. }
  130. this.state.passwordLevel = level
  131. this.state.percent = level * 30
  132. console.log('passwordLevel', this.state.passwordLevel, 'level', level)
  133. if (level >= 2) {
  134. if (level >= 3) {
  135. this.state.percent = 100
  136. }
  137. callback()
  138. } else {
  139. callback(new Error('密码强度不够'))
  140. }
  141. },
  142. handlePasswordCheck (rule, value, callback) {
  143. let password = this.form.getFieldValue('password')
  144. if (value && password && value.trim() !== password.trim()) {
  145. callback(new Error('两次密码不一致'))
  146. }
  147. callback()
  148. },
  149. handleSubmit() {
  150. this.form.validateFields((err, values) => {
  151. if (!err) {
  152. this.$router.push({ name: 'registerResult', params: {...values} })
  153. }
  154. })
  155. },
  156. getCaptcha(e) {
  157. e.preventDefault()
  158. let that = this
  159. this.form.validateFields(['mobile'], {force: true},
  160. (err, values) => {
  161. if (!err) {
  162. this.state.smsSendBtn = true;
  163. let interval = window.setInterval(() => {
  164. if (that.state.time-- <= 0) {
  165. that.state.time = 60;
  166. that.state.smsSendBtn = false;
  167. window.clearInterval(interval);
  168. }
  169. }, 1000);
  170. const hide = this.$message.loading('验证码发送中..', 0);
  171. getSmsCaptcha({mobile: values.mobile}).then(res => {
  172. setTimeout(hide, 2500);
  173. this.$notification['success']({
  174. message: '提示',
  175. description: '验证码获取成功,您的验证码为:' + res.result.captcha,
  176. duration: 8
  177. })
  178. }).catch(err => {
  179. setTimeout(hide, 1);
  180. clearInterval(interval);
  181. that.state.time = 60;
  182. that.state.smsSendBtn = false;
  183. this.requestFailed(err);
  184. });
  185. }
  186. }
  187. );
  188. },
  189. requestFailed(err) {
  190. this.$notification['error']({
  191. message: '错误',
  192. description: ((err.response || {}).data || {}).message || "请求出现错误,请稍后再试",
  193. duration: 4,
  194. });
  195. this.registerBtn = false;
  196. },
  197. },
  198. watch: {
  199. 'state.passwordLevel' (val) {
  200. console.log(val)
  201. }
  202. }
  203. }
  204. </script>
  205. <style lang="scss">
  206. .user-register {
  207. &.error {
  208. color: #ff0000;
  209. }
  210. &.warning {
  211. color: #ff7e05;
  212. }
  213. &.success {
  214. color: #52c41a;
  215. }
  216. }
  217. </style>
  218. <style lang="scss" scoped>
  219. .user-layout-register {
  220. & > h3 {
  221. font-size: 16px;
  222. margin-bottom: 20px;
  223. }
  224. .getCaptcha {
  225. display: block;
  226. width: 100%;
  227. height: 40px;
  228. }
  229. .register-button {
  230. width: 50%;
  231. }
  232. .login {
  233. float: right;
  234. line-height: 40px;
  235. }
  236. }
  237. </style>