index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. 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. },
  72. isclick() {
  73. this.chickin()
  74. setTimeout(() => {
  75. this.showchick()
  76. this.showgood()
  77. }, 400)
  78. },
  79. async getcircle() {
  80. let AIMomentquery = new Parse.Query('AIMoment');
  81. AIMomentquery.equalTo('company', company);
  82. AIMomentquery.equalTo('objectId', this.data.objectId);
  83. AIMomentquery.equalTo('isVisible', true);
  84. AIMomentquery.include('profile.user');
  85. AIMomentquery.include('profile');
  86. AIMomentquery.notEqualTo('isDeleted', true)
  87. let P = await AIMomentquery.find();
  88. let AIMoment1List = P.map(item => item.toJSON());
  89. this.setData({
  90. cicleList: AIMoment1List,
  91. })
  92. this.setData({
  93. images: this.data.cicleList[0].images
  94. })
  95. // 将 ISO 字符串转换为时间戳并传递给 formatTime
  96. const createdAt = new Date(this.data.cicleList[0].createdAt).getTime();
  97. const time = this.formatTime(createdAt);
  98. this.setData({
  99. time
  100. })
  101. this.showchick()
  102. },
  103. formatTime(timestamp) {
  104. const now = Date.now();
  105. const diff = now - timestamp;
  106. if (diff < 60000) { // 小于1分钟
  107. return '刚刚';
  108. } else if (diff < 3600000) { // 小于1小时
  109. return Math.floor(diff / 60000) + '分钟前';
  110. } else if (diff < 86400000) { // 小于24小时
  111. return Math.floor(diff / 3600000) + '小时前';
  112. } else if (diff < 172800000) { // 小于48小时
  113. return '昨天';
  114. } else {
  115. const date = new Date(timestamp);
  116. return date.toLocaleDateString(); // 显示具体日期
  117. }
  118. },
  119. //点击点赞按钮
  120. async chickin() {
  121. this.setData({
  122. isclick: !this.data.isclick
  123. })
  124. let AIMomentquery = new Parse.Query('AIMoment');
  125. AIMomentquery.equalTo('company', company);
  126. AIMomentquery.equalTo('objectId', this.data.objectId);
  127. AIMomentquery.equalTo('isVisible', true);
  128. AIMomentquery.include('profile.user');
  129. AIMomentquery.include('profile');
  130. AIMomentquery.notEqualTo('isDeleted', true)
  131. let P = await AIMomentquery.first();
  132. const currentUser = Parse.User.current();
  133. let AIMomentCommentquery = new Parse.Query('AIMomentComment');
  134. AIMomentCommentquery.equalTo('company', company);
  135. AIMomentCommentquery.equalTo('type', 'chickin');
  136. AIMomentCommentquery.equalTo('moment', P.toPointer());
  137. AIMomentCommentquery.equalTo('user', currentUser.id);
  138. let moment = await AIMomentCommentquery.first()
  139. if (moment) {
  140. moment.set('isDeleted', this.data.isclick)
  141. try {
  142. let saveDate = await moment.save();
  143. console.log(saveDate);
  144. console.log("新数据保存成功");
  145. } catch (error) {
  146. console.error("保存数据时出现错误:", error);
  147. }
  148. } else {
  149. const currentUser = Parse.User.current();
  150. let userquery = new Parse.Query('_User');
  151. userquery.equalTo('company', company);
  152. userquery.equalTo('objectId', currentUser.id);
  153. userquery.notEqualTo('isDeleted', true)
  154. let user = await userquery.first();
  155. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  156. let Comment = new Parse.Object('AIMomentComment');
  157. Comment.set('moment', P.toPointer())
  158. Comment.set('company', companyPointer);
  159. Comment.set('type', 'chickin');
  160. Comment.set('user', user.toPointer());
  161. Comment.set('isDeleted', false);
  162. try {
  163. let saveDate2 = await Comment.save();
  164. console.log(saveDate2);
  165. console.log("新数据保存成功");
  166. } catch (error) {
  167. console.error("保存数据时出现错误:", error);
  168. }
  169. }
  170. },
  171. //显示是否点过赞
  172. async showischick() {
  173. const currentUser = Parse.User.current();
  174. let AIMomentCommentquery2 = new Parse.Query('AIMomentComment');
  175. AIMomentCommentquery2.equalTo('company', company);
  176. AIMomentCommentquery2.equalTo('type', 'chickin');
  177. AIMomentCommentquery2.equalTo('moment', this.data.objectId);
  178. AIMomentCommentquery2.equalTo('user', currentUser.id);
  179. let moment2 = await AIMomentCommentquery2.find()
  180. let AIMoment1List2 = moment2.map(item => item.toJSON());
  181. this.setData({
  182. isclick:AIMoment1List2[0].isDeleted
  183. })
  184. console.log('isclick',AIMoment1List2);
  185. },
  186. //显示点赞人
  187. async showchick() {
  188. let Momentquery = new Parse.Query('AIMomentComment');
  189. Momentquery.equalTo('company', company);
  190. Momentquery.equalTo('type', 'chickin');
  191. Momentquery.equalTo('moment', this.data.objectId);
  192. Momentquery.notEqualTo('isDeleted', true)
  193. Momentquery.include('user')
  194. let r = await Momentquery.find();
  195. let chickList = r.map(item => item.toJSON());
  196. console.log(chickList);
  197. this.setData({
  198. chickList
  199. })
  200. }
  201. }
  202. })