index.js 16 KB

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