xz_bailang/pages/newTeam/newTeam.js
2020-06-23 19:14:44 +08:00

316 lines
7.9 KiB
JavaScript

// pages/newTeam/newTeam.js
Page({
/**
* 页面的初始数据
*/
data: {
baseUrl: 'http://192.168.0.111:8888/volunteer/',
isAgree: false, // 是否同意志愿者注册协议
index: 0,
array: ['区域1','区域2','区域3','区域4'],
photo: '',
teamName: '',
count:'',
teamWatchword: '',
content: '',
tempObj: {}, // 用于临时存储字典表返回的数据
tempArray: [], // 用于临时存储字典表返回的数据
teamCityIndex: 0,
teamCityResult: '请选择服务区域',
teamCity: {}, // 服务区域-市
teamCityArray: [], // 服务区域-市
teamCountyIndex: 0,
teamCountyResult: '请选择旗县区',
teamCounty: {}, // 服务区域-旗县区
teamCountyArray: [], // 服务区域-旗县区
teamAreaIndex: 0,
teamAreaResult: '请选择乡镇村',
teamArea: {}, // 服务区域-乡镇村
teamAreaArray: [], // 服务区域-乡镇村
countyShowStatus: true,
areaShowStatus: true
},
getToken: function() {
var self = this;
return new Promise(resolve =>{
wx.getStorage({
key: 'token',
success(res) {
self.setData({
token: res.data
})
return resolve();
}
})
})
},
bindPickerChange: function (e) {
this.setData({
index: e.detail.value
})
},
/**
* 上传活动照片
*/
uploadPhoto: function () {
var self = this
wx.chooseImage({
success (res) {
const tempFilePaths = res.tempFilePaths
wx.uploadFile({
url: self.data.baseUrl + 'app/file/uploadimage',
filePath: tempFilePaths[0],
name: 'image',
header: {
'token': self.data.token
},
formData: {
'image': tempFilePaths[0]
},
success (res){
var data = res.data.substr(9, res.data.length);
data = data.substr(0, data.length - 2);
self.setData({
photo: data
})
},
fail: function(res) {
console.log(res)
}
})
}
})
},
cityChange: function(e) {
var self = this;
self.setData({
teamCityIndex: e.detail.value,
areaShowStatus: true,
teamArea: {},
teamAreaArray: []
})
this.getDataFromDict(self.data.teamCity[self.data.teamCityIndex].dictionaryId, 2).then(result => {
if(self.data.tempObj.length > 0) {
self.setData({
teamCounty: self.data.tempObj,
teamCountyArray: self.data.tempArray,
countyShowStatus: false,
tempObj: {},
tempArray: []
})
}else {
self.setData({
teamCounty: {},
teamCountyArray: [],
countyShowStatus: true,
tempObj: {},
tempArray: []
})
}
});
},
countyChange: function(e) {
var self = this;
self.setData({
teamCountyIndex: e.detail.value
})
this.getDataFromDict(self.data.teamCounty[self.data.teamCountyIndex].dictionaryId, 2).then(result => {
if(self.data.tempObj.length > 0) {
self.setData({
teamArea: self.data.tempObj,
teamAreaArray: self.data.tempArray,
areaShowStatus: false,
tempObj: {},
tempArray: []
})
}else {
self.setData({
teamArea: {},
teamAreaArray: [],
areaShowStatus: true,
tempObj: {},
tempArray: []
})
}
})
},
changeAgree: function (e) {
if(e.detail.value == ''){
this.setData({
isAgree: false
})
}else{
this.setData({
isAgree: true
})
}
},
submitRegister: function () {
var self = this;
if(!self.data.isAgree) {
wx.showToast({
title: '请阅读并同意《团队注册服务协议》',
icon: 'none',
duration: 1500
})
return false;
}
var teamVO = {
photo: self.data.photo,
teamName: self.data.teamName,
count:self.data.count,
teamWatchword: self.data.teamWatchword,
content: self.data.content,
agree: self.data.isAgree,
teamCity: self.data.teamCity[self.data.teamCityIndex] != ''
&& self.data.teamCity[self.data.teamCityIndex] != null
&& self.data.teamCity[self.data.teamCityIndex] != 'undefined' ?
self.data.teamCity[self.data.teamCityIndex].dictionaryId :
'',
teamCounty: self.data.teamCounty[self.data.teamCountyIndex] != ''
&& self.data.teamCounty[self.data.teamCountyIndex] != null
&& self.data.teamCounty[self.data.teamCountyIndex] != 'undefined' ?
self.data.teamCounty[self.data.teamCountyIndex].dictionaryId :
'',
teamArea: self.data.teamArea[self.data.teamAreaIndex] != ''
&& self.data.teamArea[self.data.teamAreaIndex] != null
&& self.data.teamArea[self.data.teamAreaIndex] != 'undefined' ?
self.data.teamArea[self.data.teamAreaIndex].dictionaryId :
'',
};
wx.request({
url: self.data.baseUrl + 'wxminiapp/team/saveteam',
method: 'POST',
header: {
'token': self.data.token
},
data: teamVO,
success(res) {
if(res.statusCode == 200) {
wx.showToast({
title: '注册成功!',
icon: 'none',
duration: 1500
})
wx.navigateTo({
url: '../volunteer/volunteer',
})
}else {
console.log('登陆失败!' + res.errMsg);
}
}
})
},
/**
*从字典表拉取数据
*/
getDataFromDict: function(dictId, type) {
var self = this;
return new Promise(resolve => {
wx.request({
url: self.data.baseUrl + 'app/datadictionary/listdictionarybyparentid/' + dictId,
header: {
'token': self.data.token
},
success: function(res) {
if('200' == res.statusCode) {
if(res.data || type === 2) {
var array = [];
for(var item of res.data) {
delete item.dictionaryParentId;
delete item.dictionaryParentName;
delete item.dictionarySummary;
delete item.dictionaryCode;
delete item.dictionarySort;
delete item.subDictionary;
delete item.parent;
array.push(item.dictionaryName);
}
self.setData({
tempArray: array
})
}
self.setData({
tempObj: res.data
})
}
return resolve();
}
})
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var self = this;
this.getToken().then(result => {
this.getDataFromDict('4a03e904-81e2-48e9-9006-e15ea4a6bc69', 2).then(result => {
self.setData({
teamCity: self.data.tempObj,
teamCityArray: self.data.tempArray,
tempObj: {},
tempArray: []
})
})
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})