wx-utils.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * @Author: Mr.Mao
  3. * @LastEditors: Mr.Mao
  4. * @Date: 2020-10-12 08:49:27
  5. * @LastEditTime: 2020-12-09 13:54:10
  6. * @Description:
  7. * @任何一个傻子都能写出让电脑能懂的代码,而只有好的程序员可以写出让人能看懂的代码
  8. */
  9. import uni from "./global";
  10. import { isBaseUrl, isNetworkUrl, isTmpUrl } from './utils';
  11. // 下载指定地址图片, 如果不符合下载图片, 则直接返回
  12. export const downloadImgUrl = (url) => {
  13. const isLocalFile = isBaseUrl(url) || isTmpUrl(url) || !isNetworkUrl(url);
  14. return new Promise((resolve, reject) => {
  15. if (isLocalFile) {
  16. return resolve(url);
  17. }
  18. uni.downloadFile({
  19. url,
  20. success: (res) => resolve(res.tempFilePath),
  21. fail: reject
  22. });
  23. });
  24. };
  25. // 获取当前指定 node 节点
  26. export const getCanvas2dContext = (selector, componentThis) => {
  27. return new Promise(resolve => {
  28. const query = (componentThis ?
  29. uni.createSelectorQuery().in(componentThis) :
  30. uni.createSelectorQuery());
  31. query.select(selector)
  32. .fields({ node: true }, res => {
  33. const node = res === null || res === void 0 ? void 0 : res.node;
  34. resolve(node || {});
  35. }).exec();
  36. });
  37. };