index.js 19 KB

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