index.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <view class="container">
  3. <navBar title="绘本生成" :back="true" @onBack="onBack" color="#333333" background="#FFFFFF" />
  4. <view class="mainBox">
  5. <view class="titleBox">
  6. </view>
  7. <view class="contentBox">
  8. <view class="bgTag" :style="{'background-image':`url(${picBase+'resBg.png'})`}">
  9. {{huibenRes.title&&!showLoading?huibenRes.title:'内容生成中...'}}
  10. </view>
  11. <view class="storeBox">
  12. <view class="loading" v-if="showLoading">
  13. <!-- <view class="loading" :style="{display:!showLoading?'none':''}"> -->
  14. <image class="loadingImg" mode="widthFix" :src="picBase+'loading.gif'">
  15. </image>
  16. <view class="progress" v-if="taskId!=-1">
  17. <!-- <view class="progress"> -->
  18. <u-line-progress :percentage="percentage" activeColor="#017AFF"></u-line-progress>
  19. <view class="tipTxt">
  20. 生成大约需要2分钟,请不要离开....
  21. </view>
  22. </view>
  23. </view>
  24. <u-parse :content="content" @ready="loaded"></u-parse>
  25. </view>
  26. </view>
  27. </view>
  28. <view class="btnBox">
  29. <view class="btn b1" @click="downPdf">
  30. 生成PDF
  31. </view>
  32. <view class="btn b2" @click="downPic">
  33. 生成图片
  34. </view>
  35. </view>
  36. <u-modal showCancelButton @cancel="showLead=false" :show="showLead" title="PDF下载提示" confirmText="去预览" content=""
  37. @confirm="prePdf">
  38. <view class="slot-content">
  39. <view class="txt1">
  40. 下载PDF,请先点击预览页面右上角...菜单按钮,如图1:
  41. </view>
  42. <image src="../../../static/other/caidan.jpg" mode="widthFix" class="caidan"></image>
  43. ,再点击弹框中的保存到手机按钮,如图2:
  44. <image src="../../../static/other/caidan_s.jpg" mode="widthFix" class="caidan"></image>
  45. </view>
  46. </u-modal>
  47. </view>
  48. </template>
  49. <script>
  50. // import content from "../../../utils/pinyin.js"
  51. import {
  52. getGenRes,
  53. genPdf
  54. } from '@/api/index/index.js'
  55. // import navBar from '@/components/navBar/index.vue'
  56. export default {
  57. // components: {
  58. // navBar
  59. // },
  60. data() {
  61. return {
  62. picBase: this.$picBase2,
  63. taskId: -1,
  64. huibenRes: null,
  65. showLoading: true,
  66. timer: null,
  67. timer1: null,
  68. content: '',
  69. huibenDetail: null,
  70. percentage: 0,
  71. tempPdfile: '',
  72. showLead: false
  73. }
  74. },
  75. onLoad(o) {
  76. // console.log('content', content);
  77. // this.content = content.content
  78. if (o.taskId) {
  79. this.taskId = o.taskId
  80. this.showLoading = true
  81. console.log('this.taskId', this.taskId);
  82. this.timer = setInterval(() => {
  83. this.getGenRes()
  84. }, 1000)
  85. }
  86. if (getApp().huibenDetail) {
  87. this.timer1 = setInterval(() => {
  88. if (this.percentage <= 50) {
  89. this.percentage = this.percentage + 25
  90. console.log('this.percentage', this.percentage);
  91. }
  92. }, 1000)
  93. console.log('接收的绘本详情', getApp().huibenDetail);
  94. this.huibenRes = getApp().huibenDetail
  95. let firstImg = `<img src="${getApp().huibenDetail.image}"/>`
  96. this.content = firstImg + getApp().huibenDetail.pinyin_content
  97. getApp().huibenDetail = null
  98. // let pinyin_content = getApp().huibenDetail.pinyin_content
  99. // pinyin_content.replace(firstImg, '')
  100. // getApp().huibenDetail = null
  101. // huibenInfo.pinyin_content = firstImg + pinyin_content
  102. // this.huibenRes = huibenInfo
  103. console.log('this.content', this.content);
  104. }
  105. },
  106. methods: {
  107. prePdf() {
  108. this.showLead = false
  109. uni.openDocument({
  110. filePath: this.tempPdfile,
  111. showMenu: true, //是否可以分享
  112. success: (res) => {
  113. uni.hideLoading()
  114. console.log(res);
  115. },
  116. fail: (e) => {
  117. uni.showToast({
  118. title: '打开失败',
  119. icon: "error"
  120. })
  121. }
  122. })
  123. },
  124. downPic() {
  125. if (!uni.getStorageSync('token')) {
  126. return this.$toast('请登录后操作')
  127. }
  128. uni.showLoading({
  129. title: '生成中'
  130. });
  131. console.log('img-path', this.huibenRes.image_path);
  132. // this.pictureReview(this.huibenRes.image_path)
  133. this.download(this.huibenRes.image_path, 2)
  134. },
  135. pictureReview(arr) {
  136. uni.previewImage({
  137. urls: [arr],
  138. longPressActions: {
  139. itemList: ['发送给朋友', '保存图片', '收藏'],
  140. success: function(data) {
  141. console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  142. },
  143. fail: function(err) {
  144. console.log(err.errMsg);
  145. }
  146. }
  147. });
  148. },
  149. loaded() {
  150. clearInterval(this.timer1)
  151. this.percentage = 100
  152. this.showLoading = false
  153. console.log('loaded()------');
  154. },
  155. async downPdf() {
  156. if (!uni.getStorageSync('token')) {
  157. return this.$toast('请登录后操作')
  158. }
  159. uni.showLoading({
  160. title: '生成中'
  161. });
  162. console.log('downPdf-------------', {
  163. id: this.taskId != -1 ? this.taskId : this.huibenRes.id
  164. // id: 4
  165. });
  166. let res1 = await genPdf({
  167. id: this.taskId != -1 ? this.taskId : this.huibenRes.id
  168. // id: 4
  169. })
  170. console.log('获取pdfurl返回值--------2', res1);
  171. if (res1.code == 0) {
  172. console.log('pdf地址:', res1.data.url);
  173. this.download(res1.data.url, 1)
  174. } else {
  175. uni.hideLoading()
  176. this.$toast(res1.message)
  177. }
  178. },
  179. download(file, type) {
  180. let _this = this
  181. //下载文件
  182. uni.downloadFile({ //只能是GET请求
  183. url: file, //请求地址(后台返回的码流地址)
  184. success: (res) => {
  185. //下载成功
  186. if (res.statusCode === 200) {
  187. //保存文件
  188. let tempFile = res.tempFilePath;
  189. if (type == 2) {
  190. uni.hideLoading()
  191. // return _this.pictureReview(tempFile)
  192. return uni.navigateTo({
  193. url: '/pages/index/filePre/index?url=' + tempFile
  194. })
  195. }
  196. //保存成功之后 打开文件
  197. uni.getFileSystemManager().saveFile({
  198. tempFilePath: tempFile,
  199. filePath: wx.env.USER_DATA_PATH + '/' + this.huibenRes.title +
  200. '.pdf', //自定义文件名
  201. success(res) {
  202. console.log(res)
  203. uni.hideLoading()
  204. _this.tempPdfile = res.savedFilePath
  205. _this.showLead = true
  206. }
  207. })
  208. }
  209. },
  210. fail: (e) => {
  211. uni.hideLoading()
  212. console.log(e, '文件下载失败')
  213. uni.showToast({
  214. title: '文件下载失败',
  215. icon: "error",
  216. })
  217. }
  218. });
  219. },
  220. onBack() {
  221. this.huibenRes = null
  222. // getApp().refreshRole = true
  223. console.log('onBack()-----------');
  224. if (this.timer) {
  225. clearInterval(this.timer)
  226. }
  227. },
  228. async getGenRes() {
  229. let res1 = await getGenRes({
  230. id: this.taskId
  231. })
  232. console.log('查询生成结果返回值--------2', res1);
  233. if (res1.code == 0) {
  234. if (res1.data.state == 0) {
  235. this.percentage = 10
  236. } else if (res1.data.state == 1) {
  237. this.percentage = 16
  238. } else if (res1.data.state == 2) {
  239. this.percentage = 32
  240. } else if (res1.data.state == 3) {
  241. this.percentage = 48
  242. } else if (res1.data.state == 4) {
  243. this.percentage = 64
  244. } else if (res1.data.state == 5) {
  245. this.percentage = 80
  246. } else if (res1.data.state == 6) {
  247. this.huibenRes = res1.data
  248. let firstImg = `<img src="${res1.data.image}"/>`
  249. this.content = firstImg + res1.data.pinyin_content
  250. this.percentage = 99
  251. // this.showLoading = false
  252. clearInterval(this.timer)
  253. }
  254. // this.chargeList=[]
  255. } else {
  256. this.$toast(res1.message)
  257. }
  258. },
  259. }
  260. }
  261. </script>
  262. <style lang="scss" scoped>
  263. @import "./index.scss";
  264. @import "./util.scss";
  265. </style>