app.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. const app = {};
  2. app.sys = class appSystem{
  3. /**
  4. * 环境版本判定回调
  5. * @param {Object.Function} callbacks 回调执行方法
  6. */
  7. static envVersion(callbacks){
  8. if(!uni.getAccountInfoSync) return;
  9. const current = uni.getAccountInfoSync()?.miniProgram?.envVersion;
  10. const fn = callbacks[current];
  11. if(fn && typeof fn =='function') fn();
  12. }
  13. }
  14. app.url = class appUrl{
  15. /**
  16. * 跳转到地址
  17. * @param {String} url path字符串
  18. * @param {Boolean} mode 跳转模式; false时关闭当前窗口跳转 | null时关闭所有窗口跳转 | 默认为打开新窗口跳转(最多支持10层,页面栈超过10层后默认为false模式);
  19. */
  20. static goto(url,mode){
  21. return new Promise((resolve,reject)=>{
  22. const fail = (err) => uni.switchTab({url,success});
  23. const success = () => resolve();
  24. if(mode === false) return uni.redirectTo({url,fail});
  25. if(mode === null) return uni.reLaunch({url,fail});
  26. if(getCurrentPages().length < 10){
  27. uni.navigateTo({url,fail,success});
  28. }else{
  29. uni.redirectTo({url,fail,success});
  30. }
  31. });
  32. }
  33. /**
  34. * 返回页面
  35. * @param {int} delta 返回级数
  36. */
  37. static back(delta){
  38. return uni.navigateBack({delta: delta || 1});
  39. }
  40. /**
  41. * 刷新页面,会触发onLoad和onShow等钩子
  42. */
  43. static refresh(){
  44. const pages = getCurrentPages();
  45. const current = pages[pages.length-1];
  46. const url = current.$page.fullPath;
  47. app.urlGoto(url,false);
  48. }
  49. /**
  50. * 打开新窗口并传递数据
  51. * @param {String} url path字符串
  52. * @param {any} data 传递数据
  53. */
  54. static open(url,data){
  55. const pages = getCurrentPages();
  56. const current = pages[pages.length-1].$page.fullPath;
  57. uni.navigateTo({url,success: res => {
  58. res.eventChannel.emit(`OPEN-DATA[${current}]`, data);
  59. }});
  60. }
  61. /**
  62. * 获取open方法发送到数据
  63. * @param {Component} component 组件对象
  64. */
  65. static load(component){
  66. return new Promise((resolve,reject)=>{
  67. if(component == undefined) return console.error(`app.url.load(this);`);
  68. const pages = getCurrentPages();
  69. const current = pages[pages.length-2]?.$page?.fullPath;
  70. const caller = component?.getOpenerEventChannel;
  71. const channel = caller && caller.call(component);
  72. if(channel && channel.on) channel.on(`OPEN-DATA[${current}]`,data=>resolve(data));
  73. });
  74. }
  75. /**
  76. * 打开其他小程序
  77. * @param {String} appid 小程序APPID
  78. * @param {String} path 要打开的程序路径
  79. * @param {Object} data 要传递给小程序狗子的参数
  80. */
  81. static applet(appid,path,data){
  82. return new Promise((resolve,reject)=>{
  83. uni.navigateToMiniProgram({
  84. appId: appid,
  85. path: path,
  86. extraData: data || {},
  87. success: res => resolve(res)
  88. });
  89. });
  90. }
  91. }
  92. app.storage = class appStorage{
  93. /**
  94. * 设置本地缓存数据
  95. * @param {String} key 数据键名
  96. * @param {any} data 存储数据,支持对象
  97. */
  98. static set(key,data){
  99. try{
  100. uni.setStorageSync(key,data);
  101. }catch(err){
  102. console.error('app.setStorageSync error: ',err);
  103. }
  104. }
  105. /**
  106. * 获取本地缓存数据
  107. * @param {String} key 数据键名
  108. */
  109. static get(key){
  110. return uni.getStorageSync(key);
  111. }
  112. /**
  113. * 清除键缓存数据
  114. * @param {Object} key 数据键名
  115. */
  116. static remove(key){
  117. uni.removeStorageSync(key);
  118. }
  119. /**
  120. * 清理所有缓存数据
  121. */
  122. static clear(){
  123. uni.clearStorageSync();
  124. }
  125. }
  126. app.math = class appMath{
  127. /**
  128. * 浮点精确相加
  129. * @param {Number} arg1 参数1
  130. * @param {Number} arg2 参数2
  131. */
  132. static add(arg1,arg2){
  133. var r1,r2,m;
  134. try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
  135. try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
  136. m=Math.pow(10,Math.max(r1,r2));
  137. return (arg1*m+arg2*m)/m;
  138. }
  139. /**
  140. * 浮点精确相减
  141. * @param {Number} arg1 参数1
  142. * @param {Number} arg2 参数2
  143. */
  144. static sub(arg1,arg2){
  145. var r1,r2,m,n;
  146. try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
  147. try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
  148. m=Math.pow(10,Math.max(r1,r2));
  149. n=(r1=r2)?r1:r2;
  150. return ((arg1*m-arg2*m)/m).toFixed(n);
  151. }
  152. /**
  153. * 浮点精确相乘
  154. * @param {Number} arg1 参数1
  155. * @param {Number} arg2 参数2
  156. */
  157. static mul(arg1,arg2){
  158. var m=0,s1=arg1.toString(),s2=arg2.toString();
  159. try{m+=s1.split(".")[1].length}catch(e){}
  160. try{m+=s2.split(".")[1].length}catch(e){}
  161. return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
  162. }
  163. /**
  164. * 浮点精确相除
  165. * @param {Number} arg1 参数1
  166. * @param {Number} arg2 参数2
  167. */
  168. static div(arg1,arg2){
  169. var t1=0,t2=0,r1,r2;
  170. try{t1=arg1.toString().split(".")[1].length}catch(e){};
  171. try{t2=arg2.toString().split(".")[1].length}catch(e){};
  172. r1=Number(arg1.toString().replace(".",""));
  173. r2=Number(arg2.toString().replace(".",""));
  174. return (r1/r2)*Math.pow(10,t2-t1);
  175. }
  176. }
  177. app.date = class appDate{
  178. /**
  179. * 日期格式化
  180. * @param {String} fmt 格式规则
  181. * @param {String | Date} date 需要格式化的日期
  182. */
  183. static format(fmt,date){
  184. if(!fmt) fmt = 'yyyy-MM-dd hh:mm:sss';
  185. if(typeof(date) === 'string') date = new Date(date.replaceAll('-','/'));
  186. if(!date) date = new Date();
  187. var o = {
  188. "M+" : date.getMonth()+1, //月份
  189. "d+" : date.getDate(), //日
  190. "h+" : date.getHours(), //小时
  191. "m+" : date.getMinutes(), //分
  192. "s+" : date.getSeconds(), //秒
  193. "q+" : Math.floor((date.getMonth()+3)/3), //季度
  194. "S" : date.getMilliseconds() //毫秒
  195. };
  196. if(/(y+)/.test(fmt)) fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
  197. for(var k in o) if(new RegExp("("+ k +")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
  198. return fmt;
  199. }
  200. }
  201. app.popup = class appPopup{
  202. /**
  203. * 对话框
  204. * @param {String} content 弹窗内容
  205. * @param {String} title 弹窗标题
  206. * @param {Object} opts 参数
  207. */
  208. static confirm(content,title,opts){
  209. return new Promise((resolve,reject)=>{
  210. if(!title && title==null) title = '温馨提示';
  211. opts = Object.assign({
  212. title: title,
  213. content: content,
  214. success: res => resolve(res.confirm==true),
  215. fail: err => reject(err)
  216. },opts);
  217. uni.showModal(opts);
  218. });
  219. }
  220. /**
  221. * 提示框
  222. * @param {String} content 弹窗内容
  223. * @param {String} title 弹窗标题
  224. */
  225. static alert(content,title){
  226. return appPopup.confirm(content,title||null,{showCancel:false});
  227. };
  228. /**
  229. * 轻提示
  230. * @param {String} content 提示内容
  231. * @param {Object} opts 参数
  232. */
  233. static toast(content,opts){
  234. uni.showToast(Object.assign({
  235. title: content,
  236. position: 'bottom',
  237. duration: 2000,
  238. icon: 'none',
  239. },opts));
  240. }
  241. /**
  242. * 加载条
  243. * @param {Boolean} visible 显示
  244. * @param {Object} opts 参数
  245. */
  246. static loading(visible,opts){
  247. if(visible){
  248. uni.showLoading(Object.assign({title:'载入中',mask:true},opts));
  249. const timeout = opts?.timeout || (45 * 1000);
  250. setTimeout(()=>appPopup.loading(false),timeout);
  251. }else{
  252. uni.hideLoading();
  253. }
  254. }
  255. /**
  256. * 操作菜单
  257. * @param {Array} data 列表数据
  258. * @param {String} key 指定显示字段
  259. * @param {Object} opts 参数
  260. */
  261. static sheet(data,key,opts){
  262. return new Promise((resolve,reject)=>{
  263. uni.showActionSheet(Object.assign({
  264. title: '',
  265. itemList: data.map(item=>key? item[key] || String(item) : String(item)),
  266. success: res => resolve({index:res.tapIndex,data:data[res.tapIndex]})
  267. },opts));
  268. });
  269. }
  270. }
  271. app.map = class appMap{
  272. /**
  273. * 获取当前位置坐标
  274. * @param {Boolean} high 是否调用高精度接口
  275. * @param {Object} opts 参数
  276. */
  277. static get(high,opts){
  278. return new Promise((resolve,reject)=>{
  279. const OPTS = Object.assign({
  280. success: res =>{
  281. console.log(res);
  282. },
  283. fail: err =>{
  284. console.log(err);
  285. }
  286. },opts);
  287. high ? uni.getLocation(OPTS) : uni.getFuzzyLocation(OPTS);
  288. });
  289. }
  290. }
  291. app.act = class appAct{
  292. /**
  293. * 拨打电话
  294. * @param {String} phone
  295. */
  296. static callPhone(phone){
  297. uni.makePhoneCall({phoneNumber:phone});
  298. }
  299. /**
  300. * 调用扫描
  301. * @param {Object} opts 参数配置,详细查看 https://uniapp.dcloud.net.cn/api/system/barcode.html
  302. */
  303. static scan(opts){
  304. return new Promise((resolve,reject)=>{
  305. uni.scanCode(Object.assign({},{
  306. success: res=>resolve(res),
  307. // fail: err=>reject(err),
  308. },opts));
  309. });
  310. }
  311. /**
  312. * 获取节点信息
  313. * @param {Component} component 组件
  314. * @param {String} selector 类似于 CSS 的选择器
  315. * @param {Boolean} multi 是否多节点
  316. */
  317. static selectorQuery(component,selector,multi){
  318. return new Promise((resolve,reject)=>{
  319. const query = uni.createSelectorQuery().in(component)[multi?'selectAll':'select'](selector);
  320. query.boundingClientRect(data=>{
  321. if(data) resolve(data);
  322. }).exec();
  323. });
  324. }
  325. /**
  326. * 复制文本到剪贴板
  327. * @param {String} str 需要复制的文本
  328. */
  329. static copy(str){
  330. return new Promise((resolve,reject)=>{
  331. uni.setClipboardData({
  332. data: 'hello',
  333. success: resolve(),
  334. fail: err => reject(err)
  335. });
  336. });
  337. }
  338. /**
  339. * 客服服务
  340. * @param {Object} id
  341. * @param {Object} url
  342. */
  343. static customerService(id,url){
  344. return new Promise((resolve,reject)=>{
  345. wx.openCustomerServiceChat({
  346. extInfo: {url: url},
  347. corpId: id,
  348. success: res => resolve(res),
  349. fail: err => reject(err)
  350. });
  351. });
  352. }
  353. }
  354. module.exports = app;