publicMethods.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { toast } from "@whale.io/mini/es/utils/fn";
  2. export * from "@whale.io/common/es/utils/fn";
  3. let systemInfo;
  4. /**
  5. * 转换rpx为px.
  6. *
  7. * @param rpx responsive pixel.
  8. * @returns pixel unit.
  9. * @see https://opendocs.alipay.com/mini/framework/acss#rpx
  10. */
  11. function rpxToPx(rpx) {
  12. if (!systemInfo) {
  13. systemInfo = my.getSystemInfoSync();
  14. }
  15. return rpx * (systemInfo.windowWidth / 750);
  16. }
  17. /**
  18. * 解析小程序之间相互跳转scheme.
  19. *
  20. * @param scheme Schema需符合`alipays://platformapi/startapp?appId=&page=`形式,page中如果还有其它url参数需encode.
  21. * @returns
  22. */
  23. function parseMiniScheme(scheme) {
  24. let miniParams = {};
  25. scheme = scheme || "";
  26. if (scheme.indexOf("alipays://platformapi/startapp") >= 0) {
  27. const schemaSegments = scheme.split("?");
  28. const pathParams = schemaSegments[1];
  29. const values = pathParams.split("&");
  30. values.forEach((item) => {
  31. miniParams = miniParams || {};
  32. const [key, val] = item.split("=");
  33. miniParams[key] = val;
  34. });
  35. }
  36. return miniParams;
  37. }
  38. function convertMiniScheme(params) {
  39. const { appId, path, query, extraData } = params;
  40. let scheme = `alipays://platformapi/startapp?appId=${appId}`;
  41. if (path) {
  42. scheme += `&page=${encodeURIComponent(path)}`;
  43. }
  44. if (query) {
  45. const newQuery = Object.keys(query)
  46. .map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(query[k])}`)
  47. .join("&");
  48. scheme += `&query=${encodeURIComponent(newQuery)}`;
  49. }
  50. if (extraData && Object.keys(extraData).length) {
  51. console.log("! extraData 不支持在 scheme 中使用", params);
  52. }
  53. return scheme;
  54. }
  55. const params = {
  56. appId: "2022061812345678",
  57. path: "/pages/index/index",
  58. query: { foo: "bar" },
  59. };
  60. export { rpxToPx, toast, parseMiniScheme, convertMiniScheme };