index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import user from './user';
  2. import goods from './goods';
  3. import groupon from './groupon';
  4. export function getPosterData(options) {
  5. switch (options.shareInfo.poster.type) {
  6. case 'user':
  7. return user(options);
  8. case 'goods':
  9. return goods(options);
  10. case 'groupon':
  11. return groupon(options);
  12. }
  13. }
  14. export function formatImageUrlProtocol(url) {
  15. // #ifdef H5
  16. // H5平台 https协议下需要转换
  17. if (window.location.protocol === 'https:' && url.indexOf('http:') === 0) {
  18. url = url.replace('http:', 'https:');
  19. }
  20. // #endif
  21. // #ifdef MP-WEIXIN
  22. // 小程序平台 需要强制转换为https协议
  23. if (url.indexOf('http:') === 0) {
  24. url = url.replace('http:', 'https:');
  25. }
  26. // #endif
  27. return url;
  28. }
  29. export function getBase64Src(base64data, appType) {
  30. const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || [];
  31. switch (appType) {
  32. case 'wechat':
  33. const filePath = `${wx.env.USER_DATA_PATH}/tmp_base64src.${format}`;
  34. return new Promise((resolve, reject) => {
  35. const fileManager = uni.getFileSystemManager();
  36. fileManager.writeFile({
  37. filePath: filePath,
  38. data: bodyData, // base64 数据
  39. encoding: 'base64', // 字符编码
  40. success: () => {
  41. resolve(filePath);
  42. },
  43. file: (err) => {
  44. console.log('base64 保存失败', err);
  45. },
  46. });
  47. });
  48. default:
  49. console.warn('获得 base64 图片地址只做了微信小程序端的转换,其它端请自行实现!!!');
  50. break;
  51. }
  52. }