Password.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <template>
  2. <div class="container">
  3. <div class="close" @click="onClose" v-if="!email">
  4. <i class="el-icon-back"></i>
  5. </div>
  6. <el-form
  7. ref="loginForm"
  8. :model="form"
  9. :rules="rules"
  10. label-position="left">
  11. <el-form-item v-if="email">
  12. <div class="form-login-input no-border">
  13. <span class="svg-container">
  14. <svg-icon icon-class="user" />
  15. </span>
  16. <div class="input">&nbsp; {{email}}</div>
  17. </div>
  18. </el-form-item>
  19. <el-form-item prop="email" v-else>
  20. <div class="form-login-input">
  21. <span class="svg-container">
  22. <svg-icon icon-class="email" />
  23. </span>
  24. <el-input
  25. ref="username"
  26. v-model="form.email"
  27. name="email"
  28. type="text"
  29. tabindex="1"
  30. autocomplete="on"
  31. clearable
  32. maxlength="50"/>
  33. </div>
  34. </el-form-item>
  35. <el-form-item prop="password" label="Create Password">
  36. <div class="form-login-input">
  37. <span class="svg-container">
  38. <svg-icon icon-class="password" />
  39. </span>
  40. <el-input
  41. ref="password"
  42. v-model="form.password"
  43. name="password"
  44. :type="passwordType"
  45. tabindex="1"
  46. autocomplete="on"
  47. clearable
  48. maxlength="32"
  49. @input="text => applyStrength('', text)"/>
  50. <span class="show-pwd" @click="showPwd">
  51. <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
  52. </span>
  53. </div>
  54. </el-form-item>
  55. <div class="layout-strength">
  56. <div class="flexc">
  57. <span class="title-strength">Password Strength</span>
  58. <template v-for="item in 5">
  59. <span :class='strength >= item ? "strength" : "no-strength"'></span>
  60. </template>
  61. </div>
  62. <div class="strength-desc" v-if="enablePassword12">
  63. You Password Must Have: <br/>
  64. - 12 or more characters<br/>
  65. - upper and lower case letters<br/>
  66. - at least one number and special characters
  67. </div>
  68. <div class="strength-desc" v-else>
  69. You Password Must Have: <br/>
  70. - 8 or more characters<br/>
  71. - upper and lower case letters<br/>
  72. - at least one number
  73. </div>
  74. </div>
  75. <el-form-item prop="password2" label="Confirm Password">
  76. <div class="form-login-input">
  77. <span class="svg-container">
  78. <svg-icon icon-class="password" />
  79. </span>
  80. <el-input
  81. ref="password2"
  82. v-model="form.password2"
  83. name="password2"
  84. :type="passwordType"
  85. tabindex="1"
  86. autocomplete="on"
  87. clearable
  88. maxlength="32"/>
  89. </div>
  90. </el-form-item>
  91. <el-button
  92. :loading="loading"
  93. type="primary"
  94. class="login-button"
  95. @click="onSubmit">
  96. {{email ? "CREATE PASSWORD" : "RESET PASSWORD" }}
  97. </el-button>
  98. </el-form>
  99. </div>
  100. </template>
  101. <script>
  102. import settings from '../../settings.js'
  103. export default {
  104. name: "Password",
  105. props: {
  106. email: String
  107. },
  108. data() {
  109. return {
  110. loading: false,
  111. form: {
  112. email: "",
  113. password: "",
  114. password2: ""
  115. },
  116. rules: {
  117. email: [{
  118. required: true,
  119. trigger: 'blur',
  120. message: "Please type email"
  121. }, {
  122. pattern: /^[a-zA-Z0-9]+[\S]+@[a-zA-Z0-9_-]+[\.][\Sa-zA-Z]+$/,
  123. trigger: 'blur',
  124. message: 'Please type a correct email'
  125. }],
  126. password: [{
  127. required: true,
  128. trigger: 'blur',
  129. validator: this.applyStrength
  130. }],
  131. password2: [{
  132. required: true,
  133. trigger: 'blur',
  134. message: "Please type confirm password"
  135. }, {
  136. trigger: 'blur',
  137. validator: this.validatePassword
  138. }]
  139. },
  140. passwordType: "password",
  141. strength: 0,
  142. enablePassword12: settings.enablePassword12
  143. };
  144. },
  145. mounted() {
  146. },
  147. methods: {
  148. onClose() {
  149. this.$emit("back")
  150. },
  151. showPwd() {
  152. if (this.passwordType === 'password') {
  153. this.passwordType = 'text'
  154. } else {
  155. this.passwordType = 'password'
  156. }
  157. this.$nextTick(() => {
  158. this.$refs.password.focus()
  159. })
  160. },
  161. applyStrength(rule, text, callback) {
  162. var streng = 0;
  163. if (text) {
  164. if (text.length >= (settings.enablePassword12 ? 12 : 8)) {
  165. streng += 1;
  166. }
  167. if (/[a-z]{1,}/.test(text)) {
  168. streng += 1;
  169. }
  170. if (/[A-Z]{1,}/.test(text)) {
  171. streng += 1;
  172. }
  173. if (/\d{1,}/.test(text)) {
  174. streng += (settings.enablePassword12 ? 1 : 2);
  175. }
  176. if (/\W{1,}/.test(text)) {
  177. streng += 1;
  178. }
  179. if (callback) {
  180. if (streng < 5) {
  181. callback(new Error('The password is not strength'))
  182. } else {
  183. callback()
  184. }
  185. }
  186. } else if (callback) {
  187. callback(new Error('Please type password'))
  188. }
  189. this.strength = streng;
  190. },
  191. validatePassword(rule, value, callback) {
  192. if (value !== this.form.password) {
  193. callback(new Error('The twice passwords are inconsistent'))
  194. } else {
  195. callback()
  196. }
  197. },
  198. onSubmit() {
  199. this.$refs.loginForm.validate(valid => {
  200. if (valid) {
  201. if (this.strength >= 4) {
  202. if (this.email) {
  203. this.$emit("confirm", {
  204. type: "create",
  205. password: this.form.password
  206. });
  207. } else {
  208. this.$emit("confirm", {
  209. type: "reset",
  210. password: this.form.password
  211. });
  212. }
  213. } else {
  214. this.$message({
  215. type: 'error',
  216. message: "Your password is not strong"
  217. })
  218. }
  219. }
  220. })
  221. }
  222. }
  223. }
  224. </script>
  225. <style scoped lang="scss">
  226. @import '../../styles/element-ui.scss';
  227. .container {
  228. position: relative;
  229. }
  230. ::v-deep label.el-form-item__label {
  231. color: #333;
  232. &::before {
  233. display: none;
  234. }
  235. }
  236. .form-login-input {
  237. width: 100%;
  238. display: flex;
  239. padding: 0 10px;
  240. position: relative;
  241. align-items: center;
  242. border: 1px solid #eee;
  243. border-radius: 4px;
  244. &.no-border {
  245. border: none;
  246. }
  247. label {
  248. top: 0;
  249. left: 0;
  250. color: #aaa;
  251. font-size: 14px;
  252. position:absolute;
  253. padding-top: 7px;
  254. padding-left: 45px;
  255. font-weight: normal;
  256. pointer-events: none;
  257. transition: all 0.3s;
  258. }
  259. label.focus {
  260. top: -30px;
  261. color: #333;
  262. font-size: 15px;
  263. font-weight: bold;
  264. padding-left: 5px;
  265. }
  266. .svg-container {
  267. width: 30px;
  268. color: #333;
  269. font-size: 16px;
  270. padding: 0px 6px;
  271. }
  272. .el-input,.input {
  273. flex: 1;
  274. color: #333;
  275. height: 42px;
  276. font-size: 14px;
  277. &::v-deep input {
  278. color: #333;
  279. height: 42px;
  280. border: none;
  281. font-size: 14px;
  282. padding: 0 5px;
  283. border-radius: 0px;
  284. -webkit-appearance: none;
  285. background: transparent;
  286. }
  287. }
  288. }
  289. .layout-strength {
  290. padding-left: 16px;
  291. padding-bottom: 2px;
  292. }
  293. .title-strength {
  294. color: #333;
  295. font-size: 12px;
  296. padding-right: 10px;
  297. }
  298. .strength {
  299. width: 14px;
  300. height: 3px;
  301. margin: 3px;
  302. border-radius: 3px;
  303. background-color: $--color-accent;
  304. }
  305. .no-strength {
  306. width: 14px;
  307. height: 3px;
  308. margin: 3px;
  309. border-radius: 3px;
  310. background-color: #ccc;
  311. }
  312. .strength-desc {
  313. color: #999;
  314. font-size: 12px;
  315. padding-top: 5px;
  316. }
  317. .login-button {
  318. width:100%;
  319. color: #fff;
  320. padding: 15px;
  321. margin-top: 10px;
  322. margin-bottom: 10px;
  323. font-weight: bold;
  324. }
  325. .close {
  326. top: -43px;
  327. left: -5px;
  328. z-index: 10;
  329. padding: 5px;
  330. cursor: pointer;
  331. font-size: 16px;
  332. position: absolute;
  333. }
  334. .show-pwd {
  335. color: #666;
  336. cursor: pointer;
  337. }
  338. </style>