login.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <div class="loginPage w-full h-full bg-white">
  3. <template v-if="!isCorpWX">
  4. <div class="flex justify-center">
  5. <div class="logo-image">
  6. <img src="/src/assets/image/login_logo.png" class="w-full h-full" />
  7. </div>
  8. </div>
  9. <van-form show-error :show-error-message="false" @submit="onSubmit">
  10. <van-cell-group inset>
  11. <van-field v-model="username" name="username" label="用户名" placeholder="用户名" :rules="rules" />
  12. <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码" :rules="rules" />
  13. </van-cell-group>
  14. <div class="login-btn">
  15. <van-button round block type="primary" native-type="submit">
  16. 提交
  17. </van-button>
  18. </div>
  19. </van-form>
  20. </template>
  21. <template v-else>
  22. <div class="flex justify-center getInto">正在进入系统中...</div>
  23. </template>
  24. </div>
  25. </template>
  26. <script setup>
  27. import { ref } from "vue";
  28. import { useLifecycle } from "@hooks/useCommon.js";
  29. import { LOGIN_INTERFACE, USER_ID_LOGIN, WE_CHAT_LOGIN, GET_MESSAGE_LIST } from "@hooks/useApi.js";
  30. // import { addressRedirection, obtainEnterpriseWeChatParameters } from "@utility/corpWXparam"
  31. import { addressRedirection } from "@utility/corpWXparam"
  32. import useShowToast from "../hooks/useToast";
  33. import useRouterStore from "@store/useRouterStore.js";
  34. import useInfoStore from "@store/useInfoStore.js";
  35. import requests from "@common/requests";
  36. const { toastLoading, toastSuccess, toastFail } = useShowToast();
  37. const router = useRouterStore()
  38. const userInfo = useInfoStore()
  39. const username = ref("18122222222");
  40. const password = ref("000000");
  41. const rules = ref([{ required: true }]);
  42. const isCorpWX = ref(false)
  43. const isWX = ref(false)
  44. useLifecycle({
  45. load: () => {
  46. verifyLoginEnvironment()
  47. },
  48. init: () => {
  49. verifyLoginEnvironment()
  50. }
  51. });
  52. function verifyLoginEnvironment() {
  53. const currentEnvironment = navigator.userAgent.toLowerCase();
  54. isCorpWX.value = currentEnvironment.indexOf("wxwork") > 0 ? true : false
  55. isWX.value = currentEnvironment.indexOf("micromessenger") > 0 ? true : false
  56. const href = window.location.href;
  57. if (href.indexOf("userId") > 0) {
  58. let loginUserId = href.substring(href.indexOf("userId=") + "userId=".length);
  59. if (loginUserId.includes('#/')) {
  60. loginUserId = loginUserId.substring(0, loginUserId.indexOf('#/'));
  61. }
  62. loginByUserId(loginUserId);
  63. } else {
  64. // 判断是否为企业微信授权
  65. if (isCorpWX.value) {
  66. // 判断企业微信,是否存在授权
  67. if (href.includes("com/?code")) {
  68. bindIfNessary()
  69. } else {
  70. if (href.indexOf('hasTriedAutoLogin') == -1) {
  71. tryAutoLogin()
  72. } else {
  73. //后台经过验证后,重定向过来带上了userId
  74. let loginUserId = href.substring(href.indexOf("userId=") + "userId=".length);
  75. if (loginUserId.includes('#/')) {
  76. loginUserId = loginUserId.substring(0, loginUserId.indexOf('#/'));
  77. }
  78. loginByUserId(loginUserId);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. // 账号密码登录
  85. function onSubmit(fromVal) {
  86. toastLoading('登陆中...', 0)
  87. requests.post(LOGIN_INTERFACE, { ...fromVal }).then(({ data }) => {
  88. loginProcessing(data)
  89. })
  90. }
  91. // userId登录
  92. function loginByUserId(userId) {
  93. toastLoading('登陆中...', 0)
  94. // requests.post(USER_ID_LOGIN, { params: { userId } }).then(({ data }) => {
  95. requests.post(USER_ID_LOGIN, { userId }).then(({ data }) => {
  96. loginProcessing(data)
  97. })
  98. }
  99. // 授权回调重定向
  100. function tryAutoLogin() {
  101. window.location.href = addressRedirection()
  102. }
  103. function bindIfNessary() {
  104. const href = window.location.href;
  105. const requestUrl = isCorpWX.value ? '/wxcorp/bindCorpWeiXin' : isWX ? '/wechat/bindWeiXin' : '';
  106. if (requestUrl.length > 0) {
  107. //url包括 com/?code 证明为从微信跳转回来的
  108. if (href.includes("com/?code")) {
  109. const url = href; //vue自动在末尾加了 #/ 符号,截取去掉
  110. const jingPosit = url.indexOf("com/") + 4; //获取域名结束的位置
  111. const urlRight = url.substring(jingPosit, url.indexOf('#/') !== -1 ? url.indexOf('#/') : url.length); //url右侧部分
  112. const code = urlRight.substring('?code='.length, urlRight.indexOf('&state='));
  113. const passUserId = urlRight.substring(urlRight.indexOf('&state=') + '&state='.length);
  114. if (passUserId == '0') {
  115. requests.get(WE_CHAT_LOGIN, { code }).then((res) => {
  116. if (res.data != null && ((isWX.value && res.data.wxOpenid != undefined)
  117. || (isCorpWX.value && res.data.corpwxUserid != undefined))) {
  118. loginProcessing(res.data)
  119. window.location.href = '/#/index';
  120. }
  121. })
  122. } else {
  123. //绑定微信账号的回调, 调用后台接口,注册用户
  124. requests.get(requestUrl, { code, userId: passUserId }).then((res) => {
  125. if (res == null) {
  126. toastFail('绑定失败')
  127. return
  128. }
  129. if (res.data != null && ((isWX.value && res.data.wxOpenid != undefined)
  130. || (isCorpWX.value && res.data.corpwxUserid != undefined))) {
  131. loginProcessing(res.data)
  132. window.location.href = '/#/my/center';
  133. }
  134. })
  135. }
  136. }
  137. }
  138. }
  139. // 登陆前的处理
  140. function loginProcessing(data = {}) {
  141. sessionStorage.setItem('token', data.id)
  142. sessionStorage.setItem('isExistBusiness', data.company.isExistBusiness || 0)
  143. userInfo.updateState({
  144. userInfo: data,
  145. token: data.id,
  146. modularList: separateRouting(data.moduleList || []),
  147. permissionList: data.functionList || []
  148. })
  149. router.switchTabBar({
  150. pathName: 'home',
  151. success: function () {
  152. toastSuccess('登录成功')
  153. setTimeout(() => {
  154. getMessageList()
  155. }, 500)
  156. }
  157. })
  158. }
  159. function getMessageList() {
  160. requests.post(GET_MESSAGE_LIST, {}).then(({ data = [] }) => {
  161. userInfo.updateState({
  162. numberOfMessages: data.filter((item) => !item.checked)?.length || ''
  163. })
  164. })
  165. }
  166. function separateRouting(list = []) {
  167. return (list || []).filter(item => item.isMenu && !(['/team', '/system', '/analysis', '/corpreport'].includes(item.path)))
  168. }
  169. </script>
  170. <style lang="scss" scoped>
  171. .logo-image {
  172. width: 150px;
  173. height: 150px;
  174. margin: 100px 0 50px 0;
  175. }
  176. .getInto {
  177. padding-top: 100px;
  178. font-size: 24px;
  179. color: #999;
  180. }
  181. .login-btn {
  182. margin: 24px 16px 16px 16px;
  183. }
  184. </style>