yufp.lookup.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * @created by helin3 2017-12-04
  3. * @updated by
  4. * @description 公共数据字典管理器
  5. * 依赖:custom/plugins/yufp.service.js
  6. */
  7. (function (yufp, window, factory) {
  8. var exports = factory(yufp, window, window.document);
  9. if (typeof define === 'function') {
  10. define(exports);
  11. }
  12. window.yufp.lookup = exports;
  13. }(yufp, window, function (yufp, window, document) {
  14. /**
  15. * 统一数据字典管理器
  16. * @constructor
  17. */
  18. function Lookup () {
  19. var _options = {
  20. lookupMgr: {}, // 内存字典对象
  21. remoteUrl: backend.adminService + '/api/adminsmlookupitem/weblist', // 远程URL
  22. remoteParamName: 'lookupCodes', // 远程参数名
  23. codeKey: 'key', // 对应后台字段key
  24. codeValue: 'value', // 对应后台字段value
  25. limit: false, // 是否开启字典长度超过limitlength长度转存
  26. limitLength: 100, // 字典长度超过100,直接存储于localstorage
  27. prefix: 'YUFP-LIMIT-TYPE-', // 超长字典前缀
  28. // 存储未注册的数据对象信息
  29. unRegArray: {}
  30. };
  31. yufp.extend(this, _options);
  32. this.loadLocal();
  33. }
  34. /**
  35. * private
  36. * 加载所有本地字典
  37. * @param callback 回调方法(可选参数)
  38. */
  39. Lookup.prototype.loadLocal = function (callback) {
  40. var _this = this;
  41. yufp.extend(_this.lookupMgr, window.localLookup);
  42. };
  43. /**
  44. * private
  45. * 加载指定远程数据字典
  46. * @param types String
  47. * GENDER_TYPE
  48. * GENDER_TYPE,USER_STATUS,SYSTEM_STATUS
  49. * @param callback 暂且未处理callback,20180731
  50. */
  51. Lookup.prototype.loadRemote = function (types, callback) {
  52. var _this = this;
  53. // TODO 暂且未考虑字典请求中队列
  54. _this.forceLoad(types, function (data) {
  55. var typeArr = types.split(',');
  56. types = null; // 闭包手工设置为null
  57. for (var i = 0, len = typeArr.length; i < len; i++) {
  58. var type = typeArr[i];
  59. if (!_this.limit) {
  60. _this.lookupMgr[type] = data[type] || [];
  61. } else {
  62. if (data[type].length < _this.limitLength) {
  63. _this.lookupMgr[type] = data[type];
  64. } else {
  65. _this.storagePut(_this.prefix + type, data[type]);
  66. }
  67. }
  68. // 回调异步未刷新的相关对象,再次执行检查
  69. _this.updateReg(typeArr[i]);
  70. }
  71. callback && callback();
  72. });
  73. };
  74. /**
  75. * 用于处理表格中异步数据显示文本失败时处理数据
  76. * @param {*} type 数据字典id
  77. */
  78. Lookup.prototype.updateReg = function(type){
  79. // 取出相关未注册的表格
  80. for (var item in this.unRegArray) {
  81. // 超过1分钟未处理的数据字典就不再处理了
  82. if (new Date().getTime() - this.unRegArray[item].time > 1 * 60 * 1000){
  83. delete this.unRegArray[item];
  84. } else {
  85. var arr = yufp.clone (this.unRegArray[item].lookupArray, []);
  86. // 遍历每个表格的未注册的数据字典
  87. for (var j = arr.length -1; j >= 0 ; j--) {
  88. var el = arr[j];
  89. if (el.dataCode === type) {
  90. // 找到就将以前未注册的字典表格列刷新
  91. this.unRegArray[item]['fn'] && this.unRegArray[item]['fn'](el);
  92. // 数据字典处理后就移除
  93. this.unRegArray[item].lookupArray.splice(j, 1);
  94. }
  95. }
  96. // 当表格的数据字典全部注册后就从unRegArray[item]中移除
  97. if (this.unRegArray[item].lookupArray.length === 0) {
  98. this.unRegArray[item].fn = null;
  99. delete this.unRegArray[item];
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * private
  106. * 强制刷新远程字典
  107. * @param types
  108. * @param callback
  109. */
  110. Lookup.prototype.forceLoad = function (types, callback) {
  111. var _this = this;
  112. var param = {};
  113. param[_this.remoteParamName] = types;
  114. yufp.service.request({
  115. url: _this.remoteUrl,
  116. method: 'get',
  117. data: param,
  118. callback: function (code, msg, response) {
  119. if (code == 0 && response && yufp.type(callback) == 'function') {
  120. callback.call(this, response.data);
  121. }
  122. }
  123. });
  124. };
  125. Lookup.prototype.queueHas = function (type) {
  126. return !!this.loadingQueue[type];
  127. };
  128. Lookup.prototype.queuePush = function (type) {
  129. this.loadingQueue[type] = true;
  130. };
  131. Lookup.prototype.queuePop = function (type) {
  132. delete this.loadingQueue[type];
  133. return type;
  134. };
  135. Lookup.prototype.storagePut = function (type, array) {
  136. yufp.sessionStorage.put(this.prefix + type, JSON.stringify(array));
  137. };
  138. Lookup.prototype.storageGet = function (type) {
  139. var lookup = yufp.sessionStorage.get(this.prefix + type);
  140. if (lookup) {
  141. lookup = JSON.parse(lookup);
  142. } else {
  143. lookup = undefined;
  144. }
  145. return lookup;
  146. };
  147. /**
  148. * 字段转换方法
  149. * @param type 字典类型
  150. * @param sourceVal 要转换的值
  151. * @param source 源字段
  152. * @param target 目标字段
  153. * @returns {*}
  154. */
  155. Lookup.prototype.convert = function (type, sourceVal, source, target) {
  156. var _this = this, targetVal = sourceVal;
  157. var lookup = _this.lookupMgr[type] || _this.storageGet[type];
  158. if (!lookup) {
  159. yufp.logger.debug('【' + type + '】字典未加载!');
  160. return targetVal;
  161. }
  162. for (var i = 0, len = lookup.length; i < len; i++) {
  163. var item = lookup[i];
  164. // 不直接使用双等号而使用三等号,原因在于避免0 == false的情况
  165. if ('' + item[source] === '' + sourceVal) {
  166. targetVal = item[target];
  167. break;
  168. }
  169. }
  170. return targetVal;
  171. };
  172. /**
  173. * 将字典注册到字典管理器中
  174. * @param types string,必须
  175. * 示例:'GENDER_TYPE'
  176. * 或:'GENDER_TYPE,USER_STATUS,SYSTEM_STATUS'
  177. * @param callback ,loadRemote暂且未处理此参数,故无效20180731
  178. */
  179. Lookup.prototype.reg = function (types, callback) {
  180. var _this = this;
  181. var allArr = types.split(','), needArr = [];
  182. for (var i = 0, len = allArr.length; i < len; i++) {
  183. var type = allArr[i];
  184. if (!_this.lookupMgr[type] && !_this.storageGet[type]) {
  185. needArr.push(type);
  186. }
  187. }
  188. if (needArr.length > 0) {
  189. _this.loadRemote.call(this, needArr.join(','), callback);
  190. }
  191. };
  192. /**
  193. * 将数据字典绑定到对象上
  194. * @param type 字典类型
  195. * @param callback 回调方法参数即是字典数组对象
  196. */
  197. Lookup.prototype.bind = function (type, callback) {
  198. var _this = this;
  199. if (yufp.type(callback) != 'function') {
  200. yufp.logger.debug('【' + type + '】字典bind方法参数错误');
  201. }
  202. var lookup = this.lookupMgr[type] || _this.storageGet[type];
  203. if (lookup) {
  204. lookup = yufp.extend(true, [], lookup);
  205. callback.call(this, lookup);
  206. } else {
  207. _this.forceLoad(type, function (data) {
  208. _this.lookupMgr[type] = lookup = data[type];
  209. lookup = yufp.extend(true, [], lookup);
  210. callback.call(this, lookup);
  211. });
  212. }
  213. };
  214. /**
  215. * 根据字典类别查找
  216. * @param type 要查找的字典类型
  217. * @param isArray 是否返回数组(可选参数), true: 是,false: 否; 默认true
  218. */
  219. Lookup.prototype.find = function (type, isArray) {
  220. var _this = this;
  221. isArray = isArray !== false;
  222. var lookup = this.lookupMgr[type] || _this.storageGet[type];
  223. lookup = yufp.extend(true, [], lookup);
  224. if (!isArray) {
  225. lookup = !lookup ? {} : _this.array2Map(lookup);
  226. }
  227. return lookup;
  228. };
  229. /**
  230. * 数组转Map
  231. * @param lookup
  232. * @returns {Map}
  233. */
  234. Lookup.prototype.array2Map = function (lookup) {
  235. var _this = this;
  236. lookup = lookup ? lookup.reduce(function (acc, cur) {
  237. acc[cur[_this.codeKey]] = cur[_this.codeValue];
  238. return acc;
  239. }, {}) : {};
  240. return lookup;
  241. };
  242. /**
  243. * 字典码转换为字典值
  244. * @param type 字典类型
  245. * @param key 字典码
  246. * @returns {*} 字典值
  247. */
  248. Lookup.prototype.convertKey = function (type, key) {
  249. return this.convert(type, key, this.codeKey, this.codeValue);
  250. };
  251. /**
  252. * 字典值转换为字典码
  253. * @param type 字典类型
  254. * @param value 字典值
  255. * @returns {*} 字典码
  256. */
  257. Lookup.prototype.convertValue = function (type, value) {
  258. return this.convert(type, value, this.codeValue, this.codeKey);
  259. };
  260. /**
  261. * 批量字典码转换为字典值
  262. * @param type 字典类型
  263. * @param keys 字典码,多个用逗号分隔
  264. * @param sep 分隔符(可选参数),默认','
  265. * @returns {*}
  266. */
  267. Lookup.prototype.convertMultiKey = function (type, keys, sep) {
  268. var _this = this;
  269. var lookup = _this.lookupMgr[type] || _this.storageGet[type];
  270. if (!lookup) {
  271. yufp.logger.debug('【' + type + '】字典未加载!');
  272. return keys;
  273. }
  274. sep = !sep ? ',' : sep;
  275. var keyArr = keys.split(sep), values = [];
  276. for (var k = 0, kLen = keyArr.length; k < kLen; k++) {
  277. values.push(_this.convertKey(type, keyArr[k]));
  278. }
  279. return values.join(sep);
  280. };
  281. /**
  282. * 批量字典值转换为字典码
  283. * @param type 字典类型
  284. * @param values 字典值,多个用逗号分隔
  285. * @param sep 分隔符(可选参数),默认','
  286. * @returns {*}
  287. */
  288. Lookup.prototype.convertMultiValue = function (type, values, sep) {
  289. var _this = this;
  290. var lookup = _this.lookupMgr[type] || _this.storageGet[type];
  291. if (!lookup) {
  292. yufp.logger.debug('【' + type + '】字典未加载!');
  293. return values;
  294. }
  295. sep = !sep ? ',' : sep;
  296. var keyArr = values.split(sep), keys = [];
  297. for (var k = 0, kLen = keyArr.length; k < kLen; k++) {
  298. keys.push(_this.convertValue(type, keyArr[k]));
  299. }
  300. return keys.join(sep);
  301. };
  302. return new Lookup();
  303. }));