index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. // moment.set('isDeleted', true)
  159. try {
  160. let saveDate = await moment.save();
  161. this.showchick()
  162. console.log(saveDate);
  163. console.log("新数据保存成功");
  164. } catch (error) {
  165. console.error("保存数据时出现错误:", error);
  166. }
  167. } else {
  168. const currentUser = Parse.User.current();
  169. let userquery = new Parse.Query('_User');
  170. userquery.equalTo('company', company);
  171. userquery.equalTo('objectId', currentUser.id);
  172. userquery.notEqualTo('isDeleted', true)
  173. let user = await userquery.first();
  174. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  175. let Comment = new Parse.Object('AIMomentLike');
  176. Comment.set('moment', P.toPointer())
  177. Comment.set('company', companyPointer);
  178. Comment.set('type', 'chickin');
  179. Comment.set('user', user.toPointer());
  180. Comment.set('isLiked', true);
  181. try {
  182. let saveDate2 = await Comment.save();
  183. this.showchick()
  184. console.log(saveDate2);
  185. console.log("新数据保存成功");
  186. } catch (error) {
  187. console.error("保存数据时出现错误:", error);
  188. }
  189. }
  190. },
  191. //显示是否点过赞
  192. async showischick() {
  193. const currentUser = Parse.User.current();
  194. let AIMomentCommentquery2 = new Parse.Query('AIMomentLike');
  195. AIMomentCommentquery2.equalTo('company', company);
  196. AIMomentCommentquery2.equalTo('type', 'chickin');
  197. AIMomentCommentquery2.equalTo('moment', this.data.objectId);
  198. AIMomentCommentquery2.equalTo('user', currentUser.id);
  199. let moment2 = await AIMomentCommentquery2.find()
  200. let AIMoment1List2 = moment2.map(item => item.toJSON());
  201. if (AIMoment1List2.length != 0) {
  202. this.setData({
  203. isclick: AIMoment1List2[0].isLiked
  204. })
  205. } else {
  206. this.setData({
  207. isclick: false
  208. })
  209. }
  210. console.log('isclick', AIMoment1List2);
  211. },
  212. //显示点赞人
  213. async showchick() {
  214. let Momentquery = new Parse.Query('AIMomentLike');
  215. Momentquery.equalTo('company', company);
  216. Momentquery.equalTo('type', 'chickin');
  217. Momentquery.equalTo('moment', this.data.objectId);
  218. Momentquery.notEqualTo('isDeleted', true)
  219. Momentquery.include('user')
  220. Momentquery.equalTo('isLiked', true);
  221. let r = await Momentquery.find();
  222. let chickList = r.map(item => item.toJSON());
  223. console.log('chickList',chickList);
  224. this.setData({
  225. chickList
  226. })
  227. },
  228. //显示弹出层
  229. showPopup() {
  230. this.setData({
  231. show: true,
  232. poptype: 'one', //直接回复
  233. commenttext: '评论'
  234. });
  235. this.showgood()
  236. },
  237. onClose() {
  238. this.setData({
  239. show: false
  240. });
  241. },
  242. //输入框高度随字体增多而变大
  243. onInput: function (event) {
  244. const value = event.detail.value; // 获取当前输入的值
  245. this.setData({
  246. inputValue: value,
  247. textareaHeight: this.calculateHeight(value) // 动态计算高度
  248. });
  249. },
  250. calculateHeight: function (value) {
  251. // 计算文本高度的逻辑,返回合适的高度
  252. // 这里可以根据实际情况调整
  253. const lineHeight = 40; // 设置行高
  254. const lines = Math.ceil(value.length / 30); // 假设每行30个字符
  255. return Math.max(52, lines * lineHeight); // 确保最小高度为52rpx
  256. },
  257. // 发送评论
  258. async sendComment() {
  259. //单独发送评论
  260. if (this.data.poptype == 'one') {
  261. let AIMomentquery = new Parse.Query('AIMoment');
  262. AIMomentquery.equalTo('company', company);
  263. AIMomentquery.equalTo('objectId', this.data.objectId);
  264. AIMomentquery.equalTo('isVisible', true);
  265. AIMomentquery.notEqualTo('isDeleted', true)
  266. let P = await AIMomentquery.first();
  267. const currentUser = Parse.User.current();
  268. let userquery = new Parse.Query('_User');
  269. userquery.equalTo('company', company);
  270. userquery.equalTo('objectId', currentUser.id);
  271. userquery.notEqualTo('isDeleted', true)
  272. let user = await userquery.first();
  273. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  274. let Comment = new Parse.Object('AIMomentComment');
  275. Comment.set('moment', P.toPointer())
  276. Comment.set('company', companyPointer);
  277. Comment.set('type', 'coment');
  278. Comment.set('isDeleted', false);
  279. Comment.set('content', this.data.inputValue);
  280. Comment.set('user', user.toPointer());
  281. try {
  282. let saveDate2 = await Comment.save();
  283. console.log(saveDate2);
  284. console.log("新数据保存成功");
  285. this.getComment()
  286. } catch (error) {
  287. console.error("保存数据时出现错误:", error);
  288. }
  289. // 处理发送评论的逻辑
  290. console.log(this.data.inputValue);
  291. // 清空输入框
  292. this.setData({
  293. inputValue: '',
  294. textareaHeight: 52 // 重置高度
  295. });
  296. }
  297. //回复别人评论
  298. if (this.data.poptype == 'two') {
  299. console.log('id', this.data.userobject);
  300. this.rebackmoment()
  301. }
  302. },
  303. // 获取评论
  304. async getComment() {
  305. let Momentquery = new Parse.Query('AIMomentComment');
  306. Momentquery.equalTo('company', company);
  307. Momentquery.equalTo('type', 'coment');
  308. Momentquery.equalTo('moment', this.data.objectId);
  309. Momentquery.notEqualTo('isDeleted', true);
  310. Momentquery.include('user');
  311. Momentquery.include('comment');
  312. Momentquery.include('comment.user');
  313. // 按照创建时间升序排列(从旧到新)
  314. Momentquery.ascending('createdAt'); // 或者使用 Momentquery.descending('createdAt') 以降序排列
  315. let r = await Momentquery.find();
  316. let commentList = r.map(item => {
  317. let comment = item.toJSON();
  318. // 初始化 showdeletid 属性为 false
  319. comment.showdeletid = false;
  320. return comment;
  321. });
  322. const filteredCommentList = commentList.filter(item => !item.comment || (item.comment && !item.comment.isDeleted));
  323. console.log('评论', filteredCommentList);
  324. this.setData({
  325. commentList: filteredCommentList
  326. });
  327. },
  328. // 点击评论
  329. async showpop(e) {
  330. const objectId = e.currentTarget.dataset.id;
  331. let AIMomentCommentquery = new Parse.Query('AIMomentComment');
  332. AIMomentCommentquery.equalTo('company', company);
  333. AIMomentCommentquery.equalTo('objectId', objectId);
  334. AIMomentCommentquery.notEqualTo('isDeleted', true);
  335. AIMomentCommentquery.include('user');
  336. let r = await AIMomentCommentquery.find();
  337. let commentList = r.map(item => item.toJSON());
  338. const currentUser = Parse.User.current();
  339. let userquery = new Parse.Query('_User');
  340. userquery.equalTo('company', company);
  341. userquery.equalTo('objectId', currentUser.id);
  342. userquery.notEqualTo('isDeleted', true);
  343. let R = await userquery.find();
  344. let user = R.map(item => item.toJSON());
  345. // 检查是否点击了自己的评论
  346. if (user[0].objectId != commentList[0].user.objectId) {
  347. this.setData({
  348. show: true,
  349. poptype: 'two',
  350. userobject: objectId,
  351. commenttext: `回复${commentList[0].user.nickname}`
  352. });
  353. } else {
  354. // 点击了自己的评论,设置对应评论的 showdeletid 为 true
  355. const updatedCommentList = this.data.commentList.map(comment => {
  356. if (comment.objectId === objectId) {
  357. return {
  358. ...comment,
  359. showdeletid: true // 更新当前评论项
  360. };
  361. }
  362. return comment; // 保持其他评论项不变
  363. });
  364. this.setData({
  365. commentList: updatedCommentList // 更新 commentList 的状态
  366. });
  367. console.log('点击了自己');
  368. }
  369. console.log(this.data.commentList);
  370. },
  371. // 点击其他地方重置所有评论的 showdeletid
  372. onTapOutside() {
  373. if (!this.data.isCommentFocused) { // 只有在没有评论聚焦时才重置
  374. const updatedCommentList = this.data.commentList.map(comment => ({
  375. ...comment,
  376. showdeletid: false // 重置所有评论的 showdeletid
  377. }));
  378. this.setData({
  379. commentList: updatedCommentList,
  380. focusedCommentId: null // 清除聚焦状态
  381. });
  382. }
  383. },
  384. // 添加页面的触摸事件
  385. onTouchStart() {
  386. this.onTapOutside(); // 页面触摸时重置评论状态
  387. },
  388. //回复评论
  389. async rebackmoment() {
  390. //所属评论
  391. let AIMomentCommentquery = new Parse.Query('AIMomentComment');
  392. AIMomentCommentquery.equalTo('company', company);
  393. AIMomentCommentquery.equalTo('objectId', this.data.userobject);
  394. AIMomentCommentquery.notEqualTo('isDeleted', true)
  395. let Puser = await AIMomentCommentquery.first();
  396. //所属动态
  397. let AIMomentquery = new Parse.Query('AIMoment');
  398. AIMomentquery.equalTo('company', company);
  399. AIMomentquery.equalTo('objectId', this.data.objectId);
  400. AIMomentquery.equalTo('isVisible', true);
  401. AIMomentquery.notEqualTo('isDeleted', true)
  402. let P = await AIMomentquery.first();
  403. //获取用户
  404. const currentUser = Parse.User.current();
  405. let userquery = new Parse.Query('_User');
  406. userquery.equalTo('company', company);
  407. userquery.equalTo('objectId', currentUser.id);
  408. userquery.notEqualTo('isDeleted', true)
  409. let user = await userquery.first();
  410. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  411. let Comment = new Parse.Object('AIMomentComment');
  412. Comment.set('moment', P.toPointer())
  413. Comment.set('company', companyPointer);
  414. Comment.set('comment', Puser.toPointer());
  415. Comment.set('type', 'coment');
  416. Comment.set('isDeleted', false);
  417. Comment.set('content', this.data.inputValue);
  418. Comment.set('user', user.toPointer());
  419. try {
  420. let saveDate2 = await Comment.save();
  421. console.log(saveDate2);
  422. console.log("新数据保存成功");
  423. this.getComment()
  424. } catch (error) {
  425. console.error("保存数据时出现错误:", error);
  426. }
  427. // 处理发送评论的逻辑
  428. console.log(this.data.inputValue);
  429. // 清空输入框
  430. this.setData({
  431. inputValue: '',
  432. textareaHeight: 52 // 重置高度
  433. });
  434. },
  435. //计算底部高度
  436. getbottomheight() {
  437. const systemInfo = wx.getSystemInfoSync();
  438. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  439. this.setData({
  440. bottomNavHeight,
  441. });
  442. },
  443. //删除评论
  444. async deletecomment(e) {
  445. const objectId = e.currentTarget.dataset.id
  446. let Momentquery = new Parse.Query('AIMomentComment');
  447. Momentquery.equalTo('company', company);
  448. Momentquery.equalTo('objectId', objectId);
  449. Momentquery.notEqualTo('isDeleted', true);
  450. let d = await Momentquery.first();
  451. d.set('isDeleted', true)
  452. try {
  453. let saveDate2 = await d.save();
  454. console.log(saveDate2);
  455. console.log("删除成功");
  456. this.getComment()
  457. } catch (error) {
  458. console.error("保存数据时出现错误:", error);
  459. }
  460. console.log(objectId);
  461. },
  462. //删除动态
  463. async deletecircle() {
  464. let Profilequery = new Parse.Query('AIMoment');
  465. Profilequery.equalTo('company', company);
  466. Profilequery.equalTo('isVisible', true);
  467. Profilequery.notEqualTo('isDeleted', true)
  468. Profilequery.equalTo('objectId', this.data.objectId);
  469. let P = await Profilequery.first();
  470. P.set('isDeleted', true)
  471. try {
  472. let saveDate2 = await P.save();
  473. console.log(saveDate2);
  474. this.setData({
  475. objectId: ''
  476. })
  477. console.log("删除数据成功");
  478. } catch (error) {
  479. console.error("保存数据时出现错误:", error);
  480. }
  481. },
  482. }
  483. })