UNPKG

cyl-hooks-tools

Version:

基于react上下文实现的权限管理

2 lines 17.4 kB
/*! For license information please see index.js.LICENSE.txt */ !function(t,r){"object"==typeof exports&&"object"==typeof module?module.exports=r(require("react"),require("axios")):"function"==typeof define&&define.amd?define(["react","axios"],r):"object"==typeof exports?exports.cylHooksTools=r(require("react"),require("axios")):t.cylHooksTools=r(t.react,t.axios)}(self,(t,r)=>(()=>{"use strict";var e={155:r=>{r.exports=t},550:(t,r,e)=>{function n(t){return function(t){if(Array.isArray(t))return i(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||o(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function o(t,r){if(t){if("string"==typeof t)return i(t,r);var e={}.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?i(t,r):void 0}}function i(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=Array(r);e<r;e++)n[e]=t[e];return n}function u(t,r){var e,n={"Y+":r.getFullYear().toString(),"m+":(r.getMonth()+1).toString(),"d+":r.getDate().toString(),"H+":r.getHours().toString(),"M+":r.getMinutes().toString(),"S+":r.getSeconds().toString()};for(var o in n)(e=new RegExp("("+o+")").exec(t))&&(t=t.replace(e[1],1==e[1].length?n[o]:n[o].padStart(e[1].length,"0")));return t}e.d(r,{default:()=>c});const c={parseJsonGraceful:function(t){try{return JSON.parse(t)}catch(t){return null}},sleep:function(t){return new Promise(function(r){return setTimeout(r,t)})},logger:{info:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Date.now(),e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"blue",n=new Date(r);console.info("%c".concat(u("YYYY-mm-dd HH:MM:SS",n)," INFO: "),"color: ".concat(e,";"),t)},error:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Date.now(),e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"red",n=new Date(r);console.error("%c".concat(u("YYYY-mm-dd HH:MM:SS",n)," ERROR: "),"color: ".concat(e,";"),t)},warn:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Date.now(),e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"black",n=new Date(r);console.warn("%c".concat(u("YYYY-mm-dd HH:MM:SS",n)," WARN: "),"color: ".concat(e,";"),t)},log:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Date.now(),e=new Date(r);console.log("".concat(u("YYYY-mm-dd HH:MM:SS",e),": "),t)},success:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Date.now(),e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"green",n=new Date(r);console.log("%c".concat(u("YYYY-mm-dd HH:MM:SS",n)," SUCCESS: "),"color: ".concat(e,";"),t)}},makeFriendly:function(t){return t>=1e8?t=Math.round(t/1e7)/10+"亿":t>=1e4&&(t=Math.round(t/1e3)/10+"万"),t},formatSeconds:function(t){if("number"!=typeof t)throw new Error("value需要number类型");var r=parseInt(t),e=0,n=0;r>60&&(e=parseInt(r/60),r=parseInt(r%60),e>60&&(n=parseInt(e/60),e=parseInt(e%60)));var o=parseInt(r)+"秒";return e>0&&(o=parseInt(e)+"分"+o),n>0&&(o=parseInt(n)+"小时"+o),o},Uint8ArrayToString:function(t){for(var r="",e=0;e<t.length;e++)r+=String.fromCharCode(t[e]);return r},dataSourceSliceOfECharts:function(t,r){var e,i,u=arguments.length>2&&void 0!==arguments[2]?arguments[2]:60,c=arguments.length>3?arguments[3]:void 0,a=0,f=[],l=function(t,r){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!e){if(Array.isArray(t)||(e=o(t))||r&&t&&"number"==typeof t.length){e&&(t=e);var n=0,i=function(){};return{s:i,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var u,c=!0,a=!1;return{s:function(){e=e.call(t)},n:function(){var t=e.next();return c=t.done,t},e:function(t){a=!0,u=t},f:function(){try{c||null==e.return||e.return()}finally{if(a)throw u}}}}(t);try{var s=function(){var t=i.value;if(null==t||!t.timeStamp)throw new Error("no timeStamp"+JSON.stringify(t));var o=Math.round(t.timeStamp/1e3);if(0===a){var u;f.push([1e3*o].concat(n(null===(u=r(t,a,o))||void 0===u?void 0:u.map(function(r){return t[r]}))))}else{for(var c,l=a+1;l<o;l++){var s;f.push([1e3*l].concat(n(null===(s=r(e,l,o))||void 0===s?void 0:s.map(function(r){return t[r]}))))}f.push([1e3*o].concat(n(null===(c=r(t,a,o))||void 0===c?void 0:c.map(function(r){return t[r]}))))}a=o,e=t};for(l.s();!(i=l.n()).done;)s()}catch(t){l.e(t)}finally{l.f()}if(f.length>u)f=f.slice(f.length-u),c&&c(f,f.length-u);else if(f.length<u)for(var p=f.length;p<u;p++)a+=1,f.push([1e3*a,null]);return f},ninePath:function(t,r){var e=t.scale||2;return{"border-image-slice":"".concat(t.top," ").concat(t.right," ").concat(t.bottom," ").concat(t.left," fill"),"border-image-width":"".concat(t.top/e,"px ").concat(t.right/e,"px ").concat(t.bottom/e,"px ").concat(t.left/e,"px"),"border-image-outset":"0px 0px 0px 0px","border-image-repeat":"stretch stretch","border-image-source":"url(".concat(t.source,")"),"border-width":"".concat(r.top/e,"px ").concat(r.right/e,"px ").concat(r.bottom/e,"px ").concat(r.left/e,"px"),"border-style":"solid","box-sizing":"border-box","min-width":"".concat((t.left+t.right)/e,"px"),"min-height":"".concat((t.top+t.bottom)/e,"px")}}}},742:t=>{t.exports=r},982:(t,r,e)=>{e.d(r,{default:()=>a});var n=e(155);function o(t){return function(t){if(Array.isArray(t))return i(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,r){if(t){if("string"==typeof t)return i(t,r);var e={}.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?i(t,r):void 0}}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function i(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=Array(r);e<r;e++)n[e]=t[e];return n}function u(t){setTimeout(t,0)}function c(t,r){var e,n,i=function(){},u=!1,c=t.apply(void 0,[function(){return e}].concat(o(r)));return function t(r,e,o){var a,f;try{if(r){var l=c.return();a=l.value,f=l.done}else if(e){var s=c.throw(e);a=s.value,f=s.done}else{var p=c.next(o);a=p.value,f=p.done}}catch(t){return void console.warn("Uncaught exception in useCo",t)}if(f)return u=!0,void(n=a);var y=!1;Promise.resolve(a).then(function(r){y||t(!1,void 0,r)}).catch(function(r){y||t(!1,r,void 0)}),i=function(){y||(y=!0,t(!0,void 0,void 0))}}(!1,void 0,void 0),function(){u?"function"==typeof n&&n():(e=!0,i())}}const a={useCoEffect:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];if("function"!=typeof t||"GeneratorFunction"!==(t.constructor.displayName||t.constructor.name))throw new Error("useCo only accepts generator function");(0,n.useEffect)(function(){var r=c(t,[]);return function(){return u(function(){return r()})}},r)},useCoCallback:function(t,r){if("function"!=typeof t||"GeneratorFunction"!==(t.constructor.displayName||t.constructor.name))throw new Error("useCoCallback only accepts generator function");var e=(0,n.useRef)(function(){});return(0,n.useEffect)(function(){return function(){return u(function(){return e.current()})}},r),(0,n.useCallback)(function(){e.current();for(var r=arguments.length,n=new Array(r),o=0;o<r;o++)n[o]=arguments[o];e.current=c(t,n)},r)}}}},n={};function o(t){var r=n[t];if(void 0!==r)return r.exports;var i=n[t]={exports:{}};return e[t](i,i.exports,o),i.exports}o.n=t=>{var r=t&&t.__esModule?()=>t.default:()=>t;return o.d(r,{a:r}),r},o.d=(t,r)=>{for(var e in r)o.o(r,e)&&!o.o(t,e)&&Object.defineProperty(t,e,{enumerable:!0,get:r[e]})},o.o=(t,r)=>Object.prototype.hasOwnProperty.call(t,r);var i={};o.d(i,{default:()=>_});var u=o(155),c=o.n(u);function a(t,r){var e=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);r&&(n=n.filter(function(r){return Object.getOwnPropertyDescriptor(t,r).enumerable})),e.push.apply(e,n)}return e}function f(t){for(var r=1;r<arguments.length;r++){var e=null!=arguments[r]?arguments[r]:{};r%2?a(Object(e),!0).forEach(function(r){l(t,r,e[r])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(e)):a(Object(e)).forEach(function(r){Object.defineProperty(t,r,Object.getOwnPropertyDescriptor(e,r))})}return t}function l(t,r,e){return(r=function(t){var r=function(t,r){if("object"!=s(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var n=e.call(t,r||"default");if("object"!=s(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}(t,"string");return"symbol"==s(r)?r:r+""}(r))in t?Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[r]=e,t}function s(t){return s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},s(t)}function p(t,r){return function(t){if(Array.isArray(t))return t}(t)||function(t,r){var e=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=e){var n,o,i,u,c=[],a=!0,f=!1;try{if(i=(e=e.call(t)).next,0===r){if(Object(e)!==e)return;a=!1}else for(;!(a=(n=i.call(e)).done)&&(c.push(n.value),c.length!==r);a=!0);}catch(t){f=!0,o=t}finally{try{if(!a&&null!=e.return&&(u=e.return(),Object(u)!==u))return}finally{if(f)throw o}}return c}}(t,r)||function(t,r){if(t){if("string"==typeof t)return y(t,r);var e={}.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?y(t,r):void 0}}(t,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function y(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=Array(r);e<r;e++)n[e]=t[e];return n}var d=o(550).default.logger;function v(){var t,r,e="function"==typeof Symbol?Symbol:{},n=e.iterator||"@@iterator",o=e.toStringTag||"@@toStringTag";function i(e,n,o,i){var a=n&&n.prototype instanceof c?n:c,f=Object.create(a.prototype);return b(f,"_invoke",function(e,n,o){var i,c,a,f=0,l=o||[],s=!1,p={p:0,n:0,v:t,a:y,f:y.bind(t,4),d:function(r,e){return i=r,c=0,a=t,p.n=e,u}};function y(e,n){for(c=e,a=n,r=0;!s&&f&&!o&&r<l.length;r++){var o,i=l[r],y=p.p,d=i[2];e>3?(o=d===n)&&(a=i[(c=i[4])?5:(c=3,3)],i[4]=i[5]=t):i[0]<=y&&((o=e<2&&y<i[1])?(c=0,p.v=n,p.n=i[1]):y<d&&(o=e<3||i[0]>n||n>d)&&(i[4]=e,i[5]=n,p.n=d,c=0))}if(o||e>1)return u;throw s=!0,n}return function(o,l,d){if(f>1)throw TypeError("Generator is already running");for(s&&1===l&&y(l,d),c=l,a=d;(r=c<2?t:a)||!s;){i||(c?c<3?(c>1&&(p.n=-1),y(c,a)):p.n=a:p.v=a);try{if(f=2,i){if(c||(o="next"),r=i[o]){if(!(r=r.call(i,a)))throw TypeError("iterator result is not an object");if(!r.done)return r;a=r.value,c<2&&(c=0)}else 1===c&&(r=i.return)&&r.call(i),c<2&&(a=TypeError("The iterator does not provide a '"+o+"' method"),c=1);i=t}else if((r=(s=p.n<0)?a:e.call(n,p))!==u)break}catch(r){i=t,c=1,a=r}finally{f=1}}return{value:r,done:s}}}(e,o,i),!0),f}var u={};function c(){}function a(){}function f(){}r=Object.getPrototypeOf;var l=[][n]?r(r([][n]())):(b(r={},n,function(){return this}),r),s=f.prototype=c.prototype=Object.create(l);function p(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,f):(t.__proto__=f,b(t,o,"GeneratorFunction")),t.prototype=Object.create(s),t}return a.prototype=f,b(s,"constructor",f),b(f,"constructor",a),a.displayName="GeneratorFunction",b(f,o,"GeneratorFunction"),b(s),b(s,o,"Generator"),b(s,n,function(){return this}),b(s,"toString",function(){return"[object Generator]"}),(v=function(){return{w:i,m:p}})()}function b(t,r,e,n){var o=Object.defineProperty;try{o({},"",{})}catch(t){o=0}b=function(t,r,e,n){function i(r,e){b(t,r,function(t){return this._invoke(r,e,t)})}r?o?o(t,r,{value:e,enumerable:!n,configurable:!n,writable:!n}):t[r]=e:(i("next",0),i("throw",1),i("return",2))},b(t,r,e,n)}function m(t,r){return function(t){if(Array.isArray(t))return t}(t)||function(t,r){var e=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=e){var n,o,i,u,c=[],a=!0,f=!1;try{if(i=(e=e.call(t)).next,0===r){if(Object(e)!==e)return;a=!1}else for(;!(a=(n=i.call(e)).done)&&(c.push(n.value),c.length!==r);a=!0);}catch(t){f=!0,o=t}finally{try{if(!a&&null!=e.return&&(u=e.return(),Object(u)!==u))return}finally{if(f)throw o}}return c}}(t,r)||function(t,r){if(t){if("string"==typeof t)return g(t,r);var e={}.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?g(t,r):void 0}}(t,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function g(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=Array(r);e<r;e++)n[e]=t[e];return n}var h=o(982).default.useCoEffect,S=c().createContext(null);var w=o(742);function O(t){return O="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},O(t)}function j(t,r){var e=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);r&&(n=n.filter(function(r){return Object.getOwnPropertyDescriptor(t,r).enumerable})),e.push.apply(e,n)}return e}function x(t){for(var r=1;r<arguments.length;r++){var e=null!=arguments[r]?arguments[r]:{};r%2?j(Object(e),!0).forEach(function(r){A(t,r,e[r])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(e)):j(Object(e)).forEach(function(r){Object.defineProperty(t,r,Object.getOwnPropertyDescriptor(e,r))})}return t}function A(t,r,e){return(r=function(t){var r=function(t,r){if("object"!=O(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var n=e.call(t,r||"default");if("object"!=O(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}(t,"string");return"symbol"==O(r)?r:r+""}(r))in t?Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[r]=e,t}var P=o.n(w)().create();const _={useApi:function(){var t=(0,u.useContext)(S),r=(0,u.useMemo)(function(){return Object.create(t)},[t]);return r.__api_loading=!1,r.__api_error=null,r},useApiState:function(t,r){for(var e=arguments.length,n=new Array(e>2?e-2:0),o=2;o<e;o++)n[o-2]=arguments[o];var i=t,c=[void 0,!0,void 0],a=m((0,u.useState)(c),2),f=a[0],l=a[1];return h(v().m(function e(){var o,i;return v().w(function(e){for(;;)switch(e.p=e.n){case 0:return e.p=0,l(function(t){return[t[0],!0,void 0]}),e.n=1,r.apply(void 0,[t].concat(n));case 1:o=e.v,l([o,!1,void 0]),e.n=3;break;case 2:e.p=2,i=e.v,l(function(t){return[t[0],!1,i]});case 3:return e.a(2)}},e,null,[[0,2]])}),[].concat(n)),!i.__api_loading&&f[1]&&(i.__api_loading=!0),!i.__api_error&&f[2]&&(i.__api_error=f[2]),f},useApiAnyError:function(t){return t.__api_error},useApiAnyLoading:function(t){return t.__api_loading},ApiProvider:function(t){var r=t.children,e=t.httpClient,n=t.initAuth,o=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},e=p((0,u.useState)(function(){return r instanceof Object&&"object"===s(r)?r:{}}),2),n=e[0],o=e[1];if(!t)return d.error("httpClient Not null or Type error"),0;if(!(Object.keys(t).includes("get")&&Object.keys(t).includes("post")&&Object.keys(t).includes("delete")&&Object.keys(t).includes("put")&&Object.keys(t).includes("request")&&"function"==typeof t.get&&"function"==typeof t.post&&"function"==typeof t.delete&&"function"==typeof t.put&&"function"==typeof t.request))return d.error("get, post, delete,request and put in httpClient must be implemented and they must be functions"),0;var i=function(t){t instanceof Object&&"object"===s(t)?o(t):(o({}),d.error("auth must be an object"))};return(0,u.useMemo)(function(){return f(f({},t),{},{auth:n,handleSetAuth:i})},[n])}(e,void 0===n?{}:n);return c().createElement(S.Provider,{value:o},r)},createHttpClient:function(t){var r=t&&t instanceof Function&&"function"==typeof t&&t(P)||P;return{get:function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return r.get(t,x({params:e},n))},post:function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return r.post(t,e,x({},n))},delete:function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return r.delete(t,x({params:e},n))},put:function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return r.put(t,e,x({},n))},request:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return r.request(t)}}}};return i=i.default})());