nvue.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // #ifdef APP-NVUE
  2. import {
  3. sleep,
  4. getImageInfo,
  5. isBase64,
  6. networkReg
  7. } from './utils';
  8. const dom = weex.requireModule('dom')
  9. import {
  10. version
  11. } from '../../package.json'
  12. export default {
  13. data() {
  14. return {
  15. tempFilePath: [],
  16. isInitFile: false,
  17. osName: uni.getSystemInfoSync().osName
  18. }
  19. },
  20. methods: {
  21. getParentWeith() {
  22. return new Promise(resolve => {
  23. dom.getComponentRect(this.$refs.limepainter, (res) => {
  24. this.parentWidth = Math.ceil(res.size.width)
  25. this.canvasWidth = this.canvasWidth || this.parentWidth || 300
  26. this.canvasHeight = res.size.height || this.canvasHeight || 150
  27. resolve(res.size)
  28. })
  29. })
  30. },
  31. onPageFinish() {
  32. this.webview = this.$refs.webview
  33. this.webview.evalJS(`init(${this.dpr})`)
  34. },
  35. onMessage(e) {
  36. const res = e.detail.data[0] || null;
  37. if (res.event) {
  38. if (res.event == 'inited') {
  39. this.inited = true
  40. }
  41. if (res.event == 'fail') {
  42. this.$emit('fail', res)
  43. }
  44. if (res.event == 'layoutChange') {
  45. const data = typeof res.data == 'string' ? JSON.parse(res.data) : res.data
  46. this.canvasWidth = Math.ceil(data.width);
  47. this.canvasHeight = Math.ceil(data.height);
  48. }
  49. if (res.event == 'progressChange') {
  50. this.progress = res.data * 1
  51. }
  52. if (res.event == 'file') {
  53. this.tempFilePath.push(res.data)
  54. if (this.tempFilePath.length > 7) {
  55. this.tempFilePath.shift()
  56. }
  57. return
  58. }
  59. if (res.event == 'success') {
  60. if (res.data) {
  61. this.tempFilePath.push(res.data)
  62. if (this.tempFilePath.length > 8) {
  63. this.tempFilePath.shift()
  64. }
  65. if (this.isCanvasToTempFilePath) {
  66. this.setFilePath(this.tempFilePath.join(''), {
  67. isEmit: true
  68. })
  69. }
  70. } else {
  71. this.$emit('fail', 'canvas no data')
  72. }
  73. return
  74. }
  75. this.$emit(res.event, JSON.parse(res.data));
  76. } else if (res.file) {
  77. this.file = res.data;
  78. } else {
  79. console.info(res[0])
  80. }
  81. },
  82. getWebViewInited() {
  83. if (this.inited) return Promise.resolve(this.inited);
  84. return new Promise((resolve) => {
  85. this.$watch(
  86. 'inited',
  87. async val => {
  88. if (val) {
  89. resolve(val)
  90. }
  91. }, {
  92. immediate: true
  93. }
  94. );
  95. })
  96. },
  97. getTempFilePath() {
  98. if (this.tempFilePath.length == 8) return Promise.resolve(this.tempFilePath)
  99. return new Promise((resolve) => {
  100. this.$watch(
  101. 'tempFilePath',
  102. async val => {
  103. if (val.length == 8) {
  104. resolve(val.join(''))
  105. }
  106. }, {
  107. deep: true
  108. }
  109. );
  110. })
  111. },
  112. getWebViewDone() {
  113. if (this.progress == 1) return Promise.resolve(this.progress);
  114. return new Promise((resolve) => {
  115. this.$watch(
  116. 'progress',
  117. async val => {
  118. if (val == 1) {
  119. this.$emit('done')
  120. this.done = true
  121. this.runTask()
  122. resolve(val)
  123. }
  124. }, {
  125. immediate: true
  126. }
  127. );
  128. })
  129. },
  130. async render(args) {
  131. try {
  132. await this.getSize(args)
  133. const {
  134. width
  135. } = args.css || args
  136. if (!width && this.parentWidth) {
  137. Object.assign(args, {
  138. width: this.parentWidth
  139. })
  140. }
  141. const newNode = await this.calcImage(args);
  142. await this.getWebViewInited()
  143. this.webview.evalJS(`source(${JSON.stringify(newNode)})`)
  144. await this.getWebViewDone()
  145. await sleep(this.afterDelay)
  146. if (this.isCanvasToTempFilePath) {
  147. const params = {
  148. fileType: this.fileType,
  149. quality: this.quality
  150. }
  151. this.webview.evalJS(`save(${JSON.stringify(params)})`)
  152. }
  153. return Promise.resolve()
  154. } catch (e) {
  155. this.$emit('fail', e)
  156. }
  157. },
  158. async calcImage(args) {
  159. let node = JSON.parse(JSON.stringify(args))
  160. const urlReg = /url\((.+)\)/
  161. const {
  162. backgroundImage
  163. } = node.css || {}
  164. const isBG = backgroundImage && urlReg.exec(backgroundImage)[1]
  165. const url = node.url || node.src || isBG
  166. if (['text', 'qrcode'].includes(node.type)) {
  167. return node
  168. }
  169. if ((node.type === "image" || isBG) && url && !isBase64(url) && (this.osName == 'ios' || !networkReg
  170. .test(url))) {
  171. let {
  172. path
  173. } = await getImageInfo(url, true)
  174. if (isBG) {
  175. node.css.backgroundImage = `url(${path})`
  176. } else {
  177. node.src = path
  178. }
  179. } else if (node.views && node.views.length) {
  180. for (let i = 0; i < node.views.length; i++) {
  181. node.views[i] = await this.calcImage(node.views[i])
  182. }
  183. }
  184. return node
  185. },
  186. async canvasToTempFilePath(args = {}) {
  187. if (!this.inited) {
  188. return this.$emit('fail', 'no init')
  189. }
  190. this.tempFilePath = []
  191. if (args.fileType == 'jpg') {
  192. args.fileType = 'jpeg'
  193. }
  194. this.webview.evalJS(`save(${JSON.stringify(args)})`)
  195. try {
  196. let tempFilePath = await this.getTempFilePath()
  197. tempFilePath = await this.setFilePath(tempFilePath, args)
  198. args.success({
  199. errMsg: "canvasToTempFilePath:ok",
  200. tempFilePath
  201. })
  202. } catch (e) {
  203. console.log('e', e)
  204. args.fail({
  205. error: e
  206. })
  207. }
  208. }
  209. }
  210. }
  211. // #endif