factory.js 19 KB

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