loginOauth2.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <view class="zai-box">
  3. <!-- 登录加载弹窗 -->
  4. <view class="cu-load load-modal">
  5. <image src="https://static.jeecg.com/upload/test/login4_1595818039175.png" mode="aspectFit"
  6. class="round"></image>
  7. <view class="gray-text">正在登录中...</view>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import {ACCESS_TOKEN} from "@/common/util/constants"
  13. import {mapActions} from "vuex"
  14. import {isOAuth2AppEnv, getUrlParams} from '@/common/util/util'
  15. import configService from '@/common/service/config.service.js';
  16. export default {
  17. data() {
  18. return {
  19. loading: false,
  20. env: {
  21. thirdType: '',
  22. thirdApp: false,
  23. wxWork: false,
  24. dingtalk: false,
  25. },
  26. redirectUrl: ''
  27. };
  28. },
  29. beforeCreate() {
  30. // 如果当前 不是 OAuth2APP环境,就重定向到 /user/login 页面
  31. if (!isOAuth2AppEnv()) {
  32. this.$Router.replace({name: 'login'})
  33. }
  34. },
  35. created() {
  36. this.checkEnv()
  37. this.doOAuth2Login()
  38. },
  39. methods: {
  40. ...mapActions(['ThirdLogin']),
  41. /** 检测当前的环境 */
  42. checkEnv() {
  43. // 判断当时是否是企业微信环境
  44. if (/wxwork/i.test(navigator.userAgent)) {
  45. this.env.thirdApp = true
  46. this.env.wxWork = true
  47. //this.env.thirdType='wechat_enterprise'
  48. }
  49. // 判断当时是否是钉钉环境
  50. if (/dingtalk/i.test(navigator.userAgent)) {
  51. this.env.thirdApp = true
  52. this.env.dingtalk = true
  53. //this.env.thirdType='dingtalk'
  54. }
  55. },
  56. /** 进行OAuth2登录操作 */
  57. doOAuth2Login() {
  58. if (this.env.thirdApp) {
  59. // 判断是否携带了Token,是就说明登录成功
  60. this.redirectUrl = ''
  61. let search = window.location.search;
  62. if (search.indexOf('hasToken') > 0) {
  63. let obj = getUrlParams(search);
  64. if (obj.params.redirect) {
  65. this.redirectUrl = decodeURIComponent(decodeURIComponent(obj.params.redirect));
  66. this.goRedirectUrl();
  67. } else {
  68. alert('hasToken参数错误!')
  69. }
  70. } else if (search.indexOf('oauth2LoginToken') > 0) {
  71. let obj = getUrlParams(search);
  72. this.env.thirdType = obj.params.thirdType;
  73. let token = obj.params.oauth2LoginToken;
  74. if (obj.params.redirect) {
  75. this.redirectUrl = decodeURIComponent(obj.params.redirect)
  76. }
  77. this.doThirdLogin(token)
  78. } else if (this.env.wxWork) {
  79. this.doWechatEnterpriseOAuth2Login()
  80. } else if (this.env.dingtalk) {
  81. this.doDingTalkOAuth2Login()
  82. }
  83. }
  84. },
  85. // 根据token执行登录
  86. doThirdLogin(token) {
  87. this.ThirdLogin({token: token, thirdType: this.env.thirdType}).then(res => {
  88. if (res.data.success) {
  89. this.loginSuccess()
  90. } else {
  91. this.requestFailed(res)
  92. }
  93. }).catch((e) => {
  94. alert(e.message || e)
  95. })
  96. },
  97. loginSuccess() {
  98. this.$Router.replaceAll({name: 'index'})
  99. },
  100. requestFailed(err) {
  101. this.$message.warning("登录失败")
  102. },
  103. goRedirectUrl() {
  104. let obj = getUrlParams(this.redirectUrl);
  105. let path = obj.url;
  106. let params = obj.params;
  107. if (params.info) {
  108. let temp = JSON.parse(params.info);
  109. let query = {
  110. instanceId: temp.procInsId,
  111. taskId: temp.taskId,
  112. taskDefKey: temp.taskDefKey
  113. }
  114. if (temp.claim == 1) {
  115. query['claim'] = 1
  116. }
  117. this.$Router.replaceAll({path, query})
  118. } else {
  119. this.$Router.replaceAll({path})
  120. }
  121. },
  122. /** 企业微信OAuth2登录 */
  123. doWechatEnterpriseOAuth2Login() {
  124. this.sysOAuth2Login('wechat_enterprise')
  125. },
  126. /** 钉钉OAuth2登录 */
  127. doDingTalkOAuth2Login() {
  128. this.sysOAuth2Login('dingtalk')
  129. },
  130. /** 后台构造oauth2登录地址 */
  131. sysOAuth2Login(source) {
  132. let domainURL = configService.apiUrl;
  133. let url = `${domainURL}/sys/thirdLogin/oauth2/${source}/login`;
  134. let state = window.location.origin + window.location.search
  135. url += `?state=${encodeURIComponent(state)}`;
  136. console.log('sysOAuth2Login====》', url)
  137. window.location.href = url;
  138. },
  139. }
  140. }
  141. </script>
  142. <style>
  143. .login-paddingtop {
  144. padding-top: 100 upx;
  145. }
  146. .zai-box {
  147. padding: 0 20 upx;
  148. padding-top: 100 upx;
  149. position: relative;
  150. }
  151. .zai-logo {
  152. width: 200 upx;
  153. height: 150px;
  154. }
  155. .zai-title {
  156. font-size: 58 upx;
  157. color: #000000;
  158. text-align: center;
  159. }
  160. .input-placeholder, .zai-input {
  161. color: #94afce;
  162. }
  163. .zai-label {
  164. padding: 60 upx 0;
  165. text-align: center;
  166. font-size: 30 upx;
  167. color: #a7b6d0;
  168. }
  169. .zai-btn {
  170. background: #ff65a3;
  171. color: #fff;
  172. border: 0;
  173. border-radius: 100 upx;
  174. font-size: 36 upx;
  175. }
  176. .zai-btn:after {
  177. border: 0;
  178. }
  179. /*按钮点击效果*/
  180. .zai-btn.button-hover {
  181. transform: translate(1 upx, 1 upx);
  182. }
  183. </style>