login.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <template>
  2. <div class="login">
  3. <!-- <div v-if="!token"> -->
  4. <div v-if="companyList.length <= 1">
  5. <div class="tabs">
  6. <div :class="signInType === 'pwd' ? 'select' : ''" @click="changeSignIn('pwd')">账密</div>
  7. <div :class="signInType === 'ding' ? 'select' : ''" @click="changeSignIn('ding')">钉钉</div>
  8. </div>
  9. <div v-if="signInType === 'ding'">
  10. <h3 class="title">趣程后台管理系统</h3>
  11. <div id='login_container' />
  12. </div>
  13. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" v-if="signInType === 'pwd'">
  14. <h3 class="title">趣程后台管理系统</h3>
  15. <el-form-item prop="phone">
  16. <el-input v-model="loginForm.phone" type="text" auto-complete="off" placeholder="手机号码">
  17. <svg-icon slot="prefix" icon-class="phone" class="el-input__icon input-icon" />
  18. </el-input>
  19. </el-form-item>
  20. <el-form-item prop="code">
  21. <el-input v-model="loginForm.code" auto-complete="on" placeholder="验证码" :maxlength="4" style="width: 63%"
  22. @keyup.enter.native="handleLogin">
  23. <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
  24. </el-input>
  25. <div class="login-code">
  26. <el-button style="width: 115px" :disabled="!loginForm.phone || getCodeBtText !== '获取验证码'"
  27. @click="getCodeHandle(loginForm.phone)">{{ getCodeBtText }}</el-button>
  28. </div>
  29. </el-form-item>
  30. <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住手机号</el-checkbox>
  31. <div style="display: flex;justify-content: center; margin-bottom: 18px;">
  32. <el-radio-group v-model="msgType">
  33. <el-radio label="ding">钉钉验证码</el-radio>
  34. <el-radio label="phone">手机验证码</el-radio>
  35. </el-radio-group>
  36. </div>
  37. <el-form-item style="width:100%;">
  38. <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
  39. @click.native.prevent="handleLogin">
  40. <span v-if="!loading">登 录</span>
  41. <span v-else>登 录 中...</span>
  42. </el-button>
  43. </el-form-item>
  44. </el-form>
  45. </div>
  46. <div v-else-if="companyList.length > 1" style="margin-bottom: 160px">
  47. <h2 class="account-title">趣程后台管理系统</h2>
  48. <div class="login-form company-account">
  49. <h3>请选择公司账户登录</h3>
  50. <div class="choose-tableBlock">
  51. <div class="ac-tableLine" v-for="item in companyList" :key="item.companyId"
  52. @click="setCompanyHandle(item.companyId)">
  53. <div class="actname">{{ item.companyInfo.companyName }}</div>
  54. <div class="right">
  55. <span>{{ item.powerLevel === 999 ? '超级管理员' : item.powerLevel === 100 ? '公司超管' : item.powerLevel === 99 ?
  56. '管理员' : '普通用户' }}</span>
  57. <i class="el-icon-right"></i>
  58. </div>
  59. </div>
  60. </div>
  61. </div>
  62. <el-button type="text" size="medium" @click="logout">更换账户</el-button>
  63. </div>
  64. <!-- 底部 -->
  65. <div class="el-login-footer">
  66. <span>Copyright © 2020 quchen.</span>
  67. </div>
  68. </div>
  69. </template>
  70. <script>
  71. import { getNoteCode, getCode, getDingKey } from "@/api/login";
  72. import Cookies from "js-cookie";
  73. import { mapState } from 'vuex'
  74. export default {
  75. name: "Login",
  76. data() {
  77. let validatePass = (rule, value, callback) => {
  78. if (!value) {
  79. callback(new Error('请输入验证码'));
  80. } else if (value.length === 4) {
  81. callback()
  82. } else {
  83. callback(new Error('验证码长度4位'));
  84. }
  85. };
  86. // 验证手机号
  87. let validatorPhone = (rule, value, callback) => {
  88. // if (value === '') {
  89. // callback(new Error('手机号不能为空'))
  90. // } else if (!/^1\d{10}$/.test(value)) {
  91. // callback(new Error('手机号格式错误'))
  92. // } else {
  93. // callback()
  94. // }
  95. callback()
  96. };
  97. return {
  98. msgType: 'ding',
  99. signInType: 'pwd', // 登录类型
  100. APPID: process.env.VUE_APP_DDAPPID, // dingdsfuoevmawlyyrjx
  101. REDIRECT_URI: encodeURIComponent(process.env.VUE_APP_BASE_URL), //encodeURIComponent(location.href),
  102. state: '',
  103. dingdingLoginForm: {
  104. code: '',
  105. state: '',
  106. stateKey: ''
  107. },
  108. loginForm: {},
  109. getCodeBtText: '获取验证码',
  110. loginRules: {
  111. phone: [
  112. { required: true, validator: validatorPhone, trigger: 'blur' }
  113. ],
  114. code: [
  115. { required: true, validator: validatePass, trigger: 'blur' }
  116. ]
  117. },
  118. loading: false,
  119. redirect: undefined,
  120. timer: null
  121. };
  122. },
  123. destroyed() {
  124. this.clearTimer()
  125. },
  126. computed: {
  127. ...mapState({
  128. companyList: state => state.user.companyList,
  129. token: state => state.user.token
  130. })
  131. },
  132. watch: {
  133. $route: {
  134. handler(route) {
  135. const { code, state, token } = route.query;
  136. if (code) {
  137. this.dingdingLogin(code, state)
  138. } else if (token) {
  139. this.$store.dispatch("SetToken", encodeURIComponent(token)).then(res => {
  140. this.$router.push({ path: `/login?redirect=${this.redirect || '/index'}` });
  141. })
  142. }
  143. },
  144. immediate: true
  145. },
  146. signInType: {
  147. handler(val) {
  148. if (val === 'ding') {
  149. this.dingdingHandle()
  150. }
  151. },
  152. deep: true
  153. }
  154. },
  155. created() {
  156. const { redirect } = this.$route.query;
  157. this.redirect = redirect ? decodeURIComponent(redirect) : '/';
  158. if (!this.token) {
  159. this.getCookie();
  160. if (this.signInType === 'ding') {
  161. this.dingdingHandle()
  162. }
  163. } else {
  164. this.$store.dispatch('GetInfo').then(res => {
  165. this.setCompanyHandle(res.data.companyRelationInfo[0].companyId)
  166. });
  167. }
  168. },
  169. methods: {
  170. /** 设置公司 */
  171. setCompanyHandle(companyId) {
  172. const loading = this.$loading({
  173. lock: true,
  174. text: '拼命加载中',
  175. spinner: 'el-icon-loading',
  176. background: 'rgba(255, 255, 255, 0.5)'
  177. })
  178. this.$store.dispatch("SelectCom", companyId).then(() => {
  179. this.$router.push({ path: this.redirect || '/index' });
  180. loading.close();
  181. }).catch(() => {
  182. loading.close();
  183. })
  184. },
  185. changeSignIn(type) {
  186. this.signInType = type
  187. },
  188. // 获取钉钉登录相关
  189. async dingdingHandle() {
  190. if (this.signInType === 'ding') {
  191. let res = await getDingKey()
  192. let strArr = res.data.split('_')
  193. sessionStorage.setItem('DDK', strArr[0])
  194. this.state = strArr[1]
  195. window.DDLogin({
  196. id: "login_container",
  197. goto: encodeURIComponent(`https://oapi.dingtalk.com/connect/qrconnect?appid=${this.APPID}&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=${this.REDIRECT_URI}`),
  198. style: "border:none;padding:0 0 20px 0;background-color:rgba(255,255,255,0.7);",
  199. width: "100%",
  200. height: "400"
  201. });
  202. // 监听iframe的消息
  203. if (typeof window.addEventListener != 'undefined') {
  204. window.addEventListener('message', this.handleMessage, false);
  205. } else if (typeof window.attachEvent != 'undefined') {
  206. window.attachEvent('onmessage', this.handleMessage);
  207. }
  208. }
  209. },
  210. // 钉钉登录
  211. dingdingLogin(code, state) {
  212. if (!this.token) {
  213. this.loading = true;
  214. this.$store.dispatch("DingDingLogin", { code, state, stateKey: sessionStorage.getItem('DDK') }).then(res => {
  215. if (res) {
  216. this.setCompanyHandle(res)
  217. } else {
  218. this.$router.push({ path: `/login?redirect=${this.redirect || '/index'}` });
  219. }
  220. this.loading = false;
  221. }).catch(() => {
  222. this.loading = false;
  223. });
  224. } else {
  225. this.$router.push({ path: `/login?redirect=${this.redirect || '/index'}` });
  226. }
  227. },
  228. handleMessage(event) {
  229. // 获取loginTempCode
  230. const loginTempCode = event.data;
  231. // 获取消息来源
  232. const origin = event.origin;
  233. // 拼接 url
  234. const url = `https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${this.APPID}&response_type=code&scope=snsapi_login&state=${encodeURIComponent(this.state)}&redirect_uri=${this.REDIRECT_URI}&loginTmpCode=${loginTempCode}`
  235. // 如果来源为https://login.dingtalk.com,则在当前窗口打开回调链接
  236. if (origin === 'https://login.dingtalk.com') {
  237. window.open(encodeURI(url), '_parent')
  238. }
  239. },
  240. getCodeHandle(phone) {
  241. // if(!/^1\d{10}$/.test(phone)) {
  242. // this.$message.error('请输入正确的手机号');
  243. // return
  244. // }
  245. this.getCodeBtText = 30
  246. if (this.msgType === 'ding') {
  247. getCode(phone).then(res => {
  248. if (res.data) {
  249. this.$message.success('发送成功,请到钉钉查看')
  250. let time = 30
  251. this.timer = setInterval(() => {
  252. time--
  253. this.getCodeBtText = time
  254. if (time <= 0) {
  255. this.getCodeBtText = '获取验证码'
  256. clearInterval(this.timer)
  257. }
  258. }, 1000)
  259. }
  260. }).catch(() => this.getCodeBtText = '获取验证码');
  261. } else {
  262. getNoteCode(phone).then(res => {
  263. if (res.data) {
  264. this.$message.success('发送成功,请到手机查看')
  265. let time = 30
  266. this.timer = setInterval(() => {
  267. time--
  268. this.getCodeBtText = time
  269. if (time <= 0) {
  270. this.getCodeBtText = '获取验证码'
  271. clearInterval(this.timer)
  272. }
  273. }, 1000)
  274. }
  275. }).catch(() => this.getCodeBtText = '获取验证码');
  276. }
  277. },
  278. getCookie() {
  279. const rememberMe = Cookies.get('rememberMe')
  280. const phone = Cookies.get("phone");
  281. this.loginForm = {
  282. phone: phone === undefined ? this.loginForm.phone : phone,
  283. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  284. };
  285. console.log(rememberMe, {
  286. phone: phone === undefined ? this.loginForm.phone : phone,
  287. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  288. })
  289. },
  290. handleLogin() {
  291. this.$refs.loginForm.validate(valid => {
  292. if (valid) {
  293. this.loading = true;
  294. if (this.loginForm.rememberMe) {
  295. Cookies.set("phone", this.loginForm.phone, { expires: 30 });
  296. Cookies.set("rememberMe", this.loginForm.rememberMe, { expires: 30 });
  297. } else {
  298. Cookies.remove("phone");
  299. Cookies.remove("rememberMe");
  300. }
  301. this.$store.dispatch("Login", this.loginForm).then(res => {
  302. if (res) {
  303. this.setCompanyHandle(res)
  304. } else {
  305. // this.$router.push({ path: this.redirect });
  306. // this.$router.go(0);
  307. }
  308. this.clearTimer()
  309. this.loading = false;
  310. }).catch(() => {
  311. this.loading = false;
  312. });
  313. }
  314. });
  315. },
  316. clearTimer() {
  317. this.timer && clearInterval(this.timer)
  318. this.getCodeBtText = '获取验证码'
  319. },
  320. logout() {
  321. this.$store.dispatch('LogOut').then(() => {
  322. this.signInType = 'pwd'
  323. })
  324. }
  325. }
  326. };
  327. </script>
  328. <style rel="stylesheet/scss" lang="scss">
  329. .login {
  330. display: flex;
  331. justify-content: center;
  332. align-items: center;
  333. height: 100%;
  334. background-image: url("../assets/image/login-background.jpg");
  335. background-size: cover;
  336. &>div {
  337. display: flex;
  338. flex-direction: column;
  339. justify-content: center;
  340. align-items: center;
  341. }
  342. }
  343. .title {
  344. margin: 0px auto 30px auto;
  345. text-align: center;
  346. color: #2e2e2e;
  347. }
  348. .login-form {
  349. border-radius: 8px;
  350. background: rgba(255, 255, 255, .8);
  351. width: 400px;
  352. padding: 25px 25px 5px 25px;
  353. margin-bottom: 80px;
  354. .el-input {
  355. height: 38px;
  356. input {
  357. background: rgba(255, 255, 255, .2);
  358. border-color: #5a5a5a;
  359. height: 38px;
  360. }
  361. .el-input__prefix {
  362. color: #5a5a5a;
  363. }
  364. }
  365. .input-icon {
  366. height: 39px;
  367. width: 14px;
  368. margin-left: 2px;
  369. }
  370. }
  371. .login-tip {
  372. font-size: 13px;
  373. text-align: center;
  374. color: #bfbfbf;
  375. }
  376. .login-code {
  377. width: 33%;
  378. height: 38px;
  379. float: right;
  380. img {
  381. cursor: pointer;
  382. vertical-align: middle;
  383. }
  384. }
  385. .el-login-footer {
  386. height: 40px;
  387. line-height: 40px;
  388. position: fixed;
  389. bottom: 0;
  390. width: 100%;
  391. text-align: center;
  392. color: #fff;
  393. font-family: Arial;
  394. font-size: 12px;
  395. letter-spacing: 1px;
  396. }
  397. .login-code-img {
  398. height: 38px;
  399. }
  400. .tabs {
  401. display: flex;
  402. margin: 0 20px 30px;
  403. width: 300px;
  404. &>div {
  405. width: 50%;
  406. text-align: center;
  407. // color: rgb(36, 219, 149);
  408. color: #000000;
  409. cursor: pointer;
  410. font-size: 28px;
  411. &.select {
  412. color: #1890ff;
  413. }
  414. }
  415. }
  416. .account-title {
  417. color: #FFFFFF;
  418. margin-bottom: 40px;
  419. }
  420. .company-account {
  421. box-shadow: 0 4px 20px 0 rgba(0, 60, 179, 0.12);
  422. margin-bottom: 10px;
  423. width: 500px;
  424. padding: 48px 36px;
  425. &>h3 {
  426. margin: 0;
  427. font-size: 20px;
  428. }
  429. &>.choose-tableBlock {
  430. margin-top: 20px;
  431. border-top: 1px solid rgba(181, 181, 181, .4);
  432. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", Helvetica, Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", "Source Han Sans CN", sans-serif;
  433. max-height: 250px;
  434. overflow-y: auto;
  435. padding: 0 10px;
  436. .ac-tableLine {
  437. padding: 16px 14px;
  438. display: flex;
  439. align-items: center;
  440. font-size: 14px;
  441. cursor: pointer;
  442. color: #68779c;
  443. justify-content: space-between;
  444. border: 2px solid transparent;
  445. position: relative;
  446. border-radius: 4px;
  447. .actname {
  448. width: 250px;
  449. color: #0b1531;
  450. font-size: 16px;
  451. white-space: nowrap;
  452. text-overflow: ellipsis;
  453. overflow: hidden;
  454. }
  455. .right {
  456. display: flex;
  457. justify-content: flex-end;
  458. align-items: center;
  459. font-size: 13px;
  460. &>span {
  461. margin-right: 10px;
  462. }
  463. }
  464. i {
  465. color: #1890ff;
  466. font-size: 20px;
  467. }
  468. &:hover {
  469. border: 2px solid #296aef;
  470. z-index: 5;
  471. transition: all .2s;
  472. box-shadow: 0 0 12px rgba(0, 60, 179, 0.12);
  473. .right {
  474. color: #296aef;
  475. }
  476. i {
  477. color: #296aef;
  478. }
  479. }
  480. &::after {
  481. content: '';
  482. position: absolute;
  483. bottom: 0;
  484. left: 0;
  485. background-color: rgba(181, 181, 181, .4);
  486. height: 1px;
  487. width: 100%;
  488. }
  489. }
  490. }
  491. }
  492. </style>