login.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <div class="icore-lg">
  3. <img
  4. style="width: 100%; height: 100%; position: absolute"
  5. src="../../../assets/img/icore-shown-all.png"
  6. />
  7. <section v-if="isJump === true">
  8. <div class="jumpText">正在为您跳转,请稍后...</div>
  9. </section>
  10. <section v-else>
  11. <div>
  12. <div class="frame-area">
  13. <!-- <div class="desc-area"></div> -->
  14. <div style="position: absolute; left: 30px; width: 346px; top: 105px">
  15. <el-form
  16. class="lg-form"
  17. :inline="false"
  18. ref="loginForm"
  19. :model="loginForm"
  20. :rules="loginFormRules"
  21. >
  22. <el-form-item style="margin-bottom: 20px" prop="loginName">
  23. <el-input v-model="loginForm.loginName" placeholder="用户名">
  24. <i class="oa-login-ibg oa-login-ibg1" slot="prefix"></i>
  25. </el-input>
  26. </el-form-item>
  27. <el-form-item style="margin-bottom: 20px" prop="password">
  28. <el-input
  29. v-model="loginForm.password"
  30. :type="pwdShow ? 'text' : 'password'"
  31. placeholder="密码"
  32. @keyup.enter.native="loginSubmit('loginForm')"
  33. >
  34. <i class="oa-login-ibg oa-login-ibg2" slot="prefix"></i>
  35. <i
  36. class="icore-icons icore-icons-see oa-login-ibg4"
  37. :class="{ 'icore-icons-see-reverse': !pwdShow }"
  38. @click="showPwd"
  39. slot="suffix"
  40. ></i>
  41. </el-input>
  42. </el-form-item>
  43. <el-form-item style="margin-bottom: 20px">
  44. <el-button
  45. type="primary"
  46. class="width100"
  47. @click="loginSubmit('loginForm')"
  48. :loading="loginBtnLoading"
  49. >登&nbsp;录</el-button
  50. >
  51. </el-form-item>
  52. </el-form>
  53. </div>
  54. </div>
  55. </div>
  56. </section>
  57. </div>
  58. </template>
  59. <script>
  60. import { cookieUserId, cookieTime, cookieUserName } from '@/config/config.js'
  61. import { setCookie, getCookie, formatDate } from '@/utils/util.js'
  62. import store from '@/store/index.js'
  63. import JsSHA from 'jssha'
  64. const shaObj = new JsSHA('SHA-1', 'TEXT', { encoding: 'UTF8' })
  65. export default {
  66. data() {
  67. return {
  68. store,
  69. loginBtnLoading: false,
  70. pwdShow: false,
  71. shaObj,
  72. loginForm: {
  73. loginName: '',
  74. password: ''
  75. },
  76. loginFormRules: {
  77. loginName: [
  78. { required: true, message: '请输入用户名', trigger: 'blur' }
  79. ],
  80. password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
  81. },
  82. isJump: false
  83. }
  84. },
  85. beforeCreate() {
  86. let token = getCookie('accessToken')
  87. if (token) {
  88. this.$router.push({ path: 'default' })
  89. } else {
  90. window.top.localStorage.setItem('lockpwd', '')
  91. }
  92. window.top.localStorage.setItem('allPrivilege', '')
  93. window.top.localStorage.setItem('ownPrivilege', '')
  94. },
  95. mounted() {
  96. // 从cookie中获取上一次的登录用户名,赋值给用户名
  97. this.loginForm.loginName = getCookie('loginName')
  98. ? getCookie('loginName')
  99. : ''
  100. },
  101. methods: {
  102. loginSubmit(formName) {
  103. this.loginForm.loginName = this.loginForm.loginName.trim()
  104. this.$refs[formName].validate(valid => {
  105. if (valid) {
  106. // 登陆中 登录按钮不可用
  107. this.loginBtnLoading = true
  108. let form = JSON.parse(JSON.stringify(this.loginForm))
  109. form.app_code = 'web' //终端code暂时写死
  110. //传递密码明文进去
  111. this.shaObj.update(form.password)
  112. console.log(form)
  113. //获得密码的哈希值
  114. form.password = this.shaObj.getHash('HEX')
  115. console.log(form.password)
  116. this.store
  117. .dispatch('index/login/login', form)
  118. .then(res => {
  119. console.log(res)
  120. if (res.code === '0') {
  121. console.log('login resonse', res.data)
  122. // 登录成功
  123. // 设置缓存信息
  124. setCookie('accessToken', res.data.accessToken, cookieTime, '/')
  125. setCookie(
  126. 'refreshToken',
  127. res.data.refreshToken,
  128. cookieTime,
  129. '/'
  130. )
  131. setCookie('orgCode', res.data.user.orgCode)
  132. setCookie('userId', res.data.user.userId)
  133. setCookie('appId', res.data.appId)
  134. setCookie('loginId', res.data.user.userId) // 为配合bms取值添加
  135. setCookie('loginName', this.loginForm.loginName)
  136. let info = JSON.parse(JSON.stringify(res.data.user))
  137. for (let k in info) {
  138. if (
  139. k === 'sysGroup' ||
  140. k === 'sysCompanys' ||
  141. k === 'sysRoles' ||
  142. k === 'photo' ||
  143. k === 'sysOrgs'
  144. ) {
  145. delete info[k]
  146. }
  147. }
  148. console.log('info', JSON.stringify(info))
  149. setCookie('userInfo', JSON.stringify(info))
  150. // 判断用户集团公司情况跳转
  151. let userInfo = res.data.user
  152. let sRedirect = this.$route.query.redirect
  153. // 超级管理员
  154. if (userInfo.userCode === 'admin') {
  155. if (userInfo.sysCompanys) {
  156. // 公司列表过多时cookie存放失败,存至localStorage
  157. // setCookie('companys', JSON.stringify(userInfo.sysCompanys));
  158. window.top.localStorage.setItem(
  159. 'sysGroup',
  160. JSON.stringify(userInfo.sysGroup)
  161. )
  162. window.top.localStorage.setItem(
  163. 'companys',
  164. JSON.stringify(userInfo.sysCompanys)
  165. )
  166. if (userInfo.hasOwnProperty('sysCompanys')) {
  167. window.top.localStorage.setItem(
  168. 'companyId',
  169. userInfo.sysCompanys[0].id
  170. )
  171. }
  172. }
  173. if (sRedirect) {
  174. this.$router.push({ name: sRedirect })
  175. } else {
  176. this.$router.push({ name: 'default' }).catch(() => {})
  177. }
  178. // 普通用户
  179. } else {
  180. if (
  181. userInfo.hasOwnProperty('sysGroup') &&
  182. userInfo.sysGroup !== '' &&
  183. userInfo.sysGroup !== null &&
  184. JSON.stringify(userInfo.sysGroup) !== '{}' &&
  185. JSON.stringify(userInfo.sysGroup) !== '[]'
  186. ) {
  187. window.top.localStorage.setItem(
  188. 'sysGroup',
  189. JSON.stringify(userInfo.sysGroup)
  190. )
  191. if (userInfo.hasOwnProperty('sysCompanys')) {
  192. window.top.localStorage.setItem(
  193. 'companys',
  194. JSON.stringify(userInfo.sysCompanys)
  195. )
  196. window.top.localStorage.setItem(
  197. 'companyId',
  198. userInfo.sysCompanys[0].id
  199. )
  200. if (userInfo.sysCompanys.length > 1) {
  201. // 跳转选择集团公司页面
  202. this.$router.push({ name: 'selectCompany' })
  203. } else {
  204. if (sRedirect) {
  205. this.$router.push({ name: sRedirect })
  206. } else {
  207. this.$router.push({ name: 'default' }).catch(() => {})
  208. }
  209. }
  210. } else {
  211. this.$message.error(
  212. '后台返回数据缺少company信息,请联系开发人员!'
  213. )
  214. }
  215. } else {
  216. this.$message.error(
  217. '必须隶属一个集团!请先联系管理员添加集团信息!'
  218. )
  219. }
  220. }
  221. } else {
  222. // 登录失败
  223. // 提示
  224. this.$message({
  225. message: res.message,
  226. type: 'error',
  227. duration: 2000,
  228. offset: 300
  229. })
  230. this.loginBtnLoading = false
  231. this.$router.go(0)
  232. }
  233. })
  234. .catch(() => {
  235. this.loginBtnLoading = false
  236. })
  237. } else {
  238. return false
  239. }
  240. })
  241. },
  242. showPwd() {
  243. this.pwdShow = !this.pwdShow
  244. }
  245. }
  246. }
  247. </script>
  248. <style lang="less" scoped>
  249. .jumpText {
  250. position: absolute;
  251. width: 100%;
  252. text-align: center;
  253. line-height: 500px;
  254. color: white;
  255. font-size: 24px;
  256. }
  257. </style>