时空网前端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
6.7 KiB

  1. <template>
  2. <view>
  3. <view class="flex justify-center lf-m-t-20">
  4. <!-- <image src="../../static/images/bill.png" mode="widthFix" style="height: 1220rpx;width: 686rpx;"></image> -->
  5. <image v-if="imagePath" :src="imagePath" mode="widthFix"></image>
  6. </view>
  7. <view class="canvas-box">
  8. <canvas style="width: 375px;height: 667px;position:fixed;top:9999px" canvas-id="mycanvas" />
  9. </view>
  10. <view class="btn-bottom" style="bottom: 40rpx;">
  11. <view class="padding-lr-lg">
  12. <button class="cu-btn block bg-orange lg" style="border-radius: 42rpx;" @tap="saveBill()">
  13. <text class="lf-font-32 text-white">保存</text>
  14. </button>
  15. </view>
  16. <!-- <view class="flex justify-center align-center lf-m-t-30">
  17. <view class="lf-color-gray lf-font-28">https//hbdsufiewhuifusikj.com</view>
  18. <view class="lf-font-24 text-orange lf-m-l-30" @click="copy('https://hbdsufiewhuifusikj.com')">复制</view>
  19. </view> -->
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. export default {
  25. data() {
  26. return {
  27. base64Img: '',
  28. checkArea: 'Cannot find module',
  29. maskHidden: false,
  30. info: {
  31. avatar: '',
  32. nickname: '',
  33. id: '',
  34. tel: '',
  35. tags: []
  36. },
  37. showLogin: true,
  38. imagePath: '',
  39. userToken: '',
  40. wxCode: '',
  41. onceCode: '',
  42. backgroundImg: ''
  43. }
  44. },
  45. onLoad() {
  46. wx.showToast({
  47. title: '生成海报中...',
  48. icon: 'loading',
  49. duration: 1000
  50. });
  51. this.getWxCode()
  52. this.getBackground()
  53. },
  54. methods: {
  55. getBackground() {
  56. let _this = this
  57. _this.$http(_this.API.API_BILLBACKGROUND, {
  58. type: 'telent_share'
  59. }).then(res => {
  60. let img = res.data.img_url
  61. if (img) {
  62. wx.getImageInfo({
  63. src: img,
  64. success: function(sres) {
  65. _this.backgroundImg = sres.path
  66. console.log('临时路径',_this.backgroundImg)
  67. }
  68. })
  69. }
  70. })
  71. },
  72. getWxCode() {
  73. this.$http(this.API.API_WXBILL, {
  74. scene: 'route=home&id=2&pt=2',
  75. page: 'pages/route/index',
  76. width: '2800'
  77. }).then(res => {
  78. this.wxCode = res.data.base_url
  79. if (this.wxCode) {
  80. this.getwxCodeImg()
  81. }
  82. })
  83. },
  84. //保存二维码
  85. getwxCodeImg() {
  86. var imgSrc = this.wxCode; //base64编码
  87. var save = wx.getFileSystemManager();
  88. var number = Math.random();
  89. save.writeFile({
  90. filePath: wx.env.USER_DATA_PATH + '/pic' + number + '.jpg',
  91. data: imgSrc,
  92. encoding: 'base64',
  93. success: res => {
  94. this.onceCode = wx.env.USER_DATA_PATH + '/pic' + number + '.jpg'
  95. console.log(this.onceCode)
  96. if (this.onceCode && this.backgroundImg) {
  97. this.createNewImg()
  98. }else if(!this.onceCode){
  99. this.$msg('小程序码生成失败!')
  100. }else if(!this.backgroundImg){
  101. this.$msg('海报背景图生成失败!')
  102. }
  103. },
  104. fail: err => {
  105. console.log(err)
  106. }
  107. })
  108. },
  109. createNewImg() {
  110. var that = this;
  111. var context = wx.createCanvasContext('mycanvas');
  112. console.log('画布', that.backgroundImg)
  113. var path = that.backgroundImg;
  114. context.drawImage(path, 0, 0, 375, 667);
  115. //绘制二维码
  116. let wxcode = that.onceCode
  117. context.drawImage(wxcode, 18, 530, 120, 120);
  118. //绘制名字
  119. // context.setFontSize(24);
  120. // context.setFillStyle('#fff');
  121. // context.setTextAlign('center');
  122. // context.fillText(name, 34, 620);
  123. context.stroke();
  124. context.draw();
  125. //将生成好的图片保存到本地,需要延迟一会,绘制期间耗时
  126. setTimeout(function() {
  127. wx.canvasToTempFilePath({
  128. canvasId: 'mycanvas',
  129. success: function(res) {
  130. that.imagePath = res.tempFilePath;
  131. if (that.imagePath) {
  132. that.canvasHidden = true
  133. that.maskHidden = true
  134. }
  135. console.log('海报生成成功,图片链接', that.imagePath)
  136. },
  137. fail: function(res) {
  138. console.log(res);
  139. }
  140. });
  141. }, 900);
  142. },
  143. saveBill() {
  144. var that = this
  145. wx.saveImageToPhotosAlbum({
  146. filePath: that.imagePath,
  147. success(res) {
  148. wx.showModal({
  149. content: '图片已保存到相册,赶紧晒一下吧~',
  150. showCancel: false,
  151. confirmText: '好的',
  152. confirmColor: '#333',
  153. success: function(res) {
  154. if (res.confirm) {
  155. console.log('用户点击确定');
  156. that.maskHidden = false
  157. }
  158. },
  159. fail: function(res) {
  160. that.maskHidden = false
  161. }
  162. })
  163. }
  164. })
  165. },
  166. // 点击复制
  167. copy(text) {
  168. uni.setClipboardData({
  169. data: text
  170. });
  171. },
  172. }
  173. }
  174. </script>
  175. <style>
  176. .bgImg {
  177. display: block;
  178. width: 100%;
  179. height: 366rpx;
  180. }
  181. .mine {
  182. display: block;
  183. text-align: center;
  184. color: #333;
  185. margin-top: 44rpx;
  186. }
  187. .code {
  188. display: block;
  189. text-align: center;
  190. color: #333;
  191. font-size: 76rpx;
  192. font-weight: bold;
  193. margin-top: 30rpx;
  194. }
  195. .who {
  196. display: block;
  197. margin-top: 80rpx;
  198. font-size: 32rpx;
  199. color: #333;
  200. text-align: center;
  201. }
  202. .inputBox {
  203. text-align: center;
  204. margin-top: 44rpx;
  205. }
  206. .input {
  207. text-align: center;
  208. width: 440rpx;
  209. height: 88rpx;
  210. border-radius: 44rpx;
  211. background: #f5f5f5;
  212. font-size: 32rpx;
  213. display: inline-block;
  214. }
  215. .btn {
  216. width: 160rpx;
  217. height: 88rpx;
  218. border-radius: 44rpx;
  219. background: linear-gradient(90deg, rgba(255, 226, 0, 1), rgba(255, 200, 11, 1));
  220. box-shadow: 0px 4px 8px 0px rgba(255, 200, 11, 0.5);
  221. color: #333;
  222. font-size: 32rpx;
  223. display: inline-block;
  224. line-height: 88rpx;
  225. margin-left: 40rpx;
  226. }
  227. button[class="btn"]::after {
  228. border: 0;
  229. }
  230. .tishi {
  231. display: block;
  232. text-align: center;
  233. color: #999;
  234. margin-top: 30rpx;
  235. }
  236. .shareText {
  237. display: block;
  238. text-align: center;
  239. color: #333;
  240. font-size: 28rpx;
  241. margin-top: 100rpx;
  242. }
  243. .imgBox {
  244. text-align: center;
  245. width: 100%;
  246. margin-top: 60rpx;
  247. padding-bottom: 120rpx;
  248. }
  249. .img {
  250. display: inline-block;
  251. width: 100%;
  252. height: 100%;
  253. }
  254. .m_l {
  255. margin-left: 180rpx;
  256. }
  257. .zfbtn {
  258. display: inline-block;
  259. width: 120rpx;
  260. height: 120rpx;
  261. border-radius: 50%;
  262. background: transparent;
  263. outline: none;
  264. border: 0;
  265. padding: 0;
  266. }
  267. button[class="zfbtn"]::after {
  268. border: 0;
  269. }
  270. button[class="zfbtn m_l"]::after {
  271. border: 0;
  272. }
  273. .imagePathBox {
  274. width: 100%;
  275. height: 100%;
  276. background: rgba(0, 0, 0, 0.7);
  277. position: fixed;
  278. top: 0;
  279. left: 0;
  280. right: 0;
  281. bottom: 0;
  282. z-index: 10;
  283. }
  284. .shengcheng {
  285. width: 80%;
  286. height: 80%;
  287. position: fixed;
  288. top: 50rpx;
  289. left: 50%;
  290. margin-left: -40%;
  291. z-index: 10;
  292. }
  293. .baocun {
  294. display: block;
  295. width: 80%;
  296. height: 80rpx;
  297. padding: 0;
  298. line-height: 80rpx;
  299. text-align: center;
  300. position: fixed;
  301. bottom: 50rpx;
  302. left: 10%;
  303. background: #ffe200;
  304. color: #333;
  305. font-size: 32rpx;
  306. border-radius: 44rpx;
  307. }
  308. button[class="baocun"]::after {
  309. border: 0;
  310. }
  311. </style>