App.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <script>
  2. import Vue from 'vue'
  3. import BASE_URL from './config.js'
  4. import {mapActions} from 'vuex'
  5. import loginService from '@/api/auth/loginService'
  6. import * as dd from 'dingtalk-jsapi'
  7. export default {
  8. onLaunch: function() {
  9. uni.getSystemInfo({
  10. success: function(e) {
  11. // #ifndef MP
  12. Vue.prototype.StatusBar = e.statusBarHeight;
  13. if (e.platform == 'android') {
  14. Vue.prototype.CustomBar = e.statusBarHeight + 50;
  15. } else {
  16. Vue.prototype.CustomBar = e.statusBarHeight + 45;
  17. };
  18. // #endif
  19. // #ifdef MP-WEIXIN
  20. Vue.prototype.StatusBar = e.statusBarHeight;
  21. let custom = wx.getMenuButtonBoundingClientRect();
  22. Vue.prototype.Custom = custom;
  23. Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
  24. // #endif
  25. // #ifdef MP-ALIPAY
  26. Vue.prototype.StatusBar = e.statusBarHeight;
  27. Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
  28. // #endif
  29. }
  30. })
  31. Vue.prototype.ColorList = [{
  32. title: '嫣红',
  33. name: 'red',
  34. color: '#e54d42'
  35. },
  36. {
  37. title: '桔橙',
  38. name: 'orange',
  39. color: '#f37b1d'
  40. },
  41. {
  42. title: '明黄',
  43. name: 'yellow',
  44. color: '#fbbd08'
  45. },
  46. {
  47. title: '橄榄',
  48. name: 'olive',
  49. color: '#8dc63f'
  50. },
  51. {
  52. title: '森绿',
  53. name: 'green',
  54. color: '#39b54a'
  55. },
  56. {
  57. title: '天青',
  58. name: 'cyan',
  59. color: '#1cbbb4'
  60. },
  61. {
  62. title: '海蓝',
  63. name: 'blue',
  64. color: '#0081ff'
  65. },
  66. {
  67. title: '姹紫',
  68. name: 'purple',
  69. color: '#6739b6'
  70. },
  71. {
  72. title: '木槿',
  73. name: 'mauve',
  74. color: '#9c26b0'
  75. },
  76. {
  77. title: '桃粉',
  78. name: 'pink',
  79. color: '#e03997'
  80. },
  81. {
  82. title: '棕褐',
  83. name: 'brown',
  84. color: '#a5673f'
  85. },
  86. {
  87. title: '玄灰',
  88. name: 'grey',
  89. color: '#8799a3'
  90. },
  91. {
  92. title: '草灰',
  93. name: 'gray',
  94. color: '#aaaaaa'
  95. },
  96. {
  97. title: '墨黑',
  98. name: 'black',
  99. color: '#333333'
  100. },
  101. {
  102. title: '雅白',
  103. name: 'white',
  104. color: '#ffffff'
  105. },
  106. ]
  107. },
  108. onShow: function() {
  109. console.log('App Show')
  110. },
  111. onHide: function() {
  112. console.log('App Hide')
  113. },
  114. created: async function() {
  115. const urlParams = this.getUrlParams();
  116. if (await this.tryDingTalkAutoLogin(urlParams)) {
  117. return;
  118. }
  119. // 获取 URL 中的 code 参数
  120. const code = urlParams.get('code');
  121. console.log('传入 code -------');
  122. console.log(code);
  123. if (code) {
  124. try {
  125. // 发起请求获取用户信息
  126. const [err, res] = await uni.request({
  127. url: BASE_URL + `/flowable-server/app/proxy?appid=wxbcd608a92d6f9412&secret=69d382e18089367566df595b102fe756&code=` + code,
  128. method: 'GET'
  129. });
  130. if (res && res.statusCode === 200) {
  131. console.log('用户信息获取成功-----------');
  132. const data = res.data;
  133. console.log(data);
  134. console.log(data.openId);
  135. // 将微信用户的 openId 存到本地
  136. uni.setStorageSync('openId', data.openId);
  137. console.log('getStorageSync-> ', uni.getStorageSync('openId'));
  138. } else {
  139. console.error('获取用户信息失败:', res.data);
  140. }
  141. } catch (err) {
  142. console.error('请求失败:', err);
  143. }
  144. }
  145. // 判断当前微信号的openId 是否已经存入到数据库了,如果存入了,那么就直接通过openId匹配登录,否则就进入登录页面
  146. const flag = true;
  147. const localOpenId = uni.getStorageSync('openId');
  148. if (localOpenId) {
  149. try {
  150. // 发起请求获取库中的openId
  151. const [err, res] = await uni.request({
  152. url: BASE_URL + `/system-server/sys/user/allOpenIds`,
  153. method: 'GET'
  154. });
  155. if (res && res.statusCode === 200) {
  156. const data = res.data;
  157. console.log('allOpenIds:', data);
  158. // 确保 data 是数组
  159. if (Array.isArray(data) && data.includes(localOpenId)) {
  160. // 包含的话,则直接根据 openId来实现登录
  161. console.log(`data 包含 openId: ${localOpenId}`);
  162. const [err, res] = await uni.request({
  163. url: BASE_URL + `/auth-server/user/sys/wxLogin`,
  164. method: 'POST',
  165. data: { openId: localOpenId },
  166. header: {
  167. 'domain': 'ydddl'
  168. }
  169. });
  170. if (res && res.statusCode === 200) {
  171. this.$store.commit('SET_TOKEN',res.data.token);
  172. await this.refreshUserInfo();
  173. uni.reLaunch({
  174. url: '../index/index',
  175. });
  176. }
  177. }
  178. }
  179. } catch (err) {
  180. console.error('请求失败:', err);
  181. }
  182. }
  183. if (flag) {
  184. console.log('进来了');
  185. }
  186. // 获取 URL 中的 'flow' 参数
  187. const flowParam = this.$route.query.flow;
  188. // 将 URL 编码的 'flow' 参数解析为 JSON 对象
  189. if (!flowParam) {
  190. return;
  191. }
  192. const flowObject = JSON.parse(decodeURIComponent(flowParam));
  193. console.log("flowObject.openId---> ", flowObject.openId)
  194. if (flowObject.openId) {
  195. const [err, res] = await uni.request({
  196. url: BASE_URL + `/auth-server/user/sys/wxLogin`,
  197. method: 'POST',
  198. data: { openId: flowObject.openId },
  199. header: {
  200. 'domain': 'ydddl'
  201. }
  202. });
  203. if (res && res.statusCode === 200) {
  204. this.$store.commit('SET_TOKEN',res.data.token);
  205. await this.refreshUserInfo();
  206. }
  207. }
  208. },
  209. methods: {
  210. ...mapActions(['refreshUserInfo']),
  211. getUrlParams() {
  212. const params = new URLSearchParams(window.location.search || '');
  213. const hash = window.location.hash || '';
  214. const hashQueryIndex = hash.indexOf('?');
  215. if (hashQueryIndex > -1) {
  216. const hashParams = new URLSearchParams(hash.substring(hashQueryIndex + 1));
  217. hashParams.forEach((value, key) => {
  218. if (!params.has(key)) {
  219. params.set(key, value);
  220. }
  221. });
  222. }
  223. return params;
  224. },
  225. isDingTalkEnv() {
  226. return /DingTalk/i.test(window.navigator.userAgent || '');
  227. },
  228. loadDingTalkJs() {
  229. return Promise.resolve(dd);
  230. },
  231. requestDingTalkAuthCode(corpId) {
  232. return new Promise((resolve, reject) => {
  233. if (!dd ) {
  234. reject(new Error('钉钉资源未加载成功,请联系管理员'));
  235. return;
  236. }
  237. dd.getAuthCode({
  238. corpId: corpId,
  239. onSuccess: (res) => resolve(res.code || res.authCode),
  240. onFail: reject
  241. });
  242. dd.error(reject);
  243. });
  244. },
  245. async handleDingTalkLoginSuccess(data) {
  246. this.$store.commit('SET_TOKEN', data.token);
  247. await this.refreshUserInfo();
  248. },
  249. async tryDingTalkAutoLogin(urlParams) {
  250. if (!this.isDingTalkEnv()) {
  251. return false;
  252. }
  253. if (this.$store.state.user.token) {
  254. return true;
  255. }
  256. const tenantId = urlParams.get('tenantId') || uni.getStorageSync('dingTalkTenantId') || '10009';
  257. try {
  258. const clientConfig = await loginService.dingTalkClientConfig(tenantId);
  259. if (!clientConfig || !clientConfig.corpId) {
  260. throw new Error('DingTalk corpId is missing');
  261. }
  262. await this.loadDingTalkJs();
  263. const authCode = await this.requestDingTalkAuthCode(clientConfig.corpId);
  264. const loginResult = await loginService.dingTalkLogin({
  265. authCode: authCode,
  266. tenantId: tenantId
  267. });
  268. if (loginResult.bindRequired) {
  269. uni.setStorageSync('dingTalkBindKey', loginResult.bindKey);
  270. uni.setStorageSync('dingTalkTenantId', loginResult.tenantId || tenantId);
  271. uni.reLaunch({
  272. url: '/pages/login/login?bind=dingtalk'
  273. });
  274. return true;
  275. }
  276. await this.handleDingTalkLoginSuccess(loginResult);
  277. uni.reLaunch({
  278. url: '/pages/index/index'
  279. });
  280. } catch (e) {
  281. if(!e.isTrusted){
  282. uni.showToast({
  283. title: '钉钉异常,请联系管理员',
  284. icon: 'none'
  285. });
  286. }
  287. }
  288. return true;
  289. },
  290. }
  291. }
  292. </script>
  293. <style lang="scss">
  294. /*每个页面公共css */
  295. @import "@/uni_modules/uview-ui/index.scss";
  296. @import "static/css/jeeplus.scss";
  297. /* @import 'jeeplus-flowable/lib/jeeplus-flowable.css'; */
  298. @import "static/css/main.css";
  299. @import "static/css/icon.css";
  300. /*每个页面公共css */
  301. </style>