Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyzhang1992 committed Mar 12, 2019
1 parent cb3c5b6 commit 1385f67
Show file tree
Hide file tree
Showing 9 changed files with 804 additions and 759 deletions.
112 changes: 56 additions & 56 deletions pages/find/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ let findTimeOut = null;
let current_page = 1;
let last_page = 1;
Page({

/**
* 页面的初始数据
*/
data: {
motto: '古诗文小助手',
user_id: 0,
tags: ['科普','故事','问与答'],
tags: ['科普', '故事', '问与答'],
pins: [],
indicatorDots: true,
animationData:{},
animationData: {},
userInfo: wx.getStorageSync('user'),
topic: null
},
Expand All @@ -36,19 +36,19 @@ Page({
let that = this;
// 判断用户是否登录
if (that.data.user_id < 1) {
authLogin.authLogin('/pages/find/index','tab',app);
authLogin.authLogin('/pages/find/index', 'tab', app);
} else {
let _url = '/pages/find/new/index';
if(pin_id>0){
_url = _url +'?type=pin&id='+pin_id;
if (pin_id > 0) {
_url = _url + '?type=pin&id=' + pin_id;
}
wx.navigateTo({
url: _url
})
}
},
// 删除
deletePin: function(e){
deletePin: function (e) {
// console.log(e);
let that = this;
let id = e.target.dataset.id;
Expand All @@ -59,12 +59,12 @@ Page({
};
// 判断用户是否登录
if (that.data.user_id < 1) {
authLogin.authLogin('/pages/find/index','tab',app);
authLogin.authLogin('/pages/find/index', 'tab', app);
} else {
http.request(url,data).then(res=>{
if(res.data && res.succeeded){
http.request(url, data).then(res => {
if (res.data && res.succeeded) {
let pins = that.data.pins;
pins = pins.filter((item)=>{
pins = pins.filter((item) => {
return item.id != id;
});
wx.showToast({
Expand All @@ -76,7 +76,7 @@ Page({
that.setData({
pins: pins
})
}else{
} else {
console.log(res);
wx.showToast({
title: '删除失败',
Expand All @@ -91,37 +91,37 @@ Page({
}
},
// 详情页
pinDetail: (e)=>{
pinDetail: (e) => {
let id = e.currentTarget.dataset.id;
let type = e.currentTarget.dataset.type;
wx.navigateTo({
url: '/pages/find/detail/index?id='+id+'&type='+type
url: '/pages/find/detail/index?id=' + id + '&type=' + type
});
},
// 用户 pins 列表页
userPins: (e)=>{
userPins: (e) => {
let id = e.currentTarget.dataset.id;
wx.navigateTo({
url: '/pages/find/user/index?id=' + id
});
},
// 鼓掌
pinLike: function(e){
pinLike: function (e) {
let id = e.currentTarget.dataset.id;
let user_id = wx.getStorageSync('user') ? wx.getStorageSync('user').user_id : 0;
let wx_token = wx.getStorageSync('wx_token');
let pins = this.data.pins;
let that = this;
let url = app.globalData.url+'/wxxcx/pin/'+id+'/like';
let url = app.globalData.url + '/wxxcx/pin/' + id + '/like';
if (that.data.user_id < 1) {
authLogin.authLogin('/pages/find/index','tab',app);
authLogin.authLogin('/pages/find/index', 'tab', app);
} else {
http.request(url,{
user_id:user_id,
wx_token:wx_token
}).then(res=>{
if(res.data && res.succeeded){
if(res.data && res.data.status=='active'){
http.request(url, {
user_id: user_id,
wx_token: wx_token
}).then(res => {
if (res.data && res.succeeded) {
if (res.data && res.data.status == 'active') {
pins.map((item, index) => {
if (item.id == id) {
item.like_count = item.like_count + 1;
Expand All @@ -134,7 +134,7 @@ Page({
that.setData({
pins: pins
})
}else if(res.data.status =='delete'){
} else if (res.data.status == 'delete') {
pins.map((item, index) => {
if (item.id == id) {
item.like_count = item.like_count - 1;
Expand All @@ -147,10 +147,10 @@ Page({
that.setData({
pins: pins
})
}else{
} else {
http.loadFailL(res.data.msg);
}
}else{
} else {
http.loadFailL();
}
}).catch(error => {
Expand All @@ -168,41 +168,41 @@ Page({
wx.showLoading({
title: '加载中',
});
let url = app.globalData.url+'/wxxcx/getRecentTopic';
http.request(url,undefined).then(res=>{
if(res.data && res.succeeded){
let url = app.globalData.url + '/wxxcx/getRecentTopic';
http.request(url, undefined).then(res => {
if (res.data && res.succeeded) {
that.setData({
topic: res.data
});
that.getPins(1);
wx.hideLoading();
}else{
} else {
http.loadFailL();
}
}).catch(error => {
console.log(error);
http.loadFailL();
});
},
getPins: function(page){
getPins: function (page) {
let that = this;
let user_id = wx.getStorageSync('user') ? wx.getStorageSync('user').user_id : 0;
wx.showNavigationBarLoading();
let data = {
page: page,
user_id: user_id
};
http.request(app.globalData.url+'/wxxcx/getPins',data).then(res=>{
if(res.data && res.succeeded){
that.setData({
pins: page >1 ? [...that.data.pins, ...res.data.data] : res.data.data
});
current_page = res.data.current_page;
last_page = res.data.last_page;
wx.hideNavigationBarLoading();
}else{
http.loadFailL();
}
http.request(app.globalData.url + '/wxxcx/getPins', data).then(res => {
if (res.data && res.succeeded) {
that.setData({
pins: page > 1 ? [...that.data.pins, ...res.data.data] : res.data.data
});
current_page = res.data.current_page;
last_page = res.data.last_page;
wx.hideNavigationBarLoading();
} else {
http.loadFailL();
}
}).catch(error => {
console.log(error);
http.loadFailL();
Expand All @@ -212,9 +212,9 @@ Page({
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {

},

/**
* 生命周期函数--监听页面显示
*/
Expand All @@ -228,32 +228,32 @@ Page({
timingFunction: "ease",
delay: 0
});
animation.scale(1.3,1.3).step();
animation.scale(1.3, 1.3).step();
this.setData({
animationData: animation.export()
});
findTimeOut = setTimeout(function () {
animation.scale(1,1).step();
animation.scale(1, 1).step();
this.setData({
animationData: animation.export()
})
}.bind(this), 500)
},

/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
clearTimeout(findTimeOut);
},

/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
clearTimeout(findTimeOut);
},

/**
* 页面相关事件处理函数--监听用户下拉动作
*/
Expand All @@ -262,20 +262,20 @@ Page({
current_page = 1;
wx.stopPullDownRefresh();
},

/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
if(current_page+1>last_page){
if (current_page + 1 > last_page) {
return false;
}
wx.showNavigationBarLoading();
// Do something when page reach bottom.
this.getPins(current_page+1);
this.getPins(current_page + 1);
current_page++;
},

/**
* 用户点击右上角分享
*/
Expand All @@ -284,11 +284,11 @@ Page({
title: '发现',
path: '/pages/find/index',
// imageUrl:'/images/poem.png',
success: function(res) {
success: function (res) {
// 转发成功
console.log('转发成功!')
},
fail: function(res) {
fail: function (res) {
// 转发失败
}
}
Expand Down
Loading

0 comments on commit 1385f67

Please sign in to comment.