index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // pages/channel/index/index.js
  2. // 引入 QCloud 小程序增强 SDK
  3. Date.prototype.format = function(format){
  4. var o = {
  5. "M+" : this.getMonth()+1, //month
  6. "d+" : this.getDate(), //day
  7. "h+" : this.getHours(), //hour
  8. "m+" : this.getMinutes(), //minute
  9. "s+" : this.getSeconds(), //second
  10. "q+" : Math.floor((this.getMonth()+3)/3), //quarter
  11. "S" : this.getMilliseconds() //millisecond
  12. }
  13. if(/(y+)/.test(format)) {
  14. format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  15. }
  16. for(var k in o) {
  17. if(new RegExp("("+ k +")").test(format)) {
  18. format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
  19. }
  20. }
  21. return format;
  22. }
  23. var qcloud = require('../../../vendor/qcloud-weapp-client-sdk/index');
  24. // 引入配置
  25. var config = require('../../../config');
  26. // 显示繁忙提示
  27. var showBusy = text => wx.showToast({
  28. title: text,
  29. icon: 'loading',
  30. duration: 10000
  31. });
  32. // 显示成功提示
  33. var showSuccess = text => {
  34. wx.hideToast();
  35. wx.showToast({
  36. title: text,
  37. icon: 'success'
  38. });
  39. };
  40. // 显示失败提示
  41. var showModel = (title, content) => {
  42. wx.hideToast();
  43. wx.showModal({
  44. title,
  45. content: JSON.stringify(content),
  46. showCancel: false
  47. });
  48. };
  49. var showMsg = (title, content) => {
  50. wx.hideToast();
  51. wx.showModal({
  52. title,
  53. content: content,
  54. showCancel: false
  55. });
  56. };
  57. var busy=false;
  58. //获取应用实例
  59. var app = getApp()
  60. Page({
  61. data:{
  62. userInfo: {},
  63. loading:false,
  64. loading_more:false,
  65. next_data:true,
  66. busy:false,
  67. posts:[]
  68. },
  69. post_list:function(){
  70. var self = this;
  71. if(self.data.busy){
  72. return;
  73. }
  74. self.setData({
  75. loading:true,
  76. busy:true
  77. });
  78. var url;
  79. if(self.data.next_page_url==null){
  80. url=config.service.apiUrl+'timeline';
  81. }else{
  82. url=self.data.next_page_url
  83. }
  84. qcloud.request({
  85. // 要请求的地址
  86. url: url,
  87. success(result) {
  88. if(result.data.success==true){
  89. for (var i=0;i<result.data.posts.data.length;i++){
  90. var datestr=Date.parse((result.data.posts.data[i]['created_at']).toString().replace(/-/g, "/"));
  91. if(datestr){
  92. result.data.posts.data[i]['created_at']=new Date(datestr).format("hh:mm");
  93. }else{
  94. result.data.posts.data[i]['created_at']= '--:--'
  95. }
  96. }
  97. self.setData({
  98. posts:result.data.posts.data
  99. })
  100. }
  101. if(result.data.posts.next_page_url!==null){
  102. self.setData({
  103. next_data:true,
  104. next_page_url:result.data.posts.next_page_url
  105. })
  106. }else{
  107. self.setData({
  108. next_data:false,
  109. next_page_url:null
  110. })
  111. }
  112. console.log('request success', result);
  113. },
  114. fail(error) {
  115. console.log('request fail', error);
  116. },
  117. complete() {
  118. busy=false;
  119. self.setData({
  120. loading:false,
  121. busy:false
  122. })
  123. console.log('request complete');
  124. }
  125. });
  126. },
  127. load_more(){
  128. var self = this;
  129. var request_url=self.data.next_page_url;
  130. if(!request_url){
  131. return;
  132. }
  133. if(self.data.busy){
  134. return;
  135. }
  136. self.setData({
  137. loading:true,
  138. busy:true
  139. });
  140. qcloud.request({
  141. // 要请求的地址
  142. url: request_url,
  143. success(result) {
  144. if(result.data.success==true){
  145. var item=self.data.posts;
  146. var obj=result.data.posts.data;
  147. for (var i=0;i<obj.length;i++){
  148. var datestr=Date.parse((obj[i]['created_at']).toString().replace(/-/g, "/"));
  149. if(datestr){
  150. obj[i]['created_at']=new Date(datestr).format("hh:mm");
  151. }else{
  152. obj[i]['created_at']= '--:--'
  153. }
  154. item.push(obj[i]);
  155. }
  156. self.setData({
  157. posts:item
  158. });
  159. if(result.data.posts.next_page_url!==null){
  160. self.setData({
  161. next_data:true,
  162. next_page_url:result.data.posts.next_page_url
  163. })
  164. }else{
  165. self.setData({
  166. next_data:false,
  167. next_page_url:null
  168. })
  169. }
  170. }
  171. console.log('request success', result);
  172. },
  173. fail(error) {
  174. console.log('request fail', error);
  175. },
  176. complete() {
  177. self.setData({
  178. loading:false,
  179. busy:false
  180. })
  181. console.log('request complete');
  182. }
  183. });
  184. },
  185. onReachBottom: function() {
  186. var self = this;
  187. self.load_more();
  188. },
  189. onShareAppMessage: function () {
  190. return {
  191. title: '密频',
  192. path: 'pages/channel/index/index',
  193. success: function(res) {
  194. showSuccess('分享成功');
  195. console.log(res);
  196. // 分享成功
  197. },
  198. fail: function(res) {
  199. // 分享失败
  200. console.log(res);
  201. },
  202. complete:function(res){
  203. console.log(res);
  204. }
  205. }
  206. },
  207. onLoad:function(options){
  208. // 页面初始化 options为页面跳转所带来的参数
  209. },
  210. onReady:function(){
  211. // 页面渲染完成
  212. },
  213. onShow:function(){
  214. // 页面显示
  215. var self=this;
  216. if(busy){
  217. return;
  218. }
  219. self.setData({
  220. posts:[],
  221. loading:true,
  222. busy:false
  223. });
  224. busy=true;
  225. var self=this;
  226. app.getUserInfo(function(userInfo){
  227. self.post_list();
  228. });
  229. },
  230. onHide:function(){
  231. // 页面隐藏
  232. },
  233. onUnload:function(){
  234. // 页面关闭
  235. }
  236. })