index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. setTimeout(() => {
  260. this.goback()
  261. }, 2000);
  262. console.log('部门保存成功');
  263. } catch {
  264. console.log('部门保存失败');
  265. }
  266. } else {
  267. const currentUser = Parse.User.current();
  268. let Userquery = new Parse.Query('_User');
  269. Userquery.equalTo('company', company);
  270. Userquery.equalTo('objectId', currentUser.id);
  271. Userquery.notEqualTo('isDeleted', true)
  272. let P2 = await Userquery.first();
  273. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  274. console.log(companyPointer);
  275. let Profile = new Parse.Object('Profile');
  276. Profile.set('company', companyPointer);
  277. Profile.set('isCheck', false);
  278. Profile.set('user', P2.toPointer());
  279. Profile.set('department', Department.toPointer());
  280. Profile.set('center', union.toPointer());
  281. try {
  282. Profile.save()
  283. wx.showToast({
  284. title: '提交成功',
  285. icon: 'success',
  286. duration: 2000
  287. })
  288. setTimeout(() => {
  289. this.goback()
  290. }, 2000);
  291. console.log('部门保存成功');
  292. } catch {
  293. console.log('部门保存失败');
  294. }
  295. }
  296. },
  297. // 获取部门信息
  298. async getDepartment() {
  299. const currentUser = Parse.User.current();
  300. let Userquery = new Parse.Query('Department');
  301. Userquery.equalTo('company', company); // 假设 company 是你要查询的公司对象
  302. Userquery.equalTo('isEnabled', true);
  303. Userquery.notEqualTo('isDeleted', true);
  304. Userquery.include('parent');
  305. let P2 = await Userquery.find();
  306. let Departmentlist = P2.map(item => item.toJSON());
  307. // 创建一个以工会为键的对象,以便于匹配
  308. const unionsMap = {};
  309. console.log(Departmentlist);
  310. // 遍历部门列表,构建工会映射
  311. Departmentlist.forEach(department => {
  312. //有工会的部门
  313. if (department.parent && department.parent.objectId) {
  314. const unionId = department.parent.objectId; //工会id
  315. // 如果工会还没有在映射中,创建一个新的工会对象
  316. if (!unionsMap[unionId]) {
  317. unionsMap[unionId] = {
  318. text: department.parent.name, // 工会名称
  319. objectId: department.parent.objectId,
  320. children: [] // 初始化子部门数组
  321. };
  322. }
  323. // 将部门添加到对应的工会子项中
  324. unionsMap[unionId].children.push({
  325. text: department.name, // 部门名称
  326. id: department.objectId, // 部门 ID
  327. upid: department.parent.objectId
  328. });
  329. } else {
  330. if (!unionsMap[department.objectId]) {
  331. unionsMap[department.objectId] = {
  332. text: department.name, // 工会名称
  333. objectId: department.objectId,
  334. children: [] // 初始化子部门数组
  335. };
  336. }
  337. }
  338. });
  339. // 将映射转换为数组
  340. const result = Object.values(unionsMap);
  341. console.log(result);
  342. this.setData({
  343. items: result
  344. })
  345. },
  346. //点击工会
  347. onClickNav({
  348. detail = {}
  349. }) {
  350. this.setData({
  351. mainActiveIndex: detail.index || 0,
  352. });
  353. console.log(detail);
  354. },
  355. //点击部门
  356. onClickItem({
  357. detail = {}
  358. }) {
  359. const activeId = this.data.activeId === detail.id ? null : detail.id;
  360. this.setData({
  361. activeId,
  362. selectname: detail.text,
  363. selectobjectid: detail.id,
  364. upiobjectid: detail.upid
  365. });
  366. this.onClickHide()
  367. console.log(detail);
  368. },
  369. //显示遮罩层
  370. onClickShow() {
  371. this.setData({
  372. show: true
  373. });
  374. },
  375. //关闭遮罩层
  376. onClickHide() {
  377. this.setData({
  378. show: false
  379. });
  380. },
  381. //返回上一页
  382. goback() {
  383. wx.navigateBack({
  384. delta: 1 // 返回上一页
  385. });
  386. },
  387. //删除头像
  388. deleteavater() {
  389. this.setData({
  390. avatar: ''
  391. })
  392. },
  393. async getUptoken() {
  394. let res = await Parse.Cloud.run('qiniu_uptoken', {
  395. company: company
  396. })
  397. this.setData({
  398. uptokenURL: res.uptoken,
  399. domain: res.domain,
  400. uploadURL: res.zoneUrl
  401. })
  402. console.log(this.data.uptokenURL, this.data.domain, this.data.uploadURL);
  403. },
  404. //本地上传头像
  405. picture(event) {
  406. console.log('event', event);
  407. let FileList = event.detail
  408. let url = []
  409. for (let i = 0; i < FileList.length; i++) {
  410. url.push(FileList[i].url)
  411. }
  412. this.setData({
  413. fileList: url
  414. })
  415. this.setData({
  416. avatar:this.data.fileList[0]
  417. })
  418. console.log('图片', this.data.fileList);
  419. },
  420. })