index.js 19 KB

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