index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. wx.showToast({
  185. title: '您之前提交信息在审核当中',
  186. icon: 'none',
  187. duration: 2000
  188. })
  189. }
  190. }
  191. console.log('getdep', dep);
  192. },
  193. onSelect(event) {
  194. let {
  195. name
  196. } = event.detail
  197. this.setData({
  198. sex: name,
  199. show2: false
  200. })
  201. console.log(this.data.sex);
  202. },
  203. showSelect() {
  204. this.setData({
  205. show2: true
  206. })
  207. },
  208. //修改昵称
  209. changenickname(e) {
  210. this.setData({
  211. nickname: e.detail
  212. })
  213. console.log(this.data.nickname);
  214. },
  215. //上传信息
  216. async setinfo() {
  217. if (this.data.title == '资料认证') {
  218. if (!this.data.name || !this.data.sex || !this.data.selectobjectid || !this.data.height||!this.data.phone) {
  219. wx.showToast({
  220. title: '请输入相关内容',
  221. icon: 'none',
  222. duration: 2000
  223. })
  224. return
  225. }
  226. const currentUser = Parse.User.current();
  227. let Userquery = new Parse.Query('_User');
  228. Userquery.equalTo('company', company);
  229. Userquery.equalTo('objectId', currentUser.id);
  230. Userquery.notEqualTo('isDeleted', true)
  231. let user = await Userquery.first();
  232. user.set('sex', this.data.sex)
  233. // user.set('avatar', this.data.avatar)
  234. let diyform = {
  235. height: this.data.height
  236. }
  237. user.set('diyform', diyform)
  238. // user.set('nickname', this.data.nickname)
  239. try {
  240. user.save()
  241. console.log('昵称保存成功');
  242. } catch {
  243. console.log('保存失败');
  244. }
  245. let Profilerquery = new Parse.Query('Profile');
  246. Profilerquery.equalTo('company', company);
  247. Profilerquery.equalTo('user', currentUser.id);
  248. Profilerquery.notEqualTo('isDeleted', true)
  249. let Profile = await Profilerquery.first();
  250. //工会
  251. let Departmentquery = new Parse.Query('Department');
  252. Departmentquery.equalTo('company', company);
  253. Departmentquery.equalTo('objectId', this.data.upiobjectid);
  254. Departmentquery.notEqualTo('isDeleted', true)
  255. let Department = await Departmentquery.first();
  256. //部门
  257. let unionquery = new Parse.Query('Department');
  258. unionquery.equalTo('company', company);
  259. unionquery.equalTo('objectId', this.data.selectobjectid);
  260. unionquery.notEqualTo('isDeleted', true)
  261. let union = await unionquery.first();
  262. if (Profile) {
  263. Profile.set('department', Department.toPointer())
  264. Profile.set('center', union.toPointer())
  265. Profile.set('name', this.data.name)
  266. Profile.set('mobile', this.data.phone)
  267. try {
  268. Profile.save()
  269. wx.showToast({
  270. title: '提交成功',
  271. icon: 'success',
  272. duration: 2000
  273. })
  274. setTimeout(() => {
  275. this.goback()
  276. }, 2000);
  277. console.log('部门保存成功');
  278. } catch {
  279. console.log('部门保存失败');
  280. }
  281. } else {
  282. const currentUser = Parse.User.current();
  283. let Userquery = new Parse.Query('_User');
  284. Userquery.equalTo('company', company);
  285. Userquery.equalTo('objectId', currentUser.id);
  286. Userquery.notEqualTo('isDeleted', true)
  287. let P2 = await Userquery.first();
  288. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  289. console.log(companyPointer);
  290. let Profile = new Parse.Object('Profile');
  291. Profile.set('company', companyPointer);
  292. Profile.set('isCheck', false);
  293. Profile.set('user', P2.toPointer());
  294. Profile.set('department', Department.toPointer());
  295. Profile.set('center', union.toPointer());
  296. Profile.set('name', this.data.name)
  297. Profile.set('mobile', this.data.phone)
  298. try {
  299. Profile.save()
  300. wx.showToast({
  301. title: '提交成功',
  302. icon: 'success',
  303. duration: 2000
  304. })
  305. setTimeout(() => {
  306. this.goback()
  307. }, 2000);
  308. console.log('部门保存成功');
  309. } catch {
  310. console.log('部门保存失败');
  311. }
  312. }
  313. }
  314. },
  315. // 获取部门信息
  316. async getDepartment() {
  317. const currentUser = Parse.User.current();
  318. let Userquery = new Parse.Query('Department');
  319. Userquery.equalTo('company', company); // 假设 company 是你要查询的公司对象
  320. Userquery.equalTo('isEnabled', true);
  321. Userquery.notEqualTo('isDeleted', true);
  322. Userquery.include('parent');
  323. let P2 = await Userquery.find();
  324. let Departmentlist = P2.map(item => item.toJSON());
  325. // 创建一个以工会为键的对象,以便于匹配
  326. const unionsMap = {};
  327. console.log(Departmentlist);
  328. // 遍历部门列表,构建工会映射
  329. Departmentlist.forEach(department => {
  330. //有工会的部门
  331. if (department.parent && department.parent.objectId) {
  332. const unionId = department.parent.objectId; //工会id
  333. // 如果工会还没有在映射中,创建一个新的工会对象
  334. if (!unionsMap[unionId]) {
  335. unionsMap[unionId] = {
  336. text: department.parent.name, // 工会名称
  337. objectId: department.parent.objectId,
  338. children: [] // 初始化子部门数组
  339. };
  340. }
  341. // 将部门添加到对应的工会子项中
  342. unionsMap[unionId].children.push({
  343. text: department.name, // 部门名称
  344. id: department.objectId, // 部门 ID
  345. upid: department.parent.objectId
  346. });
  347. } else {
  348. if (!unionsMap[department.objectId]) {
  349. unionsMap[department.objectId] = {
  350. text: department.name, // 工会名称
  351. objectId: department.objectId,
  352. children: [] // 初始化子部门数组
  353. };
  354. }
  355. }
  356. });
  357. // 将映射转换为数组
  358. const result = Object.values(unionsMap);
  359. console.log(result);
  360. this.setData({
  361. items: result
  362. })
  363. console.log(this.data.items);
  364. },
  365. //点击工会
  366. onClickNav({
  367. detail = {}
  368. }) {
  369. this.setData({
  370. mainActiveIndex: detail.index || 0,
  371. });
  372. console.log(detail);
  373. },
  374. //点击部门
  375. onClickItem({
  376. detail = {}
  377. }) {
  378. const activeId = this.data.activeId === detail.id ? null : detail.id;
  379. this.data.items.forEach((item) => {
  380. if (item.objectId == detail.upid) {
  381. this.setData({
  382. departname: item.text
  383. })
  384. }
  385. })
  386. this.setData({
  387. activeId,
  388. selectname: detail.text,
  389. selectobjectid: detail.id,
  390. upiobjectid: detail.upid
  391. });
  392. this.onClickHide()
  393. console.log(detail);
  394. },
  395. //显示遮罩层
  396. onClickShow() {
  397. this.setData({
  398. show: true
  399. });
  400. },
  401. //关闭遮罩层
  402. onClickHide() {
  403. this.setData({
  404. show: false
  405. });
  406. },
  407. //返回上一页
  408. goback() {
  409. wx.navigateBack({
  410. delta: 1 // 返回上一页
  411. });
  412. },
  413. //删除头像
  414. deleteavater() {
  415. this.setData({
  416. avatar: ''
  417. })
  418. },
  419. async getUptoken() {
  420. let res = await Parse.Cloud.run('qiniu_uptoken', {
  421. company: company
  422. })
  423. this.setData({
  424. uptokenURL: res.uptoken,
  425. domain: res.domain,
  426. uploadURL: res.zoneUrl
  427. })
  428. console.log(this.data.uptokenURL, this.data.domain, this.data.uploadURL);
  429. },
  430. //本地上传头像
  431. picture(event) {
  432. console.log('event', event);
  433. let FileList = event.detail
  434. let url = []
  435. for (let i = 0; i < FileList.length; i++) {
  436. url.push(FileList[i].url)
  437. }
  438. this.setData({
  439. fileList: url
  440. })
  441. this.setData({
  442. avatar: this.data.fileList[0]
  443. })
  444. console.log('图片', this.data.fileList);
  445. },
  446. //修改姓名
  447. changename(e) {
  448. this.setData({
  449. name: e.detail
  450. })
  451. console.log(this.data.name);
  452. },
  453. //修改电话
  454. changephone(e) {
  455. const mobileNumber = e.detail.value; // 假设手机号是通过 e.detail.value 传递的
  456. console.log(mobileNumber);
  457. // 正则表达式:匹配中国大陆手机号
  458. const mobilePattern = /^1[3-9]\d{9}$/;
  459. // 验证手机号格式
  460. if (mobilePattern.test(mobileNumber)) {
  461. this.setData({
  462. phone: mobileNumber
  463. })
  464. console.log("手机号格式正确");
  465. // 这里可以执行其他操作,比如保存手机号或进行下一步操作
  466. } else {
  467. wx.showToast({
  468. title: '手机号格式不正确',
  469. icon: 'none',
  470. })
  471. console.log("手机号格式不正确");
  472. // 这里可以提示用户手机号格式不正确
  473. }
  474. console.log(this.data.phone);
  475. }
  476. })