index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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,
  143. nickname: User1List[0].nickname,
  144. sex: User1List[0].sex || '男',
  145. })
  146. console.log(this.data.User1List);
  147. },
  148. //获取部门
  149. async getdep() {
  150. const currentUser = Parse.User.current();
  151. let Userquery = new Parse.Query('Profile');
  152. Userquery.equalTo('company', company);
  153. Userquery.equalTo('user', currentUser.id);
  154. // Userquery.equalTo('isCheck', true);
  155. Userquery.notEqualTo('isDeleted', true)
  156. Userquery.include('center');
  157. let P2 = await Userquery.find();
  158. let dep = P2.map(item => item.toJSON());
  159. if (dep) {
  160. if (dep[0].isCheck == true) {
  161. this.setData({
  162. selectname: dep[0].center.name,
  163. selectobjectid: dep[0].center.objectId,
  164. upiobjectid: dep[0].department.objectId
  165. })
  166. } else {
  167. wx.showToast({
  168. title: '您之前提交信息在审核当中',
  169. icon: 'none',
  170. duration: 2000
  171. })
  172. }
  173. }
  174. console.log('getdep', dep);
  175. },
  176. onSelect(event) {
  177. let {
  178. name
  179. } = event.detail
  180. this.setData({
  181. sex: name,
  182. show2: false
  183. })
  184. console.log(this.data.sex);
  185. },
  186. showSelect() {
  187. this.setData({
  188. show2: true
  189. })
  190. },
  191. //修改名字
  192. changenickname(e) {
  193. this.setData({
  194. nickname: e.detail
  195. })
  196. console.log(this.data.nickname);
  197. },
  198. //上传信息
  199. async setinfo() {
  200. if (!this.data.nickname || !this.data.sex || !this.data.selectobjectid || !this.data.avatar) {
  201. wx.showToast({
  202. title: '请输入相关内容',
  203. icon: 'none',
  204. duration: 2000
  205. })
  206. return
  207. }
  208. const currentUser = Parse.User.current();
  209. let Userquery = new Parse.Query('_User');
  210. Userquery.equalTo('company', company);
  211. Userquery.equalTo('objectId', currentUser.id);
  212. Userquery.notEqualTo('isDeleted', true)
  213. let user = await Userquery.first();
  214. user.set('sex', this.data.sex)
  215. user.set('avatar', this.data.avatar)
  216. user.set('nickname', this.data.nickname)
  217. try {
  218. user.save()
  219. console.log('昵称保存成功');
  220. } catch {
  221. console.log('保存失败');
  222. }
  223. let Profilerquery = new Parse.Query('Profile');
  224. Profilerquery.equalTo('company', company);
  225. Profilerquery.equalTo('user', currentUser.id);
  226. Profilerquery.notEqualTo('isDeleted', true)
  227. let Profile = await Profilerquery.first();
  228. //工会
  229. let Departmentquery = new Parse.Query('Department');
  230. Departmentquery.equalTo('company', company);
  231. Departmentquery.equalTo('objectId', this.data.upiobjectid);
  232. Departmentquery.notEqualTo('isDeleted', true)
  233. let Department = await Departmentquery.first();
  234. //部门
  235. let unionquery = new Parse.Query('Department');
  236. unionquery.equalTo('company', company);
  237. unionquery.equalTo('objectId', this.data.selectobjectid);
  238. unionquery.notEqualTo('isDeleted', true)
  239. let union = await unionquery.first();
  240. if (Profile) {
  241. Profile.set('department', Department.toPointer())
  242. Profile.set('center', union.toPointer())
  243. try {
  244. Profile.save()
  245. wx.showToast({
  246. title: '提交成功',
  247. icon: 'success',
  248. duration: 2000
  249. })
  250. console.log('部门保存成功');
  251. } catch {
  252. console.log('部门保存失败');
  253. }
  254. } else {
  255. const currentUser = Parse.User.current();
  256. let Userquery = new Parse.Query('_User');
  257. Userquery.equalTo('company', company);
  258. Userquery.equalTo('objectId', currentUser.id);
  259. Userquery.notEqualTo('isDeleted', true)
  260. let P2 = await Userquery.first();
  261. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  262. console.log(companyPointer);
  263. let Profile = new Parse.Object('Profile');
  264. Profile.set('company', companyPointer);
  265. Profile.set('isCheck', false);
  266. Profile.set('user', P2.toPointer());
  267. Profile.set('department', Department.toPointer());
  268. Profile.set('center', union.toPointer());
  269. try {
  270. Profile.save()
  271. console.log('部门保存成功');
  272. } catch {
  273. console.log('部门保存失败');
  274. }
  275. }
  276. },
  277. // 获取部门信息
  278. async getDepartment() {
  279. const currentUser = Parse.User.current();
  280. let Userquery = new Parse.Query('Department');
  281. Userquery.equalTo('company', company); // 假设 company 是你要查询的公司对象
  282. Userquery.equalTo('isEnabled', true);
  283. Userquery.notEqualTo('isDeleted', true);
  284. Userquery.include('parent');
  285. let P2 = await Userquery.find();
  286. let Departmentlist = P2.map(item => item.toJSON());
  287. // 创建一个以工会为键的对象,以便于匹配
  288. const unionsMap = {};
  289. console.log(Departmentlist);
  290. // 遍历部门列表,构建工会映射
  291. Departmentlist.forEach(department => {
  292. //有工会的部门
  293. if (department.parent && department.parent.objectId) {
  294. const unionId = department.parent.objectId; //工会id
  295. // 如果工会还没有在映射中,创建一个新的工会对象
  296. if (!unionsMap[unionId]) {
  297. unionsMap[unionId] = {
  298. text: department.parent.name, // 工会名称
  299. objectId: department.parent.objectId,
  300. children: [] // 初始化子部门数组
  301. };
  302. }
  303. // 将部门添加到对应的工会子项中
  304. unionsMap[unionId].children.push({
  305. text: department.name, // 部门名称
  306. id: department.objectId, // 部门 ID
  307. upid: department.parent.objectId
  308. });
  309. } else {
  310. if (!unionsMap[department.objectId]) {
  311. unionsMap[department.objectId] = {
  312. text: department.name, // 工会名称
  313. objectId: department.objectId,
  314. children: [] // 初始化子部门数组
  315. };
  316. }
  317. }
  318. });
  319. // 将映射转换为数组
  320. const result = Object.values(unionsMap);
  321. console.log(result);
  322. this.setData({
  323. items: result
  324. })
  325. },
  326. //点击工会
  327. onClickNav({
  328. detail = {}
  329. }) {
  330. this.setData({
  331. mainActiveIndex: detail.index || 0,
  332. });
  333. console.log(detail);
  334. },
  335. //点击部门
  336. onClickItem({
  337. detail = {}
  338. }) {
  339. const activeId = this.data.activeId === detail.id ? null : detail.id;
  340. this.setData({
  341. activeId,
  342. selectname: detail.text,
  343. selectobjectid: detail.id,
  344. upiobjectid: detail.upid
  345. });
  346. this.onClickHide()
  347. console.log(detail);
  348. },
  349. //显示遮罩层
  350. onClickShow() {
  351. this.setData({
  352. show: true
  353. });
  354. },
  355. //关闭遮罩层
  356. onClickHide() {
  357. this.setData({
  358. show: false
  359. });
  360. },
  361. //返回上一页
  362. goback() {
  363. wx.navigateBack({
  364. delta: 1 // 返回上一页
  365. });
  366. },
  367. //删除头像
  368. deleteavater() {
  369. this.setData({
  370. avatar: ''
  371. })
  372. },
  373. async getUptoken() {
  374. let res = await Parse.Cloud.run('qiniu_uptoken', {
  375. company: company
  376. })
  377. this.setData({
  378. uptokenURL: res.uptoken,
  379. domain: res.domain,
  380. uploadURL: res.zoneUrl
  381. })
  382. console.log(this.data.uptokenURL, this.data.domain, this.data.uploadURL);
  383. },
  384. //本地上传头像
  385. picture(event) {
  386. console.log('event', event);
  387. let FileList = event.detail
  388. let url = []
  389. for (let i = 0; i < FileList.length; i++) {
  390. url.push(FileList[i].url)
  391. }
  392. this.setData({
  393. fileList: url
  394. })
  395. this.setData({
  396. avatar:this.data.fileList[0]
  397. })
  398. console.log('图片', this.data.fileList);
  399. },
  400. })