login.vue 5.3 KB

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