index.js 16 KB

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