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.
gitProject1/src/miniprogram/pages/profile/profile.js

173 lines
3.8 KiB

const db = wx.cloud.database().collection("user_Data");
const _ = wx.cloud.database().command
const app = getApp()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0' //默认头像地址
Page({
//页面数据
data:{
avatarUrl: defaultAvatarUrl,
nickname: "",
gender: "",
birthday: "",
region: "",
contact: "",
information: ""
},
onLoad(options) {
//检测是否有头像缓存
try {
var avatarUrl = wx.getStorageSync('avatarUrl')
if (avatarUrl) {
this.setData({
avatarUrl
})
}
} catch (e) {
console.log(e);
}
//获取用户信息
if (!app.globalData.USERID) {
db.add({
data: {
nickname: '',
gender: '',
birthday: '',
region: '',
contact: '',
information: ''
},
success: (res) => {
app.globalData.USERID = res._id
wx.setStorageSync('user_id', res._id)
console.log("[PROFILE] AddNewUser");
}
})
} else {
db.where({
_openid: app.globalData.OPENID
}).get({
success:(res) => {
var data = res.data[0]
this.setData({
nickname: data.nickname,
gender: data.gender,
birthday: data.birthday,
region: data.region,
contact: data.contact,
information: data.information
})
app.globalData.USERID = data._id
// app.globalData.ISLOGIN = true
console.log("[PROFILE] UserQuery", data);
}
})
}
},
//选择头像后存储头像地址
onChooseAvatar(e) {
var { avatarUrl } = e.detail
this.setData({
avatarUrl
})
// console.log(e.detail);
//存储头像地址到本地
try {
wx.setStorageSync('avatarUrl', avatarUrl)
} catch (e) {
console.log(e);
}
},
//表单提交方法
submit: function(e){
console.log("[PROFILE] SubmitData", e.detail.value);
//输入检测,检测输入是否为空
if(!this.cheakFrom(e.detail.value)) {
return
}
//往数据库里更新的方法
db.where({
_openid: app.globalData.OPENID
}).update({
//传入数据
data: {
nickname: this.data.nickname,
gender: this.data.gender,
birthday: this.data.birthday,
region: this.data.region,
contact: this.data.contact,
information: this.data.information
},
//成功后执行的方法
success:(res) => {
console.log("[PROFILE] SubmitDone",res)
wx.showToast({
title: '提交成功',
icon: 'success',
duration: 2000
})
}
})
},
//跳转至教师信息页面
navigateToPageTpro: function() {
wx.navigateTo({
url: '/pages/Tprofile/Tprofile',
})
},
//更新页面的生日信息
dateChange: function(e){
this.setData({
birthday: e.detail.value,
});
},
//更新页面的地区信息
regionChange: function(e){
this.setData({
region: e.detail.value,
});
},
//更新页面的性别信息
genderChange: function(e){
this.setData({
gender: e.detail.value,
});
},
cheakFrom: function(e) {
var hint;
if (e.nickname == "") {
hint = "昵称为空!"
} else if (e.gender == "") {
hint = "性别为空!"
} else if (e.birthday == "") {
hint = "年龄为空!"
} else if (e.region == "") {
hint = "地区为空!"
} else if (e.contact == "") {
hint = "联系方式为空!"
} else if (e.information == "") {
hint = "基本情况为空!"
} else{
return true
}
wx.showToast({
title: hint,
icon: 'error',
duration: 2000
})
return false
}
});