detail.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. var app = getApp();
  2. Page({
  3. data: {
  4. userInfo: null,
  5. archivesInfo: {
  6. article: {}
  7. },
  8. commentList: [],
  9. loading: false,
  10. nodata: false,
  11. nomore: false,
  12. form: {
  13. quotepid: 0,
  14. message: '',
  15. focus: false
  16. }
  17. },
  18. page: 1,
  19. onLoad: function(options) {
  20. var that = this;
  21. that.setData({
  22. userInfo: app.globalData.userInfo
  23. });
  24. app.request('/addons/cms/wxapp.archives/detail', {
  25. id: options.id
  26. }, function(data, ret) {
  27. that.setData({
  28. archivesInfo: data.archivesInfo,
  29. commentList: data.commentList
  30. });
  31. that.page++;
  32. }, function(data, ret) {
  33. app.error(ret.msg);
  34. });
  35. },
  36. reply: function(event) {
  37. var that = this;
  38. var pid = event.currentTarget.dataset.pid;
  39. var nickname = event.currentTarget.dataset.nickname;
  40. that.setData({
  41. form: {
  42. quotepid: pid,
  43. message: '@' + nickname + ' ',
  44. focus: true
  45. }
  46. });
  47. },
  48. login: function(event) {
  49. var that = this;
  50. app.login(function(data) {
  51. that.setData({
  52. userInfo: app.globalData.userInfo
  53. });
  54. that.onLoad({
  55. id: that.data.archivesInfo.id
  56. });
  57. });
  58. },
  59. formSubmit: function(event) {
  60. var that = this;
  61. var pid = event.currentTarget.dataset.pid;
  62. if (!app.globalData.userInfo) {
  63. app.error('请登录后再评论');
  64. return;
  65. }
  66. if (event.detail.value.message == '') {
  67. app.error('内容不能为空');
  68. return;
  69. }
  70. app.request('/addons/cms/wxapp.comment/post', {
  71. aid: this.data.archivesInfo.id,
  72. pid: this.data.form.quotepid,
  73. username: event.detail.value.username,
  74. content: event.detail.value.content
  75. }, function(data) {
  76. app.success('发表成功!');
  77. that.setData({
  78. form: {
  79. quotepid: 0,
  80. message: '',
  81. focus: false
  82. },
  83. commentList: [],
  84. nodata: false,
  85. nomore: false
  86. });
  87. if (that.data.commentList.length < 10) {
  88. that.page = 1;
  89. } else {
  90. that.data.commentList = that.data.commentList.slice(0, 10);
  91. that.page = 2;
  92. }
  93. that.onReachBottom();
  94. }, function(data, ret) {
  95. app.error(ret.msg);
  96. });
  97. },
  98. vote: function(event) {
  99. var that = this;
  100. app.vote(event, function(data) {
  101. that.setData({
  102. ['archivesInfo.likes']: data.likes,
  103. ['archivesInfo.dislikes']: data.dislikes,
  104. ['archivesInfo.likeratio']: data.likeratio
  105. });
  106. });
  107. },
  108. onReachBottom: function() {
  109. var that = this;
  110. this.loadComment(function(data) {
  111. if (data.commentList.length == 0) {
  112. //app.info("暂无更多数据");
  113. }
  114. });
  115. },
  116. loadComment: function(cb) {
  117. var that = this;
  118. if (that.data.nomore == true || that.data.loading == true) {
  119. return;
  120. }
  121. this.setData({
  122. loading: true
  123. });
  124. app.request('/addons/cms/wxapp.comment', {
  125. aid: this.data.archivesInfo.id,
  126. page: this.page
  127. }, function(data, ret) {
  128. that.setData({
  129. loading: false,
  130. nodata: that.page == 1 && data.commentList.length == 0 ? true : false,
  131. nomore: that.page > 1 && data.commentList.length == 0 ? true : false,
  132. commentList: that.page > 1 ? that.data.commentList.concat(data.commentList) : data.commentList,
  133. });
  134. that.page++;
  135. typeof cb == 'function' && cb(data);
  136. }, function(data, ret) {
  137. that.setData({
  138. loading: false
  139. });
  140. app.error(ret.msg);
  141. });
  142. },
  143. pay: function() {
  144. var that = this;
  145. wx.showModal({
  146. title: '温馨提示',
  147. content: '确认支付' + this.data.archivesInfo.price + '元查看付费内容?',
  148. confirmText: "确认支付",
  149. cancelText: "暂不支付",
  150. success: function(res) {
  151. if (res.confirm) {
  152. if (that.data.loading == true) {
  153. return;
  154. }
  155. that.setData({
  156. loading: true
  157. });
  158. app.request('/addons/cms/wxapp.archives/order', {
  159. id: that.data.archivesInfo.id
  160. }, function(data, ret) {
  161. if (data == null) {
  162. //余额支付或已支付过
  163. that.onLoad();
  164. } else {
  165. //发起支付
  166. wx.requestPayment({
  167. 'timeStamp': data.timeStamp,
  168. 'nonceStr': data.nonceStr,
  169. 'package': data.package,
  170. 'signType': data.signType,
  171. 'paySign': data.paySign,
  172. 'success': function(res) {
  173. if (res.errMsg == 'requestPayment:ok') {
  174. that.onLoad({
  175. id: that.data.archivesInfo.id
  176. });
  177. } else {
  178. app.error("支付失败请重试");
  179. }
  180. },
  181. 'fail': function(res) {
  182. console.log(res);
  183. app.error("支付失败请重试");
  184. },
  185. 'complete': function(res) {}
  186. });
  187. }
  188. }, function(data, ret) {
  189. that.setData({
  190. loading: false
  191. });
  192. app.error(ret.msg);
  193. });
  194. } else {
  195. console.log('用户暂不支付');
  196. }
  197. }
  198. });
  199. },
  200. share: function() {
  201. wx.showShareMenu({});
  202. },
  203. onShareAppMessage: function() {
  204. return {
  205. title: this.data.archivesInfo.title,
  206. desc: this.data.archivesInfo.intro,
  207. path: '/page/news/detail?id=' + this.data.archivesInfo.id
  208. }
  209. },
  210. })