index.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. let Parse = getApp().Parse;
  2. let company = getApp().globalData.company
  3. const request = require("../../utils/request");
  4. const qiniuUploader = require("../../utils/qiniuUploader");
  5. //获取应用实例
  6. const app = getApp()
  7. const real = require('../../utils/real')
  8. var timer
  9. function normalizeCnMobile(value) {
  10. if (value === null || value === undefined) return '';
  11. const digits = String(value).replace(/\D/g, '');
  12. if (!digits) return '';
  13. const last11 = digits.length >= 11 ? digits.slice(-11) : digits;
  14. if (/^1\d{10}$/.test(last11)) return last11;
  15. if (/^1\d{10}$/.test(digits)) return digits;
  16. return '';
  17. }
  18. function addCnMobileToSet(set, value) {
  19. const m = normalizeCnMobile(value);
  20. if (m) set.add(m);
  21. }
  22. async function localRejudgeIfNeeded() {
  23. try {
  24. const pending = wx.getStorageSync('traffic_deduct_pending');
  25. if (!pending) return;
  26. const storeId = pending && pending.storeId ? pending.storeId : null;
  27. const currentUser = Parse.User.current();
  28. const numsSet = new Set();
  29. addCnMobileToSet(numsSet, currentUser && currentUser.get('mobile'));
  30. addCnMobileToSet(numsSet, currentUser && currentUser.get('phone'));
  31. try { addCnMobileToSet(numsSet, wx.getStorageSync('user_mobile')); } catch (e) {}
  32. const uniqUserNumbers = Array.from(numsSet);
  33. console.log('🚦 [traffic] 授权页本地复判开始:', {
  34. storeId,
  35. userId: currentUser?.id,
  36. userNumbers: uniqUserNumbers
  37. });
  38. if (!storeId || !uniqUserNumbers.length) {
  39. console.log('🚦 [traffic] 本地复判条件不足,退出');
  40. return;
  41. }
  42. const phones = new Set();
  43. const mobiles = new Set();
  44. const tryPartnerQueries = [
  45. { key: 'storeId', value: storeId },
  46. { key: 'store', value: { __type: 'Pointer', className: 'ShopStore', objectId: storeId } },
  47. { key: 'shopStore', value: { __type: 'Pointer', className: 'ShopStore', objectId: storeId } }
  48. ];
  49. for (const item of tryPartnerQueries) {
  50. try {
  51. const q = new Parse.Query('Partner');
  52. q.equalTo(item.key, item.value);
  53. q.limit(200);
  54. q.select('phone', 'mobile', 'name');
  55. const list = await q.find();
  56. if (list && list.length) {
  57. list.forEach((p) => {
  58. addCnMobileToSet(phones, p.get('phone'));
  59. addCnMobileToSet(mobiles, p.get('mobile'));
  60. });
  61. }
  62. } catch (e) {}
  63. }
  64. console.log('🚦 [traffic] 授权页本地复判 Partner 集合:', {
  65. phoneCount: phones.size,
  66. mobileCount: mobiles.size
  67. });
  68. const phoneHit = uniqUserNumbers.some((n) => phones.has(n));
  69. const mobileHit = uniqUserNumbers.some((n) => mobiles.has(n));
  70. if (phoneHit || mobileHit) {
  71. console.log('🚦 [traffic] 授权页本地复判命中:', {
  72. reason: phoneHit ? 'partner_phone_match' : 'partner_mobile_match',
  73. matches: uniqUserNumbers.filter((n) => phoneHit ? phones.has(n) : mobiles.has(n))
  74. });
  75. try {
  76. currentUser.set('trafficExempt', true);
  77. currentUser.set('trafficExemptAt', new Date());
  78. currentUser.set('trafficExemptStoreId', storeId);
  79. await currentUser.save();
  80. } catch (e) {}
  81. wx.removeStorageSync('traffic_deduct_pending');
  82. } else {
  83. console.log('🚦 [traffic] 授权页本地复判未命中,等待首页复判继续');
  84. }
  85. } catch (e) {
  86. console.warn('🚦 [traffic] 授权页本地复判失败:', e?.message || e);
  87. }
  88. }
  89. /**
  90. * @class NovaAppAuth
  91. * @memberof module:components
  92. * @tutorial userauth
  93. * @desc 通用登录组件
  94. * # 登录组件相关数据表
  95. * - _User
  96. */
  97. Page({
  98. data: {
  99. phoneModal: false, //短信验证码登录弹窗
  100. logo: "https://file-cloud.fmode.cn/MldI5PBNt7/20210928/g0k1jb034826.png",
  101. name: "未来商城",
  102. desc: "江西脑控科技有限公司是国内领先的IT技术企业。专注于互联网+服务,面向政府提供区块链、大数据、物联网、人工智能解决方案",
  103. wxModel: false,
  104. avatarUrl: '',
  105. avatar: '',
  106. nickname: '',
  107. check: false,
  108. mobile: '', //手机号
  109. verilyCode: '', //验证码
  110. s: 0, //获取验证码倒计时 秒/s
  111. countDown: false,
  112. avatarKey: Date.now(), // 用于强制刷新头像显示
  113. isProcessingAuth: false, // 标记是否正在处理授权流程
  114. },
  115. onLoad: async function (options) {
  116. let Company = new Parse.Query('Company')
  117. Company.equalTo("objectId", company)
  118. let currentCompany = await Company.first()
  119. if (currentCompany && currentCompany.id) {
  120. this.setData({
  121. logo: currentCompany.get('logo'),
  122. name: currentCompany.get('name'),
  123. desc: currentCompany.get('desc')
  124. })
  125. }
  126. this.getUptoken()
  127. this.getAgreement() //用户协议
  128. },
  129. async getUptoken() {
  130. let res = await Parse.Cloud.run('qiniu_uptoken', {
  131. company: company
  132. })
  133. this.setData({
  134. uptokenURL: res.uptoken,
  135. domain: res.domain,
  136. uploadURL: res.zoneUrl
  137. })
  138. },
  139. async getAgreement() {
  140. let query = new Parse.Query('ContractAgreement')
  141. query.equalTo('type', 'wxapp')
  142. query.equalTo('company', company)
  143. query.select('title', 'content')
  144. let res = await query.first()
  145. if (res?.id) {
  146. this.setData({
  147. agreement: res.toJSON()
  148. })
  149. }
  150. },
  151. /* 是否同意授权协议 */
  152. getAgreementAuth() {
  153. if (!this.data.check && this.data.agreement) {
  154. wx.showModal({
  155. title: '提示',
  156. content: `请您仔细阅读并充分理解相关条款,点击同意即代表已阅读并同意《${this.data.agreement.title || '用户隐私协议'}》`,
  157. showCancel: true,
  158. cancelText: '取消',
  159. cancelColor: '#000000',
  160. confirmText: '同意',
  161. confirmColor: '#3CC51F',
  162. success: (result) => {
  163. if (result.confirm) {
  164. this.setData({
  165. check: true,
  166. })
  167. // this.getUserProfile()
  168. }
  169. },
  170. fail: () => { },
  171. complete: () => { }
  172. });
  173. return
  174. }
  175. // this.getUserProfile()
  176. return true
  177. },
  178. /* 判断是否绑定手机号 */
  179. async getUserProfile() {
  180. // 检查用户是否已登录
  181. let currentUser = Parse.User.current();
  182. if (!currentUser?.id) {
  183. console.log('⚠️ 用户未登录,需要先登录');
  184. // 不要在这里调用 checkAuth(true),因为它会触发微信官方的强制授权弹窗
  185. // 应该在外层(index.js)处理登录逻辑
  186. return false;
  187. }
  188. /* 如果手机号存在,已注册过判断是否上传过头像昵称信息 */
  189. if (currentUser?.get('mobile')) {
  190. wx.setStorageSync("userLogin", currentUser.id);
  191. // 检查是否需要完善信息(可选)
  192. // 如果用户没有昵称或昵称是默认的,提示完善信息
  193. const needProfile = !currentUser.get('nickname') ||
  194. currentUser.get('nickname') === '微信用户' ||
  195. currentUser.get('nickname') === '';
  196. if (needProfile) {
  197. // 显示自定义的完善信息弹窗(允许跳过)
  198. console.log('ℹ️ 显示自定义头像昵称弹窗');
  199. this.setData({
  200. wxModel: true
  201. });
  202. return false;
  203. }
  204. // 用户信息完整,返回上一页
  205. this.backLoad();
  206. return false;
  207. }
  208. // 用户已登录但没有手机号,允许继续
  209. return true;
  210. },
  211. /* 短信验证码登录弹窗 */
  212. async showDialogBtn() {
  213. let auth = this.getAgreementAuth()
  214. if (!auth) return
  215. let userProfile = await this.getUserProfile()
  216. if (!userProfile) return
  217. // 标记正在处理授权
  218. this.setData({
  219. isProcessingAuth: true,
  220. phoneModal: true
  221. })
  222. },
  223. async getPhoneNumber(e) {
  224. let auth = this.getAgreementAuth()
  225. if (!auth) return
  226. let userProfile = await this.getUserProfile()
  227. if (!userProfile) return
  228. // 标记正在处理授权
  229. this.setData({
  230. isProcessingAuth: true
  231. })
  232. let {
  233. code
  234. } = e.detail
  235. console.log(code);
  236. let phoneNumber = await request.getPhone(code)
  237. if(phoneNumber) this.authMobileUser(phoneNumber)
  238. },
  239. //合并User与绑定手机号逻辑
  240. async authMobileUser(mobile) {
  241. let currentUser = Parse.User.current()
  242. let queryMobUser = new Parse.Query('_User')
  243. queryMobUser.equalTo('mobile', mobile)
  244. queryMobUser.equalTo('company', company)
  245. queryMobUser.notEqualTo('type', 'admin')
  246. queryMobUser.notEqualTo('isDeleted', true)
  247. // queryMobUser.exists(`wxapp.${getApp().globalData.appid}`)
  248. let resMobUser = await queryMobUser.first()
  249. if (resMobUser?.id) {
  250. //请求User合并API,获取token重新登录,再更新昵称头像信息等
  251. let token = await this.getUpdateUserToken(currentUser.id, resMobUser.id)
  252. console.log(token);
  253. if (token) {
  254. Parse.User.become(token).then(async user => {
  255. // let user = Parse.User.current();
  256. wx.setStorageSync("userLogin", user.id);
  257. try {
  258. wx.setStorageSync('user_mobile', user.get('mobile') || '');
  259. } catch (e) {}
  260. try {
  261. if (typeof getApp().checkTrafficDeductPending === 'function') {
  262. console.log('🚦 [traffic] 触发暂缓扣减复判(merge-become 成功后)');
  263. await getApp().checkTrafficDeductPending();
  264. } else {
  265. await localRejudgeIfNeeded();
  266. }
  267. } catch (e) {
  268. console.warn('🚦 [traffic] 暂缓扣减复判触发失败:', e?.message || e);
  269. }
  270. if (!user.get('avatar') || user.get('nickname') == '微信用户' || !user.get('nickname')) {
  271. this.setData({
  272. phoneModal: false,
  273. wxModel: true
  274. })
  275. return
  276. }
  277. this.backLoad()
  278. return
  279. })
  280. .catch(async err => {
  281. console.log('❌ Parse.User.become 失败:', err);
  282. // 不要直接退出,而是尝试重新登录
  283. wx.showModal({
  284. title: '提示',
  285. content: '登录状态异常,是否重新登录?',
  286. showCancel: true,
  287. cancelText: '取消',
  288. confirmText: '重新登录',
  289. success: async (result) => {
  290. if (result.confirm) {
  291. // 清除登录状态
  292. wx.removeStorageSync("sessionToken");
  293. wx.removeStorageSync("userLogin");
  294. try {
  295. // 尝试重新登录
  296. await Parse.User.logOut();
  297. await getApp().checkAuth(true);
  298. // 重新获取用户信息
  299. const currentUser = Parse.User.current();
  300. if (currentUser && currentUser.get('mobile')) {
  301. wx.setStorageSync("userLogin", currentUser.id);
  302. this.backLoad();
  303. }
  304. } catch (reloginErr) {
  305. console.error('❌ 重新登录失败:', reloginErr);
  306. wx.showToast({
  307. title: '登录失败,请稍后重试',
  308. icon: 'none'
  309. });
  310. }
  311. } else {
  312. // 用户取消,返回上一页
  313. wx.navigateBack();
  314. }
  315. },
  316. });
  317. return
  318. })
  319. return
  320. }
  321. return
  322. }
  323. currentUser.set('mobile', mobile)
  324. await currentUser.save()
  325. try {
  326. wx.setStorageSync('user_mobile', mobile || '');
  327. if (typeof getApp().checkTrafficDeductPending === 'function') {
  328. console.log('🚦 [traffic] 触发暂缓扣减复判(绑定手机号后)');
  329. await getApp().checkTrafficDeductPending();
  330. } else {
  331. await localRejudgeIfNeeded();
  332. }
  333. } catch (e) {
  334. console.warn('🚦 [traffic] 暂缓扣减复判触发失败:', e?.message || e);
  335. }
  336. if (!currentUser.get('avatar') || currentUser.get('nickname') == '微信用户' || !currentUser.get('nickname')) {
  337. this.setData({
  338. phoneModal: false,
  339. wxModel: true
  340. })
  341. return
  342. }
  343. this.backLoad()
  344. return
  345. },
  346. async getUpdateUserToken(oldUser, newUserId) {
  347. return new Promise((resolve, reject) => {
  348. wx.login({
  349. success: function (res) {
  350. let parms = {
  351. oldUserId: oldUser,
  352. newUserId: newUserId,
  353. appId: getApp().globalData.appid,
  354. code: res.code,
  355. companyId: company,
  356. appType: getApp().globalData.appType || ''
  357. }
  358. if (res.code) {
  359. let url = 'https://server.fmode.cn/api/wxapp/combine/user'
  360. wx.request({
  361. url: url,
  362. data: parms,
  363. header: { 'content-type': 'application/json' },
  364. method: 'POST',
  365. async success(res) {
  366. console.log(res);
  367. let data = res.data
  368. if (data.code == 200) {
  369. wx.setStorageSync("sessionToken", data.data.token);
  370. // 用户合并成功后,更新 ScanRecord 表中的 user 字段
  371. console.log('🔄 [用户合并] 开始更新 ScanRecord 中的用户关联');
  372. console.log(' - 旧用户ID:', oldUser);
  373. console.log(' - 新用户ID:', newUserId);
  374. try {
  375. // 使用 Parse.User.become 切换到新用户
  376. await Parse.User.become(data.data.token);
  377. // 查询所有旧用户的扫码记录
  378. const ScanRecord = Parse.Object.extend('ScanRecord');
  379. const scanQuery = new Parse.Query(ScanRecord);
  380. scanQuery.equalTo('user', {
  381. __type: 'Pointer',
  382. className: '_User',
  383. objectId: oldUser
  384. });
  385. scanQuery.limit(1000); // 设置查询上限
  386. const oldRecords = await scanQuery.find();
  387. console.log(`📋 [查询结果] 找到 ${oldRecords.length} 条旧用户的扫码记录`);
  388. if (oldRecords.length > 0) {
  389. // 批量更新所有记录,将 user 指向新用户
  390. const updatePromises = oldRecords.map(record => {
  391. record.set('user', {
  392. __type: 'Pointer',
  393. className: '_User',
  394. objectId: newUserId
  395. });
  396. return record.save();
  397. });
  398. await Promise.all(updatePromises);
  399. console.log(`✅ [更新成功] 已将 ${oldRecords.length} 条扫码记录的用户更新为新用户`);
  400. } else {
  401. console.log('ℹ️ [无需更新] 旧用户没有扫码记录');
  402. }
  403. } catch (updateError) {
  404. console.error('❌ [更新失败] 更新 ScanRecord 失败:', updateError);
  405. console.error(' - 错误信息:', updateError.message);
  406. // 不影响主流程,继续返回 token
  407. }
  408. resolve(data.data.token)
  409. } else {
  410. console.log(data?.mess);
  411. wx.showModal({
  412. title: '提示',
  413. content: data?.mess,
  414. showCancel: false,
  415. cancelText: '取消',
  416. cancelColor: '#000000',
  417. confirmText: '确定',
  418. confirmColor: '#3CC51F',
  419. success: (result) => {
  420. if (result.confirm) {
  421. }
  422. },
  423. fail: () => { },
  424. complete: () => { }
  425. });
  426. resolve()
  427. }
  428. },
  429. });
  430. }
  431. },
  432. fail: function (err) {
  433. wx.showModal({
  434. title: '提示',
  435. content: '登录超时,请稍后重试',
  436. showCancel: false,
  437. cancelText: '取消',
  438. cancelColor: '#000000',
  439. confirmText: '确定',
  440. confirmColor: '#3CC51F',
  441. success: (result) => {
  442. if (result.confirm) {
  443. }
  444. },
  445. fail: () => { },
  446. complete: () => { }
  447. });
  448. console.warn('小程序wx.login失败');
  449. resolve()
  450. }
  451. });
  452. })
  453. },
  454. //获取验证码
  455. async getPhoneCode() {
  456. let { mobile, s, countDown } = this.data
  457. if (!real.isPoneAvailable(mobile)) {
  458. wx.showToast({
  459. title: '手机号格式有误',
  460. icon: 'error',
  461. duration: 1500,
  462. mask: false,
  463. });
  464. return
  465. }
  466. if(countDown || s > 0) return
  467. this.setData({
  468. countDown:true
  469. })
  470. let parsm = {
  471. company: company,
  472. mobile: mobile
  473. }
  474. let code = await this.getRequest(parsm, 'message')
  475. if(code?.code == 1){
  476. this.setData({
  477. s:60
  478. })
  479. this.decrementTime()
  480. }else{
  481. wx.showToast({
  482. title: '验证码获取失败',
  483. icon: 'error',
  484. image: '',
  485. duration: 1500,
  486. mask: false,
  487. });
  488. this.setData({
  489. countDown:false
  490. })
  491. }
  492. },
  493. //接口请求
  494. getRequest(parsm, apig) {
  495. return new Promise((resolve, rej) => {
  496. let url = 'https://server.fmode.cn/api/apig/'
  497. wx.request({
  498. url: url + apig,
  499. data: parsm,
  500. header: { 'content-type': 'application/json' },
  501. method: 'POST',
  502. dataType: 'json',
  503. responseType: 'text',
  504. success: (result) => {
  505. console.log(result);
  506. resolve(result.data)
  507. },
  508. fail: () => {
  509. resolve(false)
  510. },
  511. complete: () => { }
  512. });
  513. })
  514. },
  515. // 倒计时
  516. decrementTime(){
  517. timer = setTimeout(() => {
  518. let { s } = this.data
  519. if(s == 0){
  520. this.setData({ countDown:false })
  521. timer && clearTimeout(timer)
  522. return
  523. }
  524. s--
  525. this.setData({
  526. s:s
  527. })
  528. this.decrementTime()
  529. }, 1000);
  530. },
  531. //验证码绑定手机号登录
  532. async completePhone() {
  533. let { mobile, verilyCode } = this.data
  534. if(!real.isPoneAvailable(mobile) || !verilyCode.toString().trim()){
  535. wx.showToast({
  536. title: '手机号或验证码不正确',
  537. icon: 'none',
  538. image: '',
  539. duration: 1500,
  540. mask: false,
  541. });
  542. return
  543. }
  544. // 标记正在处理授权
  545. this.setData({
  546. isProcessingAuth: true
  547. })
  548. let parsm = {
  549. mobile: mobile,
  550. code:verilyCode
  551. }
  552. let isVerify = await this.getRequest(parsm, 'verifyCode')
  553. console.log(isVerify);
  554. if(isVerify.code == 200){
  555. this.authMobileUser(mobile.toString())
  556. }else{
  557. // 验证失败,重置标记
  558. this.setData({
  559. isProcessingAuth: false
  560. })
  561. wx.showToast({
  562. title:isVerify?.msg || '验证码错误',
  563. icon: 'none',
  564. image: '',
  565. duration: 1500,
  566. mask: false,
  567. });
  568. return
  569. }
  570. },
  571. //关闭手机号授权弹窗
  572. hideModal: function () {
  573. this.setData({
  574. phoneModal: false,
  575. isProcessingAuth: false // 重置标记
  576. })
  577. },
  578. backLoad() {
  579. console.log('===========================================');
  580. console.log('======= backLoad 方法调用 =======');
  581. let pages = getCurrentPages();
  582. console.log('当前页面栈层数:', pages.length);
  583. console.log('当前页面路由:', pages[pages.length - 1]?.route);
  584. // 检查是否有 returnUrl 参数(从 H5 页面传递过来)
  585. const currentPage = pages[pages.length - 1];
  586. const returnUrl = currentPage?.options?.returnUrl;
  587. if (returnUrl) {
  588. console.log('📍 检测到 returnUrl 参数:', returnUrl);
  589. // 如果是 web-view 页面,跳转回去
  590. const decodedUrl = decodeURIComponent(returnUrl);
  591. console.log('🔙 准备跳转回 H5 页面:', decodedUrl);
  592. wx.navigateTo({
  593. url: `/common-page/pages/web-view/index?path=${encodeURIComponent(decodedUrl)}`,
  594. success: () => {
  595. console.log('✅ 跳转回 H5 页面成功');
  596. console.log('===========================================');
  597. },
  598. fail: (err) => {
  599. console.error('❌ 跳转回 H5 页面失败:', err);
  600. // 降级:正常返回上一页
  601. this.normalBackLoad();
  602. }
  603. });
  604. return;
  605. }
  606. // 没有 returnUrl,执行正常的返回逻辑
  607. this.normalBackLoad();
  608. },
  609. normalBackLoad() {
  610. let pages = getCurrentPages();
  611. // 如果页面栈只有1层或没有上一页,直接跳转到首页
  612. if (pages.length <= 1) {
  613. console.log('⚠️ 没有上一个页面,直接跳转到首页');
  614. this.goToHome();
  615. return;
  616. }
  617. // 有上一个页面
  618. let beforePage = pages[pages.length - 2];
  619. console.log('上一个页面路由:', beforePage.route);
  620. // 尝试调用上一个页面的 onLoad 方法(如果存在)
  621. if (beforePage && typeof beforePage.onLoad === 'function') {
  622. try {
  623. let options = beforePage.options || { isInit: true };
  624. beforePage.onLoad(options);
  625. console.log('✅ 已调用上一个页面的 onLoad');
  626. } catch (err) {
  627. console.warn('⚠️ 调用上一个页面 onLoad 失败:', err);
  628. }
  629. }
  630. // 返回上一页
  631. console.log('🔙 执行 navigateBack...');
  632. wx.navigateBack({
  633. delta: 1,
  634. success: () => {
  635. console.log('✅ navigateBack 成功');
  636. console.log('===========================================');
  637. },
  638. fail: (err) => {
  639. console.error('❌ navigateBack 失败:', err);
  640. console.log('⚠️ 尝试使用 reLaunch 跳转到首页');
  641. this.goToHome();
  642. }
  643. });
  644. },
  645. // 跳转到首页
  646. goToHome() {
  647. console.log('===========================================');
  648. console.log('======= goToHome 方法调用 =======');
  649. const app = getApp();
  650. console.log('globalData.rootPage:', app.globalData.rootPage);
  651. console.log('globalData.defaultTabBar:', app.globalData.defaultTabBar);
  652. // 尝试多个可能的首页路径
  653. let rootPage = app.globalData.rootPage
  654. || app.globalData.defaultTabBar?.list?.[0]?.pagePath
  655. || '/pages/index/index'
  656. || '/index/index';
  657. // 确保路径以 / 开头
  658. if (!rootPage.startsWith('/')) {
  659. rootPage = '/' + rootPage;
  660. }
  661. console.log('准备跳转到:', rootPage);
  662. wx.reLaunch({
  663. url: rootPage,
  664. success: () => {
  665. console.log('✅ reLaunch 成功');
  666. console.log('===========================================');
  667. },
  668. fail: (err) => {
  669. console.error('❌ reLaunch 失败:', err);
  670. console.log('⚠️ 尝试使用 switchTab');
  671. // 如果是 tabBar 页面,尝试使用 switchTab
  672. wx.switchTab({
  673. url: rootPage,
  674. success: () => {
  675. console.log('✅ switchTab 成功');
  676. console.log('===========================================');
  677. },
  678. fail: (err2) => {
  679. console.error('❌ switchTab 也失败:', err2);
  680. console.log('⚠️ 最后尝试:直接 navigateBack');
  681. // 最后的兜底:直接返回
  682. wx.navigateBack({
  683. delta: 1,
  684. fail: (err3) => {
  685. console.error('❌ 所有跳转方式都失败了:', err3);
  686. console.log('===========================================');
  687. // 显示错误提示
  688. wx.showModal({
  689. title: '提示',
  690. content: '页面跳转失败,请手动返回',
  691. showCancel: false
  692. });
  693. }
  694. });
  695. }
  696. });
  697. }
  698. });
  699. },
  700. goBack: function () {
  701. wx.navigateBack({
  702. delta: 1,
  703. })
  704. },
  705. //手动获取微信头像
  706. onChooseAvatar(e) {
  707. console.log('=== onChooseAvatar 触发 ===');
  708. console.log('事件对象:', e);
  709. const { avatarUrl } = e.detail;
  710. console.log('获取到的头像URL:', avatarUrl);
  711. console.log('头像URL类型:', typeof avatarUrl);
  712. console.log('头像URL长度:', avatarUrl ? avatarUrl.length : 0);
  713. if (avatarUrl) {
  714. // 微信头像URL可能是临时路径,需要先下载
  715. // 临时路径格式:http://tmp/xxx.jpg
  716. // 相机拍照路径格式:wxfile://tmp_xxx.jpg
  717. this.setData({
  718. avatarUrl: avatarUrl,
  719. avatarKey: Date.now() // 更新key强制刷新
  720. }, () => {
  721. console.log('✅ 头像URL已更新到data:', this.data.avatarUrl);
  722. console.log('✅ avatarKey已更新:', this.data.avatarKey);
  723. });
  724. } else {
  725. console.error('❌ 未获取到头像URL');
  726. // 在开发环境下,如果获取失败,提示用户可以跳过
  727. wx.showToast({
  728. title: '开发工具不支持,请真机测试或跳过',
  729. icon: 'none',
  730. duration: 2000
  731. });
  732. }
  733. },
  734. //获取昵称
  735. onChangeName(e) {
  736. console.log('=== 昵称输入事件 ===');
  737. console.log('事件对象:', e);
  738. // 注意:type="nickname" 的 input 会在用户输入时自动更新 model:value
  739. // 这里只是记录日志,实际的值更新由 model:value 自动处理
  740. if (e.detail && e.detail.value !== undefined) {
  741. console.log('昵称值:', e.detail.value);
  742. }
  743. },
  744. //确定头像昵称
  745. async onComplete() {
  746. console.log('=== onComplete 方法被调用 ===');
  747. let {
  748. nickname,
  749. avatarUrl
  750. } = this.data
  751. console.log('昵称:', nickname);
  752. console.log('头像URL:', avatarUrl);
  753. let user = Parse.User.current();
  754. if (!user) {
  755. console.error('❌ 用户未登录,无法保存信息');
  756. wx.showToast({
  757. title: '用户未登录',
  758. icon: 'none'
  759. });
  760. return;
  761. }
  762. console.log('当前用户ID:', user.id);
  763. // 显示加载提示
  764. wx.showLoading({
  765. title: '保存中...',
  766. mask: true
  767. });
  768. try {
  769. // 如果用户填写了信息,则更新
  770. if (nickname || avatarUrl) {
  771. console.log('ℹ️ 用户填写了信息,开始更新');
  772. // 如果有头像,上传头像
  773. if (avatarUrl) {
  774. console.log('📤 开始上传头像...');
  775. let avatar = await this.updataAvatar(avatarUrl);
  776. if (avatar) {
  777. user.set("avatar", avatar);
  778. console.log('✅ 头像上传成功:', avatar);
  779. } else {
  780. console.warn('⚠️ 头像上传失败');
  781. }
  782. }
  783. // 如果有昵称,更新昵称
  784. if (nickname) {
  785. user.set("nickname", nickname);
  786. console.log('✅ 昵称已设置:', nickname);
  787. }
  788. await user.save();
  789. console.log('✅ 用户信息保存成功');
  790. } else {
  791. // 用户没有填写任何信息,使用默认值
  792. console.log('ℹ️ 用户跳过头像昵称设置,使用默认值');
  793. // 如果用户没有昵称,设置默认昵称
  794. if (!user.get('nickname') || user.get('nickname') === '微信用户') {
  795. const defaultNickname = '用户' + user.id.substring(0, 6);
  796. user.set('nickname', defaultNickname);
  797. await user.save();
  798. console.log('✅ 已设置默认昵称:', defaultNickname);
  799. }
  800. }
  801. wx.hideLoading();
  802. // 关闭弹窗
  803. this.onClose();
  804. // 延迟一下再跳转,确保弹窗关闭动画完成
  805. setTimeout(() => {
  806. console.log('🚀 准备跳转...');
  807. this.backLoad();
  808. }, 300);
  809. } catch (err) {
  810. wx.hideLoading();
  811. console.error('❌ 保存用户信息失败:', err);
  812. wx.showModal({
  813. title: '提示',
  814. content: '保存失败,是否继续?',
  815. showCancel: true,
  816. cancelText: '重试',
  817. confirmText: '继续',
  818. success: (result) => {
  819. if (result.confirm) {
  820. // 用户选择继续,直接跳转
  821. this.onClose();
  822. setTimeout(() => {
  823. this.backLoad();
  824. }, 300);
  825. }
  826. // 用户选择重试,留在当前页面
  827. }
  828. });
  829. }
  830. },
  831. //跳过头像昵称设置
  832. async onSkip() {
  833. console.log('=== onSkip 方法被调用 ===');
  834. let user = Parse.User.current();
  835. if (!user) {
  836. console.error('❌ 用户未登录');
  837. wx.showToast({
  838. title: '用户未登录',
  839. icon: 'none'
  840. });
  841. return;
  842. }
  843. console.log('当前用户ID:', user.id);
  844. wx.showLoading({
  845. title: '处理中...',
  846. mask: true
  847. });
  848. try {
  849. // 设置默认昵称(如果没有)
  850. if (!user.get('nickname') || user.get('nickname') === '微信用户') {
  851. const defaultNickname = '用户' + user.id.substring(0, 6);
  852. user.set('nickname', defaultNickname);
  853. await user.save();
  854. console.log('✅ 已设置默认昵称:', defaultNickname);
  855. } else {
  856. console.log('ℹ️ 用户已有昵称:', user.get('nickname'));
  857. }
  858. wx.hideLoading();
  859. // 关闭弹窗
  860. this.onClose();
  861. // 延迟一下再跳转
  862. setTimeout(() => {
  863. console.log('🚀 准备跳转...');
  864. this.backLoad();
  865. }, 300);
  866. } catch (err) {
  867. wx.hideLoading();
  868. console.error('❌ 设置默认昵称失败:', err);
  869. // 即使失败也允许继续
  870. this.onClose();
  871. setTimeout(() => {
  872. this.backLoad();
  873. }, 300);
  874. }
  875. },
  876. //关闭头像昵称填写弹窗
  877. onClose() {
  878. this.setData({
  879. wxModel: false,
  880. isProcessingAuth: false // 重置标记
  881. })
  882. },
  883. //上传头像
  884. updataAvatar(url) {
  885. let that = this;
  886. return new Promise((resolve, rejcet) => {
  887. qiniuUploader.upload(
  888. url,
  889. async (res) => {
  890. let img = res.imageURL;
  891. resolve(img)
  892. },
  893. (error) => {
  894. console.log("error: " + error);
  895. resolve(false)
  896. }, {
  897. region: "SCN",
  898. uploadURL: that.data.uploadURL,
  899. domain: that.data.domain,
  900. uptoken: that.data.uptokenURL,
  901. }
  902. );
  903. })
  904. },
  905. //选择勾选用户协议
  906. onCheckAgreement() {
  907. this.setData({
  908. check: !this.data.check
  909. })
  910. },
  911. //附件下载
  912. openFile() {
  913. let {
  914. agreement
  915. } = this.data
  916. let url = agreement.content,
  917. name = agreement.title
  918. const _this = this;
  919. let rep = this.getFileType(url)
  920. console.log(url, name);
  921. wx.showLoading({
  922. title: '加载中',
  923. })
  924. wx.downloadFile({
  925. url: url, //要预览的PDF的地址
  926. filePath: wx.env.USER_DATA_PATH + `/${name}.${rep}`,
  927. success: function (res) {
  928. console.log(res);
  929. if (res.statusCode === 200) { //成功
  930. var Path = res.filePath //返回的文件临时地址,用于后面打开本地预览所用
  931. console.log(Path)
  932. wx.openDocument({
  933. filePath: Path, //要打开的文件路径
  934. showMenu: true,
  935. success: function (res) {
  936. wx.hideLoading()
  937. console.log(res, '打开PDF成功');
  938. },
  939. fail: function (res) {
  940. console.log(res)
  941. wx.hideLoading()
  942. }
  943. })
  944. }
  945. },
  946. fail: function (res) {
  947. wx.hideLoading()
  948. console.log(res); //失败
  949. },
  950. })
  951. },
  952. //解析文件类型
  953. getFileType(url) {
  954. let pdfReg = /^.+(\.pdf)$/
  955. let txtReg = /^.+(\.txt)$/
  956. let wordReg = /^.+(\.doc|\.docx)$/
  957. let excelReg = /^.+(\.xls|\.xlsx)$/
  958. let jpgPng = /^.+(\.png)$/
  959. let jpgJpg = /^.+(\.jpg)$/
  960. let jpgJpeg = /^.+(\.jpeg)$/
  961. if (pdfReg.test(url)) {
  962. return 'pdf'
  963. }
  964. if (txtReg.test(url)) {
  965. return 'txt'
  966. }
  967. if (wordReg.test(url)) {
  968. return 'docx'
  969. }
  970. if (excelReg.test(url)) {
  971. return 'xls'
  972. }
  973. if (jpgPng.test(url)) {
  974. return 'png'
  975. }
  976. if (jpgJpg.test(url)) {
  977. return 'jpg'
  978. }
  979. if (jpgJpeg.test(url)) {
  980. return 'jpeg'
  981. }
  982. },
  983. onShow: function () {
  984. console.log('=== onShow 方法被调用 ===');
  985. // 如果正在处理授权流程,不要自动返回
  986. if (this.data.isProcessingAuth) {
  987. console.log('ℹ️ 正在处理授权流程,不自动返回');
  988. return;
  989. }
  990. // 如果有弹窗显示(手机号弹窗或头像昵称弹窗),不要自动返回
  991. if (this.data.phoneModal || this.data.wxModel) {
  992. console.log('ℹ️ 有弹窗显示,不自动返回');
  993. return;
  994. }
  995. let userLogin = wx.getStorageSync('userLogin');
  996. let currentUser = Parse.User.current();
  997. console.log('userLogin 存储:', userLogin);
  998. console.log('当前用户:', currentUser ? currentUser.id : '无');
  999. console.log('手机号:', currentUser?.get('mobile') || '无');
  1000. // 检查是否是从其他页面跳转过来的(有 returnUrl 参数)
  1001. const pages = getCurrentPages();
  1002. const currentPage = pages[pages.length - 1];
  1003. const hasReturnUrl = currentPage?.options?.returnUrl;
  1004. if (hasReturnUrl) {
  1005. console.log('ℹ️ 检测到 returnUrl 参数,用户需要完成登录流程');
  1006. // 不要自动返回,让用户完成登录
  1007. return;
  1008. }
  1009. // 只有当用户已登录且有手机号,并且不是从其他页面跳转过来时才返回
  1010. if (userLogin && currentUser && currentUser.get('mobile')) {
  1011. console.log('✅ 用户已完整登录,返回上一页');
  1012. wx.navigateBack({
  1013. fail: () => {
  1014. console.log('⚠️ 返回失败,可能是首页');
  1015. }
  1016. });
  1017. } else {
  1018. console.log('ℹ️ 用户未完整登录,显示授权页面');
  1019. }
  1020. },
  1021. onReady: function () {
  1022. },
  1023. })