nvue.js 5.5 KB

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