util.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // 路径转base64
  2. import { base64ToPath } from '../plugins/image-tools'
  3. /**
  4. * 计算文字长度
  5. * @param { CanvasText } Context Context
  6. * @param { String } text 文本
  7. * @param { String } fontSize 文字大小
  8. * @returns
  9. */
  10. export const countTextLength = (Context, text, fontSize) => {
  11. const length = measureText(Context, text, fontSize)
  12. return length !== 0 ? length : text.length * fontSize
  13. }
  14. /**
  15. * 计算文字大小
  16. * @param { CanvasText } Context Context
  17. * @param { String } text 文本
  18. * @param { String } fontSize 文字大小
  19. * @returns
  20. */
  21. function measureText(Context, text, fontSize) {
  22. if (Context.measureText) {
  23. return Context.measureText(text)?.width || 0
  24. }
  25. // 这是为了兼容钉钉小程序,钉钉小程序没有Context.measureText方法。。。
  26. let length = 0
  27. for (let i of text) {
  28. length += countStrLength(i)
  29. }
  30. return length * fontSize
  31. }
  32. /**
  33. * 计算字符大小
  34. * @param { String } t 字符
  35. * @returns
  36. */
  37. function countStrLength(t) {
  38. let l;
  39. if (/a/.test(t)) {
  40. l = 0.552734375
  41. } else if (/b/.test(t)) {
  42. l = 0.638671875
  43. } else if (/c/.test(t)) {
  44. l = 0.50146484375
  45. } else if (/d/.test(t)) {
  46. l = 0.6396484375
  47. } else if (/e/.test(t)) {
  48. l = 0.5673828125
  49. } else if (/f/.test(t)) {
  50. l = 0.3466796875
  51. } else if (/g/.test(t)) {
  52. l = 0.6396484375
  53. } else if (/h/.test(t)) {
  54. l = 0.61572265625
  55. } else if (/i/.test(t)) {
  56. l = 0.26611328125
  57. } else if (/j/.test(t)) {
  58. l = 0.26708984375
  59. } else if (/k/.test(t)) {
  60. l = 0.54443359375
  61. } else if (/l/.test(t)) {
  62. l = 0.26611328125
  63. } else if (/m/.test(t)) {
  64. l = 0.93701171875
  65. } else if (/n/.test(t)) {
  66. l = 0.6162109375
  67. } else if (/o/.test(t)) {
  68. l = 0.6357421875
  69. } else if (/p/.test(t)) {
  70. l = 0.638671875
  71. } else if (/q/.test(t)) {
  72. l = 0.6396484375
  73. } else if (/r/.test(t)) {
  74. l = 0.3818359375
  75. } else if (/s/.test(t)) {
  76. l = 0.462890625
  77. } else if (/t/.test(t)) {
  78. l = 0.37255859375
  79. } else if (/u/.test(t)) {
  80. l = 0.6162109375
  81. } else if (/v/.test(t)) {
  82. l = 0.52490234375
  83. } else if (/w/.test(t)) {
  84. l = 0.78955078125
  85. } else if (/x/.test(t)) {
  86. l = 0.5068359375
  87. } else if (/y/.test(t)) {
  88. l = 0.529296875
  89. } else if (/z/.test(t)) {
  90. l = 0.49169921875
  91. } else if (/A/.test(t)) {
  92. l = 0.70361328125
  93. } else if (/B/.test(t)) {
  94. l = 0.62744140625
  95. } else if (/C/.test(t)) {
  96. l = 0.6689453125
  97. } else if (/D/.test(t)) {
  98. l = 0.76171875
  99. } else if (/E/.test(t)) {
  100. l = 0.5498046875
  101. } else if (/F/.test(t)) {
  102. l = 0.53125
  103. } else if (/G/.test(t)) {
  104. l = 0.74365234375
  105. } else if (/H/.test(t)) {
  106. l = 0.7734375
  107. } else if (/I/.test(t)) {
  108. l = 0.2939453125
  109. } else if (/J/.test(t)) {
  110. l = 0.39599609375
  111. } else if (/K/.test(t)) {
  112. l = 0.634765625
  113. } else if (/L/.test(t)) {
  114. l = 0.51318359375
  115. } else if (/M/.test(t)) {
  116. l = 0.97705078125
  117. } else if (/N/.test(t)) {
  118. l = 0.81298828125
  119. } else if (/O/.test(t)) {
  120. l = 0.81494140625
  121. } else if (/P/.test(t)) {
  122. l = 0.61181640625
  123. } else if (/Q/.test(t)) {
  124. l = 0.81494140625
  125. } else if (/R/.test(t)) {
  126. l = 0.65283203125
  127. } else if (/S/.test(t)) {
  128. l = 0.5771484375
  129. } else if (/T/.test(t)) {
  130. l = 0.5732421875
  131. } else if (/U/.test(t)) {
  132. l = 0.74658203125
  133. } else if (/V/.test(t)) {
  134. l = 0.67626953125
  135. } else if (/W/.test(t)) {
  136. l = 1.017578125
  137. } else if (/X/.test(t)) {
  138. l = 0.64501953125
  139. } else if (/Y/.test(t)) {
  140. l = 0.603515625
  141. } else if (/Z/.test(t)) {
  142. l = 0.6201171875
  143. } else if (/[0-9]/.test(t)) {
  144. l = 0.58642578125
  145. } else if (/[\u4e00-\u9fa5]/.test(t)) {
  146. l = 1
  147. } else if (/ /.test(t)) {
  148. l = 0.2958984375
  149. } else if (/\`/.test(t)) {
  150. l = 0.294921875
  151. } else if (/\~/.test(t)) {
  152. l = 0.74169921875
  153. } else if (/\!/.test(t)) {
  154. l = 0.3125
  155. } else if (/\@/.test(t)) {
  156. l = 1.03125
  157. } else if (/\#/.test(t)) {
  158. l = 0.63818359375
  159. } else if (/\$/.test(t)) {
  160. l = 0.58642578125
  161. } else if (/\%/.test(t)) {
  162. l = 0.8896484375
  163. } else if (/\^/.test(t)) {
  164. l = 0.74169921875
  165. } else if (/\&/.test(t)) {
  166. l = 0.8701171875
  167. } else if (/\*/.test(t)) {
  168. l = 0.455078125
  169. } else if (/\(/.test(t)) {
  170. l = 0.333984375
  171. } else if (/\)/.test(t)) {
  172. l = 0.333984375
  173. } else if (/\_/.test(t)) {
  174. l = 0.4482421875
  175. } else if (/\-/.test(t)) {
  176. l = 0.4326171875
  177. } else if (/\+/.test(t)) {
  178. l = 0.74169921875
  179. } else if (/\=/.test(t)) {
  180. l = 0.74169921875
  181. } else if (/\|/.test(t)) {
  182. l = 0.26904296875
  183. } else if (/\\/.test(t)) {
  184. l = 0.416015625
  185. } else if (/\[/.test(t)) {
  186. l = 0.333984375
  187. } else if (/\]/.test(t)) {
  188. l = 0.333984375
  189. } else if (/\;/.test(t)) {
  190. l = 0.24072265625
  191. } else if (/\'/.test(t)) {
  192. l = 0.25634765625
  193. } else if (/\,/.test(t)) {
  194. l = 0.24072265625
  195. } else if (/\./.test(t)) {
  196. l = 0.24072265625
  197. } else if (/\//.test(t)) {
  198. l = 0.42724609375
  199. } else if (/\{/.test(t)) {
  200. l = 0.333984375
  201. } else if (/\}/.test(t)) {
  202. l = 0.333984375
  203. } else if (/\:/.test(t)) {
  204. l = 0.24072265625
  205. } else if (/\"/.test(t)) {
  206. l = 0.435546875
  207. } else if (/\</.test(t)) {
  208. l = 0.74169921875
  209. } else if (/\>/.test(t)) {
  210. l = 0.74169921875
  211. } else if (/\?/.test(t)) {
  212. l = 0.48291015625
  213. } else {
  214. l = 1
  215. }
  216. return l;
  217. }
  218. /**
  219. * 转换单位
  220. * @param { Object } params 转换的数据集合
  221. */
  222. export const conversionUnit = (params, getConvertedValue, _this) => {
  223. const filterAttrs = ['quality', 'alpha', 'deg', 'zIndex', 'e', 's', 'drawImage', 'drawModeImage']
  224. const filterObject = () => {
  225. const result = Object.assign({}, params)
  226. Object.keys(params).forEach(key => {
  227. const value = params[key]
  228. if (!filterAttrs.includes(key)) {
  229. if (typeof value === 'number') {
  230. if (value !== 375.0001) {
  231. result[key] = getConvertedValue.call(_this, value)
  232. }
  233. } else if (typeof value === 'object' && !(value instanceof Array)) {
  234. result[key] = conversionUnit(result[key], getConvertedValue, _this)
  235. } else if (value instanceof Array) {
  236. result[key] = value.map(item => typeof item === 'number' ? getConvertedValue.call(_this, item) : item)
  237. }
  238. }
  239. })
  240. return result
  241. }
  242. if (!(params instanceof Array)) {
  243. return filterObject()
  244. } else {
  245. const filterArray = () => params.map(item => conversionUnit(item, getConvertedValue, _this))
  246. return filterArray()
  247. }
  248. }
  249. /**
  250. * 下载网路图片资源
  251. * @param { String } url 图片路径
  252. * @param { Object } options 参数
  253. * @returns
  254. */
  255. export const downloadFile = (url, options = {}) => {
  256. return new Promise(resolve => {
  257. const { header = {} } = options
  258. try {
  259. const download = uni.downloadFile({
  260. url,
  261. header,
  262. success: res => {
  263. return resolve({
  264. success: true,
  265. data: res
  266. })
  267. },
  268. fail: err => {
  269. return resolve({
  270. success: false,
  271. message: `下载资源${url}失败---${JSON.stringify(err)}`
  272. })
  273. }
  274. })
  275. } catch(e) {
  276. return resolve({
  277. success: false,
  278. msg: `下载资源${url}失败---${JSON.stringify(e)}`
  279. })
  280. }
  281. })
  282. }
  283. /**
  284. * base64转本地路径
  285. * @param { String } path 路径
  286. * @returns
  287. */
  288. export const base64ToPathFn = path => {
  289. const reg =
  290. /^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s]*?)\s*$/i
  291. if (!reg.test(path)) {
  292. return Promise.resolve(path)
  293. }
  294. return base64ToPath(path)
  295. }
  296. /**
  297. * 保存图片到相册
  298. * @param { String } filePath 图片文件路径,可以是临时文件路径也可以是永久文件路径,不支持网络图片路径
  299. * @returns
  300. */
  301. export const saveImageToPhotosAlbum = filePath => {
  302. return new Promise(resolve => {
  303. uni.showLoading({ title: '保存中...' })
  304. uni.saveImageToPhotosAlbum({
  305. filePath,
  306. success: res => {
  307. resolve({
  308. success: true,
  309. data: res.file
  310. })
  311. },
  312. fail: err => {
  313. resolve({
  314. success: false,
  315. message: err
  316. })
  317. },
  318. complete: () => {
  319. uni.hideLoading()
  320. }
  321. })
  322. })
  323. }