index.js 7.7 KB

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