You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
6.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

const app = getApp();
var locationData = require("./data.js");
Page({
data: {
eduImgDir:global.config.eduImgDir,
locations:[],
cities:[],
identities:[{value:"teacher", text:"教师"},{value:"student",text:"学生"},{value:"professional", text:"专业人士"}],
technicalTitles:{
0:["教授","副教授","讲师","助教"],
2:["企业管理者","部门管理者","高级工程师","工程师","助理工程师"]
}
},
refresh(){
app.api("users.accounts")().then(res=>{
this.originInfo = res;
res.authen = res.authentication == "certified";
res.pro_authen = res.professional_certification == "certified";
this.setData(res);
this.setInfo(res);
}).catch(e=>{
this.showError(e);
});
},
setInfo(res){
let locations = Object.keys(locationData);
let location_index = locations.indexOf(res.location);
let cities = locationData[res.location] || locationData['北京'];
let city_index = cities.indexOf(res.location_city);
let identity_index = -1;
let { identities } = this.data;
for (var i = 0; i < identities.length; i++) {
if (identities[i].value == res.identity) {
identity_index = i;
break;
}
}
var technical_index = -1;
if (identity_index == 0 || identity_index == 2) {
let technicals = this.data.technicalTitles[identity_index];
for (var i = 0; i < technicals.length; i++) {
if (technicals[i] == res.technical_title) {
technical_index = i;
break;
}
}
}
this.setData({ locations, cities, location_index, city_index, identity_index, technical_index });
if (res.school_id) {
this.changeSchoolDepartment(res);
}
},
updateAccount(data){
app.api("users.accounts",{method:"PUT"})(data).then(res=>{
app.showMsg(res);
setTimeout(res=>{
wx.navigateBack({
delta:1
});
},400)
}).catch(e=>{
this.showError(e);
})
},
onGenderChange({detail:{value}}){
value = parseInt(value);
this.setData({gender:value});
},
onLocationChange(e){
let {detail:{value:[location_index, city_index]}} = e;
this.setData({location_index, city_index});
this.setData({cities:locationData[this.data.locations[location_index]]});
},
onCitiesChange(e){
let {detail:{value,column}} = e;
if(column==0)
this.setData({cities:locationData[this.data.locations[value]],location_index:value ,city_index:-1});
},
onIdentityChange(e){
let {detail:{value}} = e;
this.setData({identity_index:value, technical_index:-1});
},
onTechnicalChange(e){
;
let { detail: { value } } = e;
this.setData({technical_index: value});
},
onDepartmentChange(e){
;
let { detail: { value } } = e;
this.setData({department_index:value});
},
refreshDepartments({detail}){
;
this.setData({department_id: detail.id});
this.pullDepartments();
},
pullDepartments(){
;
let {school_id, department_id} = this.data;
app.api("schools.departments.for_option")({school_id})
.then(res=>{
let {departments} = res;
let department_index = -1;
for(var i=0; i<departments.length;i++){
if(departments[i].id==department_id){
department_index = i;
break;
}
}
this.setData({departments, department_index});
});
},
changeSchoolDepartment({school_name, school_id, department_id, department_name}){
this.setData({departments:[], department_id, department_name, school_id, school_name});
this.pullDepartments({school_id,department_id});
},
catchAvatar(){
app.navigateTo({url:"{image_crop}"});
},
addDepartment(){
this.setData({showAddDepartment:true});
},
onLoad: function (options) {
this.refresh();
},
onShow: function () {
if(this.secondShow){
app.api("users.accounts")()
.then(res=>{
let {avatar_url} = res;
this.setData({avatar_url});
})
} //@todo: better solution?
this.secondShow = 1;
let data = wx.getStorageSync("SET-SCHOOL-DEPARTMENT-KEY");
if(data){
this.changeSchoolDepartment(data);
wx.setStorageSync("SET-SCHOOL-DEPARTMENT-KEY",'');
}
},
checkProInfo(value){
let info = this.originInfo;
;
if (info.identity != value.identity)
return true;
if(info.identity=='student'){
if(info.student_id!=value.student_id)
return true;
}else{
if (info.technical_title!=value.technical_title)
return true;
}
if(info.school_id!=value.school_id||info.department_id!=value.department_id)
return true
return false;
},
onSubmit(e){
let {detail:{value}} = e;
;
if(!this.checkInput(value)) return;
if(this.data.pro_authen&&this.checkProInfo(value)){
wx.showModal({
title: '确认修改',
content: '您修改了职业信息,保存后认证信息会清空,需重新进行职业认证,\n是否保存',
confirmText:"保存",
success:res=>{
if(res.confirm)
this.saveInfo(value);
}
});
}else
this.saveInfo(value);
},
checkInput(value){
var showTip = key=>{
this.showError({ message: `请填写${key}`});
return false;
}
if(!value.nickname)
return showTip("昵称");
if(!value.name)
return showTip("姓名");
if(value.gender==null)
return showTip("性别");
if (!value.location)
return showTip("所在地");
if (!value.location_city)
return showTip("所在地");
if(!value.identity)
return showTip("职业");
if(value.identity=='student'){
if(!value.student_id)
return showTip("学号");
}else{
if (!value.technical_title)
return showTip("职称");
}
if(!value.school_id)
return showTip("学校/单位");
if (!value.department_id)
return showTip("院系/部门");
return true;
},
saveInfo(value){
value.gender = parseInt(value.gender);
app.api("users.accounts", { method: "PUT" })(value)
.then(res => {
app.syncUser({refresh:1});
res.message = "更新成功";
app.showMsg(res);
;
setTimeout(() => {
wx.navigateBack({
delta: 1
})
}, 420);
}).catch(e => {
this.showError(e);
})
},
onTap(e){
;
let { target: { id } } = e;
if (id) {
if ((id == 'name' || id == "gender") && this.data.authen)
this.showError({ message: `请重新实名认证以更改${id == "name" ? "姓名" : id == 'gender' ? "性别" : '此信息'}`, during: 1400 });
this.setData({edited:1});
}
},
showError(e){
let {message:error, during=2000} = e;
if(error){
error = error.replace(/\n/g, "");
this.setData({showError:1, error});
setTimeout(()=>{
this.setData({showError:0});
}, during);
}
},
onUnload: function () {
}
})