index.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { REQUEST_CONFIG } from '../../config';
  2. import { WebOauth2Authorize } from '../../service/base';
  3. import { useDomain } from '../use-domain';
  4. /** 缓存是否过期 */
  5. export const useIsExpiration = async () => {
  6. // 当前时间
  7. var timestamp = Date.parse(new Date());
  8. // 缓存中的过期时间
  9. var data_expiration = uni.getStorageSync('data_expiration');
  10. // 如果缓存中没有data_expiration,说明也没有token,还未登录
  11. if (data_expiration) {
  12. // 如果超时了,清除缓存,重新登录
  13. if (timestamp > data_expiration) {
  14. uni.setStorageSync('data_expiration', 0);
  15. return false;
  16. } else {
  17. return true;
  18. }
  19. }
  20. return false;
  21. };
  22. /** 小程序登录 */
  23. export const useSmallProgramLogin = async (app: App.AppInstance) => {
  24. let res;
  25. // #ifdef MP-WEIXIN
  26. res = await smallProgramLoginByMPWEIXIN(app);
  27. // #endif
  28. // #ifdef MP-GONGZHONGHAO
  29. res = await smallProgramLoginByGONGZHONGHAO(app);
  30. // #endif
  31. return res;
  32. };
  33. const smallProgramLoginByMPWEIXIN = async (app: App.AppInstance) => {
  34. return new Promise(async (resolve, reject) => {
  35. // 判断当前token是否过期(存在且没有过期直接返回true)不进行反复登录请求
  36. if ((await useIsExpiration()) && uni.getStorageSync('token')) {
  37. resolve(true);
  38. return;
  39. }
  40. uni.login({
  41. success(res) {
  42. uni.request({
  43. url:
  44. REQUEST_CONFIG.BASE_URL +
  45. 'wsgw/' +
  46. app.globalData.channelId +
  47. '/' +
  48. app.globalData.configKey +
  49. '/' +
  50. app.globalData.hosId +
  51. '/smallProgramLogin_v2.do?cfgKey=' +
  52. app.globalData.wechatConfigKey,
  53. method: 'POST',
  54. data: {
  55. appId: app.globalData.appId,
  56. smallPro_authCode: res.code,
  57. smallPro_systemInfo: app.globalData.smallPro_systemInfo,
  58. smallPro_version: app.globalData.smallPro_version,
  59. },
  60. header: {
  61. 'content-type': 'application/x-www-form-urlencoded',
  62. },
  63. success(resp: any) {
  64. if (resp.data.RespCode == '10000') {
  65. uni.setStorageSync('token', resp.data.token);
  66. getApp().globalData.token = resp.data.token;
  67. uni.setStorageSync('openid', resp.data.openId);
  68. uni.setStorageSync('unionid', resp.data.unionId);
  69. uni.setStorageSync('smallProOpenId', resp.data.smallProOpenId);
  70. uni.setStorageSync('wechatOpenid', 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 smallProgramLoginByGONGZHONGHAO = (app: App.AppInstance) => {
  118. return new Promise(async (resolve, reject) => {
  119. if ((await useIsExpiration()) && uni.getStorageSync('token')) {
  120. resolve(true);
  121. return;
  122. }
  123. const code =
  124. new URL(location.href).searchParams.get('code') || uni.getStorageSync('login_code');
  125. if (!code) {
  126. // 没有授权登录过的,需要重定向到授权页面
  127. location.href = WebOauth2Authorize({
  128. appid: app.globalData.appId,
  129. redirect_uri: encodeURIComponent(`${useDomain()}/#/`),
  130. });
  131. } else {
  132. uni.setStorageSync('login_code', code);
  133. location.href = `${useDomain()}/#/`;
  134. const { channelId, configKey, hosId, wechatConfigKey } = app.globalData;
  135. uni.request({
  136. url: `${REQUEST_CONFIG.BASE_URL}wsgw/${channelId}/${configKey}/${hosId}/smallProgramLogin_v2.do?cfgKey=${wechatConfigKey}`,
  137. method: 'POST',
  138. data: {
  139. appId: app.globalData.appId,
  140. smallPro_authCode: code,
  141. smallPro_systemInfo: app.globalData.smallPro_systemInfo,
  142. smallPro_version: app.globalData.smallPro_version,
  143. },
  144. header: {
  145. 'content-type': 'application/x-www-form-urlencoded',
  146. },
  147. success(resp: any) {
  148. console.log('1111', resp);
  149. if (resp.data.RespCode == '10000') {
  150. uni.setStorageSync('token', resp.data.token);
  151. getApp().globalData.token = resp.data.token;
  152. uni.setStorageSync('openid', resp.data.openId);
  153. uni.setStorageSync('unionid', resp.data.unionId);
  154. uni.setStorageSync('smallProOpenId', resp.data.smallProOpenId);
  155. uni.setStorageSync('wechatOpenid', resp.data.wechatOpenid);
  156. uni.setStorageSync('isCall', 0);
  157. // publicFn.preserMember();
  158. // 当前时间
  159. var timestamp = Date.parse(new Date());
  160. // 加上过期期限
  161. var expiration = timestamp + resp.data.expireTime;
  162. uni.setStorageSync('data_expiration', expiration);
  163. resolve(resp);
  164. } else {
  165. if (
  166. resp.data.RespCode == '-14019' &&
  167. resp.data.RespMessage &&
  168. resp.data.RespMessage.indexOf('非白名单用户') >= 0
  169. ) {
  170. uni.showLoading({
  171. title: '系统升级中...',
  172. mask: true,
  173. });
  174. } else {
  175. uni.showLoading({
  176. title: resp.data.RespMessage || '网络异常!',
  177. mask: true,
  178. });
  179. }
  180. resolve(false);
  181. }
  182. },
  183. fail(error) {
  184. console.log(error);
  185. uni.showLoading({
  186. title: '网络异常!',
  187. mask: true,
  188. });
  189. reject(error);
  190. },
  191. });
  192. }
  193. });
  194. };