index.js 21 KB

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