index.js 21 KB

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