textbook.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import { Injectable } from '@angular/core';
  2. import Parse from 'parse';
  3. import { HttpClient } from '@angular/common/http';
  4. import { NzMessageService } from 'ng-zorro-antd/message';
  5. // import { ProvierOssAli } from '../app/comp-upload/provider-oss-aliyun';
  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. if (!id) return [];
  60. let query = new Parse.Query('Department');
  61. query.select('name', 'parent.name', 'branch', 'hasChildren','type');
  62. let r = await query.get(id);
  63. let arr = [
  64. {
  65. title: r.get('name'),
  66. key: r.id,
  67. hasChildren: r.get('hasChildren'), //是否是最下级
  68. type: r.get('type'),
  69. branch: r?.get('branch'),
  70. parent: r?.get('parent')?.id, //上级
  71. },
  72. ];
  73. if (r?.get('parent')) {
  74. arr.unshift(...(await this.formatNode(r?.get('parent').id)));
  75. }
  76. return arr;
  77. }
  78. //获取下级所有部门
  79. async getChild(id: string): Promise<Array<string>> {
  80. // console.log(id);
  81. let arr: Array<string> = [id];
  82. let query = new Parse.Query('Department');
  83. query.equalTo('parent', id);
  84. query.notEqualTo('isDeleted', true);
  85. query.select('id', 'hasChildren');
  86. query.limit(500);
  87. let r = await query.find();
  88. for (let index = 0; index < r.length; index++) {
  89. if (r[index].get('hasChildren')) {
  90. let child: Array<string> = await this.getChild(r[index].id);
  91. arr.push(...child);
  92. } else {
  93. arr.push(r[index].id);
  94. }
  95. }
  96. return Array.from(new Set([...arr]));
  97. }
  98. userFind(phone: string) {
  99. return new Promise((res) => {
  100. Parse.Cloud.run('userFind', { mobile: phone })
  101. .then((data) => {
  102. if (data) {
  103. res(false);
  104. } else {
  105. res(true);
  106. }
  107. })
  108. .catch(() => res(true));
  109. });
  110. }
  111. async tbookExportReport(options: { processId?: string; bookList?: any[] }) {
  112. let url = Parse.serverURL + '/api/tbook/export';
  113. // console.log(url)
  114. let response = await fetch(url, {
  115. method: 'POST',
  116. headers: {
  117. 'Content-Type': 'application/json',
  118. 'X-Parse-Application-Id': 'edu-textbook',
  119. },
  120. body: JSON.stringify(options),
  121. });
  122. let result = await response.json();
  123. let zipUrl = result?.result?.zipUrl;
  124. if (result?.code == 200) {
  125. zipUrl = zipUrl.replaceAll('http://', 'https://');
  126. const a = document.createElement('a'); // 创建一个&lt;a&gt;元素
  127. a.href = zipUrl; // 设置链接的href属性为要下载的文件的URL
  128. a.download = '报送流程'; // 设置下载文件的名称
  129. document.body.appendChild(a); // 将&lt;a&gt;元素添加到文档中
  130. a.click(); // 模拟点击&lt;a&gt;元素
  131. document.body.removeChild(a); // 下载后移除&lt;a&gt;元素
  132. }
  133. // console.log(result)
  134. return result;
  135. Parse.Cloud.run('tbookExportReport', options).then((data) => {
  136. console.log(data);
  137. let url = data.zipUrl;
  138. url = url.replaceAll('http://', 'https://');
  139. const a = document.createElement('a'); // 创建一个&lt;a&gt;元素
  140. a.href = url; // 设置链接的href属性为要下载的文件的URL
  141. a.download = '报送流程'; // 设置下载文件的名称
  142. document.body.appendChild(a); // 将&lt;a&gt;元素添加到文档中
  143. a.click(); // 模拟点击&lt;a&gt;元素
  144. document.body.removeChild(a); // 下载后移除&lt;a&gt;元素
  145. });
  146. }
  147. async preview(options: { bookid: string }) {
  148. let url = Parse.serverURL + '/api/tbook/preview';
  149. let response = await fetch(url, {
  150. method: 'POST',
  151. headers: {
  152. 'Content-Type': 'application/json',
  153. 'X-Parse-Application-Id': 'edu-textbook',
  154. },
  155. body: JSON.stringify(options),
  156. });
  157. let result = await response.json();
  158. let { urlPdf } = result?.data;
  159. console.log(urlPdf);
  160. window.open(urlPdf);
  161. }
  162. async getEduProcess(id: string): Promise<string | undefined> {
  163. if (!id) return;
  164. let query = new Parse.Query('EduProcess');
  165. query.equalTo('department', id);
  166. query.lessThanOrEqualTo('startDate', new Date());
  167. query.greaterThan('deadline', new Date());
  168. query.notEqualTo('isDeleted', true);
  169. query.containedIn('status', ['200', '300']);
  170. query.select('objectId');
  171. let res = await query.first();
  172. return res?.id;
  173. }
  174. //需要删除是否存在为联系人情况
  175. async getEduProcessProf(filter: Array<string>): Promise<string | undefined> {
  176. let query = new Parse.Query('EduProcess');
  177. query.notEqualTo('isDeleted', true);
  178. query.containedIn('profileSubmitted', filter);
  179. query.select('objectId', 'name');
  180. let res = await query.first();
  181. if (res?.id) {
  182. return res.get('name');
  183. }
  184. return;
  185. }
  186. //更新工作联系人
  187. async updateProfileSubmitted(
  188. pid: string,
  189. type: string,
  190. dpid?: string,
  191. msg?: NzMessageService
  192. ): Promise<boolean | undefined> {
  193. console.log(pid, type, dpid);
  194. let query = new Parse.Query('EduProcess');
  195. if (!dpid) {
  196. query.equalTo('profileSubmitted', pid);
  197. } else {
  198. query.equalTo('department', dpid);
  199. }
  200. query.include('profileSubmitted');
  201. query.select('objectId', 'profileSubmitted');
  202. query.notEqualTo('isDeleted', true);
  203. let res = await query.first();
  204. if (!res?.id) {
  205. msg?.warning('用户绑定的单位流程不存在');
  206. return false;
  207. }
  208. if (type == 'del') {
  209. //删除工作联系人
  210. res.set('profileSubmitted', null);
  211. await res.save();
  212. return true;
  213. } else {
  214. //添加工作联系人
  215. if (res?.get('profileSubmitted')?.get('user')) {
  216. msg?.warning('该单位已有部门联系人,请先移除后再操作');
  217. return false;
  218. }
  219. res?.set('profileSubmitted', {
  220. __type: 'Pointer',
  221. className: 'Profile',
  222. objectId: pid,
  223. });
  224. await res.save();
  225. return true;
  226. }
  227. return false;
  228. }
  229. }