index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. departname: '', //工会名字
  36. selectobjectid: '',
  37. upiobjectid: '',
  38. // 图片
  39. fileList: [],
  40. uptokenURL: '',
  41. domain: '',
  42. uploadURL: '',
  43. title: '',
  44. //
  45. name: '',
  46. phone: '',
  47. },
  48. /**
  49. * 生命周期函数--监听页面加载
  50. */
  51. onLoad: function (options) {
  52. // 计算
  53. const systemInfo = wx.getSystemInfoSync();
  54. const statusBarHeight = systemInfo.statusBarHeight || 0;
  55. const screenHeight = systemInfo.screenHeight || 0;
  56. const custom = wx.getMenuButtonBoundingClientRect();
  57. const customHeight = custom.height + 10 + 2 || 0;
  58. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  59. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  60. const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  61. this.setData({
  62. statusBarHeight,
  63. screenHeight,
  64. customHeight,
  65. bottomNavHeight,
  66. contentHeight,
  67. contentpadding
  68. });
  69. this.setData({
  70. title: options.type
  71. })
  72. this.getname()
  73. this.getdep()
  74. this.getDepartment()
  75. this.getUptoken()
  76. },
  77. /**
  78. * 生命周期函数--监听页面初次渲染完成
  79. */
  80. onReady: function () {
  81. },
  82. /**
  83. * 生命周期函数--监听页面显示
  84. */
  85. onShow: function () {
  86. },
  87. /**
  88. * 生命周期函数--监听页面隐藏
  89. */
  90. onHide: function () {
  91. },
  92. /**
  93. * 生命周期函数--监听页面卸载
  94. */
  95. onUnload: function () {
  96. },
  97. /**
  98. * 页面相关事件处理函数--监听用户下拉动作
  99. */
  100. onPullDownRefresh: function () {
  101. },
  102. /**
  103. * 页面上拉触底事件的处理函数
  104. */
  105. onReachBottom: function () {
  106. },
  107. /**
  108. * 用户点击右上角分享
  109. */
  110. onShareAppMessage: function () {
  111. },
  112. //获取身高
  113. getHeight(e) {
  114. let height = e.detail.value;
  115. // 身高范围在 50 到 250 之间的正则表达式,允许浮点数
  116. let a = /^(?:[5-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|250)(\.\d)?$/;
  117. if (!height.match(a)) {
  118. wx.showToast({
  119. icon: "none",
  120. title: "请填写正确的身高格式",
  121. });
  122. return;
  123. } else {
  124. this.setData({
  125. height: height
  126. });
  127. console.log(this.data.height);
  128. }
  129. },
  130. //点击清空
  131. clearHeight() {
  132. this.setData({
  133. height: null
  134. })
  135. console.log('清空', this.data.height);
  136. },
  137. //获取头像名称
  138. async getname() {
  139. const currentUser = Parse.User.current();
  140. let Userquery = new Parse.Query('_User');
  141. Userquery.equalTo('company', company);
  142. Userquery.equalTo('objectId', currentUser.id);
  143. Userquery.notEqualTo('isDeleted', true)
  144. let P2 = await Userquery.find();
  145. let User1List = P2.map(item => item.toJSON());
  146. this.setData({
  147. User1List
  148. })
  149. this.setData({
  150. avatar: User1List[0].avatar || 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241120/95uip6030022742.png?imageView2/1/w/200/h/200',
  151. nickname: User1List[0].nickname || '微信用户',
  152. sex: User1List[0].sex || '男',
  153. })
  154. if (User1List[0].diyform) {
  155. this.setData({
  156. height: User1List[0].diyform.height
  157. })
  158. }
  159. console.log(this.data.User1List);
  160. },
  161. //获取部门
  162. async getdep() {
  163. const currentUser = Parse.User.current();
  164. let Userquery = new Parse.Query('Profile');
  165. Userquery.equalTo('company', company);
  166. Userquery.equalTo('user', currentUser.id);
  167. // Userquery.equalTo('isCheck', true);
  168. Userquery.notEqualTo('isDeleted', true)
  169. Userquery.include('center');
  170. Userquery.include('department');
  171. let P2 = await Userquery.find();
  172. let dep = P2.map(item => item.toJSON());
  173. if (dep) {
  174. if (dep[0].isCheck == true) {
  175. this.setData({
  176. name: dep[0].name,
  177. phone: dep[0].mobile,
  178. departname: dep[0].department.name,
  179. selectname: dep[0].center.name,
  180. selectobjectid: dep[0].center.objectId,
  181. upiobjectid: dep[0].department.objectId
  182. })
  183. } else {
  184. this.setData({
  185. name: dep[0].name,
  186. phone: dep[0].mobile,
  187. departname: dep[0].department.name,
  188. selectname: dep[0].center.name,
  189. selectobjectid: dep[0].center.objectId,
  190. upiobjectid: dep[0].department.objectId
  191. })
  192. wx.showToast({
  193. title: '您之前提交信息在审核当中',
  194. icon: 'none',
  195. duration: 2000
  196. })
  197. }
  198. }
  199. console.log('getdep', dep);
  200. },
  201. onSelect(event) {
  202. let {
  203. name
  204. } = event.detail
  205. this.setData({
  206. sex: name,
  207. show2: false
  208. })
  209. console.log(this.data.sex);
  210. },
  211. showSelect() {
  212. this.setData({
  213. show2: true
  214. })
  215. },
  216. //修改昵称
  217. changenickname(e) {
  218. this.setData({
  219. nickname: e.detail
  220. })
  221. console.log(this.data.nickname);
  222. },
  223. //上传信息
  224. async setinfo() {
  225. if (this.data.title == '资料认证') {
  226. if (!this.data.name || !this.data.sex || !this.data.selectobjectid || !this.data.height || !this.data.phone) {
  227. wx.showToast({
  228. title: '请输入相关内容',
  229. icon: 'none',
  230. duration: 2000
  231. })
  232. return
  233. }
  234. const currentUser = Parse.User.current();
  235. let Userquery = new Parse.Query('_User');
  236. Userquery.equalTo('company', company);
  237. Userquery.equalTo('objectId', currentUser.id);
  238. Userquery.notEqualTo('isDeleted', true)
  239. let user = await Userquery.first();
  240. user.set('sex', this.data.sex)
  241. // user.set('avatar', this.data.avatar)
  242. let diyform = {
  243. height: this.data.height
  244. }
  245. user.set('diyform', diyform)
  246. // user.set('nickname', this.data.nickname)
  247. try {
  248. user.save()
  249. console.log('昵称保存成功');
  250. } catch {
  251. console.log('保存失败');
  252. }
  253. let Profilerquery = new Parse.Query('Profile');
  254. Profilerquery.equalTo('company', company);
  255. Profilerquery.equalTo('user', currentUser.id);
  256. Profilerquery.notEqualTo('isDeleted', true)
  257. let Profile = await Profilerquery.first();
  258. //工会
  259. let Departmentquery = new Parse.Query('Department');
  260. Departmentquery.equalTo('company', company);
  261. Departmentquery.equalTo('objectId', this.data.upiobjectid);
  262. Departmentquery.notEqualTo('isDeleted', true)
  263. let Department = await Departmentquery.first();
  264. //部门
  265. let unionquery = new Parse.Query('Department');
  266. unionquery.equalTo('company', company);
  267. unionquery.equalTo('objectId', this.data.selectobjectid);
  268. unionquery.notEqualTo('isDeleted', true)
  269. let union = await unionquery.first();
  270. if (Profile) {
  271. Profile.set('department', Department.toPointer())
  272. Profile.set('center', union.toPointer())
  273. Profile.set('name', this.data.name)
  274. Profile.set('mobile', this.data.phone)
  275. try {
  276. Profile.save()
  277. wx.showToast({
  278. title: '提交成功',
  279. icon: 'success',
  280. duration: 2000
  281. })
  282. setTimeout(() => {
  283. this.goback()
  284. }, 2000);
  285. console.log('部门保存成功');
  286. } catch {
  287. console.log('部门保存失败');
  288. }
  289. } else {
  290. const currentUser = Parse.User.current();
  291. let Userquery = new Parse.Query('_User');
  292. Userquery.equalTo('company', company);
  293. Userquery.equalTo('objectId', currentUser.id);
  294. Userquery.notEqualTo('isDeleted', true)
  295. let P2 = await Userquery.first();
  296. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  297. console.log(companyPointer);
  298. let Profile = new Parse.Object('Profile');
  299. Profile.set('company', companyPointer);
  300. Profile.set('isCheck', false);
  301. Profile.set('user', P2.toPointer());
  302. Profile.set('department', Department.toPointer());
  303. Profile.set('center', union.toPointer());
  304. Profile.set('name', this.data.name)
  305. Profile.set('mobile', this.data.phone)
  306. try {
  307. Profile.save()
  308. wx.showToast({
  309. title: '提交成功',
  310. icon: 'success',
  311. duration: 2000
  312. })
  313. setTimeout(() => {
  314. this.goback()
  315. }, 2000);
  316. console.log('部门保存成功');
  317. } catch {
  318. console.log('部门保存失败');
  319. }
  320. }
  321. }
  322. if (this.data.title == '完善资料') {
  323. if (!this.data.avatar || !this.data.nickname) {
  324. wx.showToast({
  325. title: '请输入相关内容',
  326. icon: 'none',
  327. duration: 2000
  328. })
  329. return
  330. }
  331. const currentUser = Parse.User.current();
  332. let Userquery = new Parse.Query('_User');
  333. Userquery.equalTo('company', company);
  334. Userquery.equalTo('objectId', currentUser.id);
  335. Userquery.notEqualTo('isDeleted', true)
  336. let user = await Userquery.first();
  337. user.set('avatar', this.data.avatar)
  338. user.set('nickname', this.data.nickname)
  339. try {
  340. user.save()
  341. wx.showToast({
  342. title: '提交成功',
  343. icon: 'success',
  344. duration: 2000
  345. })
  346. setTimeout(() => {
  347. this.goback()
  348. }, 2000);
  349. } catch {
  350. console.log('保存失败');
  351. }
  352. }
  353. },
  354. // 获取部门信息
  355. async getDepartment() {
  356. const currentUser = Parse.User.current();
  357. let Userquery = new Parse.Query('Department');
  358. Userquery.equalTo('company', company); // 假设 company 是你要查询的公司对象
  359. Userquery.equalTo('isEnabled', true);
  360. Userquery.notEqualTo('isDeleted', true);
  361. Userquery.include('parent');
  362. let P2 = await Userquery.find();
  363. let Departmentlist = P2.map(item => item.toJSON());
  364. // 创建一个以工会为键的对象,以便于匹配
  365. const unionsMap = {};
  366. console.log(Departmentlist);
  367. // 遍历部门列表,构建工会映射
  368. Departmentlist.forEach(department => {
  369. //有工会的部门
  370. if (department.parent && department.parent.objectId) {
  371. const unionId = department.parent.objectId; //工会id
  372. // 如果工会还没有在映射中,创建一个新的工会对象
  373. if (!unionsMap[unionId]) {
  374. unionsMap[unionId] = {
  375. text: department.parent.name, // 工会名称
  376. objectId: department.parent.objectId,
  377. children: [] // 初始化子部门数组
  378. };
  379. }
  380. // 将部门添加到对应的工会子项中
  381. unionsMap[unionId].children.push({
  382. text: department.name, // 部门名称
  383. id: department.objectId, // 部门 ID
  384. upid: department.parent.objectId
  385. });
  386. } else {
  387. if (!unionsMap[department.objectId]) {
  388. unionsMap[department.objectId] = {
  389. text: department.name, // 工会名称
  390. objectId: department.objectId,
  391. children: [] // 初始化子部门数组
  392. };
  393. }
  394. }
  395. });
  396. // 将映射转换为数组
  397. const result = Object.values(unionsMap);
  398. console.log(result);
  399. this.setData({
  400. items: result
  401. })
  402. console.log(this.data.items);
  403. },
  404. //点击工会
  405. onClickNav({
  406. detail = {}
  407. }) {
  408. this.setData({
  409. mainActiveIndex: detail.index || 0,
  410. });
  411. console.log(detail);
  412. },
  413. //点击部门
  414. onClickItem({
  415. detail = {}
  416. }) {
  417. const activeId = this.data.activeId === detail.id ? null : detail.id;
  418. this.data.items.forEach((item) => {
  419. if (item.objectId == detail.upid) {
  420. this.setData({
  421. departname: item.text
  422. })
  423. }
  424. })
  425. this.setData({
  426. activeId,
  427. selectname: detail.text,
  428. selectobjectid: detail.id,
  429. upiobjectid: detail.upid
  430. });
  431. this.onClickHide()
  432. console.log(detail);
  433. },
  434. //显示遮罩层
  435. onClickShow() {
  436. this.setData({
  437. show: true
  438. });
  439. },
  440. //关闭遮罩层
  441. onClickHide() {
  442. this.setData({
  443. show: false
  444. });
  445. },
  446. //返回上一页
  447. async goback() {
  448. let pages = getCurrentPages(); //页面对象
  449. let prevpage = pages[pages.length - 2]; //上一个页面对象
  450. console.log(prevpage);
  451. await prevpage.updateCom3()
  452. wx.navigateBack({
  453. delta: 1 // 返回上一页
  454. });
  455. },
  456. //删除头像
  457. deleteavater() {
  458. this.setData({
  459. avatar: ''
  460. })
  461. },
  462. async getUptoken() {
  463. let res = await Parse.Cloud.run('qiniu_uptoken', {
  464. company: company
  465. })
  466. this.setData({
  467. uptokenURL: res.uptoken,
  468. domain: res.domain,
  469. uploadURL: res.zoneUrl
  470. })
  471. console.log(this.data.uptokenURL, this.data.domain, this.data.uploadURL);
  472. },
  473. //本地上传头像
  474. picture(event) {
  475. console.log('event', event);
  476. let FileList = event.detail
  477. let url = []
  478. for (let i = 0; i < FileList.length; i++) {
  479. url.push(FileList[i].url)
  480. }
  481. this.setData({
  482. fileList: url
  483. })
  484. this.setData({
  485. avatar: this.data.fileList[0]
  486. })
  487. console.log('图片', this.data.fileList);
  488. },
  489. //修改姓名
  490. changename(e) {
  491. this.setData({
  492. name: e.detail
  493. })
  494. console.log(this.data.name);
  495. },
  496. //修改电话
  497. changephone(e) {
  498. const mobileNumber = e.detail.value; // 假设手机号是通过 e.detail.value 传递的
  499. console.log(mobileNumber);
  500. // 正则表达式:匹配中国大陆手机号
  501. const mobilePattern = /^1[3-9]\d{9}$/;
  502. // 验证手机号格式
  503. if (mobilePattern.test(mobileNumber)) {
  504. this.setData({
  505. phone: mobileNumber
  506. })
  507. console.log("手机号格式正确");
  508. // 这里可以执行其他操作,比如保存手机号或进行下一步操作
  509. } else {
  510. wx.showToast({
  511. title: '手机号格式不正确',
  512. icon: 'none',
  513. })
  514. console.log("手机号格式不正确");
  515. // 这里可以提示用户手机号格式不正确
  516. }
  517. console.log(this.data.phone);
  518. }
  519. })