core.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. 初始化处理 vue component
  3. **/
  4. (function(window, undefined){
  5. "use strict";
  6. var vc = window.vc || {};
  7. var _vmOptions = {};
  8. var _initMethod = [];
  9. var _initEvent = [];
  10. var _component = {};
  11. var _destroyedMethod = [];
  12. var _timers = [];//定时器
  13. _vmOptions = {
  14. el:'#component',
  15. data:{
  16. },
  17. watch: {
  18. },
  19. methods:{
  20. },
  21. destroyed:function(){
  22. window.vc.destroyedMethod.forEach(function(eventMethod){
  23. eventMethod();
  24. });
  25. //清理所有定时器
  26. window.vc.timers.forEach(function(timer){
  27. clearInterval(timer);
  28. });
  29. _timers = [];
  30. }
  31. };
  32. vc = {
  33. version:"v0.0.1",
  34. name:"vue component",
  35. author:'java110',
  36. vmOptions:_vmOptions,
  37. initMethod:_initMethod,
  38. initEvent:_initEvent,
  39. component:_component,
  40. destroyedMethod:_destroyedMethod,
  41. timers:_timers
  42. };
  43. //通知window对象
  44. window.vc = vc;
  45. })(window);
  46. /**
  47. vc 函数初始化
  48. add by wuxw
  49. **/
  50. (function(vc){
  51. vc.http = {
  52. post:function(componentCode,componentMethod,param,options,successCallback,errorCallback){
  53. vc.loading('open');
  54. Vue.http.post('/callComponent/'+componentCode +"/"+componentMethod, param, options)
  55. .then(function(res){
  56. successCallback(res.bodyText,res);
  57. vc.loading('close');
  58. }, function(error){
  59. errorCallback(error.bodyText,error);
  60. vc.loading('close');
  61. });
  62. },
  63. get:function(componentCode,componentMethod,param,successCallback,errorCallback){
  64. //加入缓存机制
  65. var _getPath = '/'+componentCode +'/' +componentMethod;
  66. if(vc.constant.GET_CACHE_URL.includes(_getPath)){
  67. var _cacheData = vc.getData(_getPath);
  68. //浏览器缓存中能获取到
  69. if(_cacheData != null && _cacheData != undefined){
  70. successCallback(JSON.stringify(_cacheData),{status:200});
  71. return ;
  72. }
  73. }
  74. vc.loading('open');
  75. Vue.http.get('/callComponent/'+componentCode +"/"+componentMethod, param)
  76. .then(function(res){
  77. successCallback(res.bodyText,res);
  78. if(vc.constant.GET_CACHE_URL.includes(_getPath) && res.status == 200){
  79. vc.saveData(_getPath,JSON.parse(res.bodyText));
  80. }
  81. vc.loading('close');
  82. }, function(error){
  83. errorCallback(error.bodyText,error);
  84. vc.loading('close');
  85. });
  86. }
  87. };
  88. var vmOptions = vc.vmOptions;
  89. //继承方法,合并 _vmOptions 的数据到 vmOptions中
  90. vc.extends = function(_vmOptions){
  91. if(typeof _vmOptions !== "object"){
  92. throw "_vmOptions is not Object";
  93. }
  94. //处理 data 对象
  95. if(_vmOptions.hasOwnProperty('data')){
  96. for(var dataAttr in _vmOptions.data){
  97. vmOptions.data[dataAttr] = _vmOptions.data[dataAttr];
  98. }
  99. }
  100. //处理methods 对象
  101. if(_vmOptions.hasOwnProperty('methods')){
  102. for(var methodAttr in _vmOptions.methods){
  103. vmOptions.methods[methodAttr] = _vmOptions.methods[methodAttr];
  104. }
  105. }
  106. //处理methods 对象
  107. if(_vmOptions.hasOwnProperty('watch')){
  108. for(var watchAttr in _vmOptions.watch){
  109. vmOptions.watch[watchAttr] = _vmOptions.watch[watchAttr];
  110. }
  111. }
  112. //处理_initMethod 初始化执行函数
  113. if(_vmOptions.hasOwnProperty('_initMethod')){
  114. vc.initMethod.push(_vmOptions._initMethod);
  115. }
  116. //处理_initEvent
  117. if(_vmOptions.hasOwnProperty('_initEvent')){
  118. vc.initEvent.push(_vmOptions._initEvent);
  119. }
  120. //处理_initEvent_destroyedMethod
  121. if(_vmOptions.hasOwnProperty('_destroyedMethod')){
  122. vc.destroyedMethod.push(_vmOptions._destroyedMethod);
  123. }
  124. };
  125. //绑定跳转函数
  126. vc.jumpToPage = function(url){
  127. window.location.href = url;
  128. };
  129. //保存菜单
  130. vc.setCurrentMenu = function(_menuId){
  131. window.localStorage.setItem('hc_menuId',_menuId);
  132. };
  133. //获取菜单
  134. vc.getCurrentMenu = function(){
  135. return window.localStorage.getItem('hc_menuId');
  136. };
  137. //保存用户菜单
  138. vc.setMenus = function(_menus){
  139. window.localStorage.setItem('hc_menus',JSON.stringify(_menus));
  140. };
  141. //获取用户菜单
  142. vc.getMenus = function(){
  143. return JSON.parse(window.localStorage.getItem('hc_menus'));
  144. };
  145. //保存用户菜单
  146. vc.saveData = function(_key,_value){
  147. window.localStorage.setItem(_key,JSON.stringify(_value));
  148. };
  149. //获取用户菜单
  150. vc.getData = function(_key){
  151. return JSON.parse(window.localStorage.getItem(_key));
  152. };
  153. //保存当前小区信息 _communityInfo : {"communityId":"123213","name":"测试小区"}
  154. vc.setCurrentCommunity = function(_currentCommunityInfo){
  155. window.localStorage.setItem('hc_currentCommunityInfo',JSON.stringify(_currentCommunityInfo));
  156. };
  157. //获取当前小区信息
  158. // @return {"communityId":"123213","name":"测试小区"}
  159. vc.getCurrentCommunity = function(){
  160. return JSON.parse(window.localStorage.getItem('hc_currentCommunityInfo'));
  161. };
  162. //保存当前小区信息 _communityInfos : [{"communityId":"123213","name":"测试小区"}]
  163. vc.setCommunitys = function(_communityInfos){
  164. window.localStorage.setItem('hc_communityInfos',JSON.stringify(_communityInfos));
  165. };
  166. //获取当前小区信息
  167. // @return {"communityId":"123213","name":"测试小区"}
  168. vc.getCommunitys = function(){
  169. return JSON.parse(window.localStorage.getItem('hc_communityInfos'));
  170. };
  171. //删除缓存数据
  172. vc.clearCacheData = function(){
  173. window.localStorage.clear();
  174. };
  175. //将org 对象的属性值赋值给dst 属性名为一直的属性
  176. vc.copyObject = function(org,dst){
  177. //for(key in Object.getOwnPropertyNames(dst)){
  178. for(var key in dst){
  179. if (org.hasOwnProperty(key)){
  180. dst[key] = org[key]
  181. }
  182. }
  183. };
  184. //获取url参数
  185. vc.getParam = function(_key){
  186. //返回当前 URL 的查询部分(问号 ? 之后的部分)。
  187. var urlParameters = location.search;
  188. //如果该求青中有请求的参数,则获取请求的参数,否则打印提示此请求没有请求的参数
  189. if (urlParameters.indexOf('?') != -1)
  190. {
  191. //获取请求参数的字符串
  192. var parameters = decodeURI(urlParameters.substr(1));
  193. //将请求的参数以&分割中字符串数组
  194. parameterArray = parameters.split('&');
  195. //循环遍历,将请求的参数封装到请求参数的对象之中
  196. for (var i = 0; i < parameterArray.length; i++) {
  197. if(_key == parameterArray[i].split('=')[0]){
  198. return parameterArray[i].split('=')[1];
  199. }
  200. }
  201. }
  202. return "";
  203. };
  204. //查询url
  205. vc.getUrl = function(){
  206. //返回当前 URL 的查询部分(问号 ? 之后的部分)。
  207. var urlParameters = location.search;
  208. //如果该求青中有请求的参数,则获取请求的参数,否则打印提示此请求没有请求的参数
  209. if(urlParameters.indexOf('?') != -1){
  210. return urlParameters.substring(0, urlParameters.indexOf('?'));
  211. }
  212. return urlParameters;
  213. };
  214. //对象转get参数
  215. vc.objToGetParam =function(obj){
  216. var str = [];
  217. for (var p in obj)
  218. if (obj.hasOwnProperty(p)) {
  219. str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
  220. }
  221. return str.join("&");
  222. }
  223. })(window.vc);
  224. /**
  225. vc 定时器处理
  226. **/
  227. (function(w,vc){
  228. /**
  229. 创建定时器
  230. **/
  231. vc.createTimer = function(func,sec){
  232. var _timer = w.setInterval(func,sec);
  233. vc.timers.push(_timer); //这里将所有的定时器保存起来,页面退出时清理
  234. return _timer;
  235. };
  236. //清理定时器
  237. vc.clearTimer = function(timer){
  238. clearInterval(timer);
  239. }
  240. })(window,window.vc);
  241. /**
  242. 时间处理工具类
  243. **/
  244. (function(vc){
  245. function add0(m){return m<10?'0'+m:m }
  246. vc.dateFormat = function(shijianchuo){
  247. //shijianchuo是整数,否则要parseInt转换
  248. var time = new Date(parseInt(shijianchuo));
  249. var y = time.getFullYear();
  250. var m = time.getMonth()+1;
  251. var d = time.getDate();
  252. var h = time.getHours();
  253. var mm = time.getMinutes();
  254. var s = time.getSeconds();
  255. return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
  256. }
  257. })(window.vc);
  258. (function(vc){
  259. vc.propTypes = {
  260. string:"string",//字符串类型
  261. array:"array",
  262. object:"object",
  263. number:"number"
  264. }
  265. })(window.vc)