message.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // pages/message/message.js
  2. var app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. buttonDisabled: false,
  9. modalHidden: true,
  10. userInfo: {},
  11. hasUserInfo: false,
  12. //判断小程序的组件在当前版本是否可用
  13. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  14. messages: [],
  15. no: '',
  16. title: '',
  17. authormessages: new Array(),//作者回复信息
  18. authorBool: new Array(),//显示回复数组
  19. ishave: false , //是否有筛选留言(是否显示赞)
  20. status: new Array(),//点赞的状态
  21. goodarray: new Array(),//所有点赞数
  22. isTop: new Array(),//置顶留言
  23. g_id: '' //公众号id
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad: function (option) {
  29. var that = this;
  30. console.log("留言页面的公众号编号为:" + option.g_id);
  31. that.setData({
  32. title: option.title,
  33. no: option.no,
  34. g_id: option.g_id
  35. })
  36. //获取已精选留言内容
  37. that.getChooseCotent();
  38. console.log(app.globalData);
  39. // 如果获取到用户信息就存储
  40. if (app.globalData.userInfo) {
  41. console.log("message");
  42. this.setData({
  43. userInfo: app.globalData.userInfo,
  44. hasUserInfo: true
  45. })
  46. wx.setStorageSync('username', that.data.userInfo.nickName)
  47. wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
  48. console.log("在index页面全局app1中获取到的用户信息为:" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
  49. } else if (this.data.canIUse) {
  50. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  51. // 所以此处加入 callback 以防止这种情况
  52. app.userInfoReadyCallback = res => {
  53. // console.log("用户名2:" + res.userInfo.nickName + " " + res.userInfo.avatarUrl)
  54. // wx.setStorageSync('username', res.userInfo.nickName)
  55. // wx.setStorageSync('headpath', res.userInfo.avatarUrl)
  56. this.setData({
  57. userInfo: res.userInfo,
  58. hasUserInfo: true
  59. })
  60. wx.setStorageSync('username', that.data.userInfo.nickName)
  61. wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
  62. console.log("在index页面全局app2中获取到的用户信息为:" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
  63. }
  64. }else{
  65. wx.getUserInfo({
  66. success: res=>{
  67. app.globalData.userInfo = res.userInfo;
  68. this.setData({
  69. userInfo: res.userInfo,
  70. hasUserInfo: true
  71. })
  72. }
  73. })
  74. }
  75. },
  76. //授权弹窗
  77. //点击按钮授权
  78. bindGetUserInfo: function (e) {
  79. var that = this;
  80. var openid = wx.getStorageSync("openid");
  81. if (e.detail.userInfo) {
  82. if (!openid) {
  83. wx.login({
  84. success: function (res) {
  85. // 用户已经授权过,不需要显示授权页面,所以不需要改变 isHide 的值
  86. // 根据自己的需求有其他操作再补充
  87. // 我这里实现的是在用户授权成功后,调用微信的 wx.login 接口,从而获取code
  88. app.globalData.userInfo = res.userInfo
  89. console.log('openid');
  90. wx.request({
  91. url: 'https://applet.yull.top/index/index/login',//获取唯一标识 openId
  92. data: {
  93. code: res.code
  94. },
  95. header: {
  96. 'content-type': 'application/json' // 数据格式(默认值)
  97. },
  98. method: 'post', //上传方式
  99. success: function (res) {
  100. console.log("openid" + res.data.openid);
  101. // wx.setStorageSync('token', res.data.token)//消息模板会用到
  102. wx.setStorageSync('openid', res.data.openid);//存储openid
  103. }
  104. });
  105. }
  106. });
  107. }
  108. app.globalData.userInfo = e.detail.userInfo
  109. this.setData({
  110. userInfo: e.detail.userInfo,
  111. hasUserInfo: true
  112. })
  113. wx.setStorageSync('username', that.data.userInfo.nickName)
  114. wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
  115. console.log("在index页面临时授权中获取到的用户信息为:" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
  116. } else {
  117. wx.showToast({
  118. title: '未授权无法查看留言',
  119. icon: 'none',
  120. })
  121. return;
  122. }
  123. },
  124. //获取已精选留言内容
  125. getChooseCotent: function (){
  126. var that = this;
  127. wx.request({
  128. url: app.globalData.apiUrl +'/index/message/realyMessagesList', //获取已精选留言内容
  129. data: {
  130. no: that.data.no, //文章编号
  131. ischeckmessage: '1', //留言是否经过筛选
  132. g_id: that.data.g_id, //公众号 id
  133. openid: wx.getStorageSync('openid'), //用户唯一标识
  134. },
  135. header: {
  136. 'content-type': 'application/json' // 数据格式(默认值)
  137. },
  138. method: 'post', //上传方式
  139. success: function (res) { //回调成功
  140. console.log(res.data)
  141. if (res.statusCode == 200) {
  142. if (res.data.result == '1') {
  143. var posts_message = res.data.content;
  144. console.log(posts_message)
  145. if (posts_message.length == 0) {
  146. wx.showToast({
  147. title: '还没有用户留言',
  148. icon: 'none',
  149. })
  150. } else {
  151. var arraymessage = new Array(res.data.content.length);
  152. var isReplyArray = new Array(res.data.content.length);
  153. var isstatus = new Array(res.data.content.length);
  154. var goodarr = new Array(res.data.content.length);
  155. var istop = new Array(res.data.content.length);
  156. for (var i = 0; i < res.data.content.length; i++) {
  157. goodarr[i] = posts_message[i].zanCounts
  158. if (posts_message[i].isTop == "1") {
  159. //判断是否置顶
  160. istop[i] = true
  161. } else {
  162. istop[i] = false
  163. }
  164. if (posts_message[i].iszan == "1") {
  165. isstatus[i] = false;
  166. } else {
  167. isstatus[i] = true;
  168. }
  169. if (posts_message[i].authorMesContent != "") {
  170. isReplyArray[i] = true;
  171. arraymessage[i] = posts_message[i].authorMesContent;
  172. } else {
  173. isReplyArray[i] = false;
  174. }
  175. }
  176. that.setData({
  177. ishave: true,
  178. status: isstatus,//是否已经点赞
  179. messages: posts_message,//所有留言信息
  180. authormessages: arraymessage,//作者回复内容
  181. authorBool: isReplyArray,//作者是否回复
  182. goodarray: goodarr,//赞的总数
  183. isTop: istop //是否置顶
  184. })
  185. }
  186. } else {
  187. wx.showToast({
  188. title: '获取失败',
  189. icon: 'none',
  190. })
  191. }
  192. } else {
  193. wx.showToast({
  194. title: '服务器错误',
  195. icon: 'none',
  196. })
  197. }
  198. },
  199. //回调失败
  200. fail: function (res) {
  201. console.log(res)
  202. wx.showToast({
  203. title: '联网失败',
  204. icon: 'fail',
  205. })
  206. },
  207. })
  208. },
  209. //写留言
  210. writemessage: function () {
  211. var that = this;
  212. wx: wx.navigateTo({//this.data.title this.data.no
  213. url: '../../pages/write/write?title=' + that.data.title + '&no=' + that.data.no + '&g_id=' + that.data.g_id,
  214. })
  215. },
  216. //悬浮按钮(回到主页)
  217. onPostClick:function (){
  218. wx: wx.navigateTo({
  219. url: '../../pages/index/index'
  220. })
  221. },
  222. // 实现点赞功能
  223. setGood: function (event) {
  224. var that = this;
  225. console.log(event.currentTarget.dataset.index)
  226. var u_index = event.currentTarget.dataset.index;
  227. console.log(this.data.messages[0].p_id)
  228. console.log("点赞状态"+this.data.status);
  229. //判断如果没有点赞
  230. if (this.data.status[u_index] == true){
  231. //转换成已点赞状态
  232. that.data.status[u_index] = !that.data.status[u_index]
  233. // //计数+1
  234. // that.data.goodarray[u_index] = that.data.goodarray[u_index] + 1
  235. // //更新数据
  236. that.setData({
  237. status: that.data.status,
  238. })
  239. //获得一个赞
  240. that.getChooseCotent();
  241. wx.request({
  242. url: app.globalData.apiUrl +'/index/message/zanCount', //url
  243. data: {
  244. p_id: that.data.messages[u_index].p_id,//用户的 id
  245. openid: wx.getStorageSync('openid'),//点赞用户
  246. status: 1, //获得一个赞
  247. no: that.data.no, //文章编号
  248. g_id: that.data.g_id, //公众号 id
  249. },
  250. header: {
  251. 'content-type': 'application/json' // 数据格式(默认值)
  252. },
  253. method: 'post', //上传方式
  254. success: function (res) { //回调成功
  255. console.log(res.data)
  256. if (res.statusCode == 200) {
  257. if (res.data.result == '1') {
  258. that.getChooseCotent();
  259. } else {
  260. that.getChooseCotent();
  261. //点赞失败(回复原来未点赞状态)
  262. // that.data.status[u_index] = !that.data.status[u_index]
  263. // that.data.goodarray[u_index] = that.data.goodarray[u_index] - 1
  264. // that.setData({
  265. // status: that.data.status,
  266. // goodarray: that.data.goodarray
  267. // })
  268. wx.showToast({
  269. title: '点赞失败',
  270. icon: 'none',
  271. })
  272. }
  273. } else {
  274. that.getChooseCotent();
  275. wx.showToast({
  276. title: '服务器错误',
  277. icon: 'none',
  278. })
  279. }
  280. },
  281. //回调失败
  282. fail: function (res) {
  283. console.log(res)
  284. that.data.status[u_index] = !that.data.status[u_index]
  285. that.data.goodarray[u_index] = that.data.goodarray[u_index] - 1
  286. that.setData({
  287. status: that.data.status,
  288. goodarray: that.data.goodarray
  289. })
  290. wx.showToast({
  291. title: '请检查网络连接',
  292. icon: 'fail',
  293. })
  294. },
  295. })
  296. }else{
  297. wx.showToast({
  298. title: '您已经点过赞哦',
  299. icon: 'none',
  300. })
  301. }
  302. },
  303. /**
  304. * 生命周期函数--监听页面初次渲染完成
  305. */
  306. onReady: function () {
  307. },
  308. /**
  309. * 生命周期函数--监听页面显示
  310. */
  311. onShow: function () {
  312. var that = this;
  313. that.getChooseCotent();
  314. },
  315. /**
  316. * 生命周期函数--监听页面隐藏
  317. */
  318. onHide: function () {
  319. },
  320. /**
  321. * 生命周期函数--监听页面卸载
  322. */
  323. onUnload: function () {
  324. },
  325. /**
  326. * 页面相关事件处理函数--监听用户下拉动作
  327. */
  328. onPullDownRefresh: function () {
  329. var that = this;
  330. that.getChooseCotent();
  331. //完成停止加载
  332. wx.hideNavigationBarLoading();
  333. //停止下拉刷新
  334. wx.stopPullDownRefresh();
  335. },
  336. /**
  337. * 页面上拉触底事件的处理函数
  338. */
  339. onReachBottom: function () {
  340. },
  341. /**
  342. * 用户点击右上角分享
  343. */
  344. onShareAppMessage: function () {
  345. }
  346. })