exportToPlugin.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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('=== restart 方法被调用 ===');
  112. console.log('错误信息:', err);
  113. // 不要直接退出小程序,而是尝试重新登录
  114. wx.showModal({
  115. title: '提示',
  116. content: '登录状态异常,是否重新登录?',
  117. showCancel: true,
  118. cancelText: '取消',
  119. confirmText: '重新登录',
  120. success: async (result) => {
  121. if (result.confirm) {
  122. try {
  123. // 清除登录状态
  124. wx.removeStorageSync("sessionToken");
  125. wx.removeStorageSync("userLogin");
  126. // 重新加载首页
  127. const rootPage = getApp().globalData.rootPage || getApp().globalData.defaultTabBar?.list?.[0]?.pagePath || '/pages/index/index';
  128. wx.reLaunch({
  129. url: rootPage,
  130. success: () => {
  131. console.log('✅ 重新加载首页成功');
  132. },
  133. fail: (err) => {
  134. console.error('❌ 重新加载失败:', err);
  135. wx.showToast({
  136. title: '加载失败,请重试',
  137. icon: 'none'
  138. });
  139. }
  140. });
  141. } catch (error) {
  142. console.error('❌ 重新登录失败:', error);
  143. }
  144. }
  145. }
  146. });
  147. },
  148. router(type, url = '/index') {
  149. switch (type) {
  150. case 'navigateBack':
  151. wx.navigateBack({
  152. delta: url || 1,
  153. fail: function () {
  154. wx.reLaunch({
  155. url: "/index",
  156. });
  157. },
  158. })
  159. break;
  160. case 'navigateTo':
  161. wx.navigateTo({
  162. url: url
  163. })
  164. break;
  165. case 'reLaunch':
  166. wx.reLaunch({
  167. url: url
  168. })
  169. break;
  170. case 'redirectTo':
  171. wx.redirectTo({
  172. url: url
  173. })
  174. break;
  175. default:
  176. break;
  177. }
  178. },
  179. // 获取本地存储文件大小
  180. getFileInfo(filePath) {
  181. return new Promise((result) => {
  182. wx.getFileInfo({
  183. filePath: filePath,
  184. success(res) {
  185. result(res.size)
  186. },
  187. fail(err) {
  188. result(0)
  189. }
  190. })
  191. })
  192. }
  193. }