app.js 13 KB

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