index.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <template>
  2. <div class="login-container">
  3. <el-form
  4. ref="loginForm"
  5. :model="loginForm"
  6. :rules="loginRules"
  7. class="login-form"
  8. autocomplete="on"
  9. label-position="left"
  10. >
  11. <div class="title-container">
  12. <h3 class="title">游戏久 - 魅族充值平台</h3>
  13. </div>
  14. <el-form-item prop="mobile">
  15. <span class="svg-container">
  16. <svg-icon icon-class="user" />
  17. </span>
  18. <el-input
  19. ref="mobile"
  20. v-model="loginForm.mobile"
  21. placeholder="手机号"
  22. name="mobile"
  23. type="text"
  24. tabindex="1"
  25. maxlength="11"
  26. autocomplete="on"
  27. :clearable="true"
  28. />
  29. </el-form-item>
  30. <el-form-item prop="smsCode">
  31. <el-row :gutter="30">
  32. <el-col :span="1">
  33. <span class="svg-container"><svg-icon icon-class="message" /></span>
  34. </el-col>
  35. <el-col :span="10">
  36. <el-input
  37. ref="smsCode"
  38. v-model="loginForm.smsCode"
  39. placeholder="验证码"
  40. name="smsCode"
  41. type="text"
  42. tabindex="2"
  43. maxlength="6"
  44. autocomplete="off"
  45. :clearable="true"
  46. />
  47. </el-col>
  48. <el-col :span="5" :push="5" style="top: 6px">
  49. <el-button
  50. v-if="times_show"
  51. type="primary"
  52. @click="getCode"
  53. >获取验证码</el-button>
  54. <el-button v-if="!times_show" disabled>{{ times }}s</el-button>
  55. </el-col>
  56. </el-row>
  57. </el-form-item>
  58. <el-button
  59. :loading="loading"
  60. type="primary"
  61. style="width: 100%; margin-bottom: 30px"
  62. @click.native.prevent="handleLogin"
  63. >登录</el-button>
  64. </el-form>
  65. </div>
  66. </template>
  67. <script>
  68. import { validMobile } from '@/utils/validate'
  69. import { Message } from 'element-ui'
  70. export default {
  71. name: 'Login',
  72. data() {
  73. const validateMobile = (rule, value, callback) => {
  74. if (!validMobile(value)) {
  75. callback(new Error('手机号格式有误.'))
  76. } else {
  77. callback()
  78. }
  79. }
  80. const validateSmsCode = (rule, value, callback) => {
  81. if (value.length < 6) {
  82. callback(new Error('请输入 6 位验证码.'))
  83. } else {
  84. callback()
  85. }
  86. }
  87. return {
  88. loginForm: {
  89. mobile: '',
  90. smsCode: ''
  91. },
  92. loginRules: {
  93. mobile: [
  94. { required: true, trigger: 'blur', validator: validateMobile }
  95. ],
  96. smsCode: [
  97. { required: true, trigger: 'blur', validator: validateSmsCode }
  98. ]
  99. },
  100. loading: false,
  101. redirect: undefined,
  102. otherQuery: {},
  103. // 计时器
  104. times: 0,
  105. times_show: true
  106. }
  107. },
  108. watch: {
  109. $route: {
  110. handler: function(route) {
  111. const query = route.query
  112. if (query) {
  113. this.redirect = query.redirect
  114. this.otherQuery = this.getOtherQuery(query)
  115. }
  116. },
  117. immediate: true
  118. }
  119. },
  120. methods: {
  121. // 获取验证码
  122. getCode() {
  123. this.$store.dispatch('common/getSmsCode', this.loginForm.mobile).then((res) => {
  124. const { code, message } = res
  125. if (code === 200) {
  126. this.times = 10
  127. this.times_show = false
  128. this.timer = setInterval(() => {
  129. this.times--
  130. if (this.times === 0) {
  131. this.times_show = true
  132. clearInterval(this.timer)
  133. }
  134. }, 1000)
  135. }
  136. return Message({
  137. message: message,
  138. type: 'warning',
  139. duration: 5 * 1000
  140. })
  141. }).catch((error) => {
  142. return Message({
  143. message: error.message,
  144. type: 'error',
  145. duration: 5 * 1000
  146. })
  147. })
  148. },
  149. // 登录
  150. handleLogin() {
  151. this.$refs.loginForm.validate((valid) => {
  152. if (valid) {
  153. this.loading = true
  154. this.$store.dispatch('user/login', this.loginForm).then((res) => {
  155. const { code, message } = res
  156. if (code === 200) {
  157. Message({
  158. message: message,
  159. type: 'warning',
  160. duration: 5 * 1000
  161. })
  162. this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
  163. } else {
  164. Message({
  165. message: message,
  166. type: 'error',
  167. duration: 5 * 1000
  168. })
  169. }
  170. this.loading = false
  171. }).catch((error) => {
  172. this.loading = false
  173. return Message({
  174. message: error.message,
  175. type: 'error',
  176. duration: 5 * 1000
  177. })
  178. })
  179. } else {
  180. return Message({
  181. message: '请完善登录信息.',
  182. type: 'error',
  183. duration: 5 * 1000
  184. })
  185. }
  186. })
  187. },
  188. getOtherQuery(query) {
  189. return Object.keys(query).reduce((acc, cur) => {
  190. if (cur !== 'redirect') {
  191. acc[cur] = query[cur]
  192. }
  193. return acc
  194. }, {})
  195. }
  196. }
  197. }
  198. </script>
  199. <style lang="scss">
  200. /* 修复input 背景不协调 和光标变色 */
  201. /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
  202. $bg: #283443;
  203. $light_gray: #fff;
  204. $cursor: #fff;
  205. @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  206. .login-container .el-input input {
  207. color: $cursor;
  208. }
  209. }
  210. /* reset element-ui css */
  211. .login-container {
  212. .el-input {
  213. display: inline-block;
  214. height: 47px;
  215. width: 85%;
  216. input {
  217. background: transparent;
  218. border: 0px;
  219. -webkit-appearance: none;
  220. border-radius: 0px;
  221. padding: 12px 5px 12px 15px;
  222. color: $light_gray;
  223. height: 47px;
  224. caret-color: $cursor;
  225. &:-webkit-autofill {
  226. box-shadow: 0 0 0px 1000px $bg inset !important;
  227. -webkit-text-fill-color: $cursor !important;
  228. }
  229. }
  230. }
  231. .el-form-item {
  232. border: 1px solid rgba(255, 255, 255, 0.1);
  233. background: rgba(0, 0, 0, 0.1);
  234. border-radius: 5px;
  235. color: #454545;
  236. }
  237. }
  238. </style>
  239. <style lang="scss" scoped>
  240. $bg: #2d3a4b;
  241. $dark_gray: #889aa4;
  242. $light_gray: #eee;
  243. .login-container {
  244. min-height: 100%;
  245. width: 100%;
  246. background-color: $bg;
  247. overflow: hidden;
  248. .login-form {
  249. position: relative;
  250. width: 520px;
  251. max-width: 100%;
  252. padding: 160px 35px 0;
  253. margin: 0 auto;
  254. overflow: hidden;
  255. }
  256. .tips {
  257. font-size: 14px;
  258. color: #fff;
  259. margin-bottom: 10px;
  260. span {
  261. &:first-of-type {
  262. margin-right: 16px;
  263. }
  264. }
  265. }
  266. .svg-container {
  267. padding: 6px 5px 6px 15px;
  268. color: $dark_gray;
  269. vertical-align: middle;
  270. width: 30px;
  271. display: inline-block;
  272. }
  273. .title-container {
  274. position: relative;
  275. .title {
  276. font-size: 26px;
  277. color: $light_gray;
  278. margin: 0px auto 40px auto;
  279. text-align: center;
  280. font-weight: bold;
  281. }
  282. }
  283. .show-pwd {
  284. position: absolute;
  285. right: 10px;
  286. top: 7px;
  287. font-size: 16px;
  288. color: $dark_gray;
  289. cursor: pointer;
  290. user-select: none;
  291. }
  292. .thirdparty-button {
  293. position: absolute;
  294. right: 0;
  295. bottom: 6px;
  296. }
  297. @media only screen and (max-width: 470px) {
  298. .thirdparty-button {
  299. display: none;
  300. }
  301. }
  302. }
  303. </style>