xz_dingri/pages/index/index.js
2020-07-02 19:03:29 +08:00

352 lines
8.2 KiB
JavaScript

var app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
randomName: '',
showPhone: true,
gotCode: false,
restTime: 120,
timer: '',
indicatorDots: true,
autoplay: true,
token: '',
library: {
libraryUrl: app.libraryUrl,
bannerLibrary: [],
},
news: {
newsUrl: app.newsUrl,
newsList: []
},
venue: {
venueUrl: app.venueUrl,
venueList: []
},
activity: {
activityUrl: app.activityUrl,
activityList: []
},
live: {
liveUrl: app.liveUrl,
liveList: []
},
usercenterUrl: app.usercenterUrl
},
doLogin: function () {
var self = this;
wx.login({
success(res) {
if (res.code) {
app.restAjax.post(app.restAjax.path('{url}/wxminiapp/auth/login', [app.loginUrl]), {
jsCode: res.code
}, null, function (code, data) {
var tokenArray = data.data.split('_');
var token = tokenArray[0];
var isRandomUsername = tokenArray[1];
self.setData({
token: token
})
wx.setStorageSync('token', token);
wx.setStorageSync('isRandomUsername', isRandomUsername)
}, function (code, data) {
app.dialog.msg(data.msg);
});
} else {
app.dialog.msg('登录失败!'+ res.errMsg);
}
}
})
},
doGetLibrary: function() {
var self = this;
app.restAjax.get(app.restAjax.path('{libraryUrl}/app/intangiblelibrary/listpageintangiblelibraryrelease', [self.data.library.libraryUrl]), {
page: 1,
rows: 5
}, null, function(code, data) {
self.setData({
'library.bannerLibrary': data.rows
});
}, function(code, data){
app.dialog.msg(data.msg);
});
},
doGetVenueList: function() {
var self = this;
app.restAjax.get(app.restAjax.path('{venueUrl}/app/venuesinfo/listpagevenuesinforelease', [self.data.venue.venueUrl]), {
page: 1,
rows: 3
}, null, function(code, data) {
self.setData({
'venue.venueList': data.rows
});
}, function(code, data) {
app.dialog.msg(data.msg);
});
},
doGetActivityList: function() {
var self = this;
app.restAjax.get(app.restAjax.path('{activityUrl}/app/activitylibrary/listpageactivitylibraryrelease', [self.data.activity.activityUrl]), {
page: 1,
rows: 3
}, null, function(code, data) {
self.setData({
'activity.activityList': data.rows
});
}, function(code, data) {
app.dialog.msg(data.msg);
});
},
doGetNewsList: function() {
var self = this;
app.restAjax.get(app.restAjax.path('{newsUrl}/app/newscontent/listpagenewscontentrelease', [self.data.news.newsUrl]), {
page: 1,
rows: 5
}, null, function(code, data) {
self.setData({
'news.newsList': data.rows
})
}, function(code, data) {
app.dialog.msg(data.msg);
});
},
doGetLiveList: function() {
var self = this;
app.restAjax.get(app.restAjax.path('{liveUrl}/app/liveplan/listpageliveplanrelease', [self.data.live.liveUrl]), {
page: 1,
rows: 4
}, null, function(code, data) {
self.setData({
'live.liveList': data.rows
})
}, function(code, data) {
app.dialog.msg(data.msg);
});
},
goNewsDetail: function (event) {
wx.navigateTo({
url: '../newsDetail/newsDetail?templateRecordUrl='+ event.currentTarget.dataset.templateRecordUrl,
})
},
goNewsList: function() {
wx.navigateTo({
url: '../newsList/newsList',
})
},
goVolunteer: function () {
wx.navigateTo({
url: '../volunteer/volunteer',
})
},
goBroadcast: function () {
wx.navigateTo({
url: '../broadcast/broadcast',
})
},
goVenue: function () {
wx.switchTab({
url: '../venue/venue',
})
},
goBroadcastDetail: function (event) {
wx.navigateTo({
url: '../broadcastDetail/broadcastDetail?livePlanId='+ event.currentTarget.dataset.livePlanId,
})
},
goActivity: function () {
wx.switchTab({
url: '../activity/activity',
})
},
goHeritage: function () {
wx.switchTab({
url: '../heritage/heritage',
})
},
// 获取输入的手机号
phoneNum: function (res) {
this.setData({
phone: res.detail.value
})
console.log(this.data.phone)
},
// 校验手机号
testPhone: function () {
var self = this
if (self.data.phone) {
if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(self.data.phone)) {
wx.showToast({
title: '请输入正确的手机号',
icon: 'none',
duration: 1500
})
} else {
self.getCode()
}
} else {
wx.showToast({
title: '手机号不能为空',
icon: 'none',
duration: 1500
})
}
},
// 获取验证码
getCode: function () {
var self = this
app.restAjax.get(app.restAjax.path('{usercenterUrl}/api/sms/getverificationcode/' + self.data.phone, [app.usercenterUrl]), {}, null,
function (code, data) {
if (code == 200) {
console.log(data)
self.setData({
gotCode: true
})
self.data.timer = setInterval(function () {
var cur = self.data.restTime - 1
console.log(cur)
if (cur == 0) {
clearInterval(self.data.timer)
self.setData({
restTime: 120,
gotCode: false
})
}
self.setData({
restTime: cur
})
}, 1000)
}
},
function (code, data) {
console.log(data)
wx.showToast({
title: data.msg,
icon: 'none',
duration: 1500
})
} )
},
// 提交绑定手机
submitPhone: function () {
var self = this
app.restAjax.put(app.restAjax.path('{usercenterUrl}/app/user/updateminiappdefaultusername', [app.usercenterUrl]), {
phone: self.data.phone,
verificationCode: self.data.code
}, {
headers: {
token: self.data.token
}
}, function (code, data) {
console.log(data)
wx.setStorageSync('token', data.data);
wx.setStorageSync('isRandomUsername', 0)
clearInterval(self.data.timer)
self.setData({
showPhone: false,
restTime: 120
})
self.getUserInfo()
}, function (code, data) {
console.log(data)
})
},
// 验证码
inputCode: function (e) {
this.setData({
code: e.detail.value
})
},
// 判断用户是否为随机名称
isRandomName: function () {
var self = this
wx.getStorage({
key: 'isRandomUsername',
success: function (res) {
if (res.data == 1) {
wx.hideTabBar()
self.setData({
randomName: true
})
}
}
})
},
// 获取用户信息
getUserInfo: function () {
var self = this
app.restAjax.get(app.restAjax.path('{usercenterUrl}/app/user/getappuser', [app.usercenterUrl]), {}, {
headers: {
token: self.data.token
}
}, function (code, data) {
self.setData({
userInfo: data
})
}, function (code, data) {
console.log(data)
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var self = this;
self.doLogin();
self.doGetLibrary();
self.doGetNewsList();
self.doGetLiveList();
self.doGetVenueList();
self.doGetActivityList();
self.isRandomName();
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})