detail.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. wx.getUserProfile({
  51. lang: 'zh',
  52. desc: '授权用户信息',
  53. success: function (res) {
  54. console.log(res)
  55. app.login(res.userInfo, function () {
  56. that.setData({
  57. userInfo: app.globalData.userInfo
  58. });
  59. that.onLoad({
  60. id: that.data.archivesInfo.id
  61. });
  62. });
  63. },
  64. fail: function (e) {
  65. app.info(JSON.stringify(e));
  66. }
  67. })
  68. },
  69. formSubmit: function (event) {
  70. var that = this;
  71. var pid = event.currentTarget.dataset.pid;
  72. if (!app.globalData.userInfo) {
  73. app.error('请登录后再评论');
  74. return;
  75. }
  76. if (event.detail.value.message == '') {
  77. app.error('内容不能为空');
  78. return;
  79. }
  80. app.request('/addons/cms/wxapp.comment/post', {
  81. aid: this.data.archivesInfo.id,
  82. pid: this.data.form.quotepid,
  83. username: event.detail.value.username,
  84. content: event.detail.value.content
  85. }, function (data, ret) {
  86. app.info(ret.msg);
  87. that.setData({
  88. form: {
  89. quotepid: 0,
  90. message: '',
  91. focus: false
  92. },
  93. commentList: [],
  94. nodata: false,
  95. nomore: false
  96. });
  97. if (that.data.commentList.length < 10) {
  98. that.page = 1;
  99. } else {
  100. that.data.commentList = that.data.commentList.slice(0, 10);
  101. that.page = 2;
  102. }
  103. that.onReachBottom();
  104. }, function (data, ret) {
  105. app.error(ret.msg);
  106. });
  107. },
  108. vote: function (event) {
  109. var that = this;
  110. app.vote(event, function (data) {
  111. that.setData({
  112. ['archivesInfo.likes']: data.likes,
  113. ['archivesInfo.dislikes']: data.dislikes,
  114. ['archivesInfo.likeratio']: data.likeratio
  115. });
  116. });
  117. },
  118. onReachBottom: function () {
  119. var that = this;
  120. this.loadComment(function (data) {
  121. if (data.commentList.length == 0) {
  122. //app.info("暂无更多数据");
  123. }
  124. });
  125. },
  126. loadComment: function (cb) {
  127. var that = this;
  128. if (that.data.nomore == true || that.data.loading == true) {
  129. return;
  130. }
  131. this.setData({
  132. loading: true
  133. });
  134. app.request('/addons/cms/wxapp.comment', {
  135. aid: this.data.archivesInfo.id,
  136. page: this.page
  137. }, function (data, ret) {
  138. that.setData({
  139. loading: false,
  140. nodata: that.page == 1 && data.commentList.length == 0 ? true : false,
  141. nomore: that.page > 1 && data.commentList.length == 0 ? true : false,
  142. commentList: that.page > 1 ? that.data.commentList.concat(data.commentList) : data.commentList,
  143. });
  144. that.page++;
  145. typeof cb == 'function' && cb(data);
  146. }, function (data, ret) {
  147. that.setData({
  148. loading: false
  149. });
  150. app.error(ret.msg);
  151. });
  152. },
  153. pay: function () {
  154. var that = this;
  155. wx.showModal({
  156. title: '温馨提示',
  157. content: '确认支付' + this.data.archivesInfo.price + '元查看付费内容?',
  158. confirmText: "确认支付",
  159. cancelText: "暂不支付",
  160. success: function (res) {
  161. if (res.confirm) {
  162. if (that.data.loading == true) {
  163. return;
  164. }
  165. that.setData({
  166. loading: true
  167. });
  168. app.request('/addons/cms/wxapp.archives/order', {
  169. id: that.data.archivesInfo.id
  170. }, function (data, ret) {
  171. if (data == null) {
  172. //余额支付或已支付过
  173. that.onLoad();
  174. } else {
  175. //发起支付
  176. wx.requestPayment({
  177. 'timeStamp': data.timeStamp,
  178. 'nonceStr': data.nonceStr,
  179. 'package': data.package,
  180. 'signType': data.signType,
  181. 'paySign': data.paySign,
  182. 'success': function (res) {
  183. if (res.errMsg == 'requestPayment:ok') {
  184. that.onLoad({
  185. id: that.data.archivesInfo.id
  186. });
  187. } else {
  188. app.error("支付失败请重试");
  189. }
  190. },
  191. 'fail': function (res) {
  192. console.log(res);
  193. app.error("支付失败请重试");
  194. },
  195. 'complete': function (res) {}
  196. });
  197. }
  198. }, function (data, ret) {
  199. that.setData({
  200. loading: false
  201. });
  202. app.error(ret.msg);
  203. });
  204. } else {
  205. console.log('用户暂不支付');
  206. }
  207. }
  208. });
  209. },
  210. share: function () {
  211. wx.showShareMenu({});
  212. },
  213. onShareAppMessage: function () {
  214. return {
  215. title: this.data.archivesInfo.title,
  216. desc: this.data.archivesInfo.intro,
  217. path: '/page/news/detail?id=' + this.data.archivesInfo.id
  218. }
  219. },
  220. })