index.js 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  1. // var Parse = getApp().Parse;
  2. // var app = getApp()
  3. // const { wxLogin } = require('./utils/login')
  4. const CONFIG = require("config.js");
  5. let config = {
  6. appid: CONFIG.default.appid,
  7. company: CONFIG.default.company,
  8. rootPage: CONFIG.default.rootPage,
  9. }
  10. const plugin = requirePlugin('fm-plugin')
  11. const { Parse, checkAuth } = plugin
  12. Page({
  13. /**
  14. * 页面的初始数据
  15. */
  16. data: {
  17. splashUrl: wx.getStorageSync("enabledOptions")[0],
  18. loading:true
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: async function (options) {
  24. // 拦截插件的"登录信息已过期"提示
  25. const originalShowModal = wx.showModal;
  26. wx.showModal = function(options) {
  27. // 如果是"登录信息已过期"的提示,改为友好的重新登录提示
  28. if (options.content && options.content.includes('登录信息过期')) {
  29. console.log('⚠️ 拦截到"登录信息已过期"提示,改为友好提示');
  30. return originalShowModal.call(this, {
  31. title: '提示',
  32. content: '登录状态异常,是否重新登录?',
  33. showCancel: true,
  34. cancelText: '取消',
  35. confirmText: '重新登录',
  36. success: (result) => {
  37. if (result.confirm) {
  38. // 清除登录状态
  39. wx.removeStorageSync("sessionToken");
  40. wx.removeStorageSync("userLogin");
  41. // 重新加载首页
  42. const rootPage = getApp().globalData.rootPage || getApp().globalData.defaultTabBar?.list?.[0]?.pagePath || '/pages/index/index';
  43. wx.reLaunch({
  44. url: rootPage
  45. });
  46. }
  47. // 调用原始的 success 回调
  48. if (options.success) {
  49. options.success(result);
  50. }
  51. }
  52. });
  53. }
  54. // 其他提示正常显示
  55. return originalShowModal.call(this, options);
  56. };
  57. wx.login({
  58. success: function (res) {
  59. if (res.code) {
  60. console.log(res);
  61. // wx.request({
  62. // url: "https://server.fmode.cn/api/wxapp/auth_wxapp",
  63. // data: {
  64. // c: getApp().globalData.company,
  65. // code: res.code,
  66. // },
  67. // async success(res) {
  68. // wx.setStorageSync("userInfo", res.data);
  69. // resolve(res)
  70. // },
  71. // });
  72. }
  73. },
  74. fail: function (err) {
  75. wx.showToast({
  76. title: '服务器繁忙,请稍后重试',
  77. })
  78. }
  79. });
  80. wx.setStorageSync("invite", null);
  81. // 处理扫码链接(options.q 包含完整的URL或activityId)
  82. if (options.q) {
  83. let str = decodeURIComponent(options.q); // 扫描二维码获得的跳转链接
  84. // 兼容一些环境中把 & 编码成 & 的情况,防止参数解析错误(如 scanCount=0&storeId=...)
  85. if (str.indexOf('&') !== -1) {
  86. console.log('🔧 检测到 URL 中包含 &,自动还原为 &');
  87. str = str.replace(/&/g, '&');
  88. }
  89. // 检查是否是员工邀请链接(app.fmode.cn/dev/pobingfeng/manager/staff?invite=xxx)
  90. if (str.includes('app.fmode.cn/dev/pobingfeng/manager/staff')) {
  91. let obj = this.getParaName(str);
  92. if (obj && obj.invite) {
  93. wx.setStorageSync("invite", obj.invite);
  94. console.log('✅ 检测到员工邀请链接,invite:', obj.invite);
  95. }
  96. // 员工邀请链接不需要跳转到店铺,直接返回
  97. this.setData({ options: options });
  98. plugin.init(config, wx.getStorageSync('invite'));
  99. return;
  100. }
  101. // 检查是否是活动海报二维码(activityId作为qrCode参数值)
  102. // 如果str不包含http/https,且看起来像是一个ID,则可能是activityId
  103. if (!str.includes('http://') && !str.includes('https://') && !str.includes('?')) {
  104. // 可能是活动ID,尝试作为activityId处理
  105. if (str && str.length > 0 && !isNaN(str)) {
  106. options.activityId = str;
  107. console.log('✅ 检测到活动海报二维码,activityId:', str);
  108. // 活动二维码需要特殊处理,先保存activityId
  109. wx.setStorageSync("activityId", str);
  110. }
  111. }
  112. // 处理店铺相关的二维码链接(pwa.fmode.cn/gomini/pmd/)
  113. // 从完整URL中提取参数
  114. if (str.includes('pwa.fmode.cn/gomini/pmd') || str.includes('?')) {
  115. let obj = this.getParaName(str);
  116. if (obj && obj.invite) {
  117. wx.setStorageSync("invite", obj.invite);
  118. }
  119. // 将所有参数合并到 options 中
  120. obj && Object.keys(obj).forEach(key=> options[key] = obj[key]);
  121. }
  122. }
  123. // 兼容从编译参数或页面直达传入的 storeId
  124. if (options && options.scene && !options.storeId) {
  125. try {
  126. const sceneStr = decodeURIComponent(options.scene)
  127. if (sceneStr) {
  128. const pairs = sceneStr.split('&')
  129. for (const p of pairs) {
  130. const [k, v] = p.split('=')
  131. if (k === 'storeId' && v) {
  132. options.storeId = v
  133. break
  134. }
  135. }
  136. }
  137. } catch (e) {
  138. console.warn('解析 scene 失败:', e)
  139. }
  140. }
  141. this.setData({
  142. options: options
  143. })
  144. let {
  145. time,
  146. dramaId,
  147. roomId,
  148. orderId,
  149. shopId,
  150. invite,
  151. activityId,
  152. company,
  153. inviteHost,
  154. storeId
  155. } = options
  156. time && wx.setStorageSync("time", time);
  157. dramaId && wx.setStorageSync("dramaId", dramaId);
  158. roomId && wx.setStorageSync("roomId", roomId);
  159. orderId && wx.setStorageSync("orderId", orderId);
  160. shopId && wx.setStorageSync("shopId", shopId);
  161. invite && wx.setStorageSync("invite", invite);
  162. activityId && wx.setStorageSync("activityId", activityId);
  163. inviteHost && wx.setStorageSync("inviteHost", true);
  164. if (storeId) {
  165. // 扫码进入时,强制设置店铺 ID,标记为扫码来源
  166. wx.setStorageSync('storeId', storeId)
  167. getApp().globalData.storeId = storeId
  168. wx.setStorageSync('storeId_from_scan', true)
  169. console.log('✅ 入口页扫码进入,已设置店铺 ID(优先级最高):', storeId)
  170. }
  171. if (company) getApp().globalData.toCompany = true;
  172. // 检查是否是扫码进入(需要统计扫码次数)
  173. this.checkAndHandleScan(options);
  174. plugin.init(config, wx.getStorageSync('invite'))
  175. },
  176. /**
  177. * 生命周期函数--监听页面初次渲染完成
  178. */
  179. onReady: async function () { },
  180. /**
  181. * 生命周期函数--监听页面显示
  182. */
  183. onShow: async function () {
  184. await this.review()
  185. },
  186. async review(force){
  187. try {
  188. let options = this.data.options
  189. let url = getApp().globalData.rootPage || getApp().globalData.defaultTabBar.list[0].pagePath
  190. if (options) {
  191. let objArr = Object.keys(options)
  192. if (objArr && objArr.length > 0) {
  193. let parms = '?'
  194. objArr.forEach((o, index) => {
  195. if (index > 0) {
  196. parms += '&' + o + '=' + options[o]
  197. } else {
  198. parms += o + '=' + options[o]
  199. }
  200. })
  201. url += parms
  202. }
  203. }
  204. let currentUser = Parse.User.current()
  205. console.log('===========================================');
  206. console.log('======= index.js review 方法 =======');
  207. console.log('当前用户:', currentUser ? currentUser.id : '无');
  208. console.log('用户手机号:', currentUser?.get('mobile') || '无');
  209. console.log('用户名:', currentUser?.get('username') || '无');
  210. console.log('Session Token:', currentUser?.getSessionToken()?.substring(0, 20) || '无');
  211. console.log('userLogin 存储:', wx.getStorageSync('userLogin') || '无');
  212. console.log('force 参数:', force);
  213. console.log('===========================================');
  214. // 查询 Company 的 isPublishing 字段
  215. let isPublishing = false;
  216. try {
  217. const companyQuery = new Parse.Query('Company');
  218. companyQuery.equalTo('objectId', getApp().globalData.company);
  219. companyQuery.select('isPublishing');
  220. try {
  221. const companyObj = await companyQuery.first();
  222. if (companyObj) {
  223. isPublishing = companyObj.get('isPublishing') === true;
  224. console.log('📋 Company isPublishing:', isPublishing);
  225. } else {
  226. console.log('⚠️ 未找到 Company 记录,默认 isPublishing = false');
  227. isPublishing = false;
  228. }
  229. } catch (queryError) {
  230. console.error('❌ 查询 Company 失败(可能 token 无效):', queryError.message);
  231. // 查询失败时,默认为 false(强制登录)
  232. isPublishing = false;
  233. }
  234. // 保存到全局,供其他页面使用
  235. getApp().globalData.isPublishing = isPublishing;
  236. wx.setStorageSync('isPublishing', isPublishing);
  237. } catch (error) {
  238. console.error('❌ 查询 Company isPublishing 失败:', error);
  239. // 查询失败时,默认为 false(强制登录)
  240. isPublishing = false;
  241. }
  242. // 根据 isPublishing 决定是否强制登录
  243. if (!currentUser || force) {
  244. console.log('🔄 开始调用 checkAuth...');
  245. // isPublishing == true 时不强制授权,否则强制授权
  246. const forceAuth = !isPublishing;
  247. console.log('🔐 是否强制登录:', forceAuth);
  248. let r = await checkAuth(forceAuth);
  249. console.log('===========================================');
  250. console.log('======= checkAuth 返回结果 =======');
  251. console.log('返回值:', r);
  252. // 重新获取用户信息
  253. currentUser = Parse.User.current();
  254. console.log('checkAuth 后的用户:', currentUser ? currentUser.id : '无');
  255. console.log('checkAuth 后的手机号:', currentUser?.get('mobile') || '无');
  256. console.log('checkAuth 后的 Session Token:', currentUser?.getSessionToken()?.substring(0, 20) || '无');
  257. console.log('===========================================');
  258. // 如果强制登录但用户未登录,不允许继续访问
  259. if (forceAuth && !r) {
  260. console.log('❌ 强制登录模式,用户未登录,停止访问');
  261. return;
  262. }
  263. // 即使登录失败,也允许继续访问(仅在非强制登录模式下)
  264. if(!r) {
  265. console.log('⚠️ 用户未登录或拒绝授权,允许游客访问');
  266. // 不要 return,继续执行后面的跳转逻辑
  267. } else {
  268. // 登录成功,设置 userLogin
  269. if (currentUser && currentUser.get('mobile')) {
  270. wx.setStorageSync("userLogin", currentUser.id);
  271. console.log('✅ 授权登录成功,已设置 userLogin:', currentUser.id);
  272. console.log('✅ 用户手机号:', currentUser.get('mobile'));
  273. } else {
  274. console.warn('⚠️ checkAuth 返回成功,但用户没有手机号!');
  275. console.warn(' 用户对象:', currentUser);
  276. }
  277. // 检查是否有待记录的扫码信息
  278. await this.checkAndRecordPendingScan();
  279. // 如果用户没有来源信息,且当前有扫码参数,记录来源
  280. await this.checkAndRecordUserSourceOnLogin();
  281. }
  282. } else {
  283. console.log('✅ 用户已登录,跳过 checkAuth');
  284. // 用户已登录,确保 userLogin 已设置
  285. if (currentUser.get('mobile')) {
  286. wx.setStorageSync("userLogin", currentUser.id);
  287. console.log('✅ 已确认 userLogin:', currentUser.id);
  288. }
  289. this.updateUser(currentUser.id);
  290. // 用户已登录,检查是否有待记录的扫码信息
  291. await this.checkAndRecordPendingScan();
  292. // 如果用户没有来源信息,且当前有扫码参数,记录来源
  293. await this.checkAndRecordUserSourceOnLogin();
  294. }
  295. getApp().Parse = Parse
  296. getApp().checkAuth = checkAuth
  297. if (!await this.getCompanyServerExpire(url)) {
  298. return
  299. }
  300. // 检查是否需要跳转到活动页面
  301. if (wx.getStorageSync('need_activity_redirect') === true) {
  302. await this.redirectToActivityPage();
  303. return;
  304. }
  305. // 检查是否需要跳转到扫码统计页面
  306. if (this.shouldRedirectToScanPage()) {
  307. await this.redirectToScanPage();
  308. return;
  309. }
  310. console.log('✅ 准备跳转到:', url);
  311. wx.redirectTo({
  312. url: url,
  313. success: () => {
  314. console.log('✅ redirectTo 跳转成功');
  315. },
  316. fail: (err) => {
  317. console.error('❌ redirectTo 失败:', err);
  318. console.log('⚠️ 尝试使用 reLaunch');
  319. // 降级:尝试使用 reLaunch
  320. wx.reLaunch({
  321. url: url,
  322. success: () => {
  323. console.log('✅ reLaunch 跳转成功');
  324. },
  325. fail: (err2) => {
  326. console.error('❌ reLaunch 也失败:', err2);
  327. // 显示错误提示
  328. this.setData({ loading: false });
  329. wx.showModal({
  330. title: '温馨提示',
  331. content: '页面加载失败,请检查网络后重试。',
  332. showCancel: true,
  333. cancelText: '退出',
  334. confirmText: '重试',
  335. success: (result) => {
  336. if (result.confirm) {
  337. this.review(true);
  338. } else {
  339. wx.exitMiniProgram();
  340. }
  341. }
  342. });
  343. }
  344. });
  345. }
  346. });
  347. }
  348. catch (err) {
  349. console.log('❌ review 方法出错:', err);
  350. /* 登录身份信息到期,重新登陆 */
  351. if((err?.message?.indexOf('Session token is expired') != -1 || err?.message?.indexOf('Invalid session token') != -1) && !force){
  352. console.log('⚠️ Session Token 过期,准备重新登录');
  353. // 保存需要保留的数据
  354. const invite = wx.getStorageSync('invite');
  355. const agreementAccepted = wx.getStorageSync('user_agreement_accepted');
  356. const storeId = wx.getStorageSync('storeId');
  357. const isPublishing = wx.getStorageSync('isPublishing');
  358. // 先登出 Parse 用户
  359. try {
  360. await Parse.User.logOut();
  361. console.log('✅ Parse 用户已登出');
  362. } catch (logoutErr) {
  363. console.warn('⚠️ Parse 登出失败:', logoutErr);
  364. }
  365. // 清除所有存储
  366. wx.clearStorageSync();
  367. // 恢复需要保留的数据
  368. if (invite) wx.setStorageSync('invite', invite);
  369. if (agreementAccepted) wx.setStorageSync('user_agreement_accepted', agreementAccepted);
  370. if (storeId) wx.setStorageSync('storeId', storeId);
  371. if (isPublishing !== undefined) wx.setStorageSync('isPublishing', isPublishing);
  372. console.log('✅ 已清除过期登录信息,保留必要数据');
  373. console.log(' 保留的 invite:', invite || '无');
  374. console.log(' 保留的协议状态:', agreementAccepted || '无');
  375. console.log(' 保留的店铺ID:', storeId || '无');
  376. /* 强制重新登录 */
  377. this.review(true);
  378. return;
  379. }
  380. // 如果出错,尝试继续跳转到主页(游客模式)
  381. console.log('⚠️ 登录出错,尝试游客模式访问');
  382. let url = getApp().globalData.rootPage || getApp().globalData.defaultTabBar.list[0].pagePath;
  383. if (this.data.options) {
  384. let objArr = Object.keys(this.data.options)
  385. if (objArr && objArr.length > 0) {
  386. let parms = '?'
  387. objArr.forEach((o, index) => {
  388. if (index > 0) {
  389. parms += '&' + o + '=' + this.data.options[o]
  390. } else {
  391. parms += o + '=' + this.data.options[o]
  392. }
  393. })
  394. url += parms
  395. }
  396. }
  397. wx.redirectTo({
  398. url: url,
  399. fail: () => {
  400. // 如果跳转失败,显示错误提示
  401. this.setData({
  402. loading:false
  403. })
  404. wx.showModal({
  405. title: '温馨提示',
  406. content: '页面加载失败,请检查网络后重试。',
  407. showCancel: true,
  408. cancelText: '退出',
  409. confirmText: '重试',
  410. success: (result) => {
  411. if (result.confirm) {
  412. // 用户选择重试
  413. this.review(true)
  414. } else {
  415. // 用户选择退出
  416. wx.exitMiniProgram()
  417. }
  418. },
  419. });
  420. }
  421. });
  422. }
  423. },
  424. /**
  425. * 检查并记录待处理的扫码统计
  426. * 同时记录用户来源信息到 _User 表
  427. */
  428. async checkAndRecordPendingScan() {
  429. try {
  430. const pendingScan = wx.getStorageSync('pending_scan_record');
  431. if (!pendingScan) {
  432. return;
  433. }
  434. console.log('===========================================');
  435. console.log('======= 发现待记录的扫码信息 =======');
  436. console.log('扫码信息:', pendingScan);
  437. console.log('===========================================');
  438. // 检查是否超时(24小时)
  439. const now = Date.now();
  440. const scanTime = pendingScan.timestamp || 0;
  441. const hoursPassed = (now - scanTime) / (1000 * 60 * 60);
  442. if (hoursPassed > 24) {
  443. console.log('⚠️ 扫码信息已超过24小时,不再记录');
  444. wx.removeStorageSync('pending_scan_record');
  445. return;
  446. }
  447. // 记录用户来源信息
  448. await this.recordUserSource(pendingScan);
  449. // 记录扫码统计
  450. await this.recordScanStatistics({
  451. storeId: pendingScan.storeId,
  452. sourceType: pendingScan.sourceType,
  453. sourceId: pendingScan.sourceId,
  454. ownerId: pendingScan.ownerId,
  455. employeeId: pendingScan.employeeId,
  456. partnerId: pendingScan.partnerId,
  457. userId: pendingScan.userId,
  458. productId: pendingScan.productId,
  459. scanCount: pendingScan.scanCount
  460. });
  461. // 清除待记录的扫码信息
  462. wx.removeStorageSync('pending_scan_record');
  463. console.log('✅ 扫码统计已记录并清除');
  464. } catch (error) {
  465. console.error('❌ 记录待处理扫码信息失败:', error);
  466. }
  467. },
  468. /**
  469. * 记录用户来源信息到 _User 表
  470. * 根据扫码参数判断来源类型并保存到用户的 source 字段
  471. *
  472. * 来源类型:
  473. * 1. 渠道xxx→异业xxx(老板后台添加的异业)
  474. * 2. 渠道xxx→异业xxx→员工xxx(员工后台添加的异业)
  475. * 3. 员工xxx
  476. * 4. 老板
  477. * 5. 自主进入(无任何推荐)
  478. */
  479. async recordUserSource(scanInfo) {
  480. try {
  481. const currentUser = Parse.User.current();
  482. if (!currentUser) {
  483. console.log('⚠️ 用户未登录,无法记录来源');
  484. return;
  485. }
  486. // 如果用户已经有来源信息,不再覆盖(首次来源原则)
  487. const existingSource = currentUser.get('source');
  488. if (existingSource) {
  489. console.log('ℹ️ 用户已有来源信息,不覆盖:', existingSource);
  490. return;
  491. }
  492. console.log('📊 ===========================================');
  493. console.log('📊 [记录来源] 开始记录用户来源信息');
  494. console.log('📊 用户ID:', currentUser.id);
  495. console.log('📊 ===========================================');
  496. const { partnerId, employeeId, ownerId, userId, storeId } = scanInfo;
  497. let sourceInfo = {
  498. type: 'self_entry', // 默认为自主进入
  499. label: '自主进入',
  500. timestamp: new Date(),
  501. storeId: storeId
  502. };
  503. // 判断来源类型
  504. if (partnerId && employeeId) {
  505. // 情况2: 渠道xxx→异业xxx→员工xxx(员工后台添加的异业)
  506. console.log('🔍 [来源类型] 渠道→异业→员工');
  507. try {
  508. // 查询异业信息
  509. const partnerQuery = new Parse.Query('Partner');
  510. const partner = await partnerQuery.get(partnerId);
  511. const partnerName = partner.get('name') || '未知异业';
  512. const channelName = partner.get('channelName') || partner.get('category') || '未知渠道';
  513. // 查询员工信息
  514. const employeeQuery = new Parse.Query('Employee');
  515. const employee = await employeeQuery.get(employeeId);
  516. const employeeName = employee.get('name') || '未知员工';
  517. sourceInfo = {
  518. type: 'channel_partner_employee',
  519. label: `${channelName},${partnerName},员工${employeeName}`,
  520. channelName: channelName,
  521. partnerId: partnerId,
  522. partnerName: partnerName,
  523. employeeId: employeeId,
  524. employeeName: employeeName,
  525. timestamp: new Date(),
  526. storeId: storeId
  527. };
  528. console.log('✅ [来源信息] 渠道→异业→员工:', sourceInfo.label);
  529. } catch (error) {
  530. console.error('❌ 查询异业或员工信息失败:', error);
  531. }
  532. } else if (partnerId && !employeeId) {
  533. // 情况1: 渠道xxx→异业xxx(老板后台添加的异业)
  534. console.log('🔍 [来源类型] 渠道→异业');
  535. try {
  536. // 查询异业信息
  537. const partnerQuery = new Parse.Query('Partner');
  538. const partner = await partnerQuery.get(partnerId);
  539. const partnerName = partner.get('name') || '未知异业';
  540. const channelName = partner.get('channelName') || partner.get('category') || '未知渠道';
  541. sourceInfo = {
  542. type: 'channel_partner',
  543. label: `${channelName},${partnerName}`,
  544. channelName: channelName,
  545. partnerId: partnerId,
  546. partnerName: partnerName,
  547. timestamp: new Date(),
  548. storeId: storeId
  549. };
  550. console.log('✅ [来源信息] 渠道→异业:', sourceInfo.label);
  551. } catch (error) {
  552. console.error('❌ 查询异业信息失败:', error);
  553. }
  554. } else if (employeeId && !partnerId) {
  555. // 情况3: 员工xxx
  556. console.log('🔍 [来源类型] 员工');
  557. try {
  558. // 查询员工信息
  559. const employeeQuery = new Parse.Query('Employee');
  560. const employee = await employeeQuery.get(employeeId);
  561. const employeeName = employee.get('name') || '未知员工';
  562. sourceInfo = {
  563. type: 'employee',
  564. label: employeeName,
  565. employeeId: employeeId,
  566. employeeName: employeeName,
  567. timestamp: new Date(),
  568. storeId: storeId
  569. };
  570. console.log('✅ [来源信息] 员工:', sourceInfo.label);
  571. } catch (error) {
  572. console.error('❌ 查询员工信息失败:', error);
  573. }
  574. } else if (ownerId) {
  575. // 情况4: 老板
  576. console.log('🔍 [来源类型] 老板');
  577. sourceInfo = {
  578. type: 'owner',
  579. label: '老板',
  580. ownerId: ownerId,
  581. timestamp: new Date(),
  582. storeId: storeId
  583. };
  584. console.log('✅ [来源信息] 老板');
  585. } else if (userId) {
  586. // 推广员(如果有的话)
  587. console.log('🔍 [来源类型] 推广员');
  588. try {
  589. // 查询推广员信息
  590. const userQuery = new Parse.Query('_User');
  591. const promoter = await userQuery.get(userId);
  592. const promoterName = promoter.get('username') || promoter.get('mobile') || '未知推广员';
  593. sourceInfo = {
  594. type: 'promoter',
  595. label: `推广员${promoterName}`,
  596. userId: userId,
  597. promoterName: promoterName,
  598. timestamp: new Date(),
  599. storeId: storeId
  600. };
  601. console.log('✅ [来源信息] 推广员:', sourceInfo.label);
  602. } catch (error) {
  603. console.error('❌ 查询推广员信息失败:', error);
  604. }
  605. } else {
  606. // 情况5: 自主进入(无任何推荐)
  607. console.log('🔍 [来源类型] 自主进入');
  608. console.log('✅ [来源信息] 自主进入');
  609. }
  610. // 保存来源信息到用户
  611. currentUser.set('source', sourceInfo);
  612. await currentUser.save();
  613. console.log('✅ [保存成功] 用户来源信息已保存');
  614. console.log(' - 来源类型:', sourceInfo.type);
  615. console.log(' - 来源标签:', sourceInfo.label);
  616. console.log('📊 ===========================================');
  617. } catch (error) {
  618. console.error('❌ 记录用户来源失败:', error);
  619. }
  620. },
  621. /**
  622. * 用户登录后检查并记录来源信息
  623. * 如果用户是首次登录且没有来源信息,根据当前的扫码参数记录来源
  624. */
  625. async checkAndRecordUserSourceOnLogin() {
  626. try {
  627. const currentUser = Parse.User.current();
  628. if (!currentUser) {
  629. return;
  630. }
  631. // 如果用户已经有来源信息,不再处理
  632. const existingSource = currentUser.get('source');
  633. if (existingSource) {
  634. console.log('ℹ️ 用户已有来源信息,跳过记录');
  635. return;
  636. }
  637. // 检查是否有扫码参数
  638. const scanStoreId = wx.getStorageSync('scan_storeId');
  639. const scanPartnerId = wx.getStorageSync('scan_partnerId');
  640. const scanEmployeeId = wx.getStorageSync('scan_employeeId');
  641. const scanOwnerId = wx.getStorageSync('scan_ownerId');
  642. const scanUserId = wx.getStorageSync('scan_userId');
  643. // 如果有任何扫码参数,记录来源
  644. if (scanStoreId || scanPartnerId || scanEmployeeId || scanOwnerId || scanUserId) {
  645. console.log('📊 检测到扫码参数,记录用户来源');
  646. await this.recordUserSource({
  647. storeId: scanStoreId,
  648. partnerId: scanPartnerId,
  649. employeeId: scanEmployeeId,
  650. ownerId: scanOwnerId,
  651. userId: scanUserId
  652. });
  653. } else {
  654. // 没有任何扫码参数,标记为自主进入
  655. console.log('📊 无扫码参数,标记为自主进入');
  656. await this.recordUserSource({
  657. storeId: wx.getStorageSync('storeId') || null
  658. });
  659. }
  660. } catch (error) {
  661. console.error('❌ 检查并记录用户来源失败:', error);
  662. }
  663. },
  664. async updateUser(id) {
  665. try {
  666. let User = new Parse.Query('_User')
  667. let user = await User.get(id)
  668. let invite = wx.getStorageSync('invite')
  669. //查询邀请人user
  670. let query = new Parse.Query("_User")
  671. query.equalTo('objectId', invite)
  672. let result = await query.first()
  673. if (result && result.id && result.get("invite")?.id == user.id) {
  674. console.error('邀请人不能是自己的下级')
  675. return
  676. }
  677. if (invite && !user.get('invite') && user.id != invite && !user.get('agentLevel')) {
  678. console.log('上下级绑定成功');
  679. user.set('invite', {
  680. __type: "Pointer",
  681. className: "_User",
  682. objectId: invite
  683. })
  684. user.set('agent', {
  685. __type: "Pointer",
  686. className: "_User",
  687. objectId: invite
  688. })
  689. await Parse.Cloud.run('user_save', {
  690. userJson: user.toJSON()
  691. })
  692. }
  693. } catch (error) {
  694. console.error('❌ updateUser 失败:', error.message);
  695. // 不阻断流程,继续执行
  696. }
  697. },
  698. async getCompanyServerExpire(url) {
  699. try {
  700. let query = new Parse.Query('Company')
  701. query.equalTo('objectId', getApp().globalData.company)
  702. query.select('expireDate', 'expireMap')
  703. let com = await query.first()
  704. if (com?.id && com?.get('expireDate')) {
  705. let now = + new Date()
  706. let expireTime = + new Date(com?.get('expireDate'))
  707. if (com?.get('expireMap') && com.get('expireMap')[getApp().globalData.appid]) {
  708. expireTime = + new Date(com.get('expireMap')[getApp().globalData.appid])
  709. }
  710. if (now >= expireTime) {
  711. console.log('服务器到期');
  712. wx.reLaunch({
  713. url: `common-page/pages/loading/index?url=${url}`,
  714. });
  715. return
  716. }
  717. }
  718. return true
  719. } catch (error) {
  720. console.error('❌ getCompanyServerExpire 失败:', error.message);
  721. // 查询失败时,允许继续访问
  722. return true
  723. }
  724. },
  725. onUnload: function () {
  726. wx.setStorageSync("active", 0);
  727. },
  728. getParaName(url) {
  729. if (!url || url.indexOf('?') == -1) {
  730. return
  731. }
  732. // 兼容 URL 中 & 的情况,先统一还原为 &
  733. if (url.indexOf('&') !== -1) {
  734. url = url.replace(/&/g, '&');
  735. }
  736. // 提取查询参数部分(去除可能的 hash 部分)
  737. let queryString = url.split('?')[1];
  738. // 如果包含 #,只取 # 之前的部分
  739. if (queryString.indexOf('#') !== -1) {
  740. queryString = queryString.split('#')[0];
  741. }
  742. return this.setObject(queryString) //封装成对象
  743. },
  744. setObject(paraArr) {
  745. let obj = {}
  746. let arr1 = paraArr.split('&')
  747. arr1.forEach(item => {
  748. let str = item.split('=')
  749. let key = str[0]
  750. let val = str[1]
  751. obj[key] = val
  752. })
  753. return obj
  754. },
  755. /**
  756. * 检查并处理扫码参数
  757. * 支持所有类型的二维码:
  758. * 1. 推广员二维码: ?scanCount=0&storeId=xxx&userId=xxx
  759. * 2. 产品二维码: ?scanCount=0&storeId=xxx&productId=xxx
  760. * 3. 案例二维码: ?scanCount=0&storeId=xxx&caseId=xxx
  761. * 4. 活动海报二维码: activityId作为qrCode参数值
  762. * 5. 异业合作伙伴二维码(移动端 / PC端统一): ?scanCount=xxx&storeId=xxx&partnerId=xxx
  763. * 6. 员工邀请二维码(移动端): ?scanCount=0&storeId=xxx&employeeId=xxx
  764. * 7. 我的二维码(老板): ?scanCount=0&storeId=xxx&ownerId=xxx
  765. * 8. 我的二维码(员工): ?scanCount=0&storeId=xxx&employeeId=xxx
  766. * 9. 方案分享二维码: ?scanCount=0&storeId=xxx&planId=xxx&shareUserId=xxx
  767. */
  768. checkAndHandleScan(options) {
  769. console.log('🔍🔍🔍 ========================================');
  770. console.log('🔍 [扫码检测] 开始检查扫码参数');
  771. console.log('🔍 完整 options 对象:', JSON.stringify(options, null, 2));
  772. console.log('🔍🔍🔍 ========================================');
  773. const {
  774. scanCount,
  775. ownerId,
  776. employeeId,
  777. partnerId,
  778. storeId,
  779. productId,
  780. caseId,
  781. userId, // 推广员二维码使用userId
  782. activityId,
  783. schemeId, // 方案ID(旧参数名)
  784. planId, // 方案ID(新参数名,优先使用)
  785. shareUserId, // 分享方案的用户ID
  786. q // 扫码链接
  787. } = options;
  788. // planId 和 schemeId 兼容处理,优先使用 planId
  789. const finalPlanId = planId || schemeId;
  790. console.log('📋 [扫码参数] 提取的参数:');
  791. console.log(' - scanCount:', scanCount || '无');
  792. console.log(' - storeId:', storeId || '无');
  793. console.log(' - ownerId:', ownerId || '无');
  794. console.log(' - employeeId:', employeeId || '无');
  795. console.log(' - partnerId:', partnerId || '无');
  796. console.log(' - userId:', userId || '无');
  797. console.log(' - productId:', productId || '无');
  798. console.log(' - caseId:', caseId || '无');
  799. console.log(' - activityId:', activityId || '无');
  800. console.log(' - planId:', planId || '无');
  801. console.log(' - schemeId:', schemeId || '无');
  802. console.log(' - finalPlanId (最终使用):', finalPlanId || '无');
  803. console.log(' - shareUserId:', shareUserId || '无');
  804. console.log(' - q (扫码链接):', q || '无');
  805. // 处理活动海报二维码(activityId作为qrCode参数值)
  806. if (activityId && !storeId) {
  807. console.log('🎯 ===========================================');
  808. console.log('🎯 [活动二维码] 检测到活动海报二维码');
  809. console.log('🎯 活动ID (activityId):', activityId);
  810. console.log('🎯 来源类型: 活动海报');
  811. console.log('🎯 ===========================================');
  812. // 活动二维码需要跳转到活动页面,不需要跳转到店铺
  813. wx.setStorageSync('scan_activityId', activityId);
  814. wx.setStorageSync('need_activity_redirect', true);
  815. return;
  816. }
  817. // 如果存在 storeId,说明是扫码进入具体门店(包括异业分享)
  818. if (storeId) {
  819. console.log('🏪 ===========================================');
  820. console.log('🏪 [店铺扫码] 检测到扫码参数');
  821. console.log('🏪 店铺ID (storeId):', storeId);
  822. console.log('🏪 扫码次数 (scanCount):', scanCount || '0');
  823. // 确定来源类型
  824. let sourceType = 'unknown';
  825. let sourceId = null;
  826. if (employeeId) {
  827. sourceType = 'employee';
  828. sourceId = employeeId;
  829. console.log('👤 [来源识别] 来源类型: 员工展业');
  830. console.log('👤 员工ID (employeeId):', employeeId);
  831. } else if (userId) {
  832. // 推广员二维码使用userId
  833. sourceType = 'promoter';
  834. sourceId = userId;
  835. console.log('📢 [来源识别] 来源类型: 推广员展业');
  836. console.log('📢 推广员ID (userId):', userId);
  837. } else if (ownerId) {
  838. sourceType = 'owner';
  839. sourceId = ownerId;
  840. console.log('👔 [来源识别] 来源类型: 老板展业');
  841. console.log('👔 老板ID (ownerId):', ownerId);
  842. } else if (partnerId) {
  843. sourceType = 'partner';
  844. sourceId = partnerId;
  845. console.log('🤝 [来源识别] 来源类型: 异业合作伙伴');
  846. console.log('🤝 合作伙伴ID (partnerId):', partnerId);
  847. } else {
  848. sourceType = 'store';
  849. console.log('🏬 [来源识别] 来源类型: 店铺分享(无具体来源人)');
  850. }
  851. if (productId) {
  852. console.log('📦 [目标内容] 产品ID (productId):', productId);
  853. }
  854. if (caseId) {
  855. console.log('📸 [目标内容] 案例ID (caseId):', caseId);
  856. }
  857. if (finalPlanId) {
  858. console.log('📋 [目标内容] 方案ID (planId/schemeId):', finalPlanId);
  859. console.log('👤 [分享人] 分享用户ID (shareUserId):', shareUserId || '无');
  860. }
  861. console.log('🏪 ===========================================');
  862. // 保存到临时存储,用于后续跳转和统计
  863. console.log('💾 [保存扫码信息] 开始保存到本地存储...');
  864. wx.setStorageSync('scan_storeId', storeId);
  865. wx.setStorageSync('scan_scanCount', scanCount || '0');
  866. wx.setStorageSync('scan_sourceType', sourceType);
  867. wx.setStorageSync('scan_sourceId', sourceId || '');
  868. wx.setStorageSync('scan_ownerId', ownerId || '');
  869. wx.setStorageSync('scan_employeeId', employeeId || '');
  870. wx.setStorageSync('scan_partnerId', partnerId || '');
  871. wx.setStorageSync('scan_userId', userId || '');
  872. if (productId) {
  873. wx.setStorageSync('scan_productId', productId);
  874. }
  875. if (caseId) {
  876. wx.setStorageSync('scan_caseId', caseId);
  877. }
  878. if (finalPlanId) {
  879. wx.setStorageSync('scan_planId', finalPlanId);
  880. if (shareUserId) {
  881. wx.setStorageSync('scan_shareUserId', shareUserId);
  882. }
  883. }
  884. wx.setStorageSync('need_scan_redirect', true);
  885. console.log('✅ [保存完成] 扫码信息已保存');
  886. console.log('📋 [保存内容摘要]:');
  887. console.log(' - 来源类型:', sourceType);
  888. console.log(' - 来源ID:', sourceId || '无');
  889. console.log(' - 店铺ID:', storeId);
  890. console.log(' - 需要跳转:', true);
  891. } else if (partnerId) {
  892. // 理论上异业二维码现在也必须带 storeId,这里仅作为兜底保护
  893. console.warn('⚠️ 检测到异业合作伙伴二维码缺少 storeId,无法确定门店,请检查二维码生成逻辑');
  894. }
  895. },
  896. /**
  897. * 判断是否需要跳转到扫码统计页面
  898. */
  899. shouldRedirectToScanPage() {
  900. return wx.getStorageSync('need_scan_redirect') === true;
  901. },
  902. /**
  903. * 跳转到扫码对应的店铺页面
  904. * 根据 storeId 跳转到对应店铺,同时记录所有来源信息用于统计
  905. */
  906. async redirectToScanPage() {
  907. try {
  908. console.log('🚀 ===========================================');
  909. console.log('🚀 [跳转处理] 开始处理扫码跳转');
  910. console.log('🚀 ===========================================');
  911. // 获取所有扫码相关参数
  912. const storeId = wx.getStorageSync('scan_storeId');
  913. const scanCount = wx.getStorageSync('scan_scanCount');
  914. const sourceType = wx.getStorageSync('scan_sourceType');
  915. const sourceId = wx.getStorageSync('scan_sourceId');
  916. const ownerId = wx.getStorageSync('scan_ownerId');
  917. const employeeId = wx.getStorageSync('scan_employeeId');
  918. const partnerId = wx.getStorageSync('scan_partnerId');
  919. const userId = wx.getStorageSync('scan_userId');
  920. const productId = wx.getStorageSync('scan_productId');
  921. const caseId = wx.getStorageSync('scan_caseId');
  922. const planId = wx.getStorageSync('scan_planId');
  923. const shareUserId = wx.getStorageSync('scan_shareUserId');
  924. console.log('📖 [读取存储] 从本地存储读取扫码信息:');
  925. console.log(' - storeId:', storeId || '无');
  926. console.log(' - sourceType:', sourceType || '无');
  927. console.log(' - sourceId:', sourceId || '无');
  928. console.log(' - scanCount:', scanCount || '无');
  929. console.log(' - ownerId:', ownerId || '无');
  930. console.log(' - employeeId:', employeeId || '无');
  931. console.log(' - partnerId:', partnerId || '无');
  932. console.log(' - userId:', userId || '无');
  933. console.log(' - productId:', productId || '无');
  934. console.log(' - caseId:', caseId || '无');
  935. console.log(' - planId:', planId || '无');
  936. console.log(' - shareUserId:', shareUserId || '无');
  937. // 清除临时存储
  938. console.log('🧹 [清理存储] 清除临时扫码信息...');
  939. wx.removeStorageSync('scan_storeId');
  940. wx.removeStorageSync('scan_scanCount');
  941. wx.removeStorageSync('scan_sourceType');
  942. wx.removeStorageSync('scan_sourceId');
  943. wx.removeStorageSync('scan_ownerId');
  944. wx.removeStorageSync('scan_employeeId');
  945. wx.removeStorageSync('scan_partnerId');
  946. wx.removeStorageSync('scan_userId');
  947. wx.removeStorageSync('scan_productId');
  948. wx.removeStorageSync('scan_caseId');
  949. wx.removeStorageSync('scan_planId');
  950. wx.removeStorageSync('scan_shareUserId');
  951. wx.removeStorageSync('need_scan_redirect');
  952. console.log('✅ [清理完成] 临时存储已清除');
  953. if (!storeId) {
  954. console.error('❌ [错误] 缺少 storeId 参数,无法跳转');
  955. return;
  956. }
  957. console.log('📊 ===========================================');
  958. console.log('📊 [扫码来源汇总] 扫码进入店铺');
  959. console.log('📊 店铺 ID:', storeId);
  960. console.log('📊 来源类型:', sourceType);
  961. console.log('📊 来源 ID:', sourceId || '无');
  962. console.log('📊 扫码次数:', scanCount);
  963. if (ownerId) console.log('📊 老板 ID:', ownerId);
  964. if (employeeId) console.log('📊 员工 ID:', employeeId);
  965. if (partnerId) console.log('📊 合作伙伴 ID:', partnerId);
  966. if (userId) console.log('📊 推广员 ID:', userId);
  967. if (productId) console.log('📊 产品 ID:', productId);
  968. if (caseId) console.log('📊 案例 ID:', caseId);
  969. if (planId) {
  970. console.log('📊 方案 ID:', planId);
  971. console.log('📊 分享用户 ID:', shareUserId || '无');
  972. }
  973. console.log('📊 ===========================================');
  974. // 设置店铺 ID 到全局和本地存储
  975. wx.setStorageSync('storeId', storeId);
  976. getApp().globalData.storeId = storeId;
  977. // 保存来源信息到本地存储(用于后续统计或绑定关系)
  978. if (sourceId) {
  979. wx.setStorageSync('scan_from_sourceType', sourceType);
  980. wx.setStorageSync('scan_from_sourceId', sourceId);
  981. console.log('✅ 已记录来源信息:', sourceType, sourceId);
  982. }
  983. // 检查用户是否已登录
  984. const currentUser = Parse.User.current();
  985. console.log('👤 [登录检查] 检查用户登录状态...');
  986. console.log(' - 当前用户:', currentUser ? currentUser.id : '未登录');
  987. console.log(' - 手机号:', currentUser?.get('mobile') || '无');
  988. if (currentUser) {
  989. // 用户已登录,立即记录扫码统计
  990. console.log('✅ [已登录] 用户已登录,立即记录扫码统计');
  991. await this.recordScanStatistics({
  992. storeId,
  993. sourceType,
  994. sourceId,
  995. ownerId,
  996. employeeId,
  997. partnerId,
  998. userId,
  999. productId,
  1000. scanCount
  1001. });
  1002. } else {
  1003. // 用户未登录,保存扫码信息,等登录后再记录
  1004. console.log('⚠️ [未登录] 用户未登录,保存扫码信息待登录后记录');
  1005. const pendingData = {
  1006. storeId,
  1007. sourceType,
  1008. sourceId,
  1009. ownerId,
  1010. employeeId,
  1011. partnerId,
  1012. userId,
  1013. productId,
  1014. scanCount,
  1015. timestamp: Date.now()
  1016. };
  1017. console.log('💾 [待处理记录] 保存内容:', JSON.stringify(pendingData, null, 2));
  1018. wx.setStorageSync('pending_scan_record', pendingData);
  1019. console.log('✅ [保存成功] 待处理扫码记录已保存');
  1020. }
  1021. // 如果有产品ID,直接跳转到产品详情的 H5 页面
  1022. if (productId) {
  1023. console.log('🎯 检测到产品ID,跳转到产品详情页');
  1024. await this.redirectToProductDetail(storeId, productId, partnerId);
  1025. return;
  1026. }
  1027. // 如果有案例ID,直接跳转到案例详情的 H5 页面
  1028. if (caseId) {
  1029. console.log('🎯 检测到案例ID,跳转到案例详情页');
  1030. await this.redirectToCaseDetail(storeId, caseId, partnerId);
  1031. return;
  1032. }
  1033. // 如果有方案ID,跳转到门店首页并传递方案ID
  1034. if (planId) {
  1035. console.log('🎯 检测到方案ID,跳转到门店首页展示方案');
  1036. await this.redirectToStoreWithPlan(storeId, planId, shareUserId, partnerId);
  1037. return;
  1038. }
  1039. // 获取默认首页路径并跳转
  1040. let url = getApp().globalData.rootPage || getApp().globalData.defaultTabBar.list[0].pagePath;
  1041. url += `?storeId=${storeId}`;
  1042. // 如果有产品ID,添加到URL参数中
  1043. if (productId) {
  1044. url += `&productId=${productId}`;
  1045. }
  1046. // 如果有异业合作伙伴ID,添加到URL参数中传回web-view
  1047. if (partnerId) {
  1048. url += `&partnerId=${partnerId}`;
  1049. }
  1050. console.log('✅ 跳转到店铺页面:', url);
  1051. wx.redirectTo({
  1052. url: url,
  1053. fail: (err) => {
  1054. console.error('❌ 跳转失败:', err);
  1055. // 如果 redirectTo 失败,尝试 reLaunch
  1056. wx.reLaunch({
  1057. url: url,
  1058. fail: (err2) => {
  1059. console.error('❌ reLaunch 也失败:', err2);
  1060. }
  1061. });
  1062. }
  1063. });
  1064. } catch (error) {
  1065. console.error('❌ 跳转到店铺页面失败:', error);
  1066. }
  1067. },
  1068. /**
  1069. * 跳转到活动页面
  1070. */
  1071. async redirectToActivityPage() {
  1072. try {
  1073. const activityId = wx.getStorageSync('scan_activityId');
  1074. // 清除临时存储
  1075. wx.removeStorageSync('scan_activityId');
  1076. wx.removeStorageSync('need_activity_redirect');
  1077. if (!activityId) {
  1078. console.error('❌ 缺少 activityId 参数,无法跳转');
  1079. return;
  1080. }
  1081. console.log('===========================================');
  1082. console.log('======= 扫码进入活动页面 =======');
  1083. console.log('活动 ID (activityId):', activityId);
  1084. console.log('===========================================');
  1085. // 保存活动ID
  1086. wx.setStorageSync('activityId', activityId);
  1087. // 获取默认首页路径并跳转(活动页面可能需要根据实际路由调整)
  1088. let url = getApp().globalData.rootPage || getApp().globalData.defaultTabBar.list[0].pagePath;
  1089. url += `?activityId=${activityId}`;
  1090. console.log('✅ 跳转到活动页面:', url);
  1091. wx.redirectTo({
  1092. url: url,
  1093. fail: (err) => {
  1094. console.error('❌ 跳转失败:', err);
  1095. wx.reLaunch({
  1096. url: url,
  1097. fail: (err2) => {
  1098. console.error('❌ reLaunch 也失败:', err2);
  1099. }
  1100. });
  1101. }
  1102. });
  1103. } catch (error) {
  1104. console.error('❌ 跳转到活动页面失败:', error);
  1105. }
  1106. },
  1107. /**
  1108. * 跳转到产品详情的 H5 页面
  1109. * @param {string} storeId - 店铺 ID
  1110. * @param {string} productId - 产品 ID
  1111. * @param {string} partnerId - 可选的合作伙伴 ID
  1112. */
  1113. async redirectToProductDetail(storeId, productId, partnerId = null) {
  1114. try {
  1115. console.log('===========================================');
  1116. console.log('======= 跳转到产品详情页 =======');
  1117. console.log('店铺 ID:', storeId);
  1118. console.log('产品 ID:', productId);
  1119. if (partnerId) console.log('合作伙伴 ID:', partnerId);
  1120. console.log('===========================================');
  1121. const currentUser = Parse.User.current();
  1122. const token = currentUser ? currentUser.getSessionToken() : null;
  1123. // 构建产品详情的 H5 URL(不要在这里编码,后面统一编码)
  1124. let h5Url = `https://app.fmode.cn/dev/pobingfeng/owner/nav/products?storeId=${storeId}`;
  1125. // 如果有 token,添加到 URL
  1126. if (token) {
  1127. h5Url += `&token=${token}`;
  1128. } else {
  1129. // 如果没有 token,使用游客模式
  1130. h5Url += `&guestMode=true`;
  1131. }
  1132. // 添加产品ID(不要在这里编码,避免双重编码)
  1133. h5Url += `&productId=${productId}`;
  1134. // 如果有合作伙伴ID,也添加到URL中
  1135. if (partnerId) {
  1136. h5Url += `&partnerId=${partnerId}`;
  1137. }
  1138. console.log('🌐 H5 URL:', h5Url);
  1139. // 编码 URL(统一在这里编码一次)
  1140. const encodedUrl = encodeURIComponent(h5Url);
  1141. // 构建 web-view 页面路径
  1142. let webViewPath = `/common-page/pages/web-view/index?path=${encodedUrl}&storeId=${storeId}`;
  1143. console.log('📄 web-view 页面路径:', webViewPath.substring(0, 100) + '...');
  1144. console.log('===========================================');
  1145. wx.redirectTo({
  1146. url: webViewPath,
  1147. success: () => {
  1148. console.log('✅ 跳转到产品详情页成功');
  1149. },
  1150. fail: (err) => {
  1151. console.error('❌ 跳转失败:', err);
  1152. // 如果 redirectTo 失败,尝试 reLaunch
  1153. wx.reLaunch({
  1154. url: webViewPath,
  1155. fail: (err2) => {
  1156. console.error('❌ reLaunch 也失败:', err2);
  1157. }
  1158. });
  1159. }
  1160. });
  1161. } catch (error) {
  1162. console.error('❌ 跳转到产品详情页失败:', error);
  1163. }
  1164. },
  1165. /**
  1166. * 跳转到案例详情的 H5 页面
  1167. * @param {string} storeId - 店铺 ID
  1168. * @param {string} caseId - 案例 ID
  1169. * @param {string} partnerId - 可选的合作伙伴 ID
  1170. */
  1171. async redirectToCaseDetail(storeId, caseId, partnerId = null) {
  1172. try {
  1173. console.log('===========================================');
  1174. console.log('======= 跳转到案例详情页 =======');
  1175. console.log('店铺 ID:', storeId);
  1176. console.log('案例 ID:', caseId);
  1177. if (partnerId) console.log('合作伙伴 ID:', partnerId);
  1178. console.log('===========================================');
  1179. const currentUser = Parse.User.current();
  1180. const token = currentUser ? currentUser.getSessionToken() : null;
  1181. // 构建案例详情的 H5 URL(不要在这里编码,后面统一编码)
  1182. let h5Url = `https://app.fmode.cn/dev/pobingfeng/owner/nav/cases?storeId=${storeId}`;
  1183. // 如果有 token,添加到 URL
  1184. if (token) {
  1185. h5Url += `&token=${token}`;
  1186. } else {
  1187. // 如果没有 token,使用游客模式
  1188. h5Url += `&guestMode=true`;
  1189. }
  1190. // 添加案例ID(不要在这里编码,避免双重编码)
  1191. h5Url += `&caseId=${caseId}`;
  1192. // 如果有合作伙伴ID,也添加到URL中
  1193. if (partnerId) {
  1194. h5Url += `&partnerId=${partnerId}`;
  1195. }
  1196. console.log('🌐 H5 URL:', h5Url);
  1197. // 编码 URL(统一在这里编码一次)
  1198. const encodedUrl = encodeURIComponent(h5Url);
  1199. // 构建 web-view 页面路径
  1200. let webViewPath = `/common-page/pages/web-view/index?path=${encodedUrl}&storeId=${storeId}`;
  1201. console.log('📄 web-view 页面路径:', webViewPath.substring(0, 100) + '...');
  1202. console.log('===========================================');
  1203. wx.redirectTo({
  1204. url: webViewPath,
  1205. success: () => {
  1206. console.log('✅ 跳转到案例详情页成功');
  1207. },
  1208. fail: (err) => {
  1209. console.error('❌ 跳转失败:', err);
  1210. // 如果 redirectTo 失败,尝试 reLaunch
  1211. wx.reLaunch({
  1212. url: webViewPath,
  1213. fail: (err2) => {
  1214. console.error('❌ reLaunch 也失败:', err2);
  1215. }
  1216. });
  1217. }
  1218. });
  1219. } catch (error) {
  1220. console.error('❌ 跳转到案例详情页失败:', error);
  1221. }
  1222. },
  1223. /**
  1224. * 跳转到门店首页并展示方案
  1225. * @param {string} storeId - 店铺 ID
  1226. * @param {string} planId - 方案 ID
  1227. * @param {string} shareUserId - 分享方案的用户 ID
  1228. * @param {string} partnerId - 可选的合作伙伴 ID
  1229. */
  1230. async redirectToStoreWithPlan(storeId, planId, shareUserId = null, partnerId = null) {
  1231. try {
  1232. console.log('===========================================');
  1233. console.log('======= 跳转到门店首页展示方案 =======');
  1234. console.log('店铺 ID:', storeId);
  1235. console.log('方案 ID:', planId);
  1236. if (shareUserId) console.log('分享用户 ID:', shareUserId);
  1237. if (partnerId) console.log('合作伙伴 ID:', partnerId);
  1238. console.log('===========================================');
  1239. const currentUser = Parse.User.current();
  1240. const token = currentUser ? currentUser.getSessionToken() : null;
  1241. // 构建门店首页的 H5 URL,带上方案ID
  1242. let h5Url = `https://app.fmode.cn/dev/pobingfeng/owner/nav/home?storeId=${storeId}`;
  1243. // 如果有 token,添加到 URL
  1244. if (token) {
  1245. h5Url += `&token=${token}`;
  1246. } else {
  1247. // 如果没有 token,使用游客模式
  1248. h5Url += `&guestMode=true`;
  1249. }
  1250. // 添加方案ID(使用planId参数名)
  1251. h5Url += `&planId=${planId}`;
  1252. // 如果有分享用户ID,也添加到URL中
  1253. if (shareUserId) {
  1254. h5Url += `&shareUserId=${shareUserId}`;
  1255. }
  1256. // 如果有合作伙伴ID,也添加到URL中
  1257. if (partnerId) {
  1258. h5Url += `&partnerId=${partnerId}`;
  1259. }
  1260. console.log('🌐 H5 URL:', h5Url);
  1261. // 编码 URL
  1262. const encodedUrl = encodeURIComponent(h5Url);
  1263. // 构建 web-view 页面路径
  1264. let webViewPath = `/common-page/pages/web-view/index?path=${encodedUrl}&storeId=${storeId}`;
  1265. console.log('📄 web-view 页面路径:', webViewPath.substring(0, 100) + '...');
  1266. console.log('===========================================');
  1267. wx.redirectTo({
  1268. url: webViewPath,
  1269. success: () => {
  1270. console.log('✅ 跳转到门店首页展示方案成功');
  1271. },
  1272. fail: (err) => {
  1273. console.error('❌ 跳转失败:', err);
  1274. // 如果 redirectTo 失败,尝试 reLaunch
  1275. wx.reLaunch({
  1276. url: webViewPath,
  1277. fail: (err2) => {
  1278. console.error('❌ reLaunch 也失败:', err2);
  1279. }
  1280. });
  1281. }
  1282. });
  1283. } catch (error) {
  1284. console.error('❌ 跳转到门店首页展示方案失败:', error);
  1285. }
  1286. },
  1287. /**
  1288. * 记录扫码统计信息
  1289. * @param {Object} params - 扫码参数对象
  1290. * @param {string} params.storeId - 店铺 ID
  1291. * @param {string} params.sourceType - 来源类型 (employee/owner/partner/promoter/store)
  1292. * @param {string} params.sourceId - 来源 ID
  1293. * @param {string} params.ownerId - 老板 ID
  1294. * @param {string} params.employeeId - 员工 ID
  1295. * @param {string} params.partnerId - 合作伙伴 ID
  1296. * @param {string} params.userId - 推广员 ID
  1297. * @param {string} params.productId - 产品 ID
  1298. * @param {string} params.scanCount - 扫码次数
  1299. */
  1300. async recordScanStatistics(params) {
  1301. try {
  1302. console.log('📝 ===========================================');
  1303. console.log('📝 [扫码统计] 开始记录扫码统计');
  1304. console.log('📝 ===========================================');
  1305. const {
  1306. storeId,
  1307. sourceType,
  1308. sourceId,
  1309. ownerId,
  1310. employeeId,
  1311. partnerId,
  1312. userId,
  1313. productId,
  1314. scanCount
  1315. } = params;
  1316. console.log('📋 [统计参数] 接收到的参数:');
  1317. console.log(' - storeId:', storeId);
  1318. console.log(' - sourceType:', sourceType);
  1319. console.log(' - sourceId:', sourceId || '无');
  1320. console.log(' - ownerId:', ownerId || '无');
  1321. console.log(' - employeeId:', employeeId || '无');
  1322. console.log(' - partnerId:', partnerId || '无');
  1323. console.log(' - userId:', userId || '无');
  1324. console.log(' - productId:', productId || '无');
  1325. console.log(' - scanCount:', scanCount || '0');
  1326. // 检查用户是否已登录
  1327. const currentUser = Parse.User.current();
  1328. if (!currentUser) {
  1329. console.log('⚠️ [未登录] 用户未登录,不记录扫码统计');
  1330. return;
  1331. }
  1332. console.log('👤 [用户信息] 当前登录用户:');
  1333. console.log(' - 用户ID:', currentUser.id);
  1334. console.log(' - 手机号:', currentUser.get('mobile') || '无');
  1335. // 如果没有来源信息,不记录统计
  1336. if (!sourceId && !ownerId && !employeeId && !partnerId && !userId) {
  1337. console.log('ℹ️ [跳过] 无来源信息,跳过统计记录');
  1338. return;
  1339. }
  1340. console.log('✅ [开始记录] 用户已登录且有来源信息,开始记录');
  1341. // 创建扫码记录
  1342. const ScanRecord = Parse.Object.extend('ScanRecord');
  1343. const record = new ScanRecord();
  1344. record.set('company', {
  1345. __type: 'Pointer',
  1346. className: 'Company',
  1347. objectId: getApp().globalData.company
  1348. });
  1349. record.set('storeId', storeId);
  1350. record.set('sourceType', sourceType || 'unknown');
  1351. record.set('scanCount', parseInt(scanCount) || 0);
  1352. record.set('scanTime', new Date());
  1353. console.log('📦 [记录内容] 准备保存到 ScanRecord 表:');
  1354. console.log(' - company:', getApp().globalData.company);
  1355. console.log(' - storeId:', storeId);
  1356. console.log(' - sourceType:', sourceType || 'unknown');
  1357. console.log(' - scanCount:', parseInt(scanCount) || 0);
  1358. console.log(' - scanTime:', new Date().toISOString());
  1359. // 根据来源类型设置对应的ID
  1360. if (ownerId) {
  1361. record.set('ownerId', ownerId);
  1362. console.log(' - ownerId:', ownerId);
  1363. }
  1364. if (employeeId) {
  1365. record.set('employeeId', employeeId);
  1366. console.log(' - employeeId:', employeeId);
  1367. }
  1368. if (partnerId) {
  1369. record.set('partnerId', partnerId);
  1370. console.log(' - partnerId:', partnerId);
  1371. }
  1372. if (userId) {
  1373. record.set('userId', userId);
  1374. console.log(' - userId:', userId);
  1375. }
  1376. if (productId) {
  1377. record.set('productId', productId);
  1378. console.log(' - productId:', productId);
  1379. }
  1380. // 记录扫码用户
  1381. record.set('user', {
  1382. __type: 'Pointer',
  1383. className: '_User',
  1384. objectId: currentUser.id
  1385. });
  1386. console.log(' - user:', currentUser.id);
  1387. await record.save();
  1388. console.log('✅ [保存成功] 扫码记录已保存到数据库');
  1389. console.log(' - 记录ID:', record.id);
  1390. // 如果存在异业合作伙伴ID,处理异业绑定和扫码次数
  1391. if (partnerId) {
  1392. try {
  1393. console.log('🤝 ===========================================');
  1394. console.log('🤝 [异业处理] 开始处理异业扫码逻辑');
  1395. console.log('🤝 用户ID:', currentUser.id);
  1396. console.log('🤝 异业ID:', partnerId);
  1397. console.log('🤝 门店ID:', storeId);
  1398. console.log('🤝 ===========================================');
  1399. // 检查用户是否已经绑定了异业合作伙伴
  1400. const userBoundPartner = currentUser.get('Partner');
  1401. const userBoundPartnerId = userBoundPartner ? userBoundPartner.id : null;
  1402. console.log('🔍 [绑定检查] 用户已绑定的异业ID:', userBoundPartnerId || '无');
  1403. // 规则1:如果用户已绑定其他异业,不处理当前异业的扫码
  1404. if (userBoundPartnerId && userBoundPartnerId !== partnerId) {
  1405. console.log('⚠️ [规则1] 用户已绑定其他异业,不处理当前异业的扫码');
  1406. console.log(' - 已绑定异业:', userBoundPartnerId);
  1407. console.log(' - 当前扫码异业:', partnerId);
  1408. console.log('🤝 ===========================================');
  1409. return;
  1410. }
  1411. // 规则2:检查该用户在该门店是否已经扫过该异业的码
  1412. console.log('🔍 [重复检查] 检查是否已扫过该异业的码...');
  1413. const ScanRecord = Parse.Object.extend('ScanRecord');
  1414. const scanQuery = new Parse.Query(ScanRecord);
  1415. scanQuery.equalTo('user', {
  1416. __type: 'Pointer',
  1417. className: '_User',
  1418. objectId: currentUser.id
  1419. });
  1420. scanQuery.equalTo('partnerId', partnerId);
  1421. scanQuery.equalTo('storeId', storeId);
  1422. const existingScan = await scanQuery.first();
  1423. if (existingScan) {
  1424. console.log('⚠️ [规则2] 该用户在该门店已扫过该异业的码,不重复计数');
  1425. console.log(' - 首次扫码时间:', existingScan.get('scanTime'));
  1426. console.log(' - 记录ID:', existingScan.id);
  1427. console.log('🤝 ===========================================');
  1428. return;
  1429. }
  1430. console.log('✅ [首次扫码] 该用户在该门店首次扫该异业码');
  1431. // 规则3:如果用户还没有绑定异业,绑定当前异业
  1432. if (!userBoundPartnerId) {
  1433. console.log('🔗 [规则3] 用户首次扫异业码,开始绑定异业合作伙伴');
  1434. console.log(' - 异业ID:', partnerId);
  1435. currentUser.set('Partner', {
  1436. __type: 'Pointer',
  1437. className: 'Partner',
  1438. objectId: partnerId
  1439. });
  1440. await currentUser.save();
  1441. console.log('✅ [绑定成功] 异业绑定成功');
  1442. } else {
  1443. console.log('ℹ️ [已绑定] 用户已绑定该异业,无需重复绑定');
  1444. }
  1445. // 规则4:为该异业在该门店增加扫码次数
  1446. console.log('📊 [规则4] 开始更新异业扫码次数...');
  1447. const Partner = Parse.Object.extend('Partner');
  1448. const partnerQuery = new Parse.Query(Partner);
  1449. const partnerObj = await partnerQuery.get(partnerId);
  1450. if (partnerObj) {
  1451. console.log('📋 [异业信息] 找到异业合作伙伴记录');
  1452. console.log(' - 异业ID:', partnerObj.id);
  1453. console.log(' - 异业名称:', partnerObj.get('name') || '未设置');
  1454. // 获取或初始化门店扫码次数映射
  1455. let storeScans = partnerObj.get('storeScans') || {};
  1456. const oldCount = storeScans[storeId] || 0;
  1457. // 为该门店的扫码次数 +1
  1458. storeScans[storeId] = oldCount + 1;
  1459. partnerObj.set('storeScans', storeScans);
  1460. // 同时更新总扫码次数
  1461. const oldTotalCount = partnerObj.get('scanCount') || 0;
  1462. partnerObj.increment('scanCount', 1);
  1463. await partnerObj.save();
  1464. console.log('✅ [更新成功] 异业扫码次数已更新');
  1465. console.log(' - 门店ID:', storeId);
  1466. console.log(' - 该门店扫码次数: %d → %d', oldCount, storeScans[storeId]);
  1467. console.log(' - 总扫码次数: %d → %d', oldTotalCount, oldTotalCount + 1);
  1468. } else {
  1469. console.error('❌ [错误] 未找到异业合作伙伴记录');
  1470. }
  1471. console.log('🤝 ===========================================');
  1472. } catch (e) {
  1473. console.error('❌ [异业处理失败] 处理异业合作伙伴绑定失败:', e);
  1474. console.error(' - 错误信息:', e.message);
  1475. console.error(' - 错误堆栈:', e.stack);
  1476. console.log('🤝 ===========================================');
  1477. }
  1478. }
  1479. } catch (error) {
  1480. console.warn('⚠️ 保存扫码记录失败:', error);
  1481. // 不影响主流程
  1482. }
  1483. }
  1484. });