index.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. const dateF = require("../../../../utils/date")
  2. const Parse = getApp().Parse;
  3. const company = getApp().globalData.company;
  4. const uid = Parse.User.current()?.id
  5. const login = require("../../../../utils/login");
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. attachment: null,
  12. isAttachment: null,
  13. showpopup: false,
  14. name: null,
  15. mobile: null,
  16. sex: null,
  17. idcard: null,
  18. birthdate: null,
  19. address: null,
  20. classType: null,
  21. centerDesc: null,
  22. teacher: null,
  23. tel: null,
  24. date: '',
  25. showDate: false,
  26. storeVal: null,
  27. active: 0,
  28. comList: [],
  29. comCount: 0,
  30. comVal: null,
  31. replyObj: {
  32. placeholder: '你猜我的评论区在等谁?'
  33. },
  34. autosize: {
  35. maxHeight: 100,
  36. minHeight: 50
  37. },
  38. idcard: null,
  39. teacher: null,
  40. teacherAdd: null,
  41. remark: null,
  42. optionKeyList: [],
  43. isAttend:false,//当前活动是否可参与(报名时间结束即可参与运动)
  44. },
  45. /**
  46. * 生命周期函数--监听页面加载
  47. */
  48. onLoad: function (options) {
  49. let userInfo = wx.getStorageSync("userLogin");
  50. console.log(userInfo)
  51. let {
  52. id
  53. } = options
  54. this.setData({
  55. aid: id,
  56. userInfo
  57. })
  58. this.refersh()
  59. },
  60. login() {
  61. let userInfo = wx.getStorageSync("userLogin");
  62. if (!userInfo || userInfo == '') {
  63. login.loginNow()
  64. } else {
  65. this.setData({
  66. userInfo
  67. })
  68. this.refersh()
  69. }
  70. },
  71. async refersh() {
  72. await this.getActivity()
  73. this.getActStatus()
  74. this.getActRegister()
  75. this.getArticle()
  76. },
  77. /**获取当前活动 */
  78. async getActivity() {
  79. let {
  80. aid
  81. } = this.data
  82. let query = new Parse.Query('Activity')
  83. let data = await query.get(aid)
  84. let activity = data.toJSON()
  85. console.log(activity)
  86. let now = new Date()
  87. if(activity.signFromTo?.to?.iso){//当前时间大于可签到结束时间-可参与
  88. let signTo = new Date(activity.signFromTo?.to?.iso)
  89. if(now > signTo){
  90. this.setData({isAttend:true})
  91. }
  92. }
  93. if(activity.endDate){//当前时间大于活动结束时间-不可参与
  94. let end = new Date(activity.endDate)
  95. if(now > end){
  96. this.setData({isAttend:false})
  97. }
  98. }
  99. let optionKeyList = Object.keys(activity.limitOptions || {}) || []
  100. activity.start = dateF.formatTime("YYYY.mm.dd HH:MM", activity.startDate?.iso)
  101. activity.end = dateF.formatTime("mm.dd HH:MM", activity.endDate?.iso)
  102. activity.startDate = new Date(activity.startDate?.iso)
  103. activity.endDate = new Date(activity.endDate?.iso)
  104. if (activity.signFromTo?.from && activity.signFromTo?.to) {
  105. activity.signFromTo.fromDate = new Date(activity.signFromTo?.from?.iso) || null
  106. activity.signFromTo.toDate = new Date(activity.signFromTo?.to?.iso) || null
  107. activity.signFromTo.from = dateF.formatTime("YYYY.mm.dd HH:MM", activity.signFromTo?.from?.iso)
  108. activity.signFromTo.to = dateF.formatTime("mm.dd HH:MM", activity.signFromTo?.to?.iso)
  109. }
  110. this.setData({
  111. activity,
  112. optionKeyList,
  113. })
  114. },
  115. /**预览图片 */
  116. imagePreview(e) {
  117. let {
  118. activity
  119. } = this.data
  120. let {
  121. img
  122. } = e.currentTarget.dataset
  123. wx.previewImage({
  124. current: img, // 当前显示图片的http链接
  125. urls: activity.cover // 需要预览的图片http链接列表
  126. })
  127. },
  128. /**切换 活动详情/排行榜 */
  129. async changeTab(e) {
  130. if (e.detail.name == 1 && !this.data?.comList?.length) {
  131. // this.getHomeCom()
  132. }
  133. this.setData({
  134. active: e.detail.name
  135. })
  136. },
  137. /**获取报名记录 */
  138. async getActRegister() {
  139. let {
  140. activity
  141. } = this.data
  142. let query = new Parse.Query('ActivityRegister')
  143. query.notEqualTo('isDeleted', true)
  144. query.equalTo('activity', activity.objectId)
  145. query.equalTo('isPay', true)
  146. query.equalTo('isChecked', true)
  147. query.equalTo('user', uid)
  148. query.descending('createdAt') //大到小
  149. query.include('shopStore')
  150. let req = await query.find()
  151. let registerList = []
  152. if (req?.length > 0) {
  153. registerList = req.map(item => {
  154. let results = item?.toJSON()
  155. results.createdAt = dateF.formatTime("YYYY.mm.dd HH:MM", item.get('createdAt'))
  156. results.booking = dateF.formatTime("YYYY.mm.dd HH:MM", item.get('startDate'))
  157. results.bookObj = {}
  158. results.bookObj.from = dateF.formatTime("YYYY.mm.dd HH:MM", item.get('booking')?.from)
  159. results.bookObj.to = dateF.formatTime("mm.dd HH:MM", item.get('booking')?.to)
  160. results.shop = results?.shopStore
  161. return results
  162. })
  163. }
  164. this.setData({
  165. registerList,
  166. registerPaseList: req
  167. })
  168. },
  169. /**查看报名记录 */
  170. lookLog(e) {
  171. this.setData({
  172. checkDate: null
  173. })
  174. let {
  175. registerList,
  176. registerPaseList
  177. } = this.data
  178. let {
  179. index
  180. } = e.currentTarget.dataset
  181. console.log(registerList[index])
  182. this.setData({
  183. bookObj: registerList[index].bookObj,
  184. shop: registerList[index].shop,
  185. aRegister: registerPaseList[index],
  186. })
  187. this.onOpen()
  188. },
  189. /**判断用户参与活动状态 */
  190. async getActStatus() {
  191. let {
  192. activity
  193. } = this.data
  194. let query = new Parse.Query('ActivityRegister')
  195. query.notEqualTo('isDeleted', true)
  196. query.equalTo('activity', activity.objectId)
  197. query.equalTo('isPay', true)
  198. query.equalTo('isChecked', true)
  199. let count = await query.count()
  200. let tips = {
  201. title: '可报名',
  202. isCanSign: true,
  203. isEdit: true, //是否可编辑
  204. }
  205. let now = new Date()
  206. this.setData({
  207. tips,
  208. count,
  209. toYear: now.getFullYear(),
  210. toMonth: now.getMonth() + 1,
  211. toDate: now.getDate()
  212. })
  213. if (now > activity.endDate) {
  214. tips = {
  215. title: '活动已结束',
  216. isCanSign: false,
  217. isEdit: false,
  218. }
  219. this.setData({
  220. tips
  221. })
  222. return
  223. }
  224. if (!activity.isEnabled) {
  225. tips = {
  226. title: '活动未开启',
  227. isCanSign: false,
  228. isEdit: false,
  229. }
  230. this.setData({
  231. tips
  232. })
  233. return
  234. }
  235. if (activity.signFromTo?.fromDate && now < activity.signFromTo.fromDate) {
  236. tips = {
  237. title: '未到报名时间',
  238. isCanSign: false,
  239. isEdit: false,
  240. }
  241. this.setData({
  242. tips
  243. })
  244. return
  245. }
  246. if (activity.signFromTo?.toDate && now >= activity.signFromTo.toDate) {
  247. tips = {
  248. title: '报名时间已过',
  249. isCanSign: false,
  250. isEdit: false,
  251. }
  252. this.setData({
  253. tips
  254. })
  255. return
  256. }
  257. query.equalTo('user', uid)
  258. query.descending('createdAt') //大到小
  259. query.include('shopStore')
  260. let userCount = await query.count()
  261. if (userCount >= activity?.everyCount) {
  262. tips = {
  263. title: '已报名本活动',
  264. isCanSign: false,
  265. isCanRevise: false,
  266. // isEdit: false,
  267. isEdit: true,
  268. }
  269. this.setData({
  270. tips
  271. })
  272. return
  273. }
  274. let aRegister = await query.first()
  275. let aregisterJson = aRegister?.toJSON()
  276. if (aRegister?.id) {
  277. tips = {
  278. title: '已报名本活动',
  279. isCanSign: true,
  280. isCanRevise: true,
  281. // isEdit: false,
  282. isEdit: true,
  283. }
  284. let booking = dateF.formatTime("YYYY.mm.dd HH:MM", aRegister.get('startDate'))
  285. let bookObj = {}
  286. bookObj.from = dateF.formatTime("YYYY.mm.dd HH:MM", aRegister.get('booking')?.from)
  287. bookObj.to = dateF.formatTime("mm.dd HH:MM", aRegister.get('booking')?.to)
  288. this.setData({
  289. tips,
  290. booking,
  291. bookObj,
  292. aRegister,
  293. shop: aregisterJson.shopStore
  294. })
  295. return
  296. }
  297. if (activity.merber >= 0 && count >= activity.merber) {
  298. tips = {
  299. title: '活动人数已满',
  300. isCanSign: false,
  301. isEdit: false,
  302. }
  303. this.setData({
  304. tips
  305. })
  306. return
  307. }
  308. },
  309. /**前往报名 */
  310. application() {
  311. this.setData({
  312. aRegister: null,
  313. bookObj: null,
  314. shop: null
  315. })
  316. this.onOpen()
  317. },
  318. onOpen() {
  319. let {
  320. activity
  321. } = this.data
  322. let minDate = Infinity
  323. let maxDate = 0
  324. let checkDateList = []
  325. if (Array.isArray(activity.bookingList) && activity.bookingList.length > 0) {
  326. activity.bookingList.forEach(item => {
  327. let from = new Date(item.from.iso)
  328. let to = new Date(item.to.iso)
  329. if (minDate > from) minDate = from.getTime()
  330. if (maxDate < to) maxDate = to.getTime()
  331. let fromMonth = from.getMonth()
  332. let fromDate = from.getDate()
  333. checkDateList.push({
  334. fromDate: dateF.formatTime("YYYY-mm-dd HH:MM", from),
  335. toDate: dateF.formatTime("YYYY-mm-dd HH:MM", to),
  336. month: fromMonth,
  337. date: fromDate,
  338. from: dateF.formatTime("HH:MM", from),
  339. to: dateF.formatTime("mm月dd日 HH:MM", to),
  340. })
  341. });
  342. }
  343. let formatter = (day) => {
  344. let month = day.date.getMonth()
  345. let date = day.date.getDate()
  346. checkDateList.forEach(item => {
  347. if (item.month == month && item.date == date) day.bottomInfo = '可预约'
  348. });
  349. return day
  350. }
  351. this.setData({
  352. minDate,
  353. maxDate,
  354. formatter,
  355. checkDateList,
  356. showpopup: true
  357. })
  358. let now = new Date()
  359. let tradeNo = "C" +
  360. String(now.getFullYear()) +
  361. (now.getMonth() + 1) +
  362. now.getDate() +
  363. now.getHours() +
  364. now.getMinutes() +
  365. now.getSeconds() +
  366. Math.random().toString().slice(-6); //生成六位随机数
  367. this.setData({
  368. tradeNo
  369. })
  370. this.getUserProfile()
  371. },
  372. onClose() {
  373. this.setData({
  374. showpopup: false
  375. })
  376. },
  377. /**打开选择位置 */
  378. openStore() {
  379. this.getShop()
  380. this.setData({
  381. showStorePopup: true
  382. })
  383. },
  384. /**关闭位置选择 */
  385. closeStore() {
  386. this.setData({
  387. showStorePopup: false
  388. })
  389. },
  390. /**获取可选位置 */
  391. async getShop() {
  392. let {
  393. activity
  394. } = this.data
  395. let shopList = this.data.shopList || []
  396. if (shopList?.length > 0) return
  397. for (let i in activity?.shopStore) {
  398. let query = new Parse.Query('ShopStore')
  399. query.equalTo('company', company)
  400. query.notEqualTo('isDeleted', true)
  401. query.equalTo('isVerified', true)
  402. query.equalTo('objectId', activity?.shopStore[i].objectId)
  403. let req = await query.first()
  404. shopList.push(req.toJSON())
  405. }
  406. this.setData({
  407. shopList
  408. })
  409. },
  410. /**选择位置 */
  411. chooseShop(e) {
  412. let {
  413. shopList,tips
  414. } = this.data
  415. if(!tips.isEdit){
  416. wx.showToast({
  417. title:`${tips.title},无法修改`,
  418. icon:'none'
  419. })
  420. this.setData({
  421. showStorePopup: false
  422. })
  423. return
  424. }
  425. let {
  426. index
  427. } = e.currentTarget.dataset
  428. this.setData({
  429. shop: shopList[index],
  430. showStorePopup: false
  431. })
  432. },
  433. /**获取当前用户和身份 */
  434. async getUserProfile() {
  435. let profile
  436. let query = new Parse.Query('Profile')
  437. query.notEqualTo("isDeleted", true)
  438. query.equalTo('user', uid)
  439. profile = await query.first()
  440. if (!profile?.id) {
  441. let Pro = Parse.Object.extend('Profile')
  442. profile = new Pro()
  443. } else {
  444. this.setData({
  445. name: profile.get('name') || '',
  446. college: profile.get('college') || '',
  447. mobile: profile.get('mobile') || '',
  448. sex: profile.get('sex') || '',
  449. idcard: profile.get('idcard') || '',
  450. birthdate: profile.get('birthdate') || '',
  451. address: profile.get('address') || '',
  452. classType: profile.get('classType') || '',
  453. centerDesc: profile.get('centerDesc') || '',
  454. teacher: profile.get('teacher') || '',
  455. tel: profile.get('tel') || '',
  456. })
  457. }
  458. this.setData({
  459. profile
  460. })
  461. },
  462. onChangeSexIsAttachment(event) {
  463. this.setData({
  464. isAttachment: event.detail == '同意',
  465. });
  466. },
  467. onChangeSex(event) {
  468. this.setData({
  469. sex: event.detail,
  470. });
  471. },
  472. onChangecenterDesc(event) {
  473. this.setData({
  474. centerDesc: event.detail,
  475. });
  476. },
  477. /** 立即预约*/
  478. async enlists(e) {
  479. let date
  480. let {
  481. name,
  482. isAttachment,
  483. mobile,
  484. sex,
  485. idcard,
  486. birthdate,
  487. address,
  488. classType,
  489. centerDesc,
  490. teacher,
  491. tel,
  492. aRegister
  493. } = this.data
  494. // if (!name || !college || !mobile) {
  495. console.log(name, isAttachment,
  496. mobile,
  497. sex,
  498. idcard,
  499. birthdate,
  500. address,
  501. classType,
  502. centerDesc,
  503. teacher,
  504. tel, )
  505. if (!name ||
  506. !sex || !idcard ||
  507. !birthdate || !address || !classType || !centerDesc || !teacher
  508. ) {
  509. wx.showToast({
  510. title: '存在未填项',
  511. icon: 'none',
  512. duration: 5000
  513. })
  514. return
  515. }
  516. if (mobile.length != 11 || tel.length != 11) {
  517. wx.showToast({
  518. title: '请输入11位手机号',
  519. icon: 'none',
  520. duration: 5000
  521. })
  522. return
  523. }
  524. let {
  525. shop
  526. } = this.data
  527. if (!shop?.objectId) {
  528. wx.showToast({
  529. title: '请选择位置',
  530. icon: 'none',
  531. duration: 5000
  532. })
  533. return
  534. }
  535. if (!isAttachment) {
  536. wx.showToast({
  537. title: '请阅读并同意《参赛选手免责声明》',
  538. icon: 'none',
  539. duration: 5000
  540. })
  541. return
  542. }
  543. let {
  544. profile,
  545. activity
  546. } = this.data
  547. profile.set('name', name || '')
  548. profile.set('mobile', mobile || '')
  549. profile.set('sex', sex || '')
  550. profile.set('idcard', idcard || '')
  551. profile.set('birthdate', birthdate || '')
  552. profile.set('address', address || '')
  553. profile.set('classType', classType || '')
  554. profile.set('centerDesc', centerDesc || '')
  555. profile.set('teacher', teacher || '')
  556. profile.set('tel', tel || '')
  557. profile.set('company', {
  558. className: 'Company',
  559. __type: 'Pointer',
  560. objectId: company
  561. })
  562. profile.set('user', {
  563. className: '_User',
  564. __type: 'Pointer',
  565. objectId: uid
  566. })
  567. let proSave = await profile.save()
  568. if (aRegister?.id) {
  569. this.successPay()
  570. return
  571. }
  572. let {
  573. from,
  574. to
  575. } = e.currentTarget.dataset
  576. let booking = {}
  577. if (from && to) {
  578. booking = {
  579. from: new Date(from),
  580. to: new Date(to),
  581. }
  582. }
  583. this.setData({
  584. showPay: true,
  585. profileId: proSave?.id,
  586. date,
  587. booking
  588. })
  589. if (activity.price <= 0) {
  590. this.successPay()
  591. }
  592. },
  593. /**支付回调 */
  594. async acceptResult(e) {
  595. let {
  596. params,
  597. no,
  598. type
  599. } = e.detail;
  600. let {
  601. tradeNo,
  602. activity,
  603. aRegister
  604. } = this.data
  605. if (aRegister?.id) {
  606. this.successPay()
  607. return
  608. }
  609. try {
  610. if (params == 'ok') {
  611. await this.successPay()
  612. this.setData({
  613. showpopup: false
  614. })
  615. } else {
  616. wx.showToast({
  617. title: "取消支付",
  618. icon: "error",
  619. duration: 1500,
  620. });
  621. }
  622. } catch (error) {
  623. wx.showToast({
  624. title: "支付失败",
  625. icon: "error",
  626. duration: 1500,
  627. });
  628. wx.hideLoading()
  629. }
  630. },
  631. async successPay() {
  632. let {
  633. profileId,
  634. activity,
  635. tradeNo,
  636. booking,
  637. shop,
  638. aRegister
  639. } = this.data
  640. wx.showLoading({
  641. title: "处理中",
  642. mask: true
  643. });
  644. /**当前操作是否编辑 新增报名记录/编辑报名记录 */
  645. let isEdit = true
  646. if (!aRegister?.id) {
  647. isEdit = false
  648. let query = new Parse.Query('ActivityRegister')
  649. query.notEqualTo('isDeleted', true)
  650. query.equalTo('activity', activity.objectId)
  651. query.notEqualTo('isPay', true)
  652. query.equalTo('user', uid)
  653. query.notEqualTo('isChecked', true)
  654. aRegister = await query.first()
  655. if (!aRegister?.id) {
  656. let ActivityRegister = Parse.Object.extend('ActivityRegister')
  657. aRegister = new ActivityRegister()
  658. aRegister.set('orderNum', tradeNo)
  659. aRegister.set('company', {
  660. className: 'Company',
  661. __type: 'Pointer',
  662. objectId: company
  663. })
  664. aRegister.set('user', {
  665. className: '_User',
  666. __type: 'Pointer',
  667. objectId: uid
  668. })
  669. aRegister.set('activity', {
  670. className: 'Activity',
  671. __type: 'Pointer',
  672. objectId: activity.objectId
  673. })
  674. aRegister.set('isPayTpye', 'wx')
  675. aRegister.set('type', 'daily')
  676. aRegister.set('price', activity.price)
  677. aRegister.set('isPay', true)
  678. aRegister.set('profile', {
  679. className: 'Profile',
  680. __type: 'Pointer',
  681. objectId: profileId
  682. })
  683. aRegister.set('isChecked', true)
  684. }
  685. }
  686. if (shop.objectId) {
  687. aRegister.set('shopStore', {
  688. className: 'ShopStore',
  689. __type: 'Pointer',
  690. objectId: shop.objectId
  691. })
  692. }
  693. if (booking?.from && booking?.to) {
  694. aRegister.set('startDate', booking.from)
  695. aRegister.set('booking', booking)
  696. }
  697. await aRegister.save()
  698. await this.getActStatus()
  699. await this.getActRegister()
  700. this.setData({
  701. checkDate: null
  702. })
  703. wx.hideLoading()
  704. wx.showToast({
  705. title: "报名成功",
  706. icon: "success",
  707. duration: 1000,
  708. });
  709. this.onClose()
  710. // if (!isEdit) {
  711. // setTimeout(() => {
  712. // wx.navigateTo({
  713. // url: activity.url,
  714. // })
  715. // }, 1000);
  716. // }
  717. },
  718. /**打开时间选择 */
  719. onDisplay() {
  720. this.setData({
  721. showDate: true
  722. });
  723. },
  724. /**关闭时间选择框 */
  725. onCloseDate() {
  726. this.setData({
  727. showDate: false
  728. });
  729. },
  730. /** 日历选中*/
  731. onConfirm(event) {
  732. let {
  733. checkDateList
  734. } = this.data
  735. let checkDate = event.detail
  736. let checkTimeList = checkDateList.filter(item => item.month == checkDate.getMonth() && item.date == checkDate.getDate())
  737. this.setData({
  738. checkTimeList,
  739. checkDate: dateF.formatTime("YYYY年mm月dd日", checkDate),
  740. showDate: false,
  741. });
  742. },
  743. /**参与活动 */
  744. involved(){
  745. },
  746. async goUrl() {
  747. let {isAttachment,aRegister}=this.data
  748. let actQuery = new Parse.Query('ActivityData')
  749. actQuery.equalTo('company', company)
  750. actQuery.equalTo('user', uid)
  751. actQuery.notEqualTo('isDeleted', true)
  752. actQuery.equalTo('actRegister', aRegister?.id)
  753. let actDate = await actQuery.first()
  754. if(actDate?.get('status')=='end'){
  755. wx.showToast({
  756. title: '成绩已结算,查看其他预约或参与其他活动吧',
  757. icon:'none'
  758. })
  759. return
  760. }
  761. if (!isAttachment) {
  762. wx.showToast({
  763. title: '请阅读并同意《参赛选手免责声明》',
  764. icon: 'none',
  765. duration: 5000
  766. })
  767. return
  768. }
  769. let url = `${aRegister?.get('activity')?.get('url')}&a_reg=${aRegister?.id}`
  770. wx.navigateTo({
  771. url: url,
  772. })
  773. },
  774. /**隐私协议 */
  775. async getArticle() {
  776. let query = new Parse.Query("Article")
  777. query.equalTo("company", company)
  778. query.equalTo("type", 'activity')
  779. query.equalTo("isEnabled", true)
  780. query.select("title", "attachment")
  781. let res = await query.first()
  782. if (res && res.id) {
  783. let r = res.toJSON()
  784. if (r.attachment && r.attachment.length > 0) {
  785. this.setData({
  786. attachment: r
  787. })
  788. }
  789. }
  790. },
  791. //附件下载
  792. async openFile() {
  793. await this.getArticle()
  794. let {
  795. attachment
  796. } = this.data
  797. let url = attachment.attachment[0].url,
  798. name = attachment.title
  799. const _this = this
  800. let rep = this.getFileType(url)
  801. wx.showLoading({
  802. title: '加载中',
  803. })
  804. wx.downloadFile({
  805. url: url, //要预览的PDF的地址
  806. filePath: wx.env.USER_DATA_PATH + `/${name}.${rep}`,
  807. success: function (res) {
  808. if (res.statusCode === 200) { //成功
  809. var Path = res.filePath //返回的文件临时地址,用于后面打开本地预览所用
  810. wx.openDocument({
  811. filePath: Path, //要打开的文件路径
  812. showMenu: true,
  813. success: function (res) {
  814. wx.hideLoading()
  815. },
  816. fail: function (res) {
  817. wx.hideLoading()
  818. }
  819. })
  820. }
  821. },
  822. fail: function (res) {
  823. wx.hideLoading()
  824. },
  825. })
  826. },
  827. //解析文件类型
  828. getFileType(url) {
  829. let pdfReg = /^.+(\.pdf)$/
  830. let txtReg = /^.+(\.txt)$/
  831. let wordReg = /^.+(\.doc|\.docx)$/
  832. let excelReg = /^.+(\.xls|\.xlsx)$/
  833. let jpgPng = /^.+(\.png)$/
  834. let jpgJpg = /^.+(\.jpg)$/
  835. let jpgJpeg = /^.+(\.jpeg)$/
  836. if (pdfReg.test(url)) {
  837. return 'pdf'
  838. }
  839. if (txtReg.test(url)) {
  840. return 'txt'
  841. }
  842. if (wordReg.test(url)) {
  843. return 'doc'
  844. }
  845. if (excelReg.test(url)) {
  846. return 'xls'
  847. }
  848. if (jpgPng.test(url)) {
  849. return 'png'
  850. }
  851. if (jpgJpg.test(url)) {
  852. return 'jpg'
  853. }
  854. if (jpgJpeg.test(url)) {
  855. return 'jpeg'
  856. }
  857. },
  858. /**
  859. * 生命周期函数--监听页面初次渲染完成
  860. */
  861. onReady: function () {
  862. },
  863. /**
  864. * 生命周期函数--监听页面显示
  865. */
  866. onShow: function () {
  867. let userInfo = wx.getStorageSync("userLogin");
  868. this.setData({
  869. userInfo
  870. })
  871. },
  872. /**
  873. * 生命周期函数--监听页面隐藏
  874. */
  875. onHide: function () {
  876. },
  877. /**
  878. * 生命周期函数--监听页面卸载
  879. */
  880. onUnload: function () {
  881. },
  882. /**
  883. * 页面相关事件处理函数--监听用户下拉动作
  884. */
  885. onPullDownRefresh: function () {
  886. },
  887. /**
  888. * 页面上拉触底事件的处理函数
  889. */
  890. onReachBottom: function () {
  891. },
  892. /**
  893. * 用户点击右上角分享
  894. */
  895. onShareAppMessage: function () {
  896. },
  897. })