index.js 20 KB

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