util.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // 路径转base64
  2. import { base64ToPath } from './image-tools'
  3. /**
  4. * base64转本地路径
  5. * @param { String } path 路径
  6. * @returns
  7. */
  8. export function base64ToPathFn(path) {
  9. let reg =
  10. /^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s]*?)\s*$/i
  11. if (!reg.test(path)) {
  12. return Promise.resolve(path)
  13. }
  14. return base64ToPath(path)
  15. }
  16. /**
  17. * 下载文件资源到本地,客户端直接发起一个 HTTP GET 请求,返回文件的本地临时路径
  18. * @param { String } url 资源路径
  19. * @param { Object } options 回调
  20. * @returns
  21. */
  22. export function downloadFile(url, options = {
  23. onProgressUpdate: () => {}
  24. }) {
  25. return new Promise(resolve => {
  26. try {
  27. let download = uni.downloadFile({
  28. url,
  29. header: options.header || {},
  30. success(res) {
  31. return resolve({
  32. success: true,
  33. data: res
  34. })
  35. },
  36. fail() {
  37. return resolve({
  38. success: false,
  39. message: `下载资源${url}失败`
  40. })
  41. }
  42. })
  43. // 下载进度回调
  44. download.onProgressUpdate(data => {
  45. options.onProgressUpdate(data)
  46. })
  47. } catch(e) {
  48. return resolve({
  49. success: false,
  50. msg: `下载资源${url}失败`
  51. })
  52. }
  53. })
  54. }
  55. /**
  56. * 加载提示框
  57. * @param { String } title 提示内容
  58. * @param { Boolean } mask 是否显示透明蒙层
  59. */
  60. export function showLoading(title, mask = true) {
  61. uni.showLoading({
  62. title,
  63. mask
  64. })
  65. }
  66. /**
  67. * 关闭加载提示框
  68. */
  69. export function hideLoading() {
  70. uni.hideLoading()
  71. }
  72. /**
  73. * 不需要确认提示框
  74. * @param { String } title 提示内容
  75. * @param { Object } options 参数
  76. * @param { Stirng } options.icon 提示图片
  77. * @param { String || Number } options.duration 时效
  78. */
  79. export function showToast(title, options = {}) {
  80. uni.showToast({
  81. title,
  82. icon: options.icon || "none",
  83. duration: options.duration || 1500,
  84. mask: options.mask || false
  85. })
  86. }
  87. /**
  88. * 保存图片到系统相册。
  89. * @param { String } filePath 图片文件路径,可以是临时文件路径也可以是永久文件路径,不支持网络图片路径
  90. * @returns
  91. */
  92. export function saveImageToPhotosAlbum(filePath) {
  93. return new Promise(resolve => {
  94. showLoading('保存中...')
  95. uni.saveImageToPhotosAlbum({
  96. filePath,
  97. success(res) {
  98. hideLoading()
  99. resolve({
  100. success: true,
  101. data: res.file
  102. })
  103. },
  104. fail(err) {
  105. hideLoading()
  106. resolve({
  107. success: false,
  108. message: err
  109. })
  110. }
  111. })
  112. })
  113. }
  114. /**
  115. * 计算文字长度
  116. * @param { CanvasText } Context canvas对象
  117. * @param { String } text 文本
  118. * @param { String } size 长度
  119. * @returns
  120. */
  121. export function countTextLength(Context, text, size) {
  122. let textLength = 0
  123. Context.setFontSize(size)
  124. try {
  125. textLength = Context.measureText(text)
  126. } catch(e) {
  127. textLength = {}
  128. }
  129. textLength = textLength && textLength.width ? textLength.width : 0
  130. if (textLength == 0) {
  131. for (let i of text) {
  132. textLength += Context.measureText(text)
  133. }
  134. textLength * size
  135. }
  136. return textLength
  137. }
  138. /**
  139. * 压缩图片 H5不支持
  140. * @param { Object } params
  141. * @param { String } params.src 图片路径,图片的路径,可以是相对路径、临时文件路径、存储文件路径
  142. * @param { String } params.quality 压缩质量,范围0~100,数值越小,质量越低,压缩率越高(仅对jpg有效)
  143. * @param { String } params.width 缩放图片的宽度,支持像素值(如"100px")、百分比(如"50%")、自动计算(如"auto",即根据height与源图高的缩放比例计算,若未设置height则使用源图高度)
  144. * @param { String } params.height 缩放图片的高度,支持像素值(如"100px")、百分比(如"50%")、自动计算(如"auto",即根据height与源图高的缩放比例计算,若未设置height则使用源图高度)
  145. * @returns
  146. */
  147. export function compressImage(params = {}) {
  148. return new Promise(resolve => {
  149. uni.compressImage({
  150. src: params.src || '',
  151. quality: params.quality || 80,
  152. width: params.width || 'auto',
  153. height: params.height || 'auto',
  154. success: res => {
  155. resolve({
  156. success: true,
  157. src: res.tempFilePath
  158. })
  159. },
  160. fail: res => {
  161. resolve({
  162. success: false,
  163. message: '压缩图片失败'
  164. })
  165. }
  166. })
  167. })
  168. }
  169. /**
  170. * 获取图片信息
  171. * @param { String } src 图片地址
  172. */
  173. export function getImageInfo(src){
  174. return new Promise(resolve =>{
  175. uni.getImageInfo({
  176. src,
  177. success: res => {
  178. let { path } = res
  179. // #ifdef H5
  180. let index = path.lastIndexOf('.', path.length)
  181. let type = ''
  182. if (index != -1) {
  183. type = path.substring(index + 1, path.length)
  184. } else {
  185. type = 'png'
  186. }
  187. res.type = type
  188. // #endif
  189. resolve({
  190. success: true,
  191. ...res
  192. })
  193. },
  194. fail: e => {
  195. resolve({
  196. success: false,
  197. msg: e
  198. })
  199. }
  200. })
  201. })
  202. }
  203. /**
  204. * 获取使用模式的图片信息
  205. * @param { String | Number } oWidth 原图宽度
  206. * @param { String | Number } oHeight 原图高度
  207. * @param { String | Number } x x轴位置
  208. * @param { String | Number } y y轴位置
  209. * @param { String | Number } width 宽度
  210. * @param { String | Number } height 高度
  211. * @param { String } mode 模式
  212. * aspectFit 保持纵横比缩放图片,使图片的长边能完全显示出来。也就是说,可以完整地将图片显示出来。
  213. * aspectFill 保持纵横比缩放图片,只保证图片的短边能完全显示出来。也就是说,图片通常只在水平或垂直方向是完整的,另一个方向将会发生截取。
  214. * widthFix 宽度不变,高度自动变化,保持原图宽高比不变
  215. * heightFix 高度不变,宽度自动变化,保持原图宽高比不变
  216. */
  217. export function getModeImage(oWidth, oHeight, x, y, width, height, mode) {
  218. if (mode == 'aspectFit') {
  219. return getAspectFitModelInfo(oWidth, oHeight, x, y, width, height)
  220. }
  221. if (mode == 'aspectFill') {
  222. return getAspectFillModelInfo(oWidth, oHeight, x, y, width, height)
  223. }
  224. if (mode == 'widthFix') {
  225. return getWidthFixModelInfo(oWidth, oHeight, x, y, width, height)
  226. }
  227. if (mode == 'heightFix') {
  228. return getHeightFixModelInfo(oWidth, oHeight, x, y, width, height)
  229. }
  230. if (mode == 'default') {
  231. return {
  232. dw: width,
  233. dh: height,
  234. dx: x,
  235. dy: y
  236. }
  237. }
  238. return getAspectFillModelInfo(oWidth, oHeight, x, y, width, height)
  239. }
  240. // aspectFit 保持纵横比缩放图片,使图片的长边能完全显示出来。也就是说,可以完整地将图片显示出来。
  241. function getAspectFitModelInfo(oWidth, oHeight, x, y, width, height) {
  242. let aspect = oHeight / oWidth
  243. let sw = width
  244. let sh = aspect * sw
  245. if (aspect > 1) {
  246. aspect = oWidth / oHeight
  247. sh = height
  248. sw = aspect * sh
  249. }
  250. return {
  251. sw,
  252. sh,
  253. sx: x,
  254. sy: y,
  255. dw: oWidth,
  256. dh: oHeight,
  257. dx: 0,
  258. dy: 0
  259. }
  260. }
  261. // 保持纵横比缩放图片,只保证图片的短边能完全显示出来。也就是说,图片通常只在水平或垂直方向是完整的,另一个方向将会发生截取。
  262. function getAspectFillModelInfo(oWidth, oHeight, x, y, width, height) {
  263. // 高比宽大 宽是短边
  264. let aspect = oHeight / oWidth
  265. let sw = width
  266. let sh = aspect * sw
  267. let dx = 0
  268. let dy = (sh - height) / 2
  269. if (aspect < 1) {
  270. // 高比宽小 高是短边
  271. aspect = oWidth / oHeight
  272. sh = height
  273. sw = aspect * sh
  274. dy = 0
  275. dx = (sw - width) / 2
  276. }
  277. return {
  278. sw,
  279. sh,
  280. sx: x,
  281. sy: y,
  282. dw: oWidth,
  283. dh: oHeight,
  284. dx,
  285. dy
  286. }
  287. }
  288. // 宽度不变,高度自动变化,保持原图宽高比不变
  289. function getWidthFixModelInfo(oWidth, oHeight, x, y, width, height) {
  290. let aspect = oHeight / oWidth
  291. let sw = width
  292. let sh = sw * aspect
  293. let dx = 0
  294. let dy = 0
  295. return {
  296. sw,
  297. sh,
  298. sx: x,
  299. sy: y,
  300. dw: oWidth,
  301. dh: oHeight,
  302. dx,
  303. dy
  304. }
  305. }
  306. // 高度不变,宽度自动变化,保持原图宽高比不变
  307. function getHeightFixModelInfo(oWidth, oHeight, x, y, width, height) {
  308. let aspect = oWidth / oHeight
  309. let sh = height
  310. let sw = sh * aspect
  311. let dx = 0
  312. let dy = 0
  313. return {
  314. sw,
  315. sh,
  316. sx: x,
  317. sy: y,
  318. dw: oWidth,
  319. dh: oHeight,
  320. dx,
  321. dy
  322. }
  323. }