index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. lifetimes: {
  36. detached: function () {
  37. // 在组件实例被从页面节点树移除时执行
  38. },
  39. attached: async function () {
  40. // 在组件实例进入页面节点树时执行
  41. this.getcircle()
  42. this.showischick()
  43. this.getbottomheight()
  44. this.getComment()
  45. },
  46. },
  47. /**
  48. * 组件的方法列表
  49. */
  50. methods: {
  51. onImageLoad: function (e) {
  52. const {
  53. width,
  54. height
  55. } = e.detail; // 获取图片的宽高
  56. const imageClass = width > height ? 'image-landscape' : 'image-portrait'; // 判断横竖屏
  57. this.setData({
  58. imageclass: imageClass // 动态设置图片的类名
  59. });
  60. },
  61. previewImage: function (e) {
  62. const index = e.currentTarget.dataset.index; // 获取当前点击图片的索引
  63. const images = this.data.images; // 获取所有图片的链接
  64. wx.previewImage({
  65. current: images[index], // 当前显示图片的链接
  66. urls: images // 需要预览的图片链接列表
  67. });
  68. },
  69. gourl(e) {
  70. const url = e.currentTarget.dataset.url
  71. const objectId = e.currentTarget.dataset.id
  72. wx.navigateTo({
  73. url: `${url}?id=` + objectId // 目标页面的路径
  74. });
  75. },
  76. showgood() {
  77. this.setData({
  78. isgood: !this.data.isgood
  79. })
  80. },
  81. isclick() {
  82. this.chickin()
  83. setTimeout(() => {
  84. this.showchick()
  85. this.showgood()
  86. }, 400)
  87. },
  88. async getcircle() {
  89. let AIMomentquery = new Parse.Query('AIMoment');
  90. AIMomentquery.equalTo('company', company);
  91. AIMomentquery.equalTo('objectId', this.data.objectId);
  92. AIMomentquery.equalTo('isVisible', true);
  93. AIMomentquery.include('profile.user');
  94. AIMomentquery.include('profile');
  95. AIMomentquery.notEqualTo('isDeleted', true)
  96. let P = await AIMomentquery.find();
  97. let AIMoment1List = P.map(item => item.toJSON());
  98. this.setData({
  99. cicleList: AIMoment1List,
  100. })
  101. this.setData({
  102. images: this.data.cicleList[0].images
  103. })
  104. // 将 ISO 字符串转换为时间戳并传递给 formatTime
  105. const createdAt = new Date(this.data.cicleList[0].createdAt).getTime();
  106. const time = this.formatTime(createdAt);
  107. this.setData({
  108. time
  109. })
  110. this.showchick()
  111. },
  112. formatTime(timestamp) {
  113. const now = Date.now();
  114. const diff = now - timestamp;
  115. if (diff < 60000) { // 小于1分钟
  116. return '刚刚';
  117. } else if (diff < 3600000) { // 小于1小时
  118. return Math.floor(diff / 60000) + '分钟前';
  119. } else if (diff < 86400000) { // 小于24小时
  120. return Math.floor(diff / 3600000) + '小时前';
  121. } else if (diff < 172800000) { // 小于48小时
  122. return '昨天';
  123. } else {
  124. const date = new Date(timestamp);
  125. return date.toLocaleDateString(); // 显示具体日期
  126. }
  127. },
  128. //点击点赞按钮
  129. async chickin() {
  130. this.setData({
  131. isclick: !this.data.isclick
  132. })
  133. let AIMomentquery = new Parse.Query('AIMoment');
  134. AIMomentquery.equalTo('company', company);
  135. AIMomentquery.equalTo('objectId', this.data.objectId);
  136. AIMomentquery.equalTo('isVisible', true);
  137. AIMomentquery.include('profile.user');
  138. AIMomentquery.include('profile');
  139. AIMomentquery.notEqualTo('isDeleted', true)
  140. let P = await AIMomentquery.first();
  141. const currentUser = Parse.User.current();
  142. let AIMomentCommentquery = new Parse.Query('AIMomentComment');
  143. AIMomentCommentquery.equalTo('company', company);
  144. AIMomentCommentquery.equalTo('type', 'chickin');
  145. AIMomentCommentquery.equalTo('moment', P.toPointer());
  146. AIMomentCommentquery.equalTo('user', currentUser.id);
  147. let moment = await AIMomentCommentquery.first()
  148. if (moment) {
  149. moment.set('isDeleted', this.data.isclick)
  150. try {
  151. let saveDate = await moment.save();
  152. console.log(saveDate);
  153. console.log("新数据保存成功");
  154. } catch (error) {
  155. console.error("保存数据时出现错误:", error);
  156. }
  157. } else {
  158. const currentUser = Parse.User.current();
  159. let userquery = new Parse.Query('_User');
  160. userquery.equalTo('company', company);
  161. userquery.equalTo('objectId', currentUser.id);
  162. userquery.notEqualTo('isDeleted', true)
  163. let user = await userquery.first();
  164. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  165. let Comment = new Parse.Object('AIMomentComment');
  166. Comment.set('moment', P.toPointer())
  167. Comment.set('company', companyPointer);
  168. Comment.set('type', 'chickin');
  169. Comment.set('user', user.toPointer());
  170. Comment.set('isDeleted', false);
  171. try {
  172. let saveDate2 = await Comment.save();
  173. console.log(saveDate2);
  174. console.log("新数据保存成功");
  175. } catch (error) {
  176. console.error("保存数据时出现错误:", error);
  177. }
  178. }
  179. },
  180. //显示是否点过赞
  181. async showischick() {
  182. const currentUser = Parse.User.current();
  183. let AIMomentCommentquery2 = new Parse.Query('AIMomentComment');
  184. AIMomentCommentquery2.equalTo('company', company);
  185. AIMomentCommentquery2.equalTo('type', 'chickin');
  186. AIMomentCommentquery2.equalTo('moment', this.data.objectId);
  187. AIMomentCommentquery2.equalTo('user', currentUser.id);
  188. let moment2 = await AIMomentCommentquery2.find()
  189. let AIMoment1List2 = moment2.map(item => item.toJSON());
  190. this.setData({
  191. isclick:AIMoment1List2[0].isDeleted
  192. })
  193. console.log('isclick',AIMoment1List2);
  194. },
  195. //显示点赞人
  196. async showchick() {
  197. let Momentquery = new Parse.Query('AIMomentComment');
  198. Momentquery.equalTo('company', company);
  199. Momentquery.equalTo('type', 'chickin');
  200. Momentquery.equalTo('moment', this.data.objectId);
  201. Momentquery.notEqualTo('isDeleted', true)
  202. Momentquery.include('user')
  203. let r = await Momentquery.find();
  204. let chickList = r.map(item => item.toJSON());
  205. console.log(chickList);
  206. this.setData({
  207. chickList
  208. })
  209. },
  210. //显示弹出层
  211. showPopup() {
  212. this.setData({ show: true });
  213. this.showgood()
  214. },
  215. onClose() {
  216. this.setData({ show: false });
  217. },
  218. //输入框高度随字体增多而变大
  219. onInput: function(event) {
  220. const value = event.detail.value; // 获取当前输入的值
  221. this.setData({
  222. inputValue: value,
  223. textareaHeight: this.calculateHeight(value) // 动态计算高度
  224. });
  225. },
  226. calculateHeight: function(value) {
  227. // 计算文本高度的逻辑,返回合适的高度
  228. // 这里可以根据实际情况调整
  229. const lineHeight = 40; // 设置行高
  230. const lines = Math.ceil(value.length / 30); // 假设每行30个字符
  231. return Math.max(52, lines * lineHeight); // 确保最小高度为52rpx
  232. },
  233. // 发送评论
  234. async sendComment() {
  235. let AIMomentquery = new Parse.Query('AIMoment');
  236. AIMomentquery.equalTo('company', company);
  237. AIMomentquery.equalTo('objectId', this.data.objectId);
  238. AIMomentquery.equalTo('isVisible', true);
  239. AIMomentquery.notEqualTo('isDeleted', true)
  240. let P = await AIMomentquery.first();
  241. const currentUser = Parse.User.current();
  242. let userquery = new Parse.Query('_User');
  243. userquery.equalTo('company', company);
  244. userquery.equalTo('objectId', currentUser.id);
  245. userquery.notEqualTo('isDeleted', true)
  246. let user = await userquery.first();
  247. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  248. let Comment = new Parse.Object('AIMomentComment');
  249. Comment.set('moment', P.toPointer())
  250. Comment.set('company', companyPointer);
  251. Comment.set('type', 'coment');
  252. Comment.set('isDeleted', false);
  253. Comment.set('content', this.data.inputValue);
  254. Comment.set('user', user.toPointer());
  255. try {
  256. let saveDate2 = await Comment.save();
  257. console.log(saveDate2);
  258. console.log("新数据保存成功");
  259. this.getComment()
  260. } catch (error) {
  261. console.error("保存数据时出现错误:", error);
  262. }
  263. // 处理发送评论的逻辑
  264. console.log(this.data.inputValue);
  265. // 清空输入框
  266. this.setData({
  267. inputValue: '',
  268. textareaHeight: 52 // 重置高度
  269. });
  270. },
  271. //获取评论
  272. async getComment(){
  273. let Momentquery = new Parse.Query('AIMomentComment');
  274. Momentquery.equalTo('company', company);
  275. Momentquery.equalTo('type', 'coment');
  276. Momentquery.equalTo('moment', this.data.objectId);
  277. Momentquery.notEqualTo('isDeleted', true)
  278. Momentquery.include('user')
  279. Momentquery.include('comment.user')
  280. let r = await Momentquery.find();
  281. let commentList = r.map(item => item.toJSON());
  282. console.log('评论',commentList);
  283. this.setData({
  284. commentList
  285. })
  286. },
  287. //计算底部高度
  288. getbottomheight(){
  289. const systemInfo = wx.getSystemInfoSync();
  290. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  291. this.setData({
  292. bottomNavHeight,
  293. });
  294. }
  295. }
  296. })