factory.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. starter.factory('Tool', function ($http, $q, $translate, $ionicHistory) {
  2. var tempData = {};
  3. var translates = {
  4. "weekdays": ['sundayTitle', 'mondayTitle', 'tuesdayTitle', 'wednesdayTitle', 'thursdayTitle', 'fridayTitle', 'saturdayTitle']
  5. };
  6. return {
  7. trim: function (str) {
  8. if (str == null) {
  9. str = "";
  10. }
  11. return str.replace(/(^\s*)|(\s*$)/g, "");
  12. },
  13. getTempData: function (key) {
  14. return tempData[key]
  15. },
  16. setTempData: function (key) {
  17. tempData[key] = []
  18. },
  19. get: function (url, iscache, key) {
  20. var deferred = $q.defer();
  21. iscache = iscache || false;
  22. $http.get(url).then(function (res) {
  23. deferred.resolve(res.data);
  24. if (iscache)
  25. tempData[key] = res.data;
  26. },
  27. function (err) {
  28. deferred.reject(err);
  29. });
  30. return deferred.promise;
  31. },
  32. get_nextpage: function (url) {
  33. var deferred = $q.defer();
  34. $http.get(url).then(function (res) {
  35. deferred.resolve(res);
  36. },
  37. function (err) {
  38. deferred.reject(err);
  39. });
  40. return deferred.promise;
  41. },
  42. post: function (url, data, isjson) {
  43. var deferred = $q.defer();
  44. var header = null;
  45. if (isjson == false) {
  46. header = {
  47. 'Content-Type': 'application/x-www-form-urlencoded'
  48. };
  49. }
  50. $http.post(url, data, {
  51. headers: header
  52. }).then(function (res) {
  53. deferred.resolve(res.data);
  54. },
  55. function (err) {
  56. deferred.reject(err);
  57. })
  58. return deferred.promise;
  59. },
  60. patch: function (url, data) {
  61. var deferred = $q.defer();
  62. $http.patch(url, data).then(function (res) {
  63. deferred.resolve(res.data);
  64. }, function (err) {
  65. deferred.resolve(err);
  66. });
  67. return deferred.promise;
  68. },
  69. put: function (url, data, iscache) {
  70. var deferred = $q.defer();
  71. iscache = iscache || false;
  72. $http.put(url, data).then(function (res) {
  73. deferred.resolve(res.data);
  74. if (iscache)
  75. tempData = res.data;
  76. },
  77. function (err) {
  78. deferred.reject(err);
  79. });
  80. return deferred.promise;
  81. },
  82. delete: function (url) {
  83. var deferred = $q.defer();
  84. $http.delete(url).then(function (res) {
  85. deferred.resolve(res.data);
  86. },
  87. function (err) {
  88. deferred.reject(err);
  89. });
  90. return deferred.promise;
  91. },
  92. getTranslateByKey: function (type, key) {
  93. if (translates[type] === undefined || translates[type][key] === undefined)
  94. return null;
  95. return $translate.instant(translates[type][key]);
  96. },
  97. removeBackView: function (routes) {
  98. var view = $ionicHistory.backView();
  99. if (!view)
  100. return;
  101. if (!(_.isArray(routes)))
  102. routes = [routes];
  103. _.find(routes, function (route) {
  104. if (view.stateName == route) {
  105. return $ionicHistory.removeBackView();
  106. }
  107. });
  108. }
  109. }
  110. })
  111. .factory('showPopup', function ($timeout, $ionicLoading, $ionicPopup, $rootScope, $ionicModal) {
  112. var typeArr = [
  113. '<i class="icon icon-warn" style="font-size:3em;"></i><br/>',
  114. '<ion-spinner icon="android"></ion-spinner><br/>'
  115. ];
  116. function showLoadingToast(type, text, isShowBackDrop) {
  117. if (isShowBackDrop == undefined) {
  118. isShowBackDrop = true;
  119. }
  120. // Setup the loader
  121. $ionicLoading.show({
  122. template: typeArr[type] + text,
  123. animation: 'fade-in',
  124. showBackdrop: isShowBackDrop,
  125. maxWidth: 200,
  126. Showdelay: 0
  127. });
  128. }
  129. function hideLoadingToast() {
  130. $ionicLoading.hide();
  131. }
  132. function popupInfo(type, text, isShowBackDrop, delay) {
  133. if (delay == undefined) {
  134. delay = 1500;
  135. }
  136. //弹出缓冲提示
  137. showLoadingToast(type, text, isShowBackDrop);
  138. //这里使用定时器是为了缓存一下加载过程,防止加载过快
  139. $timeout(function () {
  140. //停止缓冲提示
  141. hideLoadingToast();
  142. }, delay);
  143. }
  144. return {
  145. //触发函数
  146. PopupWindow: function (type, text, isShowBackDrop, delay) {
  147. popupInfo(type, text, isShowBackDrop, delay);
  148. },
  149. hideLoading: function () {
  150. hideLoadingToast();
  151. $rootScope.commons.isShowPopup = false;
  152. },
  153. showLoading: function (type, text, isShowBackDrop, delay) {
  154. showLoadingToast(type, text, isShowBackDrop);
  155. $rootScope.commons.isShowPopup = true;
  156. delay = delay || 10000;
  157. $timeout(function () {
  158. if ($rootScope.commons.isShowPopup) {
  159. $ionicLoading.hide();
  160. console.log(self);
  161. popupInfo(0, '请求超时!', false);
  162. $rootScope.commons.isShowPopup = false;
  163. }
  164. }, delay);
  165. },
  166. confirm: function (title, okText, cancelText) {
  167. var confirmpopup = $ionicPopup.confirm({
  168. title: '<br/><div>' + title + '</div><br/>',
  169. okText: okText,
  170. cancelText: cancelText
  171. });
  172. return confirmpopup;
  173. },
  174. showSelectImgPopup: function (camera, imagePicker, scope) {
  175. var optionsPopup = $ionicPopup.show({
  176. templateUrl: '../../templates/template-selectimg.html',
  177. scope: scope
  178. });
  179. scope.Camera = camera;
  180. scope.ImagePicker = imagePicker;
  181. return optionsPopup;
  182. },
  183. modalTemplate: function (templateurl, animation, scope) {
  184. return $ionicModal.fromTemplateUrl(templateurl, {
  185. scope: scope,
  186. animation: animation,
  187. focusFirstInput: true
  188. })
  189. }
  190. }
  191. })
  192. .factory('ImageManage', function ($q, $cordovaCamera, $cordovaImagePicker, $cordovaFileTransfer, $cordovaFile, $http, cfg, formatFilter, Tool) {
  193. var Qiniu_UploadUrl = "http://up.qiniu.com";
  194. function S4() {
  195. return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  196. }
  197. function guid() {
  198. return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
  199. }
  200. return {
  201. ImagePicker_getPictures: function (_imagecount) {
  202. if (_imagecount == undefined) {
  203. _imagecount = 1;
  204. }
  205. var deferred = $q.defer();
  206. var options = {
  207. maximumImagesCount: _imagecount,
  208. quality: 30
  209. };
  210. var promises = [];
  211. $cordovaImagePicker.getPictures(options).then(function (results) {
  212. _.each(results, function (res) {
  213. var deferred1 = $q.defer();
  214. window.resolveLocalFileSystemURL(res, function (fileEntry) {
  215. fileEntry.file(function (fileObj) {
  216. var reader = new FileReader();
  217. reader.onloadend = function (e) {
  218. deferred1.resolve("data:image/jpeg;base64," + window.btoa(e.target.result))
  219. };
  220. reader.readAsBinaryString(fileObj);
  221. }, function (err) {
  222. alert(JSON.stringify(err))
  223. })
  224. });
  225. promises.push(deferred1.promise);
  226. });
  227. deferred.resolve(promises);
  228. }, function (err) {
  229. deferred.reject(err);
  230. });
  231. return deferred.promise;
  232. },
  233. Camera_getPicture: function (_allowEdit) {
  234. if (_allowEdit == undefined) {
  235. _allowEdit = false;
  236. }
  237. var deferred = $q.defer();
  238. var options = {
  239. quality: 30,
  240. destinationType: Camera.DestinationType.DATA_URL,
  241. sourceType: Camera.PictureSourceType.CAMERA,
  242. allowEdit: _allowEdit,
  243. encodingType: Camera.EncodingType.JPEG,
  244. popoverOptions: CameraPopoverOptions,
  245. saveToPhotoAlbum: true,
  246. correctOrientation: true
  247. };
  248. $cordovaCamera.getPicture(options).then(function (imageData) {
  249. deferred.resolve("data:image/jpeg;base64," + imageData);
  250. }, function (err) {
  251. deferred.reject(err);
  252. });
  253. return deferred.promise;
  254. },
  255. upload: function (fileurls, token, _filepath) {
  256. var promises = [];
  257. var files = fileurls;
  258. var options = new FileUploadOptions();
  259. if (!_.isArray(files)) {
  260. files = [];
  261. files.push(fileurls);
  262. }
  263. _.each(files, function (itemfile) {
  264. var params = {};
  265. options.params = params;
  266. params.token = token;
  267. params.mimeType = "image/jpg";
  268. params.key = _filepath + guid() + itemfile.substr(itemfile.lastIndexOf('.'));
  269. promises.push($cordovaFileTransfer.upload(Qiniu_UploadUrl, itemfile, options, true));
  270. });
  271. return $q.all(promises);
  272. },
  273. getToken: function (app, id, cls) {
  274. var url = formatFilter('{0}qiniuupload/?app={1}&cls={2}&pk={3}', cfg.api, app, cls, id);
  275. return $http.get(url);
  276. },
  277. get_upload_Token: function (_url, compid) {
  278. var url = formatFilter('{0}{1}/{2}/', cfg.api, _url, compid);
  279. return $http.get(url);
  280. },
  281. uploadImage: function (fileurls, app, id, cls) {
  282. var url = formatFilter('{0}qiniuupload/?app={1}&cls={2}&pk={3}', cfg.api, app, cls, id);
  283. var defer_promises = $q.defer();
  284. Tool.get(url).then(function (data) {
  285. var promises = [];
  286. var options = new FileUploadOptions();
  287. var params = {};
  288. options.params = params;
  289. params.token = data.token;
  290. params.mimeType = "image/jpg";
  291. _.each(_.isArray(fileurls) ? fileurls : [fileurls], function (itemfile) {
  292. params.key = data.filepath + guid();
  293. var defer_up = $q.defer();
  294. $cordovaFileTransfer.upload(Qiniu_UploadUrl, itemfile, options, true).then(function (res) {
  295. defer_up.resolve(res);
  296. }, function (err) {
  297. console.log(JSON.stringify(err));
  298. defer_up.reject(err);
  299. });
  300. promises.push(defer_up.promise);
  301. });
  302. defer_promises.resolve(promises);
  303. }, function (err) {
  304. defer_promises.reject(err);
  305. alert('get upload token error' + JSON.stringify(err));
  306. });
  307. return defer_promises.promise;
  308. }
  309. }
  310. })
  311. .factory('Member', function () {
  312. var member = {};
  313. var defaultmember = {};
  314. member.resourcemember = []; //基础数据
  315. member.selectedemplst = []; //选中的员工数据
  316. member.selecteddepts = []; //选中的部门数据
  317. member.isallcheck = false; //是否选中全部成员(部门)
  318. member.unenabledata = [];//未启用的数据(禁用的数据)
  319. member.homerenderdata = []; //目前为展示部门数据
  320. member.routename = ''; //点击确定后需要跳转的路由名称,默认是goBack();
  321. member.routeparams = {}; //"确定" 路由参数
  322. member.cancelroutename = ''; //点击取消后需要跳转的路由名称,默认是goBack();
  323. member.cancelrouteparams = {}; //"取消" 路由参数
  324. member.titlename = "选择成员"; //页面显示的标题名称
  325. member.datatype = ''; //成员类型 0:不计考勤成员 1:可查看考勤统计成员
  326. member.disabled = true; //是否需要禁用基础数据
  327. member.hide = false; //是否需要隐藏基础数据
  328. member.dialogresult = 1; //0:是点击了取消按钮退出,1:点击了确定按钮
  329. member.showall = false; //默认不显示‘全部成员’栏位
  330. member.showgroup = false; //默认不显示‘选择小组’栏位
  331. member.all = 1; // 全部成员/部分成员 : 1/0
  332. member.customerop = null; //自定义操作
  333. member.emit = {is_have: false, name: ''};
  334. member.member = {};//用来存放相对应部门的员工数据,一部门ID作为key
  335. return member;
  336. })
  337. .factory('Dept', function ($http, $q, cfg, formatFilter, Tool, global) {
  338. var deptlst = [];
  339. var resultdept = new Array();
  340. var resultemp = new Array();
  341. return {
  342. selecteddepts: [], //存放被选中的部门集合
  343. titleName: {
  344. name: ''
  345. },
  346. save: function (up_depts, del_deptids, add_depts, compid) {
  347. var datas = [];
  348. if (del_deptids.length > 0) {
  349. datas.push({
  350. "op": "del",
  351. "values": del_deptids
  352. });
  353. }
  354. if (add_depts.length > 0) {
  355. datas.push({
  356. "op": "add",
  357. "values": add_depts
  358. });
  359. }
  360. if (up_depts.length > 0) {
  361. datas.push({
  362. "op": "up",
  363. "values": up_depts
  364. });
  365. }
  366. var url = formatFilter('{0}depts/?compid={1}', cfg.api, compid);
  367. return Tool.patch(url, datas)
  368. },
  369. all: function () {
  370. deptlst = Tool.getTempData('deptlist') || [];
  371. return deptlst;
  372. },
  373. setdeptlst: function (data) {
  374. deptlst = data;
  375. Tool.setTempData('deptlist')
  376. },
  377. getDep: function (compid) {
  378. var url = formatFilter('{0}depts/', cfg.api);
  379. if (compid) {
  380. url = formatFilter('{0}depts/?compid={1}', cfg.api, compid);
  381. }
  382. return Tool.get(url, true, 'deptlist')
  383. },
  384. getEmp: function (deptid, compid) {
  385. var url = formatFilter('{0}usrs/', cfg.api);
  386. if (deptid != undefined) {
  387. url = formatFilter('{0}usrs/?deptid={1}', cfg.api, deptid);
  388. }
  389. if (compid != undefined && deptid != undefined) {
  390. url = formatFilter('{0}usrs/?compid={1}&deptid={2}', cfg.api, compid, deptid);
  391. }
  392. return Tool.get(url)
  393. },
  394. getChildDept: function (pid) {
  395. return _.filter(this.all(), function (dept) {
  396. return parseInt(dept.parent) == parseInt(pid);
  397. });
  398. },
  399. getUsrDetail: function () {
  400. return Tool.get(formatFilter('{0}usrs/{1}/?compid={2}', cfg.api, global.user.usrid, global.user.compno), true, 'userinfo');
  401. },
  402. putUsrInfo: function (data) {
  403. data.dept = data.dept_id;
  404. data = _.pick(data, 'username', 'sex', 'dept', 'degree', 'email');
  405. return Tool.put(formatFilter('{0}usrs/{1}/', cfg.api, global.user.usrid), data)
  406. }
  407. };
  408. })
  409. .factory('General', function ($http, $q, cfg, global, formatFilter) {
  410. var Comment = {};
  411. var Read = {};
  412. return {
  413. getComment: function (id, module) {
  414. var url = formatFilter('{0}{1}/comments/?{2}id={3}', cfg.api, module, module, id);
  415. var deferred = $q.defer();
  416. $http.get(url).then(function (res) {
  417. deferred.resolve(res.data);
  418. Comment = res.data;
  419. },
  420. function (err) {
  421. deferred.reject(err);
  422. });
  423. return deferred.promise;
  424. },
  425. getRead: function (id, module, type) {
  426. var url = formatFilter('{0}{1}/readeds/?{2}id={3}', cfg.api, module, module, id);
  427. if (type != undefined) {
  428. url += '&type=unreads'
  429. }
  430. var deferred = $q.defer();
  431. $http.get(url).then(function (res) {
  432. deferred.resolve(res.data);
  433. },
  434. function (err) {
  435. deferred.reject(err);
  436. });
  437. return deferred.promise;
  438. },
  439. postComment: function (data, module) {
  440. var url = formatFilter('{0}{1}/comment/', cfg.api, module);
  441. var deferred = $q.defer();
  442. $http.post(url, data, {
  443. headers: {
  444. 'Content-Type': 'application/json'
  445. }
  446. }).then(function (res) {
  447. console.log(res.data)
  448. deferred.resolve(res.data);
  449. res.data.user__username = global.user.usrname;
  450. res.data.user_id = global.user.usrid;
  451. res.data.create_dd = new Date();
  452. Comment.splice(0, 0, res.data);
  453. },
  454. function (err) {
  455. deferred.reject(err);
  456. });
  457. return deferred.promise;
  458. },
  459. postRead: function (data, module) {
  460. console.log(Read)
  461. var url = formatFilter('{0}{1}/readeds/', cfg.api, module);
  462. var deferred = $q.defer();
  463. $http.post(url, data, {
  464. headers: {
  465. 'Content-Type': 'application/json'
  466. }
  467. }).then(function (res) {
  468. deferred.resolve(res.data);
  469. },
  470. function (err) {
  471. deferred.reject(err);
  472. });
  473. return deferred.promise;
  474. },
  475. deleteComment: function (id, module) {
  476. var comment = _.find(Comment, function (item) {
  477. if (item.id === parseInt(id)) {
  478. return item;
  479. }
  480. })
  481. if (comment != undefined) {
  482. Comment.splice(Comment.indexOf(comment), 1);
  483. var url = formatFilter('{0}{1}/comment/{2}/', cfg.api, module, id);
  484. return $http.delete(url);
  485. }
  486. }
  487. }
  488. })