app.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. let log = console.log; // 如果在项目的APP.vue文件中的onlaunch中设置 console.log = ()=> {} 则在此也不会有打印信息
  2. // log = ()=>{}; // 打开注释则该插件不会打印任何信息
  3. let _app = {
  4. //交互控制
  5. log(t) {
  6. log(t);
  7. }, // 打印控制,
  8. showLoading(msg, ifmask) {
  9. uni.showLoading({
  10. title: msg,
  11. mask: ifmask || false
  12. })
  13. },
  14. hideLoading() {
  15. uni.hideLoading();
  16. },
  17. showToast(msg, icon) {
  18. uni.showToast({
  19. title: msg,
  20. icon: icon || 'none'
  21. })
  22. },
  23. getPosterUrl(objs) { // 后端获取背景图的url路径方法
  24. let {
  25. backgroundImage,
  26. type,
  27. formData
  28. } = objs;
  29. return new Promise((rs, rj) => {
  30. let image;
  31. if (backgroundImage) {
  32. image = backgroundImage;
  33. }else{
  34. switch (type) { //根据type获取背景图, 一般要改成request获取
  35. case 1:
  36. image = '';
  37. break;
  38. default:
  39. image = '/static/logo.png';
  40. break;
  41. }
  42. }
  43. if (image) {
  44. rs(image); // resolve图片的路径
  45. }else{
  46. rj('背景图片路径不存在');
  47. }
  48. })
  49. },
  50. //下面一般不用动他
  51. shareTypeListSheetArray: {
  52. array: [0, 1, 2, 3, 4, 5]
  53. }, // 分享类型 0-图文链接 1-纯文字 2-纯图片 3-音乐 4-视频 5-小程序
  54. isArray(arg) {
  55. return Object.prototype.toString.call(arg) === '[object Array]';
  56. },
  57. isObject(arg) {
  58. return Object.prototype.toString.call(arg) === '[object Object]';
  59. },
  60. isPromise(obj) {
  61. return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
  62. },
  63. isNull(arg) {
  64. return arg === null;
  65. },
  66. isUndefined(arg) {
  67. return arg === undefined;
  68. },
  69. isUndef(arg) {
  70. return arg === undefined;
  71. },
  72. isNotNull_string(arg) {
  73. return arg !== null && arg !== undefined && arg !== '';
  74. },
  75. isFn(fn) {
  76. return fn && typeof fn === 'function';
  77. },
  78. getStorage(key, scb, fcb) {
  79. uni.getStorage({
  80. key,
  81. success: function(res) {
  82. if (res.data && res.data != "") {
  83. if (scb) scb(res.data);
  84. } else {
  85. if (fcb) fcb();
  86. }
  87. },
  88. fail: function() {
  89. if (fcb) fcb();
  90. }
  91. })
  92. },
  93. setStorage(key, data) {
  94. log('设置缓存')
  95. log('key:' + key)
  96. log('data:' + JSON.stringify(data));
  97. uni.setStorage({
  98. key,
  99. data
  100. })
  101. },
  102. setStorageSync(key, data) {
  103. uni.setStorageSync(key, data);
  104. },
  105. getStorageSync(key) {
  106. return uni.getStorageSync(key);
  107. },
  108. clearStorageSync() {
  109. uni.clearStorageSync();
  110. },
  111. removeStorageSync(key) {
  112. uni.removeStorageSync(key);
  113. },
  114. getImageInfo(url, cb, fcb) {
  115. url = checkMPUrl(url);
  116. uni.getImageInfo({
  117. src: url,
  118. success(res) {
  119. if (cb && typeof(cb) == 'function') cb(res);
  120. },
  121. fail(err) {
  122. if (fcb && typeof(fcb) == 'function') fcb(err);
  123. }
  124. })
  125. },
  126. downloadFile(url, cb) {
  127. url = checkMPUrl(url);
  128. uni.downloadFile({
  129. url,
  130. success(res) {
  131. if (cb && typeof(cb) == 'function') cb(res);
  132. }
  133. })
  134. },
  135. downloadFile_PromiseFc(url) {
  136. return new Promise((rs, rj) => {
  137. if (url.substring(0, 4) !== 'http') {
  138. rs(url);
  139. }else {
  140. url = checkMPUrl(url);
  141. log('url:' + url);
  142. uni.downloadFile({
  143. url,
  144. success(res) {
  145. if (res && res.tempFilePath)
  146. rs(res.tempFilePath);
  147. else
  148. rj('not find tempFilePath');
  149. },
  150. fail(err) {
  151. rj(err);
  152. }
  153. })
  154. }
  155. });
  156. },
  157. saveFile(url) {
  158. uni.saveFile({
  159. tempFilePath: url,
  160. success(res) {
  161. log('保存成功:' + JSON.stringify(res))
  162. }
  163. })
  164. },
  165. downLoadAndSaveFile_PromiseFc(url) {
  166. return new Promise((rs, rj) => {
  167. log('准备下载并保存图片:' + url);
  168. if (url.substring(0, 4) === 'http') {
  169. url = checkMPUrl(url);
  170. uni.downloadFile({
  171. url,
  172. success(d_res) {
  173. log('下载背景图成功:' + JSON.stringify(d_res));
  174. if (d_res && d_res.tempFilePath) {
  175. //#ifdef H5
  176. rs(d_res.tempFilePath);
  177. // #endif
  178. //#ifndef H5
  179. uni.saveFile({
  180. tempFilePath: d_res.tempFilePath,
  181. success(s_res) {
  182. log('保存背景图成功:' + JSON.stringify(s_res));
  183. if (s_res && s_res.savedFilePath)
  184. rs(s_res.savedFilePath);
  185. else
  186. rs(d_res.tempFilePath);
  187. },
  188. fail(err) {
  189. rs(d_res.tempFilePath);
  190. }
  191. })
  192. // #endif
  193. } else {
  194. rj('not find tempFilePath');
  195. }
  196. },
  197. fail(err) {
  198. rj(err);
  199. }
  200. })
  201. }else{
  202. rs(url);
  203. }
  204. })
  205. },
  206. checkFile_PromiseFc(url) {
  207. return new Promise((rs, rj) => {
  208. uni.getSavedFileList({
  209. success(res) {
  210. let d = res.fileList;
  211. let index = d.findIndex(item => {
  212. return item.filePath === url;
  213. })
  214. rs(index);
  215. },
  216. fail(err) {
  217. rj(err);
  218. }
  219. })
  220. });
  221. },
  222. removeSavedFile(path) {
  223. uni.getSavedFileList({
  224. success(res) {
  225. let d = res.fileList;
  226. let index = d.findIndex(item => {
  227. return item.filePath === path;
  228. });
  229. if (index >= 0)
  230. uni.removeSavedFile({
  231. filePath: path
  232. })
  233. }
  234. })
  235. },
  236. fileNameInPath(path) {
  237. let s = path.split("/");
  238. return s[s.length - 1];
  239. },
  240. getImageInfo_PromiseFc(imgPath) {
  241. return new Promise((rs, rj) => {
  242. log('准备获取图片信息:' + imgPath);
  243. imgPath = checkMPUrl(imgPath);
  244. uni.getImageInfo({
  245. src: imgPath,
  246. success: res => {
  247. log('获取图片信息成功:' + JSON.stringify(res));
  248. rs(res);
  249. },
  250. fail: err => {
  251. log('获取图片信息失败:' + JSON.stringify(err));
  252. rj(err)
  253. }
  254. })
  255. });
  256. },
  257. previewImage(urls) {
  258. if (typeof(urls) == 'string')
  259. urls = [urls];
  260. uni.previewImage({
  261. urls,
  262. })
  263. },
  264. actionSheet(obj, cb) {
  265. let sheetArray = [];
  266. for (let i = 0; i < obj.array.length; i++) {
  267. switch (obj.array[i]) {
  268. case 'sinaweibo':
  269. sheetArray[i] = '新浪微博';
  270. break;
  271. case 'qq':
  272. sheetArray[i] = 'QQ';
  273. break;
  274. case 'weixin':
  275. sheetArray[i] = '微信';
  276. break;
  277. case 'WXSceneSession':
  278. sheetArray[i] = '微信好友';
  279. break;
  280. case 'WXSenceTimeline':
  281. sheetArray[i] = '微信朋友圈';
  282. break;
  283. case 'WXSceneFavorite':
  284. sheetArray[i] = '微信收藏';
  285. break;
  286. case 0:
  287. sheetArray[i] = '图文链接';
  288. break;
  289. case 1:
  290. sheetArray[i] = '纯文字';
  291. break;
  292. case 2:
  293. sheetArray[i] = '纯图片';
  294. break;
  295. case 3:
  296. sheetArray[i] = '音乐';
  297. break;
  298. case 4:
  299. sheetArray[i] = '视频';
  300. break;
  301. case 5:
  302. sheetArray[i] = '小程序';
  303. break;
  304. default:
  305. break;
  306. }
  307. }
  308. this.showActionSheet(sheetArray, cb);
  309. },
  310. showActionSheet(sheetArray, cb) {
  311. uni.showActionSheet({
  312. itemList: sheetArray,
  313. success: (e) => {
  314. if (cb && typeof(cb) == 'function') cb(e.tapIndex);
  315. }
  316. })
  317. },
  318. getProvider(type, cb, sheet) {
  319. let _this = this;
  320. uni.getProvider({
  321. service: type,
  322. success: function(res) {
  323. if (sheet) {
  324. let obj = {};
  325. obj.array = res.provider;
  326. _this.actionSheet(obj, function(index) {
  327. if (cb && typeof(cb) == "function") cb(res.provider[index]);
  328. });
  329. } else {
  330. if (type == 'payment') {
  331. let providers = res.provider;
  332. let payTypeArray = [];
  333. for (let i = 0; i < providers.length; i++) {
  334. if (providers[i] == 'wxpay') {
  335. payTypeArray[i] = {
  336. name: '微信支付',
  337. value: providers[i],
  338. img: '/static/image/wei.png'
  339. };
  340. } else if (providers[i] == 'alipay') {
  341. payTypeArray[i] = {
  342. name: "支付宝支付",
  343. value: providers[i],
  344. img: '/static/image/ali.png'
  345. };
  346. }
  347. }
  348. if (cb && typeof(cb) == "function") cb(payTypeArray);
  349. } else {
  350. if (cb && typeof(cb) == "function") cb(res);
  351. }
  352. }
  353. },
  354. })
  355. },
  356. // #ifdef APP-PLUS
  357. getShare(providerName, WXScene, shareType, title, summary, href, imageUrl, miniProgramObj, mediaUrl, scb, fcb) { //miniProgram: {path: '', type: 0, webUrl: ''}
  358. let _this = this;
  359. if (typeof(shareType) == 'number' && !isNaN(shareType) && shareType >= 0) {
  360. _this.readyShare(providerName, WXScene, shareType, title, summary, href, imageUrl, miniProgramObj, mediaUrl, scb,
  361. fcb);
  362. } else {
  363. _this.actionSheet(_this.shareTypeListSheetArray, function(index) {
  364. _this.readyShare(providerName, WXScene, _this.shareTypeListSheetArray.array[index], title, summary, href,
  365. imageUrl, miniProgramObj, mediaUrl, scb, fcb);
  366. });
  367. }
  368. },
  369. readyShare(providerName, WXScene, shareType, title, summary, href, imageUrl, miniProgramObj, mediaUrl, scb, fcb) {
  370. let _this = this;
  371. let shareObjData = {};
  372. switch (shareType) {
  373. case 0:
  374. shareObjData = {
  375. href: href,
  376. summary: summary,
  377. imageUrl: imageUrl
  378. };
  379. break;
  380. case 1:
  381. shareObjData = {
  382. summary: summary,
  383. href: href
  384. };
  385. break;
  386. case 2:
  387. shareObjData = {
  388. imageUrl: imageUrl
  389. };
  390. break;
  391. case 3:
  392. if (mediaUrl) {
  393. _this.showToast('暂不支持此分享类型');
  394. return;
  395. };
  396. shareObjData = {
  397. mediaUrl: mediaUrl
  398. };
  399. break;
  400. case 4:
  401. if (mediaUrl) {
  402. _this.showToast('暂不支持此分享类型');
  403. return;
  404. };
  405. shareObjData = {
  406. mediaUrl: mediaUrl
  407. };
  408. break;
  409. case 5:
  410. shareObjData = {
  411. miniProgram: { ...miniProgramObj,
  412. id: miniProgramId,
  413. type: miniProgramShareType
  414. },
  415. imageUrl: imageUrl
  416. };
  417. providerName = 'weixin';
  418. break;
  419. default:
  420. _this.showToast('分享参数-shareType错误');
  421. return;
  422. break;
  423. }
  424. shareObjData.title = title;
  425. _this.share(providerName, WXScene, shareType, shareObjData, function(res) {
  426. if (scb && typeof(scb) == 'function') scb(res);
  427. }, function(err) {
  428. if (fcb && typeof(fcb) == 'function') fcb(err);
  429. });
  430. },
  431. share(providerName, WXScene, shareType, data, scb, fcb) {
  432. let _this = this;
  433. let shareObj = {
  434. provider: '',
  435. success: Function,
  436. fail: Function
  437. };
  438. if (providerName && providerName != '') {
  439. shareObj.provider = providerName;
  440. if (providerName == 'weixin') {
  441. _this.readyShareWXScene(WXScene, function(Scene) {
  442. shareObj.scene = Scene;
  443. _this.doingShare(shareObj, shareType, data, scb, fcb);
  444. });
  445. } else {
  446. _this.doingShare(shareObj, shareType, data, scb, fcb);
  447. }
  448. } else {
  449. _this.getProvider('share', function(name) {
  450. shareObj.provider = name;
  451. if (name == 'weixin') {
  452. _this.readyShareWXScene(WXScene, function(Scene) {
  453. shareObj.scene = Scene;
  454. _this.doingShare(shareObj, shareType, data, scb, fcb);
  455. });
  456. } else {
  457. _this.doingShare(shareObj, shareType, data, scb, fcb);
  458. }
  459. }, true);
  460. }
  461. },
  462. readyShareWXScene(WXScene, cb) {
  463. let _this = this;
  464. let WXScenetypelist = {
  465. array: ['WXSceneSession', 'WXSenceTimeline', 'WXSceneFavorite']
  466. };
  467. if (WXScene && WXScene != "") {
  468. if (cb && typeof(cb) == 'function') cb(WXScene);
  469. } else {
  470. _this.actionSheet(WXScenetypelist, function(index) {
  471. if (cb && typeof(cb) == 'function') cb(WXScenetypelist.array[index]);
  472. });
  473. }
  474. },
  475. doingShare(shareObj, shareType, data, scb, fcb) {
  476. shareObj.type = shareType;
  477. if (data && data.title) shareObj.title = data.title;
  478. switch (shareType) {
  479. case 0: //图文链接
  480. shareObj.href = data.href;
  481. shareObj.summary = data.summary;
  482. shareObj.imageUrl = data.imageUrl;
  483. break;
  484. case 1: //纯文字
  485. shareObj.summary = data.summary;
  486. shareObj.href = data.href;
  487. break;
  488. case 2: //纯图片
  489. shareObj.imageUrl = data.imageUrl;
  490. break;
  491. case 3: //音乐
  492. if (!data.mediaUrl) {
  493. _this.showToast('暂不支持此分享类型');
  494. return;
  495. };
  496. shareObj.mediaUrl = data.mediaUrl;
  497. break;
  498. case 4: //视频
  499. if (!data.mediaUrl) {
  500. _this.showToast('暂不支持此分享类型');
  501. return;
  502. };
  503. shareObj.mediaUrl = data.mediaUrl;
  504. break;
  505. case 5: //小程序
  506. if (miniProgramId == '') {
  507. uni.showToast('未设置小程序ID, 请使用其他方式分享');
  508. return;
  509. }
  510. let mp = {
  511. id: miniProgramId
  512. };
  513. mp.path = data.miniProgram.path;
  514. mp.type = data.miniProgram.type;
  515. if (data.miniProgram.webUrl) mp.webUrl = data.miniProgram.webUrl;
  516. shareObj.miniProgram = mp;
  517. shareObj.imageUrl = data.imageUrl;
  518. break;
  519. default:
  520. appJS.showToast('分享参数-shareType错误');
  521. break;
  522. }
  523. shareObj.success = function(res) {
  524. if (scb && typeof(scb) == 'function') scb(res);
  525. }
  526. shareObj.fail = function(err) {
  527. if (fcb && typeof(fcb) == 'function') fcb(err);
  528. }
  529. log(JSON.stringify(shareObj));
  530. uni.share(shareObj);
  531. },
  532. // #endif
  533. }
  534. function checkMPUrl(url) {
  535. // #ifdef MP
  536. if(process.env.NODE_ENV !== 'development'){
  537. if(
  538. url.substring(0, 4) === 'http' &&
  539. url.substring(0, 5) !== 'https' &&
  540. url.substring(0, 12) !== 'http://store' &&
  541. url.substring(0, 10) !== 'http://tmp' &&
  542. url.substring(0, 10) !== 'http://usr'
  543. ) {
  544. url = 'https' + url.substring(4, url.length);
  545. }
  546. }
  547. // #endif
  548. return url;
  549. }
  550. module.exports = _app;