login1.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <template>
  2. <div id="login-box">
  3. <div class="header">登录</div>
  4. <div class="main">
  5. <el-form ref="form" :model="form" :rules="rules">
  6. <el-form-item prop="username">
  7. <el-input
  8. v-model="form.username"
  9. placeholder="用户名/邮箱/手机号"
  10. class="cuborder-radius"
  11. maxlength="11"
  12. @keyup.enter.native="onSubmit('form')"
  13. />
  14. </el-form-item>
  15. <el-form-item prop="password">
  16. <el-input
  17. v-model="form.password"
  18. type="password"
  19. placeholder="密码"
  20. class="cuborder-radius"
  21. @keyup.enter.native="onSubmit('form')"
  22. />
  23. </el-form-item>
  24. <el-form-item prop="captchaCode">
  25. <el-row>
  26. <el-col :span=12>
  27. <el-input
  28. v-model="form.captchaCode"
  29. placeholder="图形验证码"
  30. class="cuborder-radius"
  31. maxlength="11"
  32. @keyup.enter.native="onSubmit('form')"
  33. />
  34. </el-col>
  35. <el-col :span=12>
  36. <img :src="captchaCode" alt="图形验证码" title="点击刷新" style="cursor:pointer;" @click="getCaptcha">
  37. </el-col>
  38. </el-row>
  39. </el-form-item>
  40. <el-form-item>
  41. <el-button
  42. type="primary"
  43. class="submit-btn"
  44. :loading="registerLoading"
  45. @click="onSubmit('form')"
  46. >
  47. 登录
  48. </el-button>
  49. </el-form-item>
  50. <el-form-item>
  51. <div class="links">
  52. <el-link
  53. type="primary"
  54. :underline="false"
  55. @click="toLink('/sso/forget')"
  56. >
  57. 找回密码
  58. </el-link>
  59. <el-link
  60. type="primary"
  61. :underline="false"
  62. @click="toLink('/sso/register')"
  63. >没有账号?
  64. </el-link>
  65. </div>
  66. </el-form-item>
  67. </el-form>
  68. </div>
  69. </div>
  70. </template>
  71. <script>
  72. import { ServeCaptcha, ServeRegister, ServeSendVerifyCode } from '@/api/auth'
  73. import { isMobile } from '@/utils/validate'
  74. import SmsLock from '@/utils/sms-lock'
  75. export default {
  76. data() {
  77. let validateMobile = (rule, value, callback) => {
  78. if (value === '') {
  79. callback(new Error('手机号不能为空!'))
  80. return
  81. }
  82. if (!isMobile(value)) {
  83. callback(new Error('手机号格式不正确!'))
  84. } else {
  85. callback()
  86. }
  87. }
  88. let validatePass2 = (rule, value, callback) => {
  89. if (value === '') {
  90. callback(new Error('请再次输入密码'))
  91. } else if (value !== this.form.password) {
  92. callback(new Error('两次输入密码不一致!'))
  93. } else {
  94. callback()
  95. }
  96. }
  97. return {
  98. captchaCode: '',
  99. registerLoading: false,
  100. form: {
  101. username: '',
  102. password: '',
  103. mobile: '',
  104. smsCode: '',
  105. captchaCode: '',
  106. plat: ''
  107. },
  108. rules: {
  109. username: [
  110. {
  111. required: true,
  112. message: '用户名不能为空!',
  113. trigger: 'blur',
  114. },
  115. ],
  116. mobile: [
  117. {
  118. validator: validateMobile,
  119. trigger: 'blur',
  120. },
  121. ],
  122. smsCode: [
  123. {
  124. required: true,
  125. message: '短信验证码不能为空!',
  126. trigger: 'blur',
  127. },
  128. ],
  129. },
  130. smsLock: false,
  131. smsLockObj: null,
  132. }
  133. },
  134. created() {
  135. this.getCaptcha()
  136. this.smsLockObj = new SmsLock('REGISTER_SMS', 60)
  137. },
  138. destroyed() {
  139. this.smsLockObj.clearInterval()
  140. },
  141. methods: {
  142. getCaptcha() {
  143. ServeCaptcha().then(res => {
  144. if (res.code === 0) {
  145. this.captchaCode = res.data
  146. } else {
  147. this.$notify.info({
  148. title: '提示',
  149. message: '获取 Captcha 失败, 请重新刷新页面...',
  150. })
  151. }
  152. }).finally(() => {
  153. })
  154. },
  155. randomString(len) {
  156. len = len || 16
  157. var t = '012345678ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz'
  158. var a = t.length
  159. var res = ''
  160. for (var i = 0; i < len; i++) res += t.charAt(Math.floor(Math.random() * a))
  161. return res
  162. },
  163. resetFields() {
  164. if (!this.model) {
  165. console.warn('[Element Warn][Form]model is required for resetFields to work.');
  166. return;
  167. }
  168. this.fields.forEach(field => {
  169. field.resetField();
  170. });
  171. },
  172. toLink(url) {
  173. this.$router.push({
  174. path: url,
  175. })
  176. },
  177. onSubmit(formName) {
  178. if (this.registerLoading) return false
  179. this.$refs[formName].validate(valid => {
  180. if (!valid) return false
  181. this.registerLoading = true
  182. this.register()
  183. })
  184. },
  185. register() {
  186. ServeRegister(
  187. this.form
  188. ).then(res => {
  189. if (res.code === 0) {
  190. this.$notify({
  191. title: '成功',
  192. message: '注册成功,快去登录吧...',
  193. type: 'success',
  194. })
  195. this.$refs.form.resetFields()
  196. setTimeout(() => {
  197. this.toLink('/sso/login')
  198. }, 1500)
  199. } else {
  200. this.$notify.info({
  201. title: '提示',
  202. message: res.message,
  203. })
  204. }
  205. }).catch((e) => {
  206. console.log(e)
  207. this.$notify({
  208. message: '网络错误,请稍后再试...',
  209. })
  210. }).finally(() => {
  211. this.registerLoading = false
  212. })
  213. },
  214. // 点击发送验证码
  215. sendSms() {
  216. if (this.smsLock) return false
  217. if (!isMobile(this.form.mobile)) {
  218. this.$refs.form.validateField('mobile', (val) => {
  219. return !val;
  220. })
  221. }
  222. this.smsLock = true
  223. ServeSendVerifyCode({
  224. receiver: this.form.mobile,
  225. channel: 'register',
  226. }).then(res => {
  227. if (res.code === 0) {
  228. this.$notify({
  229. title: '成功',
  230. message: '验证码发送成功...',
  231. type: 'success',
  232. })
  233. this.smsLockObj.start()
  234. /*if (res.data.is_debug) {
  235. setTimeout(() => {
  236. this.$notify({
  237. title: '提示',
  238. message: '已自动填充验证码',
  239. })
  240. this.form.sms_code = res.data.sms_code
  241. }, 500)
  242. }*/
  243. } else {
  244. this.$notify({
  245. title: '提示',
  246. message: res.message,
  247. customClass: 'cus-notifyclass',
  248. })
  249. }
  250. }).finally(() => {
  251. this.smsLock = false
  252. })
  253. },
  254. },
  255. }
  256. </script>
  257. <style lang="scss">
  258. @import '~@/assets/css/login-auth.scss';
  259. </style>