index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. },
  28. lifetimes: {
  29. detached: function () {
  30. // 在组件实例被从页面节点树移除时执行
  31. },
  32. attached: async function () {
  33. // 在组件实例进入页面节点树时执行
  34. this.getcircle()
  35. },
  36. },
  37. /**
  38. * 组件的方法列表
  39. */
  40. methods: {
  41. onImageLoad: function (e) {
  42. const {
  43. width,
  44. height
  45. } = e.detail; // 获取图片的宽高
  46. console.log('11', e.detail);
  47. const imageClass = width > height ? 'image-landscape' : 'image-portrait'; // 判断横竖屏
  48. this.setData({
  49. imageclass: imageClass // 动态设置图片的类名
  50. });
  51. },
  52. previewImage: function (e) {
  53. const index = e.currentTarget.dataset.index; // 获取当前点击图片的索引
  54. const images = this.data.images; // 获取所有图片的链接
  55. wx.previewImage({
  56. current: images[index], // 当前显示图片的链接
  57. urls: images // 需要预览的图片链接列表
  58. });
  59. },
  60. gourl(e) {
  61. const url = e.currentTarget.dataset.url
  62. const objectId = e.currentTarget.dataset.id
  63. wx.navigateTo({
  64. url: `${url}?id=` + objectId // 目标页面的路径
  65. });
  66. },
  67. showgood() {
  68. this.setData({
  69. isgood: !this.data.isgood
  70. })
  71. console.log(this.data.isgood);
  72. },
  73. isclick() {
  74. // this.setData({
  75. // isclick: !this.data.isclick
  76. // })
  77. this.chickin()
  78. setTimeout(() => {
  79. this.showchick()
  80. this.showgood()
  81. }, 400)
  82. },
  83. async getcircle() {
  84. let AIMomentquery = new Parse.Query('AIMoment');
  85. AIMomentquery.equalTo('company', company);
  86. AIMomentquery.equalTo('objectId', this.data.objectId);
  87. AIMomentquery.equalTo('isVisible', true);
  88. AIMomentquery.include('profile.user');
  89. AIMomentquery.include('profile');
  90. AIMomentquery.notEqualTo('isDeleted', true)
  91. let P = await AIMomentquery.find();
  92. let AIMoment1List = P.map(item => item.toJSON());
  93. this.setData({
  94. cicleList: AIMoment1List,
  95. })
  96. this.setData({
  97. images: this.data.cicleList[0].images
  98. })
  99. // 将 ISO 字符串转换为时间戳并传递给 formatTime
  100. const createdAt = new Date(this.data.cicleList[0].createdAt).getTime();
  101. const time = this.formatTime(createdAt);
  102. this.setData({
  103. time
  104. })
  105. this.showchick()
  106. },
  107. formatTime(timestamp) {
  108. const now = Date.now();
  109. const diff = now - timestamp;
  110. if (diff < 60000) { // 小于1分钟
  111. return '刚刚';
  112. } else if (diff < 3600000) { // 小于1小时
  113. return Math.floor(diff / 60000) + '分钟前';
  114. } else if (diff < 86400000) { // 小于24小时
  115. return Math.floor(diff / 3600000) + '小时前';
  116. } else if (diff < 172800000) { // 小于48小时
  117. return '昨天';
  118. } else {
  119. const date = new Date(timestamp);
  120. return date.toLocaleDateString(); // 显示具体日期
  121. }
  122. },
  123. //点击点赞按钮
  124. async chickin() {
  125. let AIMomentquery = new Parse.Query('AIMoment');
  126. AIMomentquery.equalTo('company', company);
  127. AIMomentquery.equalTo('objectId', this.data.objectId);
  128. AIMomentquery.equalTo('isVisible', true);
  129. AIMomentquery.include('profile.user');
  130. AIMomentquery.include('profile');
  131. AIMomentquery.notEqualTo('isDeleted', true)
  132. let P = await AIMomentquery.first();
  133. //点赞成功
  134. this.setData({
  135. isclick: !this.data.isclick
  136. })
  137. const currentUser = Parse.User.current();
  138. let AIMomentCommentquery = new Parse.Query('AIMomentComment');
  139. AIMomentCommentquery.equalTo('company', company);
  140. AIMomentCommentquery.equalTo('moment', P.toPointer());
  141. AIMomentCommentquery.equalTo('user', currentUser.id);
  142. let moment = await AIMomentCommentquery.first()
  143. if (moment) {
  144. moment.set('isDeleted', this.data.isclick)
  145. try {
  146. let saveDate = await moment.save();
  147. console.log(saveDate);
  148. console.log("新数据保存成功");
  149. } catch (error) {
  150. console.error("保存数据时出现错误:", error);
  151. }
  152. } else {
  153. const currentUser = Parse.User.current();
  154. let userquery = new Parse.Query('_User');
  155. userquery.equalTo('company', company);
  156. userquery.equalTo('objectId', currentUser.id);
  157. userquery.notEqualTo('isDeleted', true)
  158. let user = await userquery.first();
  159. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  160. let Comment = new Parse.Object('AIMomentComment');
  161. Comment.set('moment', P.toPointer())
  162. Comment.set('company', companyPointer);
  163. Comment.set('user', user.toPointer());
  164. Comment.set('isDeleted', false);
  165. try {
  166. let saveDate2 = await Comment.save();
  167. console.log(saveDate2);
  168. console.log("新数据保存成功");
  169. } catch (error) {
  170. console.error("保存数据时出现错误:", error);
  171. }
  172. }
  173. },
  174. //显示点赞人
  175. async showchick() {
  176. const currentUser = Parse.User.current();
  177. let AIMomentCommentquery2 = new Parse.Query('AIMomentComment');
  178. AIMomentCommentquery2.equalTo('company', company);
  179. AIMomentCommentquery2.equalTo('moment', this.data.objectId);
  180. AIMomentCommentquery2.equalTo('user', currentUser.id);
  181. let moment2 = await AIMomentCommentquery2.find()
  182. let AIMoment1List2 = moment2.map(item => item.toJSON());
  183. console.log('AIMoment1List2',AIMoment1List2);
  184. this.setData({
  185. isclick:AIMoment1List2[0].isDeleted
  186. })
  187. let Momentquery = new Parse.Query('AIMomentComment');
  188. Momentquery.equalTo('company', company);
  189. Momentquery.equalTo('moment', this.data.cicleList[0].objectId);
  190. Momentquery.notEqualTo('isDeleted', true)
  191. Momentquery.include('user')
  192. let r = await Momentquery.find();
  193. let chickList = r.map(item => item.toJSON());
  194. console.log(chickList);
  195. this.setData({
  196. chickList
  197. })
  198. }
  199. }
  200. })