index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. this.setData({
  198. isclick: AIMoment1List2[0].isDeleted
  199. })
  200. console.log('isclick', AIMoment1List2);
  201. },
  202. //显示点赞人
  203. async showchick() {
  204. let Momentquery = new Parse.Query('AIMomentComment');
  205. Momentquery.equalTo('company', company);
  206. Momentquery.equalTo('type', 'chickin');
  207. Momentquery.equalTo('moment', this.data.objectId);
  208. Momentquery.notEqualTo('isDeleted', true)
  209. Momentquery.include('user')
  210. let r = await Momentquery.find();
  211. let chickList = r.map(item => item.toJSON());
  212. console.log(chickList);
  213. this.setData({
  214. chickList
  215. })
  216. },
  217. //显示弹出层
  218. showPopup() {
  219. this.setData({
  220. show: true,
  221. poptype: 'one', //直接回复
  222. commenttext: '评论'
  223. });
  224. this.showgood()
  225. },
  226. onClose() {
  227. this.setData({
  228. show: false
  229. });
  230. },
  231. //输入框高度随字体增多而变大
  232. onInput: function (event) {
  233. const value = event.detail.value; // 获取当前输入的值
  234. this.setData({
  235. inputValue: value,
  236. textareaHeight: this.calculateHeight(value) // 动态计算高度
  237. });
  238. },
  239. calculateHeight: function (value) {
  240. // 计算文本高度的逻辑,返回合适的高度
  241. // 这里可以根据实际情况调整
  242. const lineHeight = 40; // 设置行高
  243. const lines = Math.ceil(value.length / 30); // 假设每行30个字符
  244. return Math.max(52, lines * lineHeight); // 确保最小高度为52rpx
  245. },
  246. // 发送评论
  247. async sendComment() {
  248. //单独发送评论
  249. if (this.data.poptype == 'one') {
  250. let AIMomentquery = new Parse.Query('AIMoment');
  251. AIMomentquery.equalTo('company', company);
  252. AIMomentquery.equalTo('objectId', this.data.objectId);
  253. AIMomentquery.equalTo('isVisible', true);
  254. AIMomentquery.notEqualTo('isDeleted', true)
  255. let P = await AIMomentquery.first();
  256. const currentUser = Parse.User.current();
  257. let userquery = new Parse.Query('_User');
  258. userquery.equalTo('company', company);
  259. userquery.equalTo('objectId', currentUser.id);
  260. userquery.notEqualTo('isDeleted', true)
  261. let user = await userquery.first();
  262. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  263. let Comment = new Parse.Object('AIMomentComment');
  264. Comment.set('moment', P.toPointer())
  265. Comment.set('company', companyPointer);
  266. Comment.set('type', 'coment');
  267. Comment.set('isDeleted', false);
  268. Comment.set('content', this.data.inputValue);
  269. Comment.set('user', user.toPointer());
  270. try {
  271. let saveDate2 = await Comment.save();
  272. console.log(saveDate2);
  273. console.log("新数据保存成功");
  274. this.getComment()
  275. } catch (error) {
  276. console.error("保存数据时出现错误:", error);
  277. }
  278. // 处理发送评论的逻辑
  279. console.log(this.data.inputValue);
  280. // 清空输入框
  281. this.setData({
  282. inputValue: '',
  283. textareaHeight: 52 // 重置高度
  284. });
  285. }
  286. //回复别人评论
  287. if (this.data.poptype == 'two') {
  288. console.log('id', this.data.userobject);
  289. this.rebackmoment()
  290. }
  291. },
  292. // 获取评论
  293. async getComment() {
  294. let Momentquery = new Parse.Query('AIMomentComment');
  295. Momentquery.equalTo('company', company);
  296. Momentquery.equalTo('type', 'coment');
  297. Momentquery.equalTo('moment', this.data.objectId);
  298. Momentquery.notEqualTo('isDeleted', true);
  299. Momentquery.include('user');
  300. Momentquery.include('comment.user');
  301. // 按照创建时间升序排列(从旧到新)
  302. Momentquery.ascending('createdAt'); // 或者使用 Momentquery.descending('createdAt') 以降序排列
  303. let r = await Momentquery.find();
  304. let commentList = r.map(item => {
  305. let comment = item.toJSON();
  306. // 初始化 showdeletid 属性为 false
  307. comment.showdeletid = false;
  308. return comment;
  309. });
  310. console.log('评论', commentList);
  311. this.setData({
  312. commentList
  313. });
  314. },
  315. // 点击评论
  316. async showpop(e) {
  317. const objectId = e.currentTarget.dataset.id;
  318. let AIMomentCommentquery = new Parse.Query('AIMomentComment');
  319. AIMomentCommentquery.equalTo('company', company);
  320. AIMomentCommentquery.equalTo('objectId', objectId);
  321. AIMomentCommentquery.notEqualTo('isDeleted', true);
  322. AIMomentCommentquery.include('user');
  323. let r = await AIMomentCommentquery.find();
  324. let commentList = r.map(item => item.toJSON());
  325. const currentUser = Parse.User.current();
  326. let userquery = new Parse.Query('_User');
  327. userquery.equalTo('company', company);
  328. userquery.equalTo('objectId', currentUser.id);
  329. userquery.notEqualTo('isDeleted', true);
  330. let R = await userquery.find();
  331. let user = R.map(item => item.toJSON());
  332. // 检查是否点击了自己的评论
  333. if (user[0].objectId != commentList[0].user.objectId) {
  334. this.setData({
  335. show: true,
  336. poptype: 'two',
  337. userobject: objectId,
  338. commenttext: `回复${commentList[0].user.nickname}`
  339. });
  340. } else {
  341. // 点击了自己的评论,设置对应评论的 showdeletid 为 true
  342. const updatedCommentList = this.data.commentList.map(comment => {
  343. if (comment.objectId === objectId) {
  344. return {
  345. ...comment,
  346. showdeletid: true // 更新当前评论项
  347. };
  348. }
  349. return comment; // 保持其他评论项不变
  350. });
  351. this.setData({
  352. commentList: updatedCommentList // 更新 commentList 的状态
  353. });
  354. console.log('点击了自己');
  355. }
  356. console.log(this.data.commentList);
  357. },
  358. // 点击其他地方重置所有评论的 showdeletid
  359. onTapOutside() {
  360. if (!this.data.isCommentFocused) { // 只有在没有评论聚焦时才重置
  361. const updatedCommentList = this.data.commentList.map(comment => ({
  362. ...comment,
  363. showdeletid: false // 重置所有评论的 showdeletid
  364. }));
  365. this.setData({
  366. commentList: updatedCommentList,
  367. focusedCommentId: null // 清除聚焦状态
  368. });
  369. }
  370. },
  371. // 添加页面的触摸事件
  372. onTouchStart() {
  373. this.onTapOutside(); // 页面触摸时重置评论状态
  374. },
  375. //回复评论
  376. async rebackmoment() {
  377. //所属评论
  378. let AIMomentCommentquery = new Parse.Query('AIMomentComment');
  379. AIMomentCommentquery.equalTo('company', company);
  380. AIMomentCommentquery.equalTo('objectId', this.data.userobject);
  381. AIMomentCommentquery.notEqualTo('isDeleted', true)
  382. let Puser = await AIMomentCommentquery.first();
  383. //所属动态
  384. let AIMomentquery = new Parse.Query('AIMoment');
  385. AIMomentquery.equalTo('company', company);
  386. AIMomentquery.equalTo('objectId', this.data.objectId);
  387. AIMomentquery.equalTo('isVisible', true);
  388. AIMomentquery.notEqualTo('isDeleted', true)
  389. let P = await AIMomentquery.first();
  390. //获取用户
  391. const currentUser = Parse.User.current();
  392. let userquery = new Parse.Query('_User');
  393. userquery.equalTo('company', company);
  394. userquery.equalTo('objectId', currentUser.id);
  395. userquery.notEqualTo('isDeleted', true)
  396. let user = await userquery.first();
  397. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  398. let Comment = new Parse.Object('AIMomentComment');
  399. Comment.set('moment', P.toPointer())
  400. Comment.set('company', companyPointer);
  401. Comment.set('comment', Puser.toPointer());
  402. Comment.set('type', 'coment');
  403. Comment.set('isDeleted', false);
  404. Comment.set('content', this.data.inputValue);
  405. Comment.set('user', user.toPointer());
  406. try {
  407. let saveDate2 = await Comment.save();
  408. console.log(saveDate2);
  409. console.log("新数据保存成功");
  410. this.getComment()
  411. } catch (error) {
  412. console.error("保存数据时出现错误:", error);
  413. }
  414. // 处理发送评论的逻辑
  415. console.log(this.data.inputValue);
  416. // 清空输入框
  417. this.setData({
  418. inputValue: '',
  419. textareaHeight: 52 // 重置高度
  420. });
  421. },
  422. //计算底部高度
  423. getbottomheight() {
  424. const systemInfo = wx.getSystemInfoSync();
  425. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  426. this.setData({
  427. bottomNavHeight,
  428. });
  429. }
  430. }
  431. })