wx-app/pages/user/index.js
2019-06-27 23:30:34 +08:00

245 lines
7.1 KiB
JavaScript

// pages/user/index.js
import allCities from './../../utils/allCities.js';
import api from './../../utils/api.js';
const medicalList = [
{ checked: false, text: '慢性支气管炎、支气管哮喘、支气管扩张病、肺心病' },
{ checked: false, text: '高血压、冠心病、心肌病、先天性心脏病、风湿性心脏病' },
{ checked: false, text: '反流性食道炎、慢性胃炎、胃溃疡、慢性胰腺炎、肠易激惹综合征、结肠炎' },
{ checked: false, text: '3个月内脑梗塞和/或脑出血、癫痫、脑炎、脑膜炎' },
{ checked: false, text: '特发性或继发性肺动脉高压症' },
{ checked: false, text: '其他疾病' }
];
const smokeList = [
{ checked: false, text: '不吸烟' },
{ checked: false, text: '10支/天以下' },
{ checked: false, text: '10~20支/天' },
{ checked: false, text: '20支/天以上' }
];
const cacheData = {
key:'',
value:''
};
const app =getApp();
Page({
/**
* 页面的初始数据
*/
data: {
allCities,
userInfo: {
avatarUrl: '',
address: '北京 东城区',
realname: '',
},
subjects: {
smoke: [],
medical_histories: []
},
provinces: allCities,
cities: allCities[0].children,
province:0,
city:0,
showRecord: true,
dialog: {
title: '姓名',
default: '请输入',
show: false,
value: '',
key:null
},
userDetail: {},
dataList: [],
collapseTags: []
},
onLoad: function() {
this.initUserData();
},
/**
* 初始化用户数据
*/
async initUserData() {
let d = app.globalData.userInfo.detail;
medicalList.forEach((o, i) => {
medicalList[i].checked = d.medical_history.indexOf(i) != -1
})
smokeList[d.smoke].checked = true;
this.setData({
userDetail: d,
userInfo: app.globalData.userInfo,
subjects: {
smoke: smokeList,
medical_histories: medicalList
}
});
let evaluationData = await api.queryAllEvaluation();
var tags = [];
evaluationData.forEach((o, i) => {
tags.push(true);
});
this.setData({
dataList: evaluationData,
collapseTags: tags
});
},
parseUserAddress(province,city){
if (province && province > 0){
for(var i = 0;i < allCities.length;i++){
if(allCities[i].code == province){
if (!city || city < 1){
return allCities[i].name;
}
for (var j = 0; j < allCities[i].children.length;j ++){
if(allCities[i].children[j].code == city){
return allCities[i].name + ' ' + allCities[i].children[j].name;
}
}
}
}
}
return '未知';
},
switchView() {
this.setData({
showRecord: !this.data.showRecord
})
},
toggleCollapse(e) {
console.log(e);
let index = e.currentTarget.dataset.index;
this.data.collapseTags[index] = !this.data.collapseTags[index];
this.setData({
collapseTags: this.data.collapseTags
});
},
/* 输入框相关样式 */
bindDialogInput: function(e) {
this.setDialog('value',e.detail.value)
},
async updateUserDetail(key,value){
try{
let data = {};
data[key] = value
await api.updateUserDetail(data)
this.setUserDetail(key,value);
wx.setStorageSync('refresh-userinfo', true)
}catch(e){
console.log('update error',e);
}
},
setUserDetail(key, value) {
let d = this.data.userDetail;
d[key] = value;
this.setData({ userDetail: d });
},
setDialog(key,value){
/*
title: '姓名',
default: '请输入',
show: false,
value: '',
key:null
*/
let d = this.data.dialog;
d[key] = value;
this.setData({dialog:d});
},
hideInputDialog() {
this.setDialog('show',false);
},
checkInputDialog(){
let data = this.data.dialog.value
if(!data){
wx.showToast({
title: '请填写内容后确定',icon:'none'
})
return;
}
this.hideInputDialog();
this.updateUserDetail(this.data.dialog.key,data);
},
showModal(e) {
this.setData({
modalName: e.currentTarget.dataset.target
})
},
hideModal(e) {
this.setData({
modalName: null
})
},
async updateAddress() {
let { province,city,userDetail} = this.data;
let privinceCode = allCities[province].code,
cityCode = allCities[province].children[city].code,
address = allCities[province].name + ' ' + allCities[province].children[city].name;
this.hideModal();
if (privinceCode == userDetail.province && cityCode == userDetail.city){
console.log('not modify');
}else{
// 更新地址
await api.updateUserDetail({
province:privinceCode,
city:cityCode,
address
})
this.setUserDetail('province', privinceCode);
this.setUserDetail('city', cityCode);
this.setUserDetail('address', address);
}
},
// 城市选择
bindCityChange(e) {
let data = e.detail.value;
let province = data[0],city = data[1]
this.setData({
cities: allCities[province].children,
province,city
})
},
// 显示可输入的值
showAlert(e) {
let key = e.currentTarget.dataset.key;
let title = e.currentTarget.dataset.title;
let value = this.data.userDetail[key];
this.setData({
dialog:{
default: '请输入' + title,
show: true,
title,value,key
}
})
},
noTap() { },
bindSelectChange(e){
cacheData.key = e.currentTarget.dataset.key;
cacheData.value = e.detail.value;
if (Array.isArray(cacheData.value)){
cacheData.value.forEach((v,i)=>{
cacheData.value[i] = parseInt(v);
})
}else{
cacheData.value = parseInt(cacheData.value);
}
console.log(cacheData);
},
// 更新数据
async updateSelectData(e){
this.hideModal();
if(!cacheData.key){
return;
}
cacheData.value = Array.isArray(cacheData.value) ? cacheData.value.join(',') : cacheData.value;
var data = {};
data[cacheData.key] = cacheData.value;
await api.updateUserDetail(data)
this.setUserDetail(cacheData.key, cacheData.value);
}
})