textbook.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. import { Injectable } from '@angular/core';
  2. import Parse from 'parse';
  3. import { HttpClient } from '@angular/common/http';
  4. import { updateDept } from './importDept';
  5. import { NzMessageService } from 'ng-zorro-antd/message';
  6. @Injectable({
  7. providedIn: 'root',
  8. })
  9. export class textbookServer {
  10. company: string = localStorage.getItem('company')!;
  11. theme: boolean = false; //深色主题模式
  12. profile: any = JSON.parse(localStorage.getItem('profile')!);
  13. constructor(private http: HttpClient) {}
  14. authMobile(mobile: string): boolean {
  15. let a = /^1[3456789]\d{9}$/;
  16. if (!String(mobile).match(a)) {
  17. return false;
  18. }
  19. return true;
  20. }
  21. randomPassword(): string {
  22. let sCode =
  23. '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';
  24. let aCode = sCode.split(',');
  25. let aLength = aCode.length; //获取到数组的长度
  26. let str = [];
  27. for (let i = 0; i <= 16; i++) {
  28. let j = Math.floor(Math.random() * aLength); //获取到随机的索引值
  29. let txt = aCode[j]; //得到随机的一个内容
  30. str.push(txt);
  31. }
  32. return str.join('');
  33. }
  34. formatTime(fmt: string, date1: Date) {
  35. let ret;
  36. let date = new Date(date1);
  37. const opt: any = {
  38. 'Y+': date.getFullYear().toString(), // 年
  39. 'm+': (date.getMonth() + 1).toString(), // 月
  40. 'd+': date.getDate().toString(), // 日
  41. 'H+': date.getHours().toString(), // 时
  42. 'M+': date.getMinutes().toString(), // 分
  43. 'S+': date.getSeconds().toString(), // 秒
  44. // 有其他格式化字符需求可以继续添加,必须转化成字符串
  45. };
  46. for (let k in opt) {
  47. ret = new RegExp('(' + k + ')').exec(fmt);
  48. if (ret) {
  49. fmt = fmt.replace(
  50. ret[1],
  51. ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0')
  52. );
  53. }
  54. }
  55. return fmt;
  56. }
  57. //格式化链
  58. async formatNode(id: string): Promise<Array<any>> {
  59. let query = new Parse.Query('Department');
  60. query.select('name', 'parent', 'hasChildren', 'type', 'branch');
  61. let r = await query.get(id);
  62. let arr = [
  63. {
  64. title: r.get('name'),
  65. key: r.id,
  66. hasChildren: r.get('hasChildren'), //是否是最下级
  67. type: r.get('type'),
  68. branch: r?.get('branch'),
  69. parent: r?.get('parent')?.id, //上级
  70. },
  71. ];
  72. if (r?.get('parent')) {
  73. arr.unshift(...(await this.formatNode(r?.get('parent').id)));
  74. }
  75. return arr;
  76. }
  77. //获取下级所有部门
  78. async getChild(id: string): Promise<Array<string>> {
  79. console.log(id);
  80. let arr: Array<string> = [id];
  81. let query = new Parse.Query('Department');
  82. query.equalTo('parent', id);
  83. query.notEqualTo('isDeleted', true);
  84. query.select('id', 'hasChildren');
  85. query.limit(200);
  86. let r = await query.find();
  87. for (let index = 0; index < r.length; index++) {
  88. if (r[index].get('hasChildren')) {
  89. let child: Array<string> = await this.getChild(r[index].id);
  90. arr.push(...child);
  91. } else {
  92. arr.push(r[index].id);
  93. }
  94. }
  95. return Array.from(new Set([...arr]));
  96. }
  97. userFind(phone: string) {
  98. return new Promise((res) => {
  99. Parse.Cloud.run('userFind', { mobile: phone })
  100. .then((data) => {
  101. if (data) {
  102. res(false);
  103. } else {
  104. res(true);
  105. }
  106. })
  107. .catch(() => res(true));
  108. });
  109. }
  110. async tbookExportReport(options: { processId?: string; bookList?: any[] }) {
  111. let url = Parse.serverURL + '/api/tbook/export';
  112. // console.log(url)
  113. let response = await fetch(url, {
  114. method: 'POST',
  115. headers: {
  116. 'Content-Type': 'application/json',
  117. 'X-Parse-Application-Id': 'edu-textbook',
  118. },
  119. body: JSON.stringify(options),
  120. });
  121. let result = await response.json();
  122. let zipUrl = result?.result?.zipUrl;
  123. if (result?.code == 200) {
  124. zipUrl = zipUrl.replaceAll('http://', 'https://');
  125. const a = document.createElement('a'); // 创建一个&lt;a&gt;元素
  126. a.href = zipUrl; // 设置链接的href属性为要下载的文件的URL
  127. a.download = '报送流程'; // 设置下载文件的名称
  128. document.body.appendChild(a); // 将&lt;a&gt;元素添加到文档中
  129. a.click(); // 模拟点击&lt;a&gt;元素
  130. document.body.removeChild(a); // 下载后移除&lt;a&gt;元素
  131. }
  132. // console.log(result)
  133. return result;
  134. Parse.Cloud.run('tbookExportReport', options).then((data) => {
  135. console.log(data);
  136. let url = data.zipUrl;
  137. url = url.replaceAll('http://', 'https://');
  138. const a = document.createElement('a'); // 创建一个&lt;a&gt;元素
  139. a.href = url; // 设置链接的href属性为要下载的文件的URL
  140. a.download = '报送流程'; // 设置下载文件的名称
  141. document.body.appendChild(a); // 将&lt;a&gt;元素添加到文档中
  142. a.click(); // 模拟点击&lt;a&gt;元素
  143. document.body.removeChild(a); // 下载后移除&lt;a&gt;元素
  144. });
  145. }
  146. async getEduProcess(id: string): Promise<string | undefined> {
  147. if (!id) return;
  148. let query = new Parse.Query('EduProcess');
  149. query.equalTo('department', id);
  150. query.lessThanOrEqualTo('startDate', new Date());
  151. query.greaterThan('deadline', new Date());
  152. query.notEqualTo('isDeleted', true);
  153. query.containedIn('status', ['200', '300']);
  154. query.select('objectId');
  155. let res = await query.first();
  156. return res?.id;
  157. }
  158. //需要删除是否存在为联系人情况
  159. async getEduProcessProf(filter: Array<string>): Promise<boolean> {
  160. let query = new Parse.Query('EduProcess');
  161. query.notEqualTo('isDeleted', true);
  162. query.containedIn('profileSubmitted', filter);
  163. query.select('objectId');
  164. let res = await query.first();
  165. if (res?.id) {
  166. return true;
  167. }
  168. return false;
  169. }
  170. //更新工作联系人
  171. async updateProfileSubmitted(
  172. pid: string,
  173. type: string,
  174. dpid?: string,
  175. msg?: NzMessageService
  176. ): Promise<boolean | undefined> {
  177. console.log(pid, type, dpid);
  178. let query = new Parse.Query('EduProcess');
  179. if(!dpid){
  180. query.equalTo('profileSubmitted', pid);
  181. }else{
  182. query.equalTo('department', dpid);
  183. }
  184. query.include('profileSubmitted');
  185. query.select('objectId', 'profileSubmitted');
  186. query.notEqualTo('isDeleted',true)
  187. let res = await query.first();
  188. if (!res?.id) {
  189. msg?.warning('所属单位不存在')
  190. return false;
  191. }
  192. if (type == 'del') {
  193. //删除工作联系人
  194. res.set('profileSubmitted', null);
  195. await res.save();
  196. return true;
  197. } else {
  198. //添加工作联系人
  199. if (res?.get('profileSubmitted')?.get('user')) {
  200. msg?.warning('该单位已有部门联系人,请先移除后再操作')
  201. return false;
  202. }
  203. res?.set('profileSubmitted',{
  204. __type: 'Pointer',
  205. className: 'Profile',
  206. objectId: pid,
  207. })
  208. await res.save()
  209. return true
  210. }
  211. return false;
  212. }
  213. /* 批量预设(临时) */
  214. async saveProcess() {
  215. // let count = 0
  216. // let query = new Parse.Query('EduProcess')
  217. // // query.equalTo('num',null)
  218. // query.notEqualTo('isDeleted',true)
  219. // // query.select('name','num')
  220. // query.limit(2000)
  221. // query.select('objectId')
  222. // let res = await query.find()
  223. // console.log(res);
  224. // for (let index = 0; index < res.length; index++) {
  225. // const item = res[index]
  226. // item?.set('startDate', new Date('2024-08-05 8:00'));
  227. // item?.set('deadline', new Date('2024-09-30 23:59'));
  228. // await item.save()
  229. // console.log(count);
  230. // }
  231. // let list = updateDept.list2
  232. // for (let index = 0; index < list.length; index++) {
  233. // const item = list[index]
  234. // let queryPareet = new Parse.Query('Department')
  235. // queryPareet.equalTo('code', item.code)
  236. // queryPareet.select('name')
  237. // let r = await queryPareet.first()
  238. // if(r?.id){
  239. // if(r.get('name') != item.new){
  240. // r.set('name',item.new)
  241. // await r.save()
  242. // let queryEdupro = new Parse.Query('EduProcess')
  243. // queryEdupro.equalTo('department', r.id)
  244. // queryEdupro.select('objectId')
  245. // let eduProcess = await queryEdupro.first()
  246. // eduProcess?.set('name', item.new);
  247. // eduProcess?.set('desc', item.new + '流程');
  248. // await eduProcess?.save()
  249. // count++
  250. // console.log('已修改:',item.old,item.new,count);
  251. // }
  252. // }else{
  253. // console.log('未找到code:',item);
  254. // }
  255. // }
  256. // let query = new Parse.Query('Department')
  257. // query.equalTo('branch', '中央有关部门教育司')
  258. // query.limit(2000);
  259. // let r = await query.find()
  260. // console.log(r);
  261. // for (let index = 0; index < r.length; index++) {
  262. // const element = r[index];
  263. // element.set('branch', '有关部门(单位)教育司(局)')
  264. // await element.save()
  265. // count++
  266. // console.log(count);
  267. // }
  268. // let query = new Parse.Query('Department')
  269. // query.equalTo('parent',null)
  270. // query.equalTo('name','省级教育行政部门')
  271. // let r = await query.find()
  272. // let addCount = 0
  273. // let list = updateDept.list3
  274. // for (let index = 0; index < list.length; index++) {
  275. // const item = list[index];
  276. // let queryPareet = new Parse.Query('Department')
  277. // queryPareet.equalTo('name', item.name)
  278. // let dep = await queryPareet.first()
  279. // console.log(dep?.get('name'));
  280. // // for (let index = 0; index < prents.length; index++) {
  281. // // let item = prents[index];
  282. // let query = new Parse.Query('EduProcess')
  283. // query.equalTo('department',dep?.id)
  284. // query.notEqualTo('isDeleted',true)
  285. // let eduProcess = await query.first()
  286. // if(!eduProcess?.id){
  287. // console.log('不存在,新建'+dep?.get('name'),addCount);
  288. // let obj = Parse.Object.extend('EduProcess');
  289. // eduProcess = new obj()
  290. // }
  291. // eduProcess?.set('company', {
  292. // __type: 'Pointer',
  293. // className: 'Company',
  294. // objectId: 'RbIKpmuaMC',
  295. // });
  296. // eduProcess?.set('branch', {
  297. // __type: 'Pointer',
  298. // className: 'Department',
  299. // objectId:dep?.get('parent')?.id,
  300. // });
  301. // eduProcess?.set('department', {
  302. // __type: 'Pointer',
  303. // className: 'Department',
  304. // objectId: dep?.id,
  305. // });
  306. // eduProcess?.set('name', dep?.get('name'));
  307. // eduProcess?.set('desc',item.desc ? item.desc : dep?.get('name') + '流程');
  308. // eduProcess?.set('code', dep?.get('code') || dep?.id);
  309. // if(dep?.get('branch') == '出版单位') {
  310. // eduProcess?.set('startDate', new Date('2024-07-20 18:00'));
  311. // eduProcess?.set('deadline', new Date('2024-09-20 18:00'));
  312. // }
  313. // eduProcess?.set('num',item.num)
  314. // eduProcess?.set('status', '200');
  315. // await eduProcess?.save();
  316. // count ++
  317. // console.log('已完成'+dep?.get('name')+'流程');
  318. // console.log(count);
  319. // // }
  320. // }
  321. }
  322. }