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.

334 lines
9.5 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.

import {throttle} from "../../../js/utils";
const app = getApp();
var locationData = require("./data.js");
const risk_message = "检测到名称中含有敏感词汇";
function msgSecCheck({name, nickname}){
let content = name+","+nickname;
console.log("secCheck", content);
if(this.tmp_promise&&this.tmp_content==content)
return this.tmp_promise;
this.tmp_promise = app.openapi("security.msgSecCheck")({content});
this.tmp_content = content;
this.tmp_promise.then(res=>{
if(res.errCode==87014){
wx.showToast({
title: risk_message,icon:"none",duration:2600
})
}
}).catch(e=>{
this.tmp_promise = null;
})
return this.tmp_promise;
}
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);
});
},
msgSecCheck,
throttledMsgSecCheck: throttle(msgSecCheck, 1000, {}),
updateNickName({detail:{value}}){
this.setData({nickname: value});
this.throttledMsgSecCheck(this.data);
},
updateName({detail:{value}}){
this.setData({name: value});
this.throttledMsgSecCheck(this.data);
},
onGetUserInfo(e){
// 填入微信个人信息
console.log(e);
let {detail:{userInfo}} = e;
if(!userInfo)
return wx.showToast({
title: '获取信息失败',icon:"none"
})
let {nickName:nickname, gender, province:location, city:location_city, avatarUrl} = userInfo;
if(gender)
gender = 1 - gender;
let location_index = this.data.locations.indexOf(location);
let cities = locationData[location] || locationData['北京'];
let city_index = cities.indexOf(location_city);
this.setData({nickname, gender, cities, location, location_city, city_index, location_index});
avatarUrl = "" //@todo
if(avatarUrl){
wx.showModal({
title:"提示",
content:"是否使用微信头像",
confirmText:"使用",
success: res=>{
if(res.confirm)
this.updateWechatAvatar({avatarUrl});
},
complete: ()=>{
wx.showToast({
title: '填入成功'
});
}
});
}else{
wx.showToast({
title: '填入成功'
});
}
},
updateWechatAvatar({avatarUrl}){
// @todo
},
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(){
/*wx.showActionSheet({
itemList: ["更改头像", "使用微信头像"],
})*/
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){
wx.showLoading({
title: '提交中'
});
value.gender = parseInt(value.gender);
this.msgSecCheck(value).then(res=>{
if(res.errCode==87014){
wx.hideLoading();
wx.showModal({
content: risk_message,showCancel:false
})
}else{
app.api("users.accounts", { method: "PUT" })(value)
.then(res => {
app.syncUser({refresh:1});//更新用户信息
res.message = "更新成功";
wx.hideLoading();
app.showMsg(res);
setTimeout(() => {
wx.navigateBack({
delta: 1
})
}, 420);
}).catch(e => {
wx.hideLoading();
this.showError(e);
})
}
}).catch(e=>{
wx.hideLoading();
wx.showToast({
title: '信息检查时出错了\n︿',icon:"none"
});
throw 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);
}
}
})