UNPKG

jobsys-mpower

Version:

Enhanced component based on Taro & NutUI

3 lines (2 loc) 5.51 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("axios"),d=require("./index-e3c6cc71.cjs"),o=require("lodash-es"),f=require("dayjs"),T={STATE_CODE_SUCCESS:"SUCCESS",STATE_CODE_FAIL:"FAIL",STATE_CODE_NOT_FOUND:"NOT_FOUND",STATE_CODE_INFO_NOT_COMPLETE:"INCOMPLETE",STATE_CODE_NOT_ALLOWED:"NOT_ALLOWED"};function S(e){return e||(e={}),e.loading=!0,{get(s,n){return new Promise((l,t)=>{h.get(s,n).then(i=>{l(i)}).catch(i=>{t(i)}).finally(()=>{e.loading=!1})})},post(s,n,l){return new Promise((t,i)=>{h.post(s,n,l).then(u=>{t(u)}).catch(u=>{i(u)}).finally(()=>{e.loading=!1})})}}}const D=async(e,s,n)=>{if(e.finishedText=e.finishedText||"加载完毕",e.loading=!0,!e.uri){console.error("URI is required in pagination");return}let l={};try{d.Taro.showLoading({title:"加载中...",mask:!0}),l=await S().get(e.uri,{params:{...e.params||{},page:s||!e.page?1:e.page+1}})}catch{e.loading=!1,e.finished=!1,e.error=!0;return}finally{d.Taro.hideLoading()}if(l.status!==T.STATE_CODE_SUCCESS){e.loading=!1,e.error=!0,e.errorText=l.result;return}let t;n?t=n(l):t=l.result;let i=t.data;!s&&e.items&&(i=e.items.concat(i));const u=t.current_page||t.current||t.page||1,r=t.last_page||t.lastPage||t.pages||0,c=t.total_size||t.totalSize||t.total||0;e.loading=!1,e.error=!1,e.page=u,e.items=i,e.finished=r===0||r===u,e.empty=c===0};function y(e,s){if(!s)return"";const n=s.find(l=>l.value===e);return n?n.text:""}function C(e,s,n){n=n||{value:"value",label:"text",children:"children"};const l=[];function t(i){var u;s.includes(i[n.value])&&l.push(i[n.label]),(u=i[n.children])!=null&&u.length&&i[n.children].forEach(r=>{t(r)})}return e.forEach(i=>{t(i)}),l}function O(e,s,n){n=n||{value:"value",label:"text",children:"children"};let l=[];return o.reduce(s,(t,i)=>{const u=o.find(t,{[n.value]:i});if(u)return l.push(u[n.label]),u[n.children]},e),l}function A(e,s,n){return n=n||{value:"value",label:"text",children:"children"},O(e,s,n)}function P(e,s,n){n=n||{value:"value",label:"text",children:"children"};const l=[];function t(i,u,r){for(const c of i)if(c[n.value]===u){r.unshift(c[n.value]);break}else if(c[n.children]&&t(c[n.children],u,r).length>0){r.unshift(c[n.value]);break}return r}return t(e,s,l)}const{STATE_CODE_FAIL:g,STATE_CODE_INFO_NOT_COMPLETE:v,STATE_CODE_NOT_FOUND:x,STATE_CODE_NOT_ALLOWED:w,STATE_CODE_SUCCESS:F}=T;function L(e){const{url:s,data:n,csrfToken:l}=e;let{method:t}=e;t=t||"post";const i=document.createElement("form");if(i.action=s,i.method=t,i.target="_blank",i.style.display="none",Object.keys(n).forEach(u=>{const r=document.createElement("input");r.type="hidden",r.name=u,r.value=n[u],i.appendChild(r)}),!l){const u=document.createElement("input");u.type="hidden",u.name="_token",u.value=document.querySelector('meta[name="csrf-token"]').getAttribute("content"),i.appendChild(u)}return document.body.appendChild(i),i}function _(e,s){const{status:n}=e,l=e.result,t={};t.default="请求失败, 请检查数据并重试",t[g]="系统错误,请稍候再试",t[x]="请求的内容不存在",t[v]="信息不完整",t[w]="没有权限";const i={[F]:"success"},u=s[n]||s[i[n]]||t[n]||t.def;o.isString(u)?n===F?d.Taro.showToast({title:u,icon:"success",duration:2e3}):d.Taro.showToast({title:l||u,icon:"none",duration:2e3}):o.isFunction(u)&&u()}function U(e,s){_(e,{success:s})}function I(e){e&&e.errorFields?e.errorFields.forEach(s=>{d.Taro.showToast({title:s.errors.join(" "),icon:"none",duration:2e3})}):e&&e.response?d.Taro.showToast({title:"网络异常",icon:"error",duration:2e3}):d.Taro.showToast({title:"请检查填写项",icon:"error",duration:2e3})}function N(e,s){const n=o.cloneDeep(e);s=s||{};const l=t=>{var i,u;for(let r in t){let c;if(f.isDayjs(t[r])?c=t[r]:Object.prototype.toString.call(t[r])==="[object Date]"&&(c=f(t[r])),c&&s.date){o.isString(s.date)?t[r]=c.format(s.date):o.isFunction(s.date)?t[r]=s.date(c):t[r]=t[r].unix();continue}if(o.isBoolean(t[r])&&s.boolean){s.boolean===!0?t[r]=t[r]?1:0:Array.isArray(s.boolean)&&(t[r]=t[r]?((i=s.boolean)==null?void 0:i[0])||1:((u=s.boolean)==null?void 0:u[1])||0);continue}if(s.attachment){const a=s.attachment;if(o.isObject(t[r])&&o.isString(a)&&!o.isUndefined(t[r][a])){t[r]=t[r][a];continue}else if(o.isObject(t[r])&&o.isFunction(a)){t[r]=a(t[r]);continue}}if(o.isArray(t[r]))for(let a in t[r])t[r][a]=l(t[r][a])}return t};return l(n)}function M(e,s){return s?f(e,s):f(e)}function m(e,s){return e?(f.isDayjs(e)||(e=f(e)),e.format(s||"YYYY-MM-DD HH:mm")):""}function k(e){return e?(f.isDayjs(e)||(e=f(e)),e.unix()):""}function R(e,s){return e&&o.isString(e)&&(e=parseInt(e)),e<9999999999&&(e*=1e3),m(new Date(e),s)}const V=["showToast","showNotify","showDialog","showImagePreview"];function E(e,s){return console.log(e,"MpUIResolver",s),{name:e,from:"jobsys-mpower",sideEffects:"jobsys/dist/style.css"}}function p(e={}){return{type:"component",resolve:s=>{const{autoImport:n=!1}=e;if(n&&V.includes(s))return E(s,e);if(console.log("component",s),s.startsWith("Mp"))return E(s.slice(2),e)}}}exports.MpUIResolver=p;exports.STATUS=T;exports.useDateFormat=m;exports.useDateUnix=k;exports.useDayjs=M;exports.useFetch=S;exports.useFindLabelsFromPath=O;exports.useFindParentValues=P;exports.useFindTextsFromPath=A;exports.useFindTextsInValues=C;exports.useFormFail=I;exports.useFormFormat=N;exports.useHiddenForm=L;exports.usePage=D;exports.useProcessStatus=_;exports.useProcessStatusSuccess=U;exports.useTextFromOptionsValue=y;exports.useTimestampFormat=R; //# sourceMappingURL=hooks.cjs.map