exportToPlugin.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * @Author: 'warrior' 772238918@qq.com
  3. * @Date: 2024-05-30 15:59:43
  4. * @LastEditors: 'warrior' 772238918@qq.com
  5. * @LastEditTime: 2024-06-03 18:12:31
  6. * @FilePath: \nova-wapp\exportToPlugin.js
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. */
  9. const CONFIG = require("./config.js");
  10. let {
  11. appid,
  12. company,
  13. rootPage,
  14. } = CONFIG.default
  15. module.exports = {
  16. appid,
  17. company,
  18. rootPage,
  19. updateLocal(config, isUpdate) {
  20. console.log('===========================================');
  21. console.log('======= 插件授权回调 =======');
  22. console.log('config:', config);
  23. console.log('isUpdate:', isUpdate);
  24. console.log('===========================================');
  25. isUpdate && wx.setStorageSync('config', config)
  26. if (config.token) {
  27. wx.setStorageSync("sessionToken", config.token)
  28. console.log('✅ 已保存 sessionToken');
  29. }
  30. if (config.userInfo) {
  31. wx.setStorageSync("userInfo", config.userInfo)
  32. console.log('✅ 已保存 userInfo');
  33. }
  34. if (config.userLogin) {
  35. wx.setStorageSync("userLogin", config.userLogin)
  36. console.log('✅ 已保存 userLogin:', config.userLogin);
  37. }
  38. // 授权成功后,自动返回首页
  39. if (config.userLogin && isUpdate) {
  40. console.log('🔄 授权成功,准备返回首页...');
  41. // 检查是否有待处理的跳转
  42. const pendingNavigation = wx.getStorageSync('pendingNavigation');
  43. // 延迟一下,确保数据保存完成
  44. setTimeout(() => {
  45. const pages = getCurrentPages();
  46. console.log('当前页面栈层数:', pages.length);
  47. if (pages.length > 1) {
  48. // 如果有上一页,返回上一页
  49. console.log('📱 返回上一页');
  50. wx.navigateBack({
  51. delta: 1,
  52. success: () => {
  53. console.log('✅ 返回成功');
  54. // 如果有待处理的跳转,触发跳转
  55. if (pendingNavigation) {
  56. console.log('🎯 检测到待处理的跳转:', pendingNavigation);
  57. wx.removeStorageSync('pendingNavigation');
  58. // 再延迟一下,确保页面已经返回并刷新
  59. setTimeout(() => {
  60. // 触发页面的跳转方法
  61. const currentPages = getCurrentPages();
  62. const currentPage = currentPages[currentPages.length - 1];
  63. if (currentPage && currentPage.selectComponent) {
  64. const homeComponent = currentPage.selectComponent('#home-component');
  65. if (homeComponent && homeComponent.navigateToConsultation) {
  66. console.log('✅ 触发咨询页面跳转');
  67. homeComponent.navigateToConsultation();
  68. } else {
  69. console.warn('⚠️ 未找到 home 组件或 navigateToConsultation 方法');
  70. }
  71. }
  72. }, 500);
  73. }
  74. },
  75. fail: (err) => {
  76. console.error('❌ 返回失败:', err);
  77. // 如果返回失败,重新加载首页
  78. wx.reLaunch({
  79. url: rootPage || '/nova-pbf/pages/index/index'
  80. });
  81. }
  82. });
  83. } else {
  84. // 如果没有上一页,重新加载首页
  85. console.log('📱 重新加载首页');
  86. wx.reLaunch({
  87. url: rootPage || '/nova-pbf/pages/index/index'
  88. });
  89. }
  90. }, 500);
  91. }
  92. console.log('===========================================');
  93. },
  94. getCode() {
  95. return new Promise((resolve) => {
  96. wx.login({
  97. success: function (res) {
  98. if (res.code) {
  99. console.log(res.code);
  100. resolve(res.code)
  101. }
  102. },
  103. fail: function (err) {
  104. console.warn('小程序wx.login失败');
  105. resolve()
  106. }
  107. });
  108. })
  109. },
  110. restart(err) {
  111. console.log(err);
  112. wx.exitMiniProgram()
  113. },
  114. router(type, url = '/index') {
  115. switch (type) {
  116. case 'navigateBack':
  117. wx.navigateBack({
  118. delta: url || 1,
  119. fail: function () {
  120. wx.reLaunch({
  121. url: "/index",
  122. });
  123. },
  124. })
  125. break;
  126. case 'navigateTo':
  127. wx.navigateTo({
  128. url: url
  129. })
  130. break;
  131. case 'reLaunch':
  132. wx.reLaunch({
  133. url: url
  134. })
  135. break;
  136. case 'redirectTo':
  137. wx.redirectTo({
  138. url: url
  139. })
  140. break;
  141. default:
  142. break;
  143. }
  144. },
  145. // 获取本地存储文件大小
  146. getFileInfo(filePath) {
  147. return new Promise((result) => {
  148. wx.getFileInfo({
  149. filePath: filePath,
  150. success(res) {
  151. result(res.size)
  152. },
  153. fail(err) {
  154. result(0)
  155. }
  156. })
  157. })
  158. }
  159. }