// nova-werun/pages/my/my-profile/index.js const Parse = getApp().Parse; const company = getApp().globalData.company; Page({ /** * 页面的初始数据 */ data: { //屏幕高度 statusBarHeight: 0, // 状态栏高度 screenHeight: 0, // 屏幕高度 customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮) bottomNavHeight: 0, // 底部导航栏高度 contentHeight: 0, // 可用内容高度 contentpadding: 0, //顶部padding高度 User1List: [], actions: [{ name: '男', }, { name: '女', }, ], avatar: null, sex: '男', nickname: null, height: null, // 选择部门 mainActiveIndex: 0, activeId: null, items: [], //显示遮罩层 show: false, selectname: '', selectobjectid: '', upiobjectid: '', // 图片 fileList: [], uptokenURL: '', domain: '', uploadURL: '', }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { // 计算 const systemInfo = wx.getSystemInfoSync(); const statusBarHeight = systemInfo.statusBarHeight || 0; const screenHeight = systemInfo.screenHeight || 0; const custom = wx.getMenuButtonBoundingClientRect(); const customHeight = custom.height + 10 + 2 || 0; const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0; const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth; const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth; this.setData({ statusBarHeight, screenHeight, customHeight, bottomNavHeight, contentHeight, contentpadding }); this.getname() this.getdep() this.getDepartment() this.getUptoken() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, //获取身高 getHeight(e) { let height = e.detail.value; // 身高范围在 50 到 250 之间的正则表达式,允许浮点数 let a = /^(?:[5-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|250)(\.\d)?$/; if (!height.match(a)) { wx.showToast({ icon: "none", title: "请填写正确的身高格式", }); return; } else { this.setData({ height: height }); console.log(this.data.height); } }, //点击清空 clearHeight() { this.setData({ height: null }) console.log('清空', this.data.height); }, //获取头像名称 async getname() { const currentUser = Parse.User.current(); let Userquery = new Parse.Query('_User'); Userquery.equalTo('company', company); Userquery.equalTo('objectId', currentUser.id); Userquery.notEqualTo('isDeleted', true) let P2 = await Userquery.find(); let User1List = P2.map(item => item.toJSON()); this.setData({ User1List }) this.setData({ avatar: User1List[0].avatar, nickname: User1List[0].nickname, sex: User1List[0].sex || '男', }) console.log(this.data.User1List); }, //获取部门 async getdep() { const currentUser = Parse.User.current(); let Userquery = new Parse.Query('Profile'); Userquery.equalTo('company', company); Userquery.equalTo('user', currentUser.id); // Userquery.equalTo('isCheck', true); Userquery.notEqualTo('isDeleted', true) Userquery.include('center'); let P2 = await Userquery.find(); let dep = P2.map(item => item.toJSON()); if (dep) { if (dep[0].isCheck == true) { this.setData({ selectname: dep[0].center.name, selectobjectid: dep[0].center.objectId, upiobjectid: dep[0].department.objectId }) } else { wx.showToast({ title: '您之前提交信息在审核当中', icon: 'none', duration: 2000 }) } } console.log('getdep', dep); }, onSelect(event) { let { name } = event.detail this.setData({ sex: name, show2: false }) console.log(this.data.sex); }, showSelect() { this.setData({ show2: true }) }, //修改名字 changenickname(e) { this.setData({ nickname: e.detail }) console.log(this.data.nickname); }, //上传信息 async setinfo() { if (!this.data.nickname || !this.data.sex || !this.data.selectobjectid || !this.data.avatar) { wx.showToast({ title: '请输入相关内容', icon: 'none', duration: 2000 }) return } const currentUser = Parse.User.current(); let Userquery = new Parse.Query('_User'); Userquery.equalTo('company', company); Userquery.equalTo('objectId', currentUser.id); Userquery.notEqualTo('isDeleted', true) let user = await Userquery.first(); user.set('sex', this.data.sex) user.set('avatar', this.data.avatar) user.set('nickname', this.data.nickname) try { user.save() console.log('昵称保存成功'); } catch { console.log('保存失败'); } let Profilerquery = new Parse.Query('Profile'); Profilerquery.equalTo('company', company); Profilerquery.equalTo('user', currentUser.id); Profilerquery.notEqualTo('isDeleted', true) let Profile = await Profilerquery.first(); //工会 let Departmentquery = new Parse.Query('Department'); Departmentquery.equalTo('company', company); Departmentquery.equalTo('objectId', this.data.upiobjectid); Departmentquery.notEqualTo('isDeleted', true) let Department = await Departmentquery.first(); //部门 let unionquery = new Parse.Query('Department'); unionquery.equalTo('company', company); unionquery.equalTo('objectId', this.data.selectobjectid); unionquery.notEqualTo('isDeleted', true) let union = await unionquery.first(); if (Profile) { Profile.set('department', Department.toPointer()) Profile.set('center', union.toPointer()) try { Profile.save() wx.showToast({ title: '提交成功', icon: 'success', duration: 2000 }) console.log('部门保存成功'); } catch { console.log('部门保存失败'); } } else { const currentUser = Parse.User.current(); let Userquery = new Parse.Query('_User'); Userquery.equalTo('company', company); Userquery.equalTo('objectId', currentUser.id); Userquery.notEqualTo('isDeleted', true) let P2 = await Userquery.first(); let companyPointer = Parse.Object.extend('Company').createWithoutData(company); console.log(companyPointer); let Profile = new Parse.Object('Profile'); Profile.set('company', companyPointer); Profile.set('isCheck', false); Profile.set('user', P2.toPointer()); Profile.set('department', Department.toPointer()); Profile.set('center', union.toPointer()); try { Profile.save() console.log('部门保存成功'); } catch { console.log('部门保存失败'); } } }, // 获取部门信息 async getDepartment() { const currentUser = Parse.User.current(); let Userquery = new Parse.Query('Department'); Userquery.equalTo('company', company); // 假设 company 是你要查询的公司对象 Userquery.equalTo('isEnabled', true); Userquery.notEqualTo('isDeleted', true); Userquery.include('parent'); let P2 = await Userquery.find(); let Departmentlist = P2.map(item => item.toJSON()); // 创建一个以工会为键的对象,以便于匹配 const unionsMap = {}; console.log(Departmentlist); // 遍历部门列表,构建工会映射 Departmentlist.forEach(department => { //有工会的部门 if (department.parent && department.parent.objectId) { const unionId = department.parent.objectId; //工会id // 如果工会还没有在映射中,创建一个新的工会对象 if (!unionsMap[unionId]) { unionsMap[unionId] = { text: department.parent.name, // 工会名称 objectId: department.parent.objectId, children: [] // 初始化子部门数组 }; } // 将部门添加到对应的工会子项中 unionsMap[unionId].children.push({ text: department.name, // 部门名称 id: department.objectId, // 部门 ID upid: department.parent.objectId }); } else { if (!unionsMap[department.objectId]) { unionsMap[department.objectId] = { text: department.name, // 工会名称 objectId: department.objectId, children: [] // 初始化子部门数组 }; } } }); // 将映射转换为数组 const result = Object.values(unionsMap); console.log(result); this.setData({ items: result }) }, //点击工会 onClickNav({ detail = {} }) { this.setData({ mainActiveIndex: detail.index || 0, }); console.log(detail); }, //点击部门 onClickItem({ detail = {} }) { const activeId = this.data.activeId === detail.id ? null : detail.id; this.setData({ activeId, selectname: detail.text, selectobjectid: detail.id, upiobjectid: detail.upid }); this.onClickHide() console.log(detail); }, //显示遮罩层 onClickShow() { this.setData({ show: true }); }, //关闭遮罩层 onClickHide() { this.setData({ show: false }); }, //返回上一页 goback() { wx.navigateBack({ delta: 1 // 返回上一页 }); }, //删除头像 deleteavater() { this.setData({ avatar: '' }) }, async getUptoken() { let res = await Parse.Cloud.run('qiniu_uptoken', { company: company }) this.setData({ uptokenURL: res.uptoken, domain: res.domain, uploadURL: res.zoneUrl }) console.log(this.data.uptokenURL, this.data.domain, this.data.uploadURL); }, //本地上传头像 picture(event) { console.log('event', event); let FileList = event.detail let url = [] for (let i = 0; i < FileList.length; i++) { url.push(FileList[i].url) } this.setData({ fileList: url }) this.setData({ avatar:this.data.fileList[0] }) console.log('图片', this.data.fileList); }, })