index.js 20 KB

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