index.js 20 KB

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