login.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <template>
  2. <div class="bg">
  3. <div class="login-box">
  4. <el-form
  5. ref="ruleFormRef"
  6. :rules="rules"
  7. class="login_from"
  8. label-position="left"
  9. :model="loginForm"
  10. style="max-width: 400px"
  11. @submit.prevent="submitForm(ruleFormRef)">
  12. <el-tabs v-model="login_typ" style="width: 50%;" @tab-change="resetForm(ruleFormRef)">
  13. <el-tab-pane label="密码登录" name="pwd" />
  14. <el-tab-pane label="验证码登录" name="sms" />
  15. </el-tabs>
  16. <template v-if="login_typ === 'pwd'">
  17. <el-form-item label="" prop="account">
  18. <el-input v-model="loginForm.account" type="text" placeholder="请输入账号" clearable :prefix-icon="User" />
  19. </el-form-item>
  20. <el-form-item label="" prop="password">
  21. <el-input
  22. v-model="loginForm.password"
  23. type="password"
  24. show-password
  25. placeholder="请输入密码"
  26. autocomplete="off"
  27. :prefix-icon="Lock" />
  28. <el-input
  29. v-model="loginForm.password"
  30. type="hidden"
  31. autocomplete="off"
  32. id="md5_password"
  33. show-password
  34. placeholder="请输入密码"
  35. :prefix-icon="Lock" />
  36. </el-form-item>
  37. </template>
  38. <template v-else-if="login_typ === 'sms'">
  39. <el-form-item label="" prop="account">
  40. <el-input v-model="loginForm.account" type="text" placeholder="请输入手机号" clearable :prefix-icon="Cellphone" />
  41. </el-form-item>
  42. <el-form-item label="" prop="smsCaptcha">
  43. <div style="display: flex;align-items: center;justify-content: space-between;width: 100%;">
  44. <el-input v-model="loginForm.smsCaptcha" type="number" placeholder="请输入验证码" :prefix-icon="Lock"/>
  45. <el-button @click="getCaptcha" type="primary" style="width: 100px;" :disabled ="!can_send">{{smsMessage}}</el-button>
  46. </div>
  47. </el-form-item>
  48. </template>
  49. <!-- <el-form-item style="margin-top:-10px;margin-bottom:-5px;">
  50. <el-checkbox v-model="checked" style="color:#a0a0a0;margin-top:-10px;">记住我</el-checkbox>
  51. </el-form-item> -->
  52. <div class="btn df aic jcsb">
  53. <el-button
  54. round
  55. class="submit_btn"
  56. block
  57. type="primary"
  58. size="large"
  59. native-type="submit">登&emsp;录</el-button>
  60. </div>
  61. </el-form>
  62. </div>
  63. </div>
  64. <div class="mob-login">
  65. <div class="login-box" :style="'transform: translateX('+transWidth+')'">
  66. <div class="item">
  67. <div class="text">欢迎登录</div>
  68. <img v-if="user.isQingQue" class="logo" :src="img('logo.png')" alt=""/>
  69. <div class="tel-login">
  70. <van-form @submit="submitFormM">
  71. <van-cell-group inset>
  72. <van-field
  73. style="font-size: 16px;"
  74. v-model="loginForm.account"
  75. autocomplete="off"
  76. left-icon="user-circle-o"
  77. placeholder="请输入用户名"
  78. :rules="[{ required: true, message: '请填写用户名' }]" />
  79. <van-field
  80. style="font-size: 16px;"
  81. v-model="loginForm.password"
  82. autocomplete="off"
  83. left-icon="shield-o"
  84. type="password"
  85. placeholder="请输入密码"
  86. :rules="[{ required: true, message: '请填写密码' }]" />
  87. </van-cell-group>
  88. <div style="margin: 16px;margin-top: 30px;">
  89. <van-button round type="primary" block native-type="submit">
  90. 登录
  91. </van-button>
  92. </div>
  93. </van-form>
  94. </div>
  95. <div class="trans" @click="login_typ = 'sms', transWidth = '0vw'">
  96. <van-icon name="exchange" />
  97. 验证码登录
  98. </div>
  99. </div>
  100. <div class="item">
  101. <div class="text">欢迎登录</div>
  102. <img v-if="user.isQingQue" class="logo" :src="img('logo.png')" alt=""/>
  103. <img v-else class="logo" style="width: 100px;" :src="img('default_logo.png')" alt=""/>
  104. <div class="tel-login">
  105. <van-form >
  106. <van-cell-group inset>
  107. <van-field
  108. type="tel"
  109. style="font-size: 16px;"
  110. v-model="loginForm.account"
  111. autocomplete="off"
  112. left-icon="phone-o"
  113. placeholder="请输入手机号"
  114. :rules="[{ required: true, message: '请填写手机号' }]" />
  115. </van-cell-group>
  116. <div style="margin: 16px;margin-top: 30px;">
  117. <van-button round block type="primary" :disabled="!can_send || loginForm.account.length!==11" @click="getCaptcha">
  118. {{ smsMessage }}
  119. </van-button>
  120. </div>
  121. </van-form>
  122. </div>
  123. <div class="trans" @click="login_typ = 'pwd', transWidth = '100vw'">
  124. <van-icon name="exchange" />
  125. 密码登录
  126. </div>
  127. </div>
  128. <div class="item">
  129. <div class="text">输入验证码
  130. <div class="notice">验证码已发送至{{loginForm.account}}</div>
  131. </div>
  132. <div class="tel-login">
  133. <van-form @submit="submitFormM">
  134. <van-password-input
  135. :value="loginForm.smsCaptcha"
  136. :mask="false"
  137. info="请输入6位手机验证码"
  138. :error-info="errorInfo"
  139. length="6"
  140. :gutter="10"
  141. :focused="showKeyboard"
  142. @focus="showKeyboard = true"
  143. />
  144. <div style="margin: 16px;margin-top: 30px;">
  145. <van-button round block type="primary" :disabled="loginForm.smsCaptcha === undefined || loginForm.smsCaptcha.length!==6" native-type="submit">
  146. 登录
  147. </van-button>
  148. </div>
  149. </van-form>
  150. </div>
  151. <div class="trans" @click="transWidth = '0vw'">
  152. <van-icon name="arrow-left" />
  153. 返回
  154. </div>
  155. </div>
  156. </div>
  157. </div>
  158. <van-number-keyboard
  159. v-model="loginForm.smsCaptcha"
  160. :show="showKeyboard"
  161. @blur="showKeyboard = false"
  162. />
  163. </template>
  164. <script setup lang="ts">
  165. import { reactive, ref, onMounted, inject } from 'vue'
  166. import { Lock, User, Cellphone } from '@element-plus/icons-vue'
  167. import type { FormInstance, FormRules } from 'element-plus'
  168. import { getUserLogin, captchaLogin, getCaptchaHttp } from '@/api/index'
  169. import { showSuccessToast, showFailToast } from 'vant'
  170. // import '@/utils/md5'
  171. import Message from '@/utils/Message'
  172. import checkRestTime from '@/utils/captcha'
  173. import { useRouter } from 'vue-router'
  174. import { useStore } from '@/store/index'
  175. import local from '@/utils/local'
  176. declare let md5: any
  177. const transWidth = ref('0vw')
  178. const { user } = useStore()
  179. const router = useRouter()
  180. const VALID = 60
  181. const login_typ = ref<'pwd'| 'sms'>('sms')
  182. const smsMessage = ref<'获取验证码' | number>('获取验证码')
  183. const errorInfo = ref<string>('')
  184. const ruleFormRef = ref<FormInstance>()
  185. const img:any = inject('img')
  186. const can_send = ref<boolean>(true)
  187. interface login {
  188. account: string,
  189. password: string,
  190. timestamp: number | string,
  191. smsCaptcha:string | undefined
  192. }
  193. const loginForm = reactive<login>({
  194. account: '',
  195. password: '',
  196. timestamp: parseInt(`${Date.now() / 1000}`),
  197. smsCaptcha: undefined
  198. })
  199. const showKeyboard = ref(false)
  200. const rules = reactive<FormRules>({
  201. account: [
  202. { required: true, message: '请输入账号', trigger: 'blur' },
  203. { min: 3, max: 255, message: '请正确输入账号', trigger: 'blur' }
  204. ],
  205. password: [
  206. { required: true, message: '请输入密码', trigger: 'blur' },
  207. { min: 6, max: 20, message: '请输入6-20位密码', trigger: 'blur,change' }
  208. ],
  209. smsCaptcha: [
  210. { required: true, message: '请输入验证码', trigger: 'blur' }
  211. ]
  212. })
  213. const submitForm = async(formEl: FormInstance | undefined) => {
  214. if (!formEl) return
  215. await formEl.validate(async(valid, fields) => {
  216. if (valid) {
  217. if (login_typ.value === 'pwd') {
  218. // 登录密码加密
  219. let md5Pwd: any = document.querySelector('#md5_password')
  220. // eslint-disable-next-line no-undef
  221. md5Pwd = md5(md5(md5(loginForm.password)) + loginForm.timestamp)
  222. const params = {
  223. account: loginForm.account,
  224. password: md5Pwd,
  225. timestamp: loginForm.timestamp
  226. }
  227. getUserLogin(params).then(async(res) => {
  228. // console.log('res---->', res);
  229. if (res.data.code === 200) {
  230. Message.success('登录成功')
  231. // 保存用户信息 和 token
  232. sessionStorage.setItem('token', res.data.data.token)
  233. sessionStorage.setItem('account', loginForm.account)
  234. localStorage.setItem('from', '1')
  235. user.isLogin = true
  236. await user.getUserProfile()
  237. // 跳转我的游戏页面
  238. setTimeout(() => {
  239. router.push({ path: '/my_game', query: { account: loginForm.account }})
  240. local.set('headerPath', '我的')
  241. }, 1000)
  242. }
  243. }).catch(err => {
  244. // console.log('err===>', err);
  245. Message.error(err.data.msg)
  246. })
  247. } else if (login_typ.value === 'sms') {
  248. const params = {
  249. mobile: loginForm.account,
  250. captcha: loginForm.smsCaptcha
  251. }
  252. console.log(params)
  253. await captchaLogin(params).then(async(res) => {
  254. console.log(res)
  255. if (res.data.code === 200) {
  256. Message.success('登录成功')
  257. sessionStorage.setItem('token', res.data.data.token)
  258. sessionStorage.setItem('account', loginForm.account)
  259. localStorage.setItem('from', '2')
  260. user.isLogin = true
  261. await user.getUserProfile()
  262. setTimeout(() => {
  263. router.push({ path: '/my_game', query: { account: loginForm.account }})
  264. local.set('headerPath', '我的')
  265. }, 1000)
  266. }
  267. }).catch((error) => {
  268. console.log(error)
  269. Message.error(error.data.msg)
  270. })
  271. }
  272. } else {
  273. console.log('登录失败', fields)
  274. }
  275. })
  276. }
  277. const submitFormM = async() => {
  278. console.log(login_typ.value)
  279. if (login_typ.value === 'pwd') {
  280. // 登录密码加密
  281. let md5Pwd: any = document.querySelector('#md5_password')
  282. // eslint-disable-next-line no-undef
  283. md5Pwd = md5(md5(md5(loginForm.password)) + loginForm.timestamp)
  284. const params = {
  285. account: loginForm.account,
  286. password: md5Pwd,
  287. timestamp: loginForm.timestamp
  288. }
  289. await getUserLogin(params).then(async(res) => {
  290. // console.log('res---->', res);
  291. if (res.data.code === 200) {
  292. Message.success('登录成功')
  293. // 保存用户信息 和 token
  294. sessionStorage.setItem('token', res.data.data.token)
  295. sessionStorage.setItem('account', loginForm.account)
  296. localStorage.setItem('from', '1')
  297. user.isLogin = true
  298. await user.getUserProfile()
  299. // 跳转我的游戏页面
  300. setTimeout(() => {
  301. router.push({ path: '/my_game', query: { account: loginForm.account }})
  302. local.set('headerPath', '我的')
  303. }, 1000)
  304. }
  305. }).catch(err => {
  306. // console.log('err===>', err);
  307. errorInfo.value = err.data.msg
  308. showFailToast(err.data.msg)
  309. })
  310. } else if (login_typ.value === 'sms') {
  311. const params = {
  312. mobile: loginForm.account,
  313. captcha: loginForm.smsCaptcha
  314. }
  315. console.log(params)
  316. await captchaLogin(params).then(async(res) => {
  317. console.log(res)
  318. if (res.data.code === 200) {
  319. showSuccessToast('登录成功')
  320. sessionStorage.setItem('token', res.data.data.token)
  321. sessionStorage.setItem('account', loginForm.account)
  322. localStorage.setItem('from', '2')
  323. user.isLogin = true
  324. await user.getUserProfile()
  325. setTimeout(() => {
  326. router.push({ path: '/my_game', query: { account: loginForm.account }})
  327. local.set('headerPath', '我的')
  328. }, 1000)
  329. }
  330. }).catch((error) => {
  331. errorInfo.value = error.data.msg
  332. showFailToast(error.data.msg)
  333. })
  334. }
  335. }
  336. const resetForm = (formEl: FormInstance | undefined) => {
  337. if (!formEl) return
  338. formEl.resetFields()
  339. }
  340. const getCaptcha = async() => {
  341. if (loginForm.account.length !== 11) {
  342. return Message.error('请输入有效手机号!')
  343. }
  344. await getCaptchaHttp({ mobile: loginForm.account }).then((res) => {
  345. console.log(res)
  346. can_send.value = false
  347. smsMessage.value = VALID
  348. settimes()
  349. sessionStorage.setItem('LOGIN_CAPTCHA', Math.round(new Date().getTime() / 1000).toString())
  350. Message.success('验证码已发送,请注意查收')
  351. transWidth.value = '-100vw'
  352. }).catch((error) => {
  353. console.log(error)
  354. Message.error(error.data.msg)
  355. })
  356. }
  357. const settimes = () => {
  358. var interval:any
  359. const setTimeFn = () => {
  360. (smsMessage.value as number)--
  361. if (smsMessage.value as number < 0 || can_send.value === true) {
  362. clearInterval(interval)
  363. can_send.value = true
  364. smsMessage.value = '获取验证码'
  365. }
  366. }
  367. interval = setInterval(function() {
  368. setTimeFn()
  369. }, 1000)
  370. }
  371. onMounted(() => {
  372. const res = checkRestTime('LOGIN_CAPTCHA', VALID)
  373. if (res) {
  374. smsMessage.value = res
  375. can_send.value = false
  376. settimes()
  377. }
  378. })
  379. </script>
  380. <style lang="scss" scoped>
  381. .bg{
  382. width: 100%;
  383. height: 29rem;
  384. background: linear-gradient(#d9e8fe, #ddf2f4, #e3fee9);
  385. // background-position: 4rem;
  386. // background-size: cover;
  387. border-radius: .8rem;
  388. overflow: hidden;
  389. margin-top: 3rem;
  390. position: relative;
  391. .login-box{
  392. position: absolute;
  393. width: 22rem;
  394. height: 17.5rem;
  395. left: 3rem;
  396. top: 50%;
  397. transform: translateY(-45%);
  398. background-color: rgba($color: #fff, $alpha: 0.5);
  399. border-radius: .5rem;
  400. box-sizing: border-box;
  401. padding: 2rem 1rem 0 1rem;
  402. .submit_btn{
  403. width: 100%;
  404. }
  405. }
  406. }
  407. .mob-login{
  408. display: none;
  409. width: 100%;
  410. height: 100%;
  411. min-height: calc(100vh - 60px);
  412. background: #EBEEFD url('../assets/img/mobbg.png') no-repeat center;
  413. background-size: cover;
  414. // position: relative;
  415. // align-items: center;
  416. padding-top: 70px;
  417. justify-content: center;
  418. box-sizing: border-box;
  419. .login-box{
  420. width: 300vw;
  421. height: 400px;
  422. border-radius: 15px;
  423. padding: 20px 10px;
  424. box-sizing: border-box;
  425. // padding-left: 7.5vw;
  426. display: flex;
  427. align-items: center;
  428. justify-content: space-between;
  429. transition: all 300ms ease;
  430. .item{
  431. width: 100vw;
  432. // margin-right: 7.5vw;
  433. padding: 0 7.5vw;
  434. }
  435. .logo{
  436. width: 150px;
  437. display: block;
  438. margin: 30px auto;
  439. }
  440. .text{
  441. font-size: 25px;
  442. margin-bottom: 40px;
  443. border-bottom: 5px solid #99ccff;
  444. border-radius: 10px;
  445. padding-bottom: 5px;
  446. width: fit-content;
  447. font-family: 'youshe';
  448. font-weight: 400;
  449. position: relative;
  450. box-sizing: border-box;
  451. .notice{
  452. position: absolute;
  453. width: 80vw;
  454. height: 20px;
  455. bottom: -30px;
  456. left: 0;
  457. font-family:Arial, Helvetica, sans-serif;
  458. font-size: 12px;
  459. color: rgba($color: #000000, $alpha: .8);
  460. }
  461. }
  462. .trans{
  463. margin-top: 30px;
  464. }
  465. .pwd-login{
  466. transform: translateX(-100vw);
  467. }
  468. }
  469. }
  470. </style>