index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // nova-werun/pages/my/my-profile/index.js
  2. const Parse = getApp().Parse;
  3. const company = getApp().globalData.company;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. //屏幕高度
  10. statusBarHeight: 0, // 状态栏高度
  11. screenHeight: 0, // 屏幕高度
  12. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  13. bottomNavHeight: 0, // 底部导航栏高度
  14. contentHeight: 0, // 可用内容高度
  15. contentpadding: 0, //顶部padding高度
  16. User1List: [],
  17. actions: [{
  18. name: '男',
  19. },
  20. {
  21. name: '女',
  22. },
  23. ],
  24. avatar: null,
  25. sex: '男',
  26. nickname: null,
  27. height: null,
  28. // 选择部门
  29. mainActiveIndex: 0,
  30. activeId: null,
  31. items: [],
  32. //显示遮罩层
  33. show: false,
  34. selectname: '',
  35. selectobjectid: '',
  36. upiobjectid: '',
  37. // 图片
  38. fileList: [],
  39. uptokenURL: '',
  40. domain: '',
  41. uploadURL: '',
  42. },
  43. /**
  44. * 生命周期函数--监听页面加载
  45. */
  46. onLoad: function (options) {
  47. // 计算
  48. const systemInfo = wx.getSystemInfoSync();
  49. const statusBarHeight = systemInfo.statusBarHeight || 0;
  50. const screenHeight = systemInfo.screenHeight || 0;
  51. const custom = wx.getMenuButtonBoundingClientRect();
  52. const customHeight = custom.height + 10 + 2 || 0;
  53. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  54. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  55. const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  56. this.setData({
  57. statusBarHeight,
  58. screenHeight,
  59. customHeight,
  60. bottomNavHeight,
  61. contentHeight,
  62. contentpadding
  63. });
  64. this.getname()
  65. this.getdep()
  66. this.getDepartment()
  67. this.getUptoken()
  68. },
  69. /**
  70. * 生命周期函数--监听页面初次渲染完成
  71. */
  72. onReady: function () {
  73. },
  74. /**
  75. * 生命周期函数--监听页面显示
  76. */
  77. onShow: function () {
  78. },
  79. /**
  80. * 生命周期函数--监听页面隐藏
  81. */
  82. onHide: function () {
  83. },
  84. /**
  85. * 生命周期函数--监听页面卸载
  86. */
  87. onUnload: function () {
  88. },
  89. /**
  90. * 页面相关事件处理函数--监听用户下拉动作
  91. */
  92. onPullDownRefresh: function () {
  93. },
  94. /**
  95. * 页面上拉触底事件的处理函数
  96. */
  97. onReachBottom: function () {
  98. },
  99. /**
  100. * 用户点击右上角分享
  101. */
  102. onShareAppMessage: function () {
  103. },
  104. //获取身高
  105. getHeight(e) {
  106. let height = e.detail.value;
  107. // 身高范围在 50 到 250 之间的正则表达式,允许浮点数
  108. let a = /^(?:[5-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|250)(\.\d)?$/;
  109. if (!height.match(a)) {
  110. wx.showToast({
  111. icon: "none",
  112. title: "请填写正确的身高格式",
  113. });
  114. return;
  115. } else {
  116. this.setData({
  117. height: height
  118. });
  119. console.log(this.data.height);
  120. }
  121. },
  122. //点击清空
  123. clearHeight() {
  124. this.setData({
  125. height: null
  126. })
  127. console.log('清空', this.data.height);
  128. },
  129. //获取头像名称
  130. async getname() {
  131. const currentUser = Parse.User.current();
  132. let Userquery = new Parse.Query('_User');
  133. Userquery.equalTo('company', company);
  134. Userquery.equalTo('objectId', currentUser.id);
  135. Userquery.notEqualTo('isDeleted', true)
  136. let P2 = await Userquery.find();
  137. let User1List = P2.map(item => item.toJSON());
  138. this.setData({
  139. User1List
  140. })
  141. this.setData({
  142. avatar: User1List[0].avatar||'https://file-cloud.fmode.cn/qpFbRRSZrO/20241120/95uip6030022742.png?imageView2/1/w/200/h/200',
  143. nickname: User1List[0].nickname||'微信用户',
  144. sex: User1List[0].sex || '男',
  145. })
  146. if(User1List[0].diyform){
  147. this.setData({
  148. height:User1List[0].diyform.height
  149. })
  150. }
  151. console.log(this.data.User1List);
  152. },
  153. //获取部门
  154. async getdep() {
  155. const currentUser = Parse.User.current();
  156. let Userquery = new Parse.Query('Profile');
  157. Userquery.equalTo('company', company);
  158. Userquery.equalTo('user', currentUser.id);
  159. // Userquery.equalTo('isCheck', true);
  160. Userquery.notEqualTo('isDeleted', true)
  161. Userquery.include('center');
  162. let P2 = await Userquery.find();
  163. let dep = P2.map(item => item.toJSON());
  164. if (dep) {
  165. if (dep[0].isCheck == true) {
  166. this.setData({
  167. selectname: dep[0].center.name,
  168. selectobjectid: dep[0].center.objectId,
  169. upiobjectid: dep[0].department.objectId
  170. })
  171. } else {
  172. wx.showToast({
  173. title: '您之前提交信息在审核当中',
  174. icon: 'none',
  175. duration: 2000
  176. })
  177. }
  178. }
  179. console.log('getdep', dep);
  180. },
  181. onSelect(event) {
  182. let {
  183. name
  184. } = event.detail
  185. this.setData({
  186. sex: name,
  187. show2: false
  188. })
  189. console.log(this.data.sex);
  190. },
  191. showSelect() {
  192. this.setData({
  193. show2: true
  194. })
  195. },
  196. //修改名字
  197. changenickname(e) {
  198. this.setData({
  199. nickname: e.detail
  200. })
  201. console.log(this.data.nickname);
  202. },
  203. //上传信息
  204. async setinfo() {
  205. if (!this.data.nickname || !this.data.sex || !this.data.selectobjectid || !this.data.avatar||!this.data.height) {
  206. wx.showToast({
  207. title: '请输入相关内容',
  208. icon: 'none',
  209. duration: 2000
  210. })
  211. return
  212. }
  213. const currentUser = Parse.User.current();
  214. let Userquery = new Parse.Query('_User');
  215. Userquery.equalTo('company', company);
  216. Userquery.equalTo('objectId', currentUser.id);
  217. Userquery.notEqualTo('isDeleted', true)
  218. let user = await Userquery.first();
  219. user.set('sex', this.data.sex)
  220. user.set('avatar', this.data.avatar)
  221. let diyform = {
  222. height:this.data.height
  223. }
  224. user.set('diyform', diyform)
  225. user.set('nickname', this.data.nickname)
  226. try {
  227. user.save()
  228. console.log('昵称保存成功');
  229. } catch {
  230. console.log('保存失败');
  231. }
  232. let Profilerquery = new Parse.Query('Profile');
  233. Profilerquery.equalTo('company', company);
  234. Profilerquery.equalTo('user', currentUser.id);
  235. Profilerquery.notEqualTo('isDeleted', true)
  236. let Profile = await Profilerquery.first();
  237. //工会
  238. let Departmentquery = new Parse.Query('Department');
  239. Departmentquery.equalTo('company', company);
  240. Departmentquery.equalTo('objectId', this.data.upiobjectid);
  241. Departmentquery.notEqualTo('isDeleted', true)
  242. let Department = await Departmentquery.first();
  243. //部门
  244. let unionquery = new Parse.Query('Department');
  245. unionquery.equalTo('company', company);
  246. unionquery.equalTo('objectId', this.data.selectobjectid);
  247. unionquery.notEqualTo('isDeleted', true)
  248. let union = await unionquery.first();
  249. if (Profile) {
  250. Profile.set('department', Department.toPointer())
  251. Profile.set('center', union.toPointer())
  252. try {
  253. Profile.save()
  254. wx.showToast({
  255. title: '提交成功',
  256. icon: 'success',
  257. duration: 2000
  258. })
  259. console.log('部门保存成功');
  260. } catch {
  261. console.log('部门保存失败');
  262. }
  263. } else {
  264. const currentUser = Parse.User.current();
  265. let Userquery = new Parse.Query('_User');
  266. Userquery.equalTo('company', company);
  267. Userquery.equalTo('objectId', currentUser.id);
  268. Userquery.notEqualTo('isDeleted', true)
  269. let P2 = await Userquery.first();
  270. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  271. console.log(companyPointer);
  272. let Profile = new Parse.Object('Profile');
  273. Profile.set('company', companyPointer);
  274. Profile.set('isCheck', false);
  275. Profile.set('user', P2.toPointer());
  276. Profile.set('department', Department.toPointer());
  277. Profile.set('center', union.toPointer());
  278. try {
  279. Profile.save()
  280. console.log('部门保存成功');
  281. } catch {
  282. console.log('部门保存失败');
  283. }
  284. }
  285. },
  286. // 获取部门信息
  287. async getDepartment() {
  288. const currentUser = Parse.User.current();
  289. let Userquery = new Parse.Query('Department');
  290. Userquery.equalTo('company', company); // 假设 company 是你要查询的公司对象
  291. Userquery.equalTo('isEnabled', true);
  292. Userquery.notEqualTo('isDeleted', true);
  293. Userquery.include('parent');
  294. let P2 = await Userquery.find();
  295. let Departmentlist = P2.map(item => item.toJSON());
  296. // 创建一个以工会为键的对象,以便于匹配
  297. const unionsMap = {};
  298. console.log(Departmentlist);
  299. // 遍历部门列表,构建工会映射
  300. Departmentlist.forEach(department => {
  301. //有工会的部门
  302. if (department.parent && department.parent.objectId) {
  303. const unionId = department.parent.objectId; //工会id
  304. // 如果工会还没有在映射中,创建一个新的工会对象
  305. if (!unionsMap[unionId]) {
  306. unionsMap[unionId] = {
  307. text: department.parent.name, // 工会名称
  308. objectId: department.parent.objectId,
  309. children: [] // 初始化子部门数组
  310. };
  311. }
  312. // 将部门添加到对应的工会子项中
  313. unionsMap[unionId].children.push({
  314. text: department.name, // 部门名称
  315. id: department.objectId, // 部门 ID
  316. upid: department.parent.objectId
  317. });
  318. } else {
  319. if (!unionsMap[department.objectId]) {
  320. unionsMap[department.objectId] = {
  321. text: department.name, // 工会名称
  322. objectId: department.objectId,
  323. children: [] // 初始化子部门数组
  324. };
  325. }
  326. }
  327. });
  328. // 将映射转换为数组
  329. const result = Object.values(unionsMap);
  330. console.log(result);
  331. this.setData({
  332. items: result
  333. })
  334. },
  335. //点击工会
  336. onClickNav({
  337. detail = {}
  338. }) {
  339. this.setData({
  340. mainActiveIndex: detail.index || 0,
  341. });
  342. console.log(detail);
  343. },
  344. //点击部门
  345. onClickItem({
  346. detail = {}
  347. }) {
  348. const activeId = this.data.activeId === detail.id ? null : detail.id;
  349. this.setData({
  350. activeId,
  351. selectname: detail.text,
  352. selectobjectid: detail.id,
  353. upiobjectid: detail.upid
  354. });
  355. this.onClickHide()
  356. console.log(detail);
  357. },
  358. //显示遮罩层
  359. onClickShow() {
  360. this.setData({
  361. show: true
  362. });
  363. },
  364. //关闭遮罩层
  365. onClickHide() {
  366. this.setData({
  367. show: false
  368. });
  369. },
  370. //返回上一页
  371. goback() {
  372. wx.navigateBack({
  373. delta: 1 // 返回上一页
  374. });
  375. },
  376. //删除头像
  377. deleteavater() {
  378. this.setData({
  379. avatar: ''
  380. })
  381. },
  382. async getUptoken() {
  383. let res = await Parse.Cloud.run('qiniu_uptoken', {
  384. company: company
  385. })
  386. this.setData({
  387. uptokenURL: res.uptoken,
  388. domain: res.domain,
  389. uploadURL: res.zoneUrl
  390. })
  391. console.log(this.data.uptokenURL, this.data.domain, this.data.uploadURL);
  392. },
  393. //本地上传头像
  394. picture(event) {
  395. console.log('event', event);
  396. let FileList = event.detail
  397. let url = []
  398. for (let i = 0; i < FileList.length; i++) {
  399. url.push(FileList[i].url)
  400. }
  401. this.setData({
  402. fileList: url
  403. })
  404. this.setData({
  405. avatar:this.data.fileList[0]
  406. })
  407. console.log('图片', this.data.fileList);
  408. },
  409. })