时空网前端
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.

339 lines
6.8 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: 600px;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: 100000
  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. let userInfo = uni.getStorageSync('userinfo') || {};
  74. this.$http(this.API.API_WXBILL, {
  75. scene: 'route=home&id=2&pt=2'+'&share_id='+userInfo.id,
  76. page: 'pages/route/index',
  77. width: '2800'
  78. }).then(res => {
  79. this.wxCode = res.data.base_url
  80. if (this.wxCode) {
  81. this.getwxCodeImg()
  82. }
  83. })
  84. },
  85. //保存二维码
  86. getwxCodeImg() {
  87. var imgSrc = this.wxCode; //base64编码
  88. var save = wx.getFileSystemManager();
  89. var number = Math.random();
  90. save.writeFile({
  91. filePath: wx.env.USER_DATA_PATH + '/pic' + number + '.jpg',
  92. data: imgSrc,
  93. encoding: 'base64',
  94. success: res => {
  95. this.onceCode = wx.env.USER_DATA_PATH + '/pic' + number + '.jpg'
  96. console.log(this.onceCode)
  97. if (this.onceCode && this.backgroundImg) {
  98. this.createNewImg()
  99. }else if(!this.onceCode){
  100. this.$msg('小程序码生成失败!')
  101. }else if(!this.backgroundImg){
  102. this.$msg('海报背景图生成失败!')
  103. }
  104. },
  105. fail: err => {
  106. console.log(err)
  107. }
  108. })
  109. },
  110. createNewImg() {
  111. var that = this;
  112. var context = wx.createCanvasContext('mycanvas');
  113. console.log('画布', that.backgroundImg)
  114. var path = that.backgroundImg;
  115. context.drawImage(path, 0, 0, 375, 600);
  116. //绘制二维码
  117. let wxcode = that.onceCode
  118. context.drawImage(wxcode, 18, 460, 120, 120);
  119. //绘制名字
  120. // context.setFontSize(24);
  121. // context.setFillStyle('#fff');
  122. // context.setTextAlign('center');
  123. // context.fillText(name, 34, 620);
  124. context.stroke();
  125. context.draw();
  126. //将生成好的图片保存到本地,需要延迟一会,绘制期间耗时
  127. setTimeout(function() {
  128. wx.canvasToTempFilePath({
  129. canvasId: 'mycanvas',
  130. success: function(res) {
  131. that.imagePath = res.tempFilePath;
  132. if (that.imagePath) {
  133. that.canvasHidden = true
  134. that.maskHidden = true
  135. wx.hideToast()
  136. }
  137. console.log('海报生成成功,图片链接', that.imagePath)
  138. },
  139. fail: function(res) {
  140. console.log(res);
  141. }
  142. });
  143. }, 900);
  144. },
  145. saveBill() {
  146. var that = this
  147. wx.saveImageToPhotosAlbum({
  148. filePath: that.imagePath,
  149. success(res) {
  150. wx.showModal({
  151. content: '图片已保存到相册,赶紧晒一下吧~',
  152. showCancel: false,
  153. confirmText: '好的',
  154. confirmColor: '#333',
  155. success: function(res) {
  156. if (res.confirm) {
  157. console.log('用户点击确定');
  158. that.maskHidden = false
  159. }
  160. },
  161. fail: function(res) {
  162. that.maskHidden = false
  163. }
  164. })
  165. }
  166. })
  167. },
  168. // 点击复制
  169. copy(text) {
  170. uni.setClipboardData({
  171. data: text
  172. });
  173. },
  174. }
  175. }
  176. </script>
  177. <style>
  178. .bgImg {
  179. display: block;
  180. width: 100%;
  181. height: 366rpx;
  182. }
  183. .mine {
  184. display: block;
  185. text-align: center;
  186. color: #333;
  187. margin-top: 44rpx;
  188. }
  189. .code {
  190. display: block;
  191. text-align: center;
  192. color: #333;
  193. font-size: 76rpx;
  194. font-weight: bold;
  195. margin-top: 30rpx;
  196. }
  197. .who {
  198. display: block;
  199. margin-top: 80rpx;
  200. font-size: 32rpx;
  201. color: #333;
  202. text-align: center;
  203. }
  204. .inputBox {
  205. text-align: center;
  206. margin-top: 44rpx;
  207. }
  208. .input {
  209. text-align: center;
  210. width: 440rpx;
  211. height: 88rpx;
  212. border-radius: 44rpx;
  213. background: #f5f5f5;
  214. font-size: 32rpx;
  215. display: inline-block;
  216. }
  217. .btn {
  218. width: 160rpx;
  219. height: 88rpx;
  220. border-radius: 44rpx;
  221. background: linear-gradient(90deg, rgba(255, 226, 0, 1), rgba(255, 200, 11, 1));
  222. box-shadow: 0px 4px 8px 0px rgba(255, 200, 11, 0.5);
  223. color: #333;
  224. font-size: 32rpx;
  225. display: inline-block;
  226. line-height: 88rpx;
  227. margin-left: 40rpx;
  228. }
  229. button[class="btn"]::after {
  230. border: 0;
  231. }
  232. .tishi {
  233. display: block;
  234. text-align: center;
  235. color: #999;
  236. margin-top: 30rpx;
  237. }
  238. .shareText {
  239. display: block;
  240. text-align: center;
  241. color: #333;
  242. font-size: 28rpx;
  243. margin-top: 100rpx;
  244. }
  245. .imgBox {
  246. text-align: center;
  247. width: 100%;
  248. margin-top: 60rpx;
  249. padding-bottom: 120rpx;
  250. }
  251. .img {
  252. display: inline-block;
  253. width: 100%;
  254. height: 100%;
  255. }
  256. .m_l {
  257. margin-left: 180rpx;
  258. }
  259. .zfbtn {
  260. display: inline-block;
  261. width: 120rpx;
  262. height: 120rpx;
  263. border-radius: 50%;
  264. background: transparent;
  265. outline: none;
  266. border: 0;
  267. padding: 0;
  268. }
  269. button[class="zfbtn"]::after {
  270. border: 0;
  271. }
  272. button[class="zfbtn m_l"]::after {
  273. border: 0;
  274. }
  275. .imagePathBox {
  276. width: 100%;
  277. height: 100%;
  278. background: rgba(0, 0, 0, 0.7);
  279. position: fixed;
  280. top: 0;
  281. left: 0;
  282. right: 0;
  283. bottom: 0;
  284. z-index: 10;
  285. }
  286. .shengcheng {
  287. width: 80%;
  288. height: 80%;
  289. position: fixed;
  290. top: 50rpx;
  291. left: 50%;
  292. margin-left: -40%;
  293. z-index: 10;
  294. }
  295. .baocun {
  296. display: block;
  297. width: 80%;
  298. height: 80rpx;
  299. padding: 0;
  300. line-height: 80rpx;
  301. text-align: center;
  302. position: fixed;
  303. bottom: 50rpx;
  304. left: 10%;
  305. background: #ffe200;
  306. color: #333;
  307. font-size: 32rpx;
  308. border-radius: 44rpx;
  309. }
  310. button[class="baocun"]::after {
  311. border: 0;
  312. }
  313. </style>