index.js 19 KB

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