login.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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, GET_MESSAGE_LIST } 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. init: () => {
  38. verifyLoginEnvironment()
  39. }
  40. });
  41. function verifyLoginEnvironment() {
  42. const currentEnvironment = navigator.userAgent.toLowerCase();
  43. isCorpWX.value = currentEnvironment.indexOf("wxwork") > 0 ? true : false
  44. isWX.value = currentEnvironment.indexOf("micromessenger") > 0 ? true : false
  45. const href = window.location.href;
  46. if (href.indexOf("userId") > 0) {
  47. let loginUserId = href.substring(href.indexOf("userId=") + "userId=".length);
  48. if (loginUserId.includes('#/')) {
  49. loginUserId = loginUserId.substring(0, loginUserId.indexOf('#/'));
  50. }
  51. loginByUserId(loginUserId);
  52. } else {
  53. // 判断是否为企业微信授权
  54. if (isCorpWX.value) {
  55. // 判断企业微信,是否存在授权
  56. if (href.includes("com/?code")) {
  57. bindIfNessary()
  58. } else {
  59. if (href.indexOf('hasTriedAutoLogin') == -1) {
  60. tryAutoLogin()
  61. } else {
  62. //后台经过验证后,重定向过来带上了userId
  63. let loginUserId = href.substring(href.indexOf("userId=") + "userId=".length);
  64. if (loginUserId.includes('#/')) {
  65. loginUserId = loginUserId.substring(0, loginUserId.indexOf('#/'));
  66. }
  67. loginByUserId(loginUserId);
  68. }
  69. }
  70. }
  71. }
  72. }
  73. // 账号密码登录
  74. function onSubmit(fromVal) {
  75. toastLoading('登陆中...', 0)
  76. requests.post(LOGIN_INTERFACE, { ...fromVal }).then(({ data }) => {
  77. loginProcessing(data)
  78. })
  79. }
  80. // userId登录
  81. function loginByUserId(userId) {
  82. toastLoading('登陆中...', 0)
  83. // requests.post(USER_ID_LOGIN, { params: { userId } }).then(({ data }) => {
  84. requests.post(USER_ID_LOGIN, { userId }).then(({ data }) => {
  85. loginProcessing(data)
  86. })
  87. }
  88. // 授权回调重定向
  89. function tryAutoLogin() {
  90. window.location.href = addressRedirection()
  91. }
  92. function bindIfNessary() {
  93. const href = window.location.href;
  94. const requestUrl = isCorpWX.value ? '/wxcorp/bindCorpWeiXin' : isWX ? '/wechat/bindWeiXin' : '';
  95. if (requestUrl.length > 0) {
  96. //url包括 com/?code 证明为从微信跳转回来的
  97. if (href.includes("com/?code")) {
  98. const url = href; //vue自动在末尾加了 #/ 符号,截取去掉
  99. const jingPosit = url.indexOf("com/") + 4; //获取域名结束的位置
  100. const urlRight = url.substring(jingPosit, url.indexOf('#/') !== -1 ? url.indexOf('#/') : url.length); //url右侧部分
  101. const code = urlRight.substring('?code='.length, urlRight.indexOf('&state='));
  102. const passUserId = urlRight.substring(urlRight.indexOf('&state=') + '&state='.length);
  103. if (passUserId == '0') {
  104. requests.get(WE_CHAT_LOGIN, { code }).then((res) => {
  105. if (res.data != null && ((isWX.value && res.data.wxOpenid != undefined)
  106. || (isCorpWX.value && res.data.corpwxUserid != undefined))) {
  107. loginProcessing(res.data)
  108. window.location.href = '/#/index';
  109. }
  110. })
  111. } else {
  112. //绑定微信账号的回调, 调用后台接口,注册用户
  113. requests.get(requestUrl, { code, userId: passUserId }).then((res) => {
  114. if (res == null) {
  115. toastFail('绑定失败')
  116. return
  117. }
  118. if (res.data != null && ((isWX.value && res.data.wxOpenid != undefined)
  119. || (isCorpWX.value && res.data.corpwxUserid != undefined))) {
  120. loginProcessing(res.data)
  121. window.location.href = '/#/my/center';
  122. }
  123. })
  124. }
  125. }
  126. }
  127. }
  128. // 登陆前的处理
  129. function loginProcessing(data = {}) {
  130. sessionStorage.setItem('token', data.id)
  131. userInfo.updateState({
  132. userInfo: data,
  133. token: data.id,
  134. modularList: separateRouting(data.moduleList || []),
  135. permissionList: data.functionList || []
  136. })
  137. router.switchTabBar({
  138. pathName: 'home',
  139. success: function () {
  140. alert(isCorpWX.value)
  141. if (isCorpWX.value) {
  142. obtainEnterpriseWeChatParameters(data)
  143. }
  144. toastSuccess('登陆成功')
  145. setTimeout(() => {
  146. getMessageList()
  147. }, 500)
  148. }
  149. })
  150. }
  151. function getMessageList() {
  152. requests.post(GET_MESSAGE_LIST, {}).then(({ data = [] }) => {
  153. userInfo.updateState({
  154. numberOfMessages: data.filter((item) => !item.checked)?.length || ''
  155. })
  156. })
  157. }
  158. function separateRouting(list = []) {
  159. return (list || []).filter(item => item.isMenu && !(['/team', '/system', '/analysis', '/corpreport'].includes(item.path)))
  160. }
  161. </script>
  162. <style lang="scss" scoped></style>