uniApi.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. export function navigateTo(type,url,opt){
  2. let toUrl=url;
  3. let api='navigateTo';
  4. toUrl=opt?toUrl+'?'+convertObj(opt):toUrl;
  5. switch(type){
  6. case 1:
  7. api='navigateTo';
  8. break;
  9. case 2:
  10. api='redirectTo';
  11. break;
  12. case 3:
  13. api='reLaunch';
  14. break;
  15. case 4:
  16. api='switchTab';
  17. break;
  18. default:
  19. api='navigateTo'
  20. break;
  21. }
  22. uni[api]({
  23. url:toUrl,
  24. animationType:'slide-in-right',
  25. animationDuration:200
  26. });
  27. }
  28. export function navigateBack(delta){
  29. uni.navigateBack({
  30. delta:delta
  31. });
  32. }
  33. export function setStorage(key,val){
  34. if(typeof val=='string'){
  35. uni.setStorageSync(key,val);
  36. return val
  37. }
  38. uni.setStorageSync(key,JSON.stringify(val));
  39. }
  40. export function getStorage(key){
  41. let uu=uni.getStorageSync(key);
  42. try{
  43. if(typeof JSON.parse(uu)!='number'){
  44. uu=JSON.parse(uu);
  45. }
  46. }catch(e){}
  47. return uu;
  48. }
  49. export function removeStorage(key){
  50. if(key){
  51. uni.removeStorageSync(key);
  52. }
  53. }
  54. export function clearStorage(){
  55. try{
  56. uni.clearStorageSync();
  57. }catch(e){
  58. throw new Error('处理失败');
  59. }
  60. }
  61. export function Toast(title,icon='none',obj={},duration=800){
  62. let toastData={
  63. title:title,
  64. duration:duration,
  65. position:'center',
  66. mask:true,
  67. icon:icon?icon:'none',
  68. ...obj
  69. };
  70. uni.showToast(toastData);
  71. }
  72. export function Loading(title='正在加载...',obj={}){
  73. uni.showLoading({
  74. title:title,
  75. mask:true,
  76. ...obj
  77. });
  78. }
  79. export function hideLoading(){
  80. try{
  81. uni.hideLoading();
  82. }catch(e){
  83. throw new Error('处理失败');
  84. }
  85. }
  86. export function Modal(title='提示',content='这是一个模态弹窗!',obj={
  87. showCancel:true,
  88. cancelText:'取消',
  89. confirmText:'确定'
  90. }){
  91. obj.cancelText='确定';
  92. obj.confirmText='取消';
  93. return new Promise((reslove,reject)=>{
  94. uni.showModal({
  95. title:title,
  96. content:content,
  97. ...obj,
  98. success:(res)=>{
  99. if(res.confirm){
  100. reslove()
  101. }
  102. if(res.cancel){
  103. reject()
  104. }
  105. }
  106. });
  107. })
  108. }
  109. export function ActionSheet(itemList,itemColor="#000000"){
  110. return new Promise((reslove,reject)=>{
  111. uni.showActionSheet({
  112. itemList:itemList,
  113. itemColor:itemColor,
  114. success:(res)=>{
  115. reslove(res.tapIndex);
  116. },
  117. fail:function(res){
  118. reject(res.errMsg);
  119. }
  120. });
  121. })
  122. }
  123. export function ScrollTo(ScrollTop){
  124. uni.pageScrollTo({
  125. scrollTop:ScrollTop,
  126. duration:300
  127. })
  128. }
  129. export function GetUserInfo(){
  130. return new Promise((reslove,reject)=>{
  131. uni.getUserInfo({
  132. success(res){
  133. console.log(res);
  134. reslove(res);
  135. },
  136. fail(rej){
  137. reject(rej);
  138. }
  139. })
  140. })
  141. }
  142. export function Authorize(scoped='scope.userInfo'){
  143. return new Promise((reslove,reject)=>{
  144. uni.authorize({
  145. scope:scoped,
  146. success(res){
  147. reslove(res);
  148. },
  149. fail(rej){
  150. reject(rej);
  151. }
  152. })
  153. })
  154. }
  155. export function convertObj(opt){
  156. let str='';
  157. let arr=[];
  158. Object.keys(opt).forEach(item=>{
  159. arr.push(`${item}=${opt[item]}`);
  160. })
  161. str=arr.join('&');
  162. return str;
  163. }
  164. export function throttle(fn,delay){
  165. var lastArgs;
  166. var timer;
  167. var delay=delay||200;
  168. return function(...args){
  169. lastArgs=args;
  170. if(!timer){
  171. timer=setTimeout(()=>{
  172. timer=null;
  173. fn.apply(this,lastArgs);
  174. },delay);
  175. }
  176. }
  177. }
  178. export function chooseImage(count){
  179. return new Promise((reslove,reject)=>{
  180. uni.chooseImage({
  181. count:count,
  182. sizeType:['original','compressed'],
  183. sourceType:['album','camera'],
  184. success:(res)=>{
  185. reslove(res);
  186. },
  187. fail:(rej)=>{
  188. reject(rej);
  189. }
  190. });
  191. })
  192. }
  193. export function serialize(data){
  194. if(data!=null&&data!=''){
  195. try{
  196. return JSON.parse(JSON.stringify(data));
  197. }catch(e){
  198. if(data instanceof Array){
  199. return[];
  200. }
  201. return{};
  202. }
  203. }
  204. return data;
  205. }
  206. Date.prototype.format=function(fmt){
  207. let o={
  208. 'M+':this.getMonth()+1,
  209. 'd+':this.getDate(),
  210. 'h+':this.getHours(),
  211. 'm+':this.getMinutes(),
  212. 's+':this.getSeconds(),
  213. 'q+':Math.floor((this.getMonth()+3)/3),
  214. S:this.getMilliseconds()
  215. };
  216. if(/(y+)/.test(fmt)){
  217. fmt=fmt.replace(RegExp.$1,String(this.getFullYear()).substr(4-RegExp.$1.length));
  218. }
  219. for(let k in o){
  220. if(new RegExp('('+k+')').test(fmt)){
  221. fmt=fmt.replace(RegExp.$1,RegExp.$1.length==1?o[k]:('00'+o[k]).substr(String(o[k]).length));
  222. }
  223. }
  224. return fmt;
  225. };
  226. export function formatDate(nS,format){
  227. if(!nS){
  228. return'';
  229. }
  230. format=format||'yyyy-MM-dd hh:mm:ss';
  231. return new Date(nS).format(format);
  232. }
  233. export function pathToBase64(path){
  234. return new Promise(function(resolve,reject){
  235. if(typeof window==='object'&&'document'in window){
  236. if(typeof FileReader==='function'){
  237. var xhr=new XMLHttpRequest()
  238. xhr.open('GET',path,true)
  239. xhr.responseType='blob'
  240. xhr.onload=function(){
  241. if(this.status===200){
  242. let fileReader=new FileReader()
  243. fileReader.onload=function(e){
  244. resolve(e.target.result)
  245. }
  246. fileReader.onerror=reject
  247. fileReader.readAsDataURL(this.response)
  248. }
  249. }
  250. xhr.onerror=reject
  251. xhr.send()
  252. return
  253. }
  254. var canvas=document.createElement('canvas')
  255. var c2x=canvas.getContext('2d')
  256. var img=new Image
  257. img.onload=function(){
  258. canvas.width=img.width
  259. canvas.height=img.height
  260. c2x.drawImage(img,0,0)
  261. resolve(canvas.toDataURL())
  262. canvas.height=canvas.width=0
  263. }
  264. img.onerror=reject
  265. img.src=path
  266. return
  267. }
  268. if(typeof plus==='object'){
  269. plus.io.resolveLocalFileSystemURL(getLocalFilePath(path),function(entry){
  270. entry.file(function(file){
  271. var fileReader=new plus.io.FileReader()
  272. fileReader.onload=function(data){
  273. resolve(data.target.result)
  274. }
  275. fileReader.onerror=function(error){
  276. reject(error)
  277. }
  278. fileReader.readAsDataURL(file)
  279. },function(error){
  280. reject(error)
  281. })
  282. },function(error){
  283. reject(error)
  284. })
  285. return
  286. }
  287. if(typeof wx==='object'&&wx.canIUse('getFileSystemManager')){
  288. wx.getFileSystemManager().readFile({
  289. filePath:path,
  290. encoding:'base64',
  291. success:function(res){
  292. resolve('data:image/png;base64,'+res.data)
  293. },
  294. fail:function(error){
  295. reject(error)
  296. }
  297. })
  298. return
  299. }
  300. reject(new Error('not support'))
  301. })
  302. }
  303. export function showWeekFirstDay(){
  304. var date=new Date();
  305. var weekday=date.getDay()||7;
  306. date.setDate(date.getDate()-weekday+1);
  307. return formatDate(date,'yyyy-MM-dd');
  308. }
  309. export function showMonthFirstDay(){
  310. var MonthFirstDay=new Date().setDate(1);
  311. return formatDate(new Date(MonthFirstDay).getTime(),'yyyy-MM-dd');
  312. }
  313. var now=new Date();
  314. var nowMonth=now.getMonth();
  315. var nowYear=now.getYear();
  316. nowYear+=(nowYear<2000)?1900:0;
  317. function getQuarterStartMonth(){
  318. var quarterStartMonth=0;
  319. if(nowMonth<3){
  320. quarterStartMonth=0;
  321. }
  322. if(2<nowMonth&&nowMonth<6){
  323. quarterStartMonth=3;
  324. }
  325. if(5<nowMonth&&nowMonth<9){
  326. quarterStartMonth=6;
  327. }
  328. if(nowMonth>8){
  329. quarterStartMonth=9;
  330. }
  331. return quarterStartMonth;
  332. }
  333. export function getQuarterStartDate(){
  334. var quarterStartDate=new Date(nowYear,getQuarterStartMonth(),1);
  335. return formatDate(quarterStartDate,'yyyy-MM-dd');
  336. }
  337. export function unique(data){
  338. data=data||[];
  339. var n={};
  340. for(var i=0;i<data.length;i++){
  341. var v=JSON.stringify(data[i]);
  342. if(typeof(v)=="undefined"){
  343. n[v]=1;
  344. }
  345. }
  346. data.length=0;
  347. for(var i in n){
  348. data[data.length]=i;
  349. }
  350. return data;
  351. }