index.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { REQUEST_CONFIG } from '@kasite/uni-app-base/config';
  2. import { WebOauthAuthorize } from '@kasite/uni-app-base/service/base';
  3. import { useDomain } from '@kasite/uni-app-base/hook/system/use-domain';
  4. import store from '@kasite/uni-app-base/store';
  5. /** 缓存是否过期 */
  6. export const useIsExpirationDiy = async () => {
  7. // 当前时间
  8. var timestamp = Date.parse(new Date());
  9. // 缓存中的过期时间
  10. var data_expiration = uni.getStorageSync('data_expiration');
  11. // 如果缓存中没有data_expiration,说明也没有token,还未登录
  12. if (data_expiration) {
  13. // 如果超时了,清除缓存,重新登录
  14. if (timestamp > data_expiration) {
  15. uni.setStorageSync('data_expiration', 0);
  16. return false;
  17. } else {
  18. return true;
  19. }
  20. }
  21. return false;
  22. };
  23. /** 小程序登录 */
  24. export const useSmallProgramLoginDiy = async (app: App.AppInstance) => {
  25. let res;
  26. // #ifdef MP-WEIXIN
  27. res = await smallProgramLoginByMPWEIXINDiy(app);
  28. // #endif
  29. // #ifdef MP-GONGZHONGHAO
  30. res = await smallProgramLoginByGONGZHONGHAODiy(app);
  31. // #endif
  32. return res;
  33. };
  34. const smallProgramLoginByMPWEIXINDiy = async (app: App.AppInstance) => {
  35. return new Promise(async (resolve, reject) => {
  36. // 判断当前token是否过期(存在且没有过期直接返回true)不进行反复登录请求
  37. if ((await useIsExpirationDiy()) && uni.getStorageSync('token')) {
  38. resolve(true);
  39. return;
  40. }
  41. uni.login({
  42. success(res) {
  43. uni.request({
  44. url:
  45. REQUEST_CONFIG.BASE_URL +
  46. 'wsgw/' +
  47. app.globalData.channelId +
  48. '/' +
  49. app.globalData.configKey +
  50. '/' +
  51. app.globalData.hosId +
  52. '/smallProgramLogin_v2.do?cfgKey=' +
  53. app.globalData.wechatConfigKey,
  54. method: 'POST',
  55. data: {
  56. appId: app.globalData.appId,
  57. smallPro_authCode: res.code,
  58. smallPro_systemInfo: app.globalData.smallPro_systemInfo,
  59. smallPro_version: app.globalData.smallPro_version,
  60. },
  61. header: {
  62. 'content-type': 'application/x-www-form-urlencoded',
  63. },
  64. success(resp: any) {
  65. if (resp.data.RespCode == '10000') {
  66. store.commit('setToken', resp.data.token);
  67. store.commit('setOpenId', resp.data.openId);
  68. store.commit('setUnionId', resp.data.unionId);
  69. store.commit('setSmallProOpenId', resp.data.smallProOpenId);
  70. store.commit('setWechatOpenId', resp.data.wechatOpenid);
  71. uni.setStorageSync('isCall', 0);
  72. // publicFn.preserMember();
  73. // 当前时间
  74. var timestamp = Date.parse(new Date());
  75. // 加上过期期限
  76. var expiration = timestamp + resp.data.expireTime;
  77. uni.setStorageSync('data_expiration', expiration);
  78. resolve(resp);
  79. } else {
  80. if (
  81. resp.data.RespCode == '-14019' &&
  82. resp.data.RespMessage &&
  83. resp.data.RespMessage.indexOf('非白名单用户') >= 0
  84. ) {
  85. uni.showLoading({
  86. title: '系统升级中...',
  87. mask: true,
  88. });
  89. } else {
  90. uni.showLoading({
  91. title: resp.data.RespMessage || '网络异常!',
  92. mask: true,
  93. });
  94. }
  95. resolve(false);
  96. }
  97. },
  98. fail(error) {
  99. uni.showLoading({
  100. title: '网络异常!',
  101. mask: true,
  102. });
  103. reject(error);
  104. },
  105. });
  106. },
  107. fail(error) {
  108. uni.showLoading({
  109. title: '网络异常!',
  110. mask: true,
  111. });
  112. reject(error);
  113. },
  114. });
  115. });
  116. };
  117. const smallProgramLoginByGONGZHONGHAODiy = (app: App.AppInstance) => {
  118. return new Promise(async (resolve, reject) => {
  119. // if ((await useIsExpirationDiy()) && uni.getStorageSync('token')) {
  120. // resolve(true);
  121. // if (!store.state.token) {
  122. // store.commit('setToken', uni.getStorageSync('token'));
  123. // }
  124. // return;
  125. // }
  126. const token = new URL(location.href).searchParams.get('token');
  127. const openid = new URL(location.href).searchParams.get('openid');
  128. console.log(token);
  129. const { channelId, configKey, hosId, wechatConfigKey } = app.globalData;
  130. if (!token) {
  131. // 没有授权登录过的,需要重定向到授权页面
  132. const url = WebOauthAuthorize({
  133. hosId,
  134. wechatConfigKey,
  135. toUrl: `${useDomain(true)}/#/`, //encodeURIComponent(`${useDomain(true)}/#/`),
  136. });
  137. location.href = url;
  138. } else {
  139. store.commit('setToken', token);
  140. store.commit('setOpenId', openid);
  141. // 当前时间
  142. var timestamp = Date.parse(new Date());
  143. // 加上过期期限
  144. var expiration = timestamp + 24 * 60 * 60 * 1000;
  145. uni.setStorageSync('data_expiration', expiration);
  146. // location.href = `${useDomain(true)}/#/`;
  147. resolve(true);
  148. }
  149. });
  150. };