123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- // pages/channel/index/index.js
- // 引入 QCloud 小程序增强 SDK
- Date.prototype.format = function(format){
- var o = {
- "M+" : this.getMonth()+1, //month
- "d+" : this.getDate(), //day
- "h+" : this.getHours(), //hour
- "m+" : this.getMinutes(), //minute
- "s+" : this.getSeconds(), //second
- "q+" : Math.floor((this.getMonth()+3)/3), //quarter
- "S" : this.getMilliseconds() //millisecond
- }
- if(/(y+)/.test(format)) {
- format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
- }
- for(var k in o) {
- if(new RegExp("("+ k +")").test(format)) {
- format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
- }
- }
- return format;
- }
- var qcloud = require('../../../vendor/qcloud-weapp-client-sdk/index');
- // 引入配置
- var config = require('../../../config');
- // 显示繁忙提示
- var showBusy = text => wx.showToast({
- title: text,
- icon: 'loading',
- duration: 10000
- });
- // 显示成功提示
- var showSuccess = text => {
- wx.hideToast();
- wx.showToast({
- title: text,
- icon: 'success'
- });
- };
- // 显示失败提示
- var showModel = (title, content) => {
- wx.hideToast();
- wx.showModal({
- title,
- content: JSON.stringify(content),
- showCancel: false
- });
- };
- var showMsg = (title, content) => {
- wx.hideToast();
- wx.showModal({
- title,
- content: content,
- showCancel: false
- });
- };
- var busy=false;
- //获取应用实例
- var app = getApp()
- Page({
- data:{
- userInfo: {},
- loading:false,
- loading_more:false,
- next_data:true,
- busy:false,
- posts:[]
- },
- post_list:function(){
- var self = this;
- if(self.data.busy){
- return;
- }
- self.setData({
- loading:true,
- busy:true
- });
- var url;
- if(self.data.next_page_url==null){
- url=config.service.apiUrl+'timeline';
- }else{
- url=self.data.next_page_url
- }
- qcloud.request({
- // 要请求的地址
- url: url,
- success(result) {
- if(result.data.success==true){
- for (var i=0;i<result.data.posts.data.length;i++){
- var datestr=Date.parse((result.data.posts.data[i]['created_at']).toString().replace(/-/g, "/"));
- if(datestr){
- result.data.posts.data[i]['created_at']=new Date(datestr).format("hh:mm");
- }else{
- result.data.posts.data[i]['created_at']= '--:--'
- }
- }
- self.setData({
- posts:result.data.posts.data
- })
- }
- if(result.data.posts.next_page_url!==null){
- self.setData({
- next_data:true,
- next_page_url:result.data.posts.next_page_url
- })
- }else{
- self.setData({
- next_data:false,
- next_page_url:null
- })
- }
- console.log('request success', result);
- },
- fail(error) {
- console.log('request fail', error);
- },
- complete() {
- busy=false;
- self.setData({
- loading:false,
- busy:false
- })
- console.log('request complete');
- }
- });
- },
- load_more(){
- var self = this;
- var request_url=self.data.next_page_url;
- if(!request_url){
- return;
- }
- if(self.data.busy){
- return;
- }
- self.setData({
- loading:true,
- busy:true
- });
- qcloud.request({
- // 要请求的地址
- url: request_url,
- success(result) {
- if(result.data.success==true){
- var item=self.data.posts;
- var obj=result.data.posts.data;
- for (var i=0;i<obj.length;i++){
- var datestr=Date.parse((obj[i]['created_at']).toString().replace(/-/g, "/"));
- if(datestr){
- obj[i]['created_at']=new Date(datestr).format("hh:mm");
- }else{
- obj[i]['created_at']= '--:--'
- }
- item.push(obj[i]);
- }
- self.setData({
- posts:item
- });
- if(result.data.posts.next_page_url!==null){
- self.setData({
- next_data:true,
- next_page_url:result.data.posts.next_page_url
- })
- }else{
- self.setData({
- next_data:false,
- next_page_url:null
- })
- }
- }
- console.log('request success', result);
- },
- fail(error) {
- console.log('request fail', error);
- },
- complete() {
- self.setData({
- loading:false,
- busy:false
- })
- console.log('request complete');
- }
- });
- },
- onReachBottom: function() {
- var self = this;
- self.load_more();
- },
- onShareAppMessage: function () {
- return {
- title: '密频',
- path: 'pages/channel/index/index',
- success: function(res) {
- showSuccess('分享成功');
- console.log(res);
- // 分享成功
- },
- fail: function(res) {
- // 分享失败
- console.log(res);
- },
- complete:function(res){
- console.log(res);
- }
- }
- },
- onLoad:function(options){
- // 页面初始化 options为页面跳转所带来的参数
- },
- onReady:function(){
- // 页面渲染完成
- },
- onShow:function(){
- // 页面显示
- var self=this;
- if(busy){
- return;
- }
- self.setData({
- posts:[],
- loading:true,
- busy:false
- });
- busy=true;
- var self=this;
- app.getUserInfo(function(userInfo){
- self.post_list();
- });
- },
- onHide:function(){
- // 页面隐藏
- },
- onUnload:function(){
- // 页面关闭
- }
- })
|