import { Injectable } from '@angular/core'; import Parse from 'parse'; import { HttpClient } from '@angular/common/http'; import { updateDept } from './importDept'; import { NzMessageService } from 'ng-zorro-antd/message'; @Injectable({ providedIn: 'root', }) export class textbookServer { company: string = localStorage.getItem('company')!; theme: boolean = false; //深色主题模式 profile: any = JSON.parse(localStorage.getItem('profile')!); constructor(private http: HttpClient) {} authMobile(mobile: string): boolean { let a = /^1[3456789]\d{9}$/; if (!String(mobile).match(a)) { return false; } return true; } randomPassword(): string { let sCode = 'A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0,q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m'; let aCode = sCode.split(','); let aLength = aCode.length; //获取到数组的长度 let str = []; for (let i = 0; i <= 16; i++) { let j = Math.floor(Math.random() * aLength); //获取到随机的索引值 let txt = aCode[j]; //得到随机的一个内容 str.push(txt); } return str.join(''); } formatTime(fmt: string, date1: Date) { let ret; let date = new Date(date1); const opt: any = { 'Y+': date.getFullYear().toString(), // 年 'm+': (date.getMonth() + 1).toString(), // 月 'd+': date.getDate().toString(), // 日 'H+': date.getHours().toString(), // 时 'M+': date.getMinutes().toString(), // 分 'S+': date.getSeconds().toString(), // 秒 // 有其他格式化字符需求可以继续添加,必须转化成字符串 }; for (let k in opt) { ret = new RegExp('(' + k + ')').exec(fmt); if (ret) { fmt = fmt.replace( ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0') ); } } return fmt; } //格式化链 async formatNode(id: string): Promise> { let query = new Parse.Query('Department'); query.select('name', 'parent', 'hasChildren', 'type', 'branch'); let r = await query.get(id); let arr = [ { title: r.get('name'), key: r.id, hasChildren: r.get('hasChildren'), //是否是最下级 type: r.get('type'), branch: r?.get('branch'), parent: r?.get('parent')?.id, //上级 }, ]; if (r?.get('parent')) { arr.unshift(...(await this.formatNode(r?.get('parent').id))); } return arr; } //获取下级所有部门 async getChild(id: string): Promise> { console.log(id); let arr: Array = [id]; let query = new Parse.Query('Department'); query.equalTo('parent', id); query.notEqualTo('isDeleted', true); query.select('id', 'hasChildren'); query.limit(200); let r = await query.find(); for (let index = 0; index < r.length; index++) { if (r[index].get('hasChildren')) { let child: Array = await this.getChild(r[index].id); arr.push(...child); } else { arr.push(r[index].id); } } return Array.from(new Set([...arr])); } userFind(phone: string) { return new Promise((res) => { Parse.Cloud.run('userFind', { mobile: phone }) .then((data) => { if (data) { res(false); } else { res(true); } }) .catch(() => res(true)); }); } async tbookExportReport(options: { processId?: string; bookList?: any[] }) { let url = Parse.serverURL + '/api/tbook/export'; // console.log(url) let response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Parse-Application-Id': 'edu-textbook', }, body: JSON.stringify(options), }); let result = await response.json(); let zipUrl = result?.result?.zipUrl; if (result?.code == 200) { zipUrl = zipUrl.replaceAll('http://', 'https://'); const a = document.createElement('a'); // 创建一个<a>元素 a.href = zipUrl; // 设置链接的href属性为要下载的文件的URL a.download = '报送流程'; // 设置下载文件的名称 document.body.appendChild(a); // 将<a>元素添加到文档中 a.click(); // 模拟点击<a>元素 document.body.removeChild(a); // 下载后移除<a>元素 } // console.log(result) return result; Parse.Cloud.run('tbookExportReport', options).then((data) => { console.log(data); let url = data.zipUrl; url = url.replaceAll('http://', 'https://'); const a = document.createElement('a'); // 创建一个<a>元素 a.href = url; // 设置链接的href属性为要下载的文件的URL a.download = '报送流程'; // 设置下载文件的名称 document.body.appendChild(a); // 将<a>元素添加到文档中 a.click(); // 模拟点击<a>元素 document.body.removeChild(a); // 下载后移除<a>元素 }); } async getEduProcess(id: string): Promise { if (!id) return; let query = new Parse.Query('EduProcess'); query.equalTo('department', id); query.lessThanOrEqualTo('startDate', new Date()); query.greaterThan('deadline', new Date()); query.notEqualTo('isDeleted', true); query.containedIn('status', ['200', '300']); query.select('objectId'); let res = await query.first(); return res?.id; } //需要删除是否存在为联系人情况 async getEduProcessProf(filter: Array): Promise { let query = new Parse.Query('EduProcess'); query.notEqualTo('isDeleted', true); query.containedIn('profileSubmitted', filter); query.select('objectId'); let res = await query.first(); if (res?.id) { return true; } return false; } //更新工作联系人 async updateProfileSubmitted( pid: string, type: string, dpid?: string, msg?: NzMessageService ): Promise { console.log(pid, type, dpid); let query = new Parse.Query('EduProcess'); if(!dpid){ query.equalTo('profileSubmitted', pid); }else{ query.equalTo('department', dpid); } query.include('profileSubmitted'); query.select('objectId', 'profileSubmitted'); query.notEqualTo('isDeleted',true) let res = await query.first(); if (!res?.id) { msg?.warning('所属单位不存在') return false; } if (type == 'del') { //删除工作联系人 res.set('profileSubmitted', null); await res.save(); return true; } else { //添加工作联系人 if (res?.get('profileSubmitted')?.get('user')) { msg?.warning('该单位已有部门联系人,请先移除后再操作') return false; } res?.set('profileSubmitted',{ __type: 'Pointer', className: 'Profile', objectId: pid, }) await res.save() return true } return false; } /* 批量预设(临时) */ async saveProcess() { // let count = 0 // let query = new Parse.Query('EduProcess') // // query.equalTo('num',null) // query.notEqualTo('isDeleted',true) // // query.select('name','num') // query.limit(2000) // query.select('objectId') // let res = await query.find() // console.log(res); // for (let index = 0; index < res.length; index++) { // const item = res[index] // item?.set('startDate', new Date('2024-08-05 8:00')); // item?.set('deadline', new Date('2024-09-30 23:59')); // await item.save() // console.log(count); // } // let list = updateDept.list2 // for (let index = 0; index < list.length; index++) { // const item = list[index] // let queryPareet = new Parse.Query('Department') // queryPareet.equalTo('code', item.code) // queryPareet.select('name') // let r = await queryPareet.first() // if(r?.id){ // if(r.get('name') != item.new){ // r.set('name',item.new) // await r.save() // let queryEdupro = new Parse.Query('EduProcess') // queryEdupro.equalTo('department', r.id) // queryEdupro.select('objectId') // let eduProcess = await queryEdupro.first() // eduProcess?.set('name', item.new); // eduProcess?.set('desc', item.new + '流程'); // await eduProcess?.save() // count++ // console.log('已修改:',item.old,item.new,count); // } // }else{ // console.log('未找到code:',item); // } // } // let query = new Parse.Query('Department') // query.equalTo('branch', '中央有关部门教育司') // query.limit(2000); // let r = await query.find() // console.log(r); // for (let index = 0; index < r.length; index++) { // const element = r[index]; // element.set('branch', '有关部门(单位)教育司(局)') // await element.save() // count++ // console.log(count); // } // let query = new Parse.Query('Department') // query.equalTo('parent',null) // query.equalTo('name','省级教育行政部门') // let r = await query.find() // let addCount = 0 // let list = updateDept.list3 // for (let index = 0; index < list.length; index++) { // const item = list[index]; // let queryPareet = new Parse.Query('Department') // queryPareet.equalTo('name', item.name) // let dep = await queryPareet.first() // console.log(dep?.get('name')); // // for (let index = 0; index < prents.length; index++) { // // let item = prents[index]; // let query = new Parse.Query('EduProcess') // query.equalTo('department',dep?.id) // query.notEqualTo('isDeleted',true) // let eduProcess = await query.first() // if(!eduProcess?.id){ // console.log('不存在,新建'+dep?.get('name'),addCount); // let obj = Parse.Object.extend('EduProcess'); // eduProcess = new obj() // } // eduProcess?.set('company', { // __type: 'Pointer', // className: 'Company', // objectId: 'RbIKpmuaMC', // }); // eduProcess?.set('branch', { // __type: 'Pointer', // className: 'Department', // objectId:dep?.get('parent')?.id, // }); // eduProcess?.set('department', { // __type: 'Pointer', // className: 'Department', // objectId: dep?.id, // }); // eduProcess?.set('name', dep?.get('name')); // eduProcess?.set('desc',item.desc ? item.desc : dep?.get('name') + '流程'); // eduProcess?.set('code', dep?.get('code') || dep?.id); // if(dep?.get('branch') == '出版单位') { // eduProcess?.set('startDate', new Date('2024-07-20 18:00')); // eduProcess?.set('deadline', new Date('2024-09-20 18:00')); // } // eduProcess?.set('num',item.num) // eduProcess?.set('status', '200'); // await eduProcess?.save(); // count ++ // console.log('已完成'+dep?.get('name')+'流程'); // console.log(count); // // } // } } }