Register.vue 9.0 KB

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