index.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. // nova-werun/components/circle-card/index.js
  2. const Parse = getApp().Parse;
  3. const company = getApp().globalData.company;
  4. Component({
  5. /**
  6. * 组件的属性列表
  7. */
  8. properties: {
  9. objectId: '',
  10. type: '',
  11. permission: ''
  12. },
  13. /**
  14. * 组件的初始数据
  15. */
  16. data: {
  17. //图片
  18. images: [],
  19. imageclass: '',
  20. //是否展示点赞评论按钮
  21. isgood: false,
  22. isclick: false,
  23. //朋友圈
  24. cicleList: [],
  25. time: '',
  26. //点赞人
  27. chickList: '',
  28. show: false,
  29. //
  30. inputValue: '', // 用于存储输入的内容
  31. textareaHeight: 52, // 初始高度,单位为 rpx
  32. bottomNavHeight: 0,
  33. //
  34. commentList: [],
  35. //
  36. poptype: '',
  37. userobject: '',
  38. //评论
  39. commenttext: "评论",
  40. focusedCommentId: null,
  41. isCommentFocused: false,
  42. noCommentCount: 0,
  43. //
  44. showconfirm: false,
  45. //
  46. maxLength: 110, // 设置最大长度
  47. showFull: false, // 控制是否显示完整内容
  48. //
  49. imgList:[],
  50. currentImg:'',
  51. previewHideStatus:false
  52. },
  53. lifetimes: {
  54. detached: function () {
  55. // 在组件实例被从页面节点树移除时执行
  56. },
  57. attached: async function () {
  58. // 在组件实例进入页面节点树时执行
  59. this.getcircle()
  60. this.showischick()
  61. this.getbottomheight()
  62. this.getComment()
  63. },
  64. },
  65. /**
  66. * 组件的方法列表
  67. */
  68. methods: {
  69. onImageLoad: function (e) {
  70. const {
  71. width,
  72. height
  73. } = e.detail; // 获取图片的宽高
  74. const imageClass = width > height ? 'image-landscape' : 'image-portrait'; // 判断横竖屏
  75. this.setData({
  76. imageclass: imageClass // 动态设置图片的类名
  77. });
  78. },
  79. previewImage: function (e) {
  80. const index = e.currentTarget.dataset.index; // 获取当前点击图片的索引
  81. const images = this.data.images; // 获取所有图片的链接
  82. wx.previewImage({
  83. current: images[index], // 当前显示图片的链接
  84. urls: images // 需要预览的图片链接列表
  85. });
  86. },
  87. gourl(e) {
  88. const url = e.currentTarget.dataset.url
  89. const objectId = e.currentTarget.dataset.id
  90. wx.navigateTo({
  91. url: `${url}?id=` + objectId // 目标页面的路径
  92. });
  93. },
  94. showgood() {
  95. this.setData({
  96. isgood: !this.data.isgood
  97. })
  98. },
  99. isclick() {
  100. this.chickin()
  101. setTimeout(() => {
  102. this.showchick()
  103. this.showgood()
  104. }, 400)
  105. },
  106. async getcircle() {
  107. let AIMomentquery = new Parse.Query('AIMoment');
  108. AIMomentquery.equalTo('company', company);
  109. AIMomentquery.equalTo('objectId', this.data.objectId);
  110. AIMomentquery.equalTo('isVisible', true);
  111. AIMomentquery.include('profile.user');
  112. AIMomentquery.include('profile');
  113. AIMomentquery.include('user');
  114. AIMomentquery.notEqualTo('isDeleted', true)
  115. let P = await AIMomentquery.find();
  116. let AIMoment1List = P.map(item => item.toJSON());
  117. this.setData({
  118. cicleList: AIMoment1List,
  119. })
  120. console.log('cicleList',this.data.cicleList);
  121. this.setData({
  122. images: this.data.cicleList[0].images
  123. })
  124. // 将 ISO 字符串转换为时间戳并传递给 formatTime
  125. const createdAt = new Date(this.data.cicleList[0].createdAt).getTime();
  126. const time = this.formatTime(createdAt);
  127. this.setData({
  128. time
  129. })
  130. console.log('时间', time);
  131. this.showchick()
  132. },
  133. formatTime(timestamp) {
  134. const now = Date.now();
  135. const diff = now - timestamp;
  136. if (diff < 60000) { // 小于1分钟
  137. return '刚刚';
  138. } else if (diff < 3600000) { // 小于1小时
  139. return Math.floor(diff / 60000) + '分钟前';
  140. } else if (diff < 86400000) { // 小于24小时
  141. return Math.floor(diff / 3600000) + '小时前';
  142. } else if (diff < 172800000) { // 小于48小时
  143. return '昨天';
  144. } else {
  145. const date = new Date(timestamp);
  146. return this.formatDate2(date); // 显示具体日期
  147. }
  148. },
  149. formatDate2(date1) {
  150. date1 = new Date(date1);
  151. // 获取年份、月份和日期
  152. const year1 = date1.getFullYear();
  153. const month1 = date1.getMonth() + 1; // 月份从0开始,所以要加1
  154. const day1 = date1.getDate();
  155. // 返回格式化的字符串
  156. return `${year1}/${month1}/${day1}`;
  157. },
  158. //点击点赞按钮
  159. async chickin() {
  160. this.setData({
  161. isclick: !this.data.isclick
  162. })
  163. let AIMomentquery = new Parse.Query('AIMoment');
  164. AIMomentquery.equalTo('company', company);
  165. AIMomentquery.equalTo('objectId', this.data.objectId);
  166. AIMomentquery.equalTo('isVisible', true);
  167. AIMomentquery.include('profile.user');
  168. AIMomentquery.include('profile');
  169. AIMomentquery.notEqualTo('isDeleted', true)
  170. let P = await AIMomentquery.first();
  171. const currentUser = Parse.User.current();
  172. let AIMomentCommentquery = new Parse.Query('AIMomentLike');
  173. AIMomentCommentquery.equalTo('company', company);
  174. AIMomentCommentquery.equalTo('type', 'chickin');
  175. AIMomentCommentquery.equalTo('moment', P.toPointer());
  176. AIMomentCommentquery.equalTo('user', currentUser.id);
  177. let moment = await AIMomentCommentquery.first()
  178. if (moment) {
  179. moment.set('isLiked', this.data.isclick)
  180. try {
  181. let saveDate = await moment.save();
  182. this.showchick()
  183. console.log(saveDate);
  184. console.log("新数据保存成功");
  185. } catch (error) {
  186. console.error("保存数据时出现错误:", error);
  187. }
  188. } else {
  189. const currentUser = Parse.User.current();
  190. let userquery = new Parse.Query('_User');
  191. userquery.equalTo('company', company);
  192. userquery.equalTo('objectId', currentUser.id);
  193. userquery.notEqualTo('isDeleted', true)
  194. let user = await userquery.first();
  195. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  196. let Comment = new Parse.Object('AIMomentLike');
  197. Comment.set('moment', P.toPointer())
  198. Comment.set('company', companyPointer);
  199. Comment.set('type', 'chickin');
  200. Comment.set('user', user.toPointer());
  201. Comment.set('isLiked', true);
  202. try {
  203. let saveDate2 = await Comment.save();
  204. this.showchick()
  205. console.log(saveDate2);
  206. console.log("新数据保存成功");
  207. } catch (error) {
  208. console.error("保存数据时出现错误:", error);
  209. }
  210. }
  211. },
  212. //显示是否点过赞
  213. async showischick() {
  214. const currentUser = Parse.User.current();
  215. let AIMomentCommentquery2 = new Parse.Query('AIMomentLike');
  216. AIMomentCommentquery2.equalTo('company', company);
  217. AIMomentCommentquery2.equalTo('type', 'chickin');
  218. AIMomentCommentquery2.equalTo('moment', this.data.objectId);
  219. AIMomentCommentquery2.equalTo('user', currentUser.id);
  220. let moment2 = await AIMomentCommentquery2.find()
  221. let AIMoment1List2 = moment2.map(item => item.toJSON());
  222. if (AIMoment1List2.length != 0) {
  223. this.setData({
  224. isclick: AIMoment1List2[0].isLiked
  225. })
  226. } else {
  227. this.setData({
  228. isclick: false
  229. })
  230. }
  231. console.log('isclick', AIMoment1List2);
  232. },
  233. //显示点赞人
  234. async showchick() {
  235. let Momentquery = new Parse.Query('AIMomentLike');
  236. Momentquery.equalTo('company', company);
  237. Momentquery.equalTo('type', 'chickin');
  238. Momentquery.equalTo('moment', this.data.objectId);
  239. Momentquery.notEqualTo('isDeleted', true)
  240. Momentquery.include('user')
  241. Momentquery.equalTo('isLiked', true);
  242. let r = await Momentquery.find();
  243. let chickList = r.map(item => item.toJSON());
  244. console.log('chickList', chickList);
  245. this.setData({
  246. chickList
  247. })
  248. },
  249. //显示弹出层
  250. showPopup() {
  251. this.setData({
  252. show: true,
  253. poptype: 'one', //直接回复
  254. commenttext: '评论'
  255. });
  256. this.showgood()
  257. },
  258. onClose() {
  259. this.setData({
  260. show: false
  261. });
  262. },
  263. //输入框高度随字体增多而变大
  264. onInput: function (event) {
  265. const value = event.detail.value; // 获取当前输入的值
  266. this.setData({
  267. inputValue: value,
  268. textareaHeight: this.calculateHeight(value) // 动态计算高度
  269. });
  270. },
  271. calculateHeight: function (value) {
  272. // 计算文本高度的逻辑,返回合适的高度
  273. // 这里可以根据实际情况调整
  274. const lineHeight = 40; // 设置行高
  275. const lines = Math.ceil(value.length / 30); // 假设每行30个字符
  276. return Math.max(52, lines * lineHeight); // 确保最小高度为52rpx
  277. },
  278. // 发送评论
  279. async sendComment() {
  280. //单独发送评论
  281. if (this.data.poptype == 'one') {
  282. let AIMomentquery = new Parse.Query('AIMoment');
  283. AIMomentquery.equalTo('company', company);
  284. AIMomentquery.equalTo('objectId', this.data.objectId);
  285. AIMomentquery.equalTo('isVisible', true);
  286. AIMomentquery.notEqualTo('isDeleted', true)
  287. let P = await AIMomentquery.first();
  288. const currentUser = Parse.User.current();
  289. let userquery = new Parse.Query('_User');
  290. userquery.equalTo('company', company);
  291. userquery.equalTo('objectId', currentUser.id);
  292. userquery.notEqualTo('isDeleted', true)
  293. let user = await userquery.first();
  294. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  295. let Comment = new Parse.Object('AIMomentComment');
  296. Comment.set('moment', P.toPointer())
  297. Comment.set('company', companyPointer);
  298. Comment.set('type', 'coment');
  299. Comment.set('isDeleted', false);
  300. Comment.set('content', this.data.inputValue);
  301. Comment.set('user', user.toPointer());
  302. try {
  303. let saveDate2 = await Comment.save();
  304. console.log(saveDate2);
  305. console.log("新数据保存成功");
  306. this.getComment()
  307. } catch (error) {
  308. console.error("保存数据时出现错误:", error);
  309. }
  310. // 处理发送评论的逻辑
  311. console.log(this.data.inputValue);
  312. // 清空输入框
  313. this.setData({
  314. inputValue: '',
  315. textareaHeight: 52 // 重置高度
  316. });
  317. }
  318. //回复别人评论
  319. if (this.data.poptype == 'two') {
  320. console.log('id', this.data.userobject);
  321. this.rebackmoment()
  322. }
  323. },
  324. // 获取评论
  325. async getComment() {
  326. let Momentquery = new Parse.Query('AIMomentComment');
  327. Momentquery.equalTo('company', company);
  328. Momentquery.equalTo('type', 'coment');
  329. Momentquery.equalTo('moment', this.data.objectId);
  330. Momentquery.notEqualTo('isDeleted', true);
  331. Momentquery.include('user');
  332. Momentquery.include('comment');
  333. Momentquery.include('comment.user');
  334. // 按照创建时间升序排列(从旧到新)
  335. Momentquery.ascending('createdAt'); // 或者使用 Momentquery.descending('createdAt') 以降序排列
  336. let r = await Momentquery.find();
  337. let commentList = r.map(item => {
  338. let comment = item.toJSON();
  339. // 初始化 showdeletid 属性为 false
  340. comment.showdeletid = false;
  341. return comment;
  342. });
  343. const filteredCommentList = commentList.filter(item => !item.comment || (item.comment && !item.comment.isDeleted));
  344. const noCommentCount = commentList.filter(item => !item.comment).length;
  345. console.log('评论', filteredCommentList);
  346. this.setData({
  347. commentList: filteredCommentList,
  348. noCommentCount,
  349. });
  350. },
  351. // 点击评论
  352. async showpop(e) {
  353. const objectId = e.currentTarget.dataset.id;
  354. let AIMomentCommentquery = new Parse.Query('AIMomentComment');
  355. AIMomentCommentquery.equalTo('company', company);
  356. AIMomentCommentquery.equalTo('objectId', objectId);
  357. AIMomentCommentquery.notEqualTo('isDeleted', true);
  358. AIMomentCommentquery.include('user');
  359. let r = await AIMomentCommentquery.find();
  360. let commentList = r.map(item => item.toJSON());
  361. const currentUser = Parse.User.current();
  362. let userquery = new Parse.Query('_User');
  363. userquery.equalTo('company', company);
  364. userquery.equalTo('objectId', currentUser.id);
  365. userquery.notEqualTo('isDeleted', true);
  366. let R = await userquery.find();
  367. let user = R.map(item => item.toJSON());
  368. // 检查是否点击了自己的评论
  369. if (user[0].objectId != commentList[0].user.objectId) {
  370. this.setData({
  371. show: true,
  372. poptype: 'two',
  373. userobject: objectId,
  374. commenttext: `回复${commentList[0].user.nickname}`
  375. });
  376. } else {
  377. // 点击了自己的评论,设置对应评论的 showdeletid 为 true
  378. const updatedCommentList = this.data.commentList.map(comment => {
  379. if (comment.objectId === objectId) {
  380. return {
  381. ...comment,
  382. showdeletid: true // 更新当前评论项
  383. };
  384. }
  385. return comment; // 保持其他评论项不变
  386. });
  387. this.setData({
  388. commentList: updatedCommentList // 更新 commentList 的状态
  389. });
  390. console.log('点击了自己');
  391. }
  392. console.log(this.data.commentList);
  393. },
  394. // 点击其他地方重置所有评论的 showdeletid
  395. onTapOutside() {
  396. if (!this.data.isCommentFocused) { // 只有在没有评论聚焦时才重置
  397. const updatedCommentList = this.data.commentList.map(comment => ({
  398. ...comment,
  399. showdeletid: false, // 重置所有评论的 showdeletid
  400. }));
  401. this.setData({
  402. commentList: updatedCommentList,
  403. focusedCommentId: null // 清除聚焦状态
  404. });
  405. }
  406. },
  407. // 添加页面的触摸事件
  408. onTouchStart() {
  409. this.onTapOutside(); // 页面触摸时重置评论状态
  410. },
  411. //回复评论
  412. async rebackmoment() {
  413. //所属评论
  414. let AIMomentCommentquery = new Parse.Query('AIMomentComment');
  415. AIMomentCommentquery.equalTo('company', company);
  416. AIMomentCommentquery.equalTo('objectId', this.data.userobject);
  417. AIMomentCommentquery.notEqualTo('isDeleted', true)
  418. let Puser = await AIMomentCommentquery.first();
  419. //所属动态
  420. let AIMomentquery = new Parse.Query('AIMoment');
  421. AIMomentquery.equalTo('company', company);
  422. AIMomentquery.equalTo('objectId', this.data.objectId);
  423. AIMomentquery.equalTo('isVisible', true);
  424. AIMomentquery.notEqualTo('isDeleted', true)
  425. let P = await AIMomentquery.first();
  426. //获取用户
  427. const currentUser = Parse.User.current();
  428. let userquery = new Parse.Query('_User');
  429. userquery.equalTo('company', company);
  430. userquery.equalTo('objectId', currentUser.id);
  431. userquery.notEqualTo('isDeleted', true)
  432. let user = await userquery.first();
  433. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  434. let Comment = new Parse.Object('AIMomentComment');
  435. Comment.set('moment', P.toPointer())
  436. Comment.set('company', companyPointer);
  437. Comment.set('comment', Puser.toPointer());
  438. Comment.set('type', 'coment');
  439. Comment.set('isDeleted', false);
  440. Comment.set('content', this.data.inputValue);
  441. Comment.set('user', user.toPointer());
  442. try {
  443. let saveDate2 = await Comment.save();
  444. console.log(saveDate2);
  445. console.log("新数据保存成功");
  446. this.getComment()
  447. } catch (error) {
  448. console.error("保存数据时出现错误:", error);
  449. }
  450. // 处理发送评论的逻辑
  451. console.log(this.data.inputValue);
  452. // 清空输入框
  453. this.setData({
  454. inputValue: '',
  455. textareaHeight: 52 // 重置高度
  456. });
  457. },
  458. //计算底部高度
  459. getbottomheight() {
  460. const systemInfo = wx.getSystemInfoSync();
  461. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  462. this.setData({
  463. bottomNavHeight,
  464. });
  465. },
  466. //删除评论
  467. async deletecomment(e) {
  468. const objectId = e.currentTarget.dataset.id
  469. let Momentquery = new Parse.Query('AIMomentComment');
  470. Momentquery.equalTo('company', company);
  471. Momentquery.equalTo('objectId', objectId);
  472. Momentquery.notEqualTo('isDeleted', true);
  473. let d = await Momentquery.first();
  474. d.set('isDeleted', true)
  475. try {
  476. let saveDate2 = await d.save();
  477. console.log(saveDate2);
  478. console.log("删除成功");
  479. this.getComment()
  480. } catch (error) {
  481. console.error("保存数据时出现错误:", error);
  482. }
  483. console.log(objectId);
  484. },
  485. //删除动态
  486. async deletecircle() {
  487. let Profilequery = new Parse.Query('AIMoment');
  488. Profilequery.equalTo('company', company);
  489. Profilequery.equalTo('isVisible', true);
  490. Profilequery.notEqualTo('isDeleted', true)
  491. Profilequery.equalTo('objectId', this.data.objectId);
  492. let P = await Profilequery.first();
  493. P.set('isDeleted', true)
  494. try {
  495. let saveDate2 = await P.save();
  496. console.log(saveDate2);
  497. this.setData({
  498. objectId: ''
  499. })
  500. console.log("删除数据成功");
  501. } catch (error) {
  502. console.error("保存数据时出现错误:", error);
  503. }
  504. },
  505. //显示确认框
  506. showconfirm() {
  507. this.setData({
  508. showconfirm: true
  509. })
  510. },
  511. //取消确认框
  512. cancleconfirm() {
  513. this.setData({
  514. showconfirm: false
  515. })
  516. },
  517. //点击确认
  518. clickconfirm() {
  519. console.log('删除了');
  520. this.deletecircle()
  521. },
  522. // 显示完整内容
  523. showFullContent() {
  524. this.setData({
  525. showFull: true
  526. });
  527. },
  528. // 收起内容
  529. showLessContent() {
  530. this.setData({
  531. showFull: false
  532. });
  533. },
  534. //
  535. // 点击预览大图
  536. openpreviewImg(e){
  537. const index = e.currentTarget.dataset.index
  538. this.selectComponent("#previewComponent").showPreview();
  539. this.setData({
  540. //imgList 图片地址数组
  541. imgList:this.data.images,
  542. currentImg:this.data.images[index],
  543. // previewHideStatus:true
  544. })
  545. },
  546. }
  547. })