index.ts 4.5 KB

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