// pages/student/student.js
const app = getApp()
const util = require("../../utils/util")
const urlDef = require("../../utils/urls")
Page({
/**
* 页面的初始数据
*/
data: {
height: 0,
animation: {},
left: 750,
shareImg: '/images/student-share.jpg',
inviteConfig: {},
count: 0,
enroll: 0,
coin: '0',
invitations: [],
showShareImg: false,
showCodeImg: false,
posterConfig: {
'width': 700,
'height': 900,
'backgroundColor': '#fff',
'debug': false,
'preload': true,
'hide-loading': true,
images: [{
x: 0,
y: 0,
url: '/images/student-share.jpg',
width: 700,
height: 900,
zIndex:1,
}, {
x: 460,
y: 670,
url: '',
width: 150,
height: 150,
zIndex: 2
}],
},
qrCodeImg: '',
sid: null,
oid: null,
},
onPosterSuccess(e) {
let that = this;
let {
detail
} = e;
wx.saveImageToPhotosAlbum({
filePath: detail,
success(res) {
wx.showToast({
title: '图片已保存到本地相册',
icon: 'none'
})
that.setData({
showShareImg: false
})
that.triggerEvent("callMethod")
}, fail(res) {
console.log(res)
that.getSetting()
}
})
},
getSetting: function () {
// 相册授权
wx.getSetting({
success(res) {
// 进行授权检测,未授权则进行弹层授权
if (!res.authSetting["scope.writePhotosAlbum"]) {
wx.showModal({
title: '提示',
content: '您未授权相册使用权限,是否重新授权?',
success: function (res) {
if (res.confirm) {
wx.openSetting({
success(settingdata) {
if (settingdata.authSetting["scope.writePhotosAlbum"]) {
console.log("获取权限成功,再次点击图片保存到相册")
} else {
console.log("获取权限失败")
}
}, fail(res) {
console.log(res)
}
})
}
}
})
}
},
fail(res) {
console.log(res);
}
});
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
if (options.sid && options.oid) {
this.setData({ sid: options.sid, oid: options.oid })
} else {
let stu = wx.getStorageSync('student');
this.setData({ sid: stu.studentId, oid: stu.orgId })
}
wx.getSystemInfo({
success: (result) => {
this.setData({
height: result.windowHeight
})
},
})
this.queryInvitation(null);
let t = this;
setTimeout(() => {
t.doSearch();
}, 1000);
},
showCodeImg: function () {
this.setData({
showCodeImg: true
})
},
closeImg: function () {
this.setData({
showShareImg: false,
showCodeImg: false
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
return {
title: '邀请有礼',
path: '/pages/student/student?sid=' + this.data.sid + '&oid=' + this.data.oid,
image: this.data.qrCodeImg
}
},
queryInvitation: function (methodName) {
let urls = urlDef.urls
let inviteConfig = {}
util.apiPost(urls.get_activity_share + '&q.orgId=' + this.data.oid).then((rs) => {
if (rs && rs.length > 0) {
let r = rs[0];
inviteConfig.title = (r.name ? r.name : '邀请有礼');
inviteConfig.description = (r.remark ? r.remark : '邀请新学员,赢得奖励');
inviteConfig.imageUrl = r.imgUrl;
if (r.activityUrl && r.activityUrl.length > 0) {
inviteConfig.url = r.activityUrl + (r.activityUrl.indexOf('?') === -1 ? '?' : '&') + 'studentId=' + this.data.sid + '&orgId=' + this.data.oid;
}
let qrCodeImg = urls.get_qr_code + '?content=' + encodeURIComponent(inviteConfig.url)
this.setData({ 'posterConfig.images[1].url': qrCodeImg })
this.setData({ qrCodeImg: qrCodeImg })
this.setData({ inviteConfig: inviteConfig })
if ('billShare' === methodName) {
this.billShare();
}
if ('share' === methodName) {
this.share();
}
if ('codeInvite' === methodName) {
this.codeInvite();
}
}
});
},
doSearch: function () {
let urls = urlDef.urls
util.apiPost(urls.get_invitation_count + '&q.introducer=' + this.data.sid).then(rs => {
if (rs) {
this.setData({ count: rs.num })
}
});
util.apiPost(urls.get_invitation_enroll + '&q.introducer=' + this.data.sid).then(rs => {
if (rs) {
this.setData({ enroll: rs.num })
}
});
util.apiPost(urls.get_invitation_coin + '&q.introducer=' + this.data.sid).then(rs => {
if (rs && rs.length > 0) {
let i = 0;
let coin = ''
rs.forEach(r => {
coin += (r.amount + '' + (r.type === 1 ? '元' : '课时') + '');
if (i < rs.length - 1) {
coin += '/';
}
i++;
});
this.setData({ coin: coin })
}
});
util.apiPost(urls.query_invitation_list + '&q.introducer=' + this.data.sid).then(rs => {
if (rs) {
rs.map(o => {
o.name = o.studentName ? o.studentName : o.clientName
o.statusText = this.getStatus(o)
o.face = this.getHead(o)
})
this.setData({ invitations: rs })
}
});
},
getHead(item) {
let urls = urlDef.urls
return item.imageUrl ? urls.oss_file + 'image/' + item.imageUrl : '/images/head.png';
},
getStatus(item) {
if (item.orderCount > 0) { // 有已付款的订单
return '已报名';
} else {
if (item.isAudition === 0) { // 未试听
return '已注册';
}
return '已试听';
}
},
billShare: function () {
if (this.data.inviteConfig.url && this.data.inviteConfig.url.length > 4) {
this.setData({
showShareImg: true
})
} else {
this.queryInvitation('billShare');
}
},
codeInvite: function () {
if(!this.data.showCodeImg){
this.setData({
showCodeImg: true
})
} else {
this.setData({
showCodeImg: false
})
}
}
})