exportToPlugin.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. const configData = CONFIG.default || CONFIG;
  11. let {
  12. appid,
  13. company,
  14. rootPage,
  15. } = configData
  16. module.exports = {
  17. appid,
  18. company,
  19. rootPage,
  20. updateLocal(config, isUpdate) {
  21. console.log('===========================================');
  22. console.log('======= 插件授权回调 =======');
  23. console.log('config:', config);
  24. console.log('isUpdate:', isUpdate);
  25. console.log('===========================================');
  26. isUpdate && wx.setStorageSync('config', config)
  27. if (config.token) {
  28. wx.setStorageSync("sessionToken", config.token)
  29. console.log('✅ 已保存 sessionToken');
  30. }
  31. if (config.userInfo) {
  32. wx.setStorageSync("userInfo", config.userInfo)
  33. console.log('✅ 已保存 userInfo');
  34. }
  35. if (config.userLogin) {
  36. wx.setStorageSync("userLogin", config.userLogin)
  37. console.log('✅ 已保存 userLogin:', config.userLogin);
  38. }
  39. // 授权成功后,自动返回首页
  40. if (config.userLogin && isUpdate) {
  41. console.log('🔄 授权成功,准备返回首页...');
  42. // 检查是否有待处理的跳转
  43. const pendingNavigation = wx.getStorageSync('pendingNavigation');
  44. // 延迟一下,确保数据保存完成
  45. setTimeout(() => {
  46. const pages = getCurrentPages();
  47. console.log('当前页面栈层数:', pages.length);
  48. if (pages.length > 1) {
  49. // 如果有上一页,返回上一页
  50. console.log('📱 返回上一页');
  51. wx.navigateBack({
  52. delta: 1,
  53. success: () => {
  54. console.log('✅ 返回成功');
  55. // 如果有待处理的跳转,触发跳转
  56. if (pendingNavigation) {
  57. console.log('🎯 检测到待处理的跳转:', pendingNavigation);
  58. wx.removeStorageSync('pendingNavigation');
  59. // 再延迟一下,确保页面已经返回并刷新
  60. setTimeout(() => {
  61. // 触发页面的跳转方法
  62. const currentPages = getCurrentPages();
  63. const currentPage = currentPages[currentPages.length - 1];
  64. if (currentPage && currentPage.selectComponent) {
  65. const homeComponent = currentPage.selectComponent('#home-component');
  66. if (homeComponent && homeComponent.navigateToConsultation) {
  67. console.log('✅ 触发咨询页面跳转');
  68. homeComponent.navigateToConsultation();
  69. } else {
  70. console.warn('⚠️ 未找到 home 组件或 navigateToConsultation 方法');
  71. }
  72. }
  73. }, 500);
  74. }
  75. },
  76. fail: (err) => {
  77. console.error('❌ 返回失败:', err);
  78. // 如果返回失败,重新加载首页
  79. wx.reLaunch({
  80. url: rootPage || '/nova-pbf/pages/index/index'
  81. });
  82. }
  83. });
  84. } else {
  85. // 如果没有上一页,重新加载首页
  86. console.log('📱 重新加载首页');
  87. wx.reLaunch({
  88. url: rootPage || '/nova-pbf/pages/index/index'
  89. });
  90. }
  91. }, 500);
  92. }
  93. console.log('===========================================');
  94. },
  95. getCode() {
  96. return new Promise((resolve) => {
  97. wx.login({
  98. success: function (res) {
  99. if (res.code) {
  100. console.log(res.code);
  101. resolve(res.code)
  102. }
  103. },
  104. fail: function (err) {
  105. console.warn('小程序wx.login失败');
  106. resolve()
  107. }
  108. });
  109. })
  110. },
  111. restart(err) {
  112. console.log('=== restart 方法被调用 ===');
  113. console.log('错误信息:', err);
  114. // 不要直接退出小程序,而是尝试重新登录
  115. wx.showModal({
  116. title: '提示',
  117. content: '登录状态异常,是否重新登录?',
  118. showCancel: true,
  119. cancelText: '取消',
  120. confirmText: '重新登录',
  121. success: async (result) => {
  122. if (result.confirm) {
  123. try {
  124. // 清除登录状态
  125. wx.removeStorageSync("sessionToken");
  126. wx.removeStorageSync("userLogin");
  127. // 重新加载首页
  128. const rootPage = getApp().globalData.rootPage || getApp().globalData.defaultTabBar?.list?.[0]?.pagePath || '/pages/index/index';
  129. wx.reLaunch({
  130. url: rootPage,
  131. success: () => {
  132. console.log('✅ 重新加载首页成功');
  133. },
  134. fail: (err) => {
  135. console.error('❌ 重新加载失败:', err);
  136. wx.showToast({
  137. title: '加载失败,请重试',
  138. icon: 'none'
  139. });
  140. }
  141. });
  142. } catch (error) {
  143. console.error('❌ 重新登录失败:', error);
  144. }
  145. }
  146. }
  147. });
  148. },
  149. router(type, url = '/index') {
  150. switch (type) {
  151. case 'navigateBack':
  152. wx.navigateBack({
  153. delta: url || 1,
  154. fail: function () {
  155. wx.reLaunch({
  156. url: "/index",
  157. });
  158. },
  159. })
  160. break;
  161. case 'navigateTo':
  162. wx.navigateTo({
  163. url: url
  164. })
  165. break;
  166. case 'reLaunch':
  167. wx.reLaunch({
  168. url: url
  169. })
  170. break;
  171. case 'redirectTo':
  172. wx.redirectTo({
  173. url: url
  174. })
  175. break;
  176. default:
  177. break;
  178. }
  179. },
  180. // 获取本地存储文件大小
  181. getFileInfo(filePath) {
  182. return new Promise((result) => {
  183. wx.getFileInfo({
  184. filePath: filePath,
  185. success(res) {
  186. result(res.size)
  187. },
  188. fail(err) {
  189. result(0)
  190. }
  191. })
  192. })
  193. }
  194. }