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

338 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: 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. 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. }
  136. console.log('海报生成成功,图片链接', that.imagePath)
  137. },
  138. fail: function(res) {
  139. console.log(res);
  140. }
  141. });
  142. }, 900);
  143. },
  144. saveBill() {
  145. var that = this
  146. wx.saveImageToPhotosAlbum({
  147. filePath: that.imagePath,
  148. success(res) {
  149. wx.showModal({
  150. content: '图片已保存到相册,赶紧晒一下吧~',
  151. showCancel: false,
  152. confirmText: '好的',
  153. confirmColor: '#333',
  154. success: function(res) {
  155. if (res.confirm) {
  156. console.log('用户点击确定');
  157. that.maskHidden = false
  158. }
  159. },
  160. fail: function(res) {
  161. that.maskHidden = false
  162. }
  163. })
  164. }
  165. })
  166. },
  167. // 点击复制
  168. copy(text) {
  169. uni.setClipboardData({
  170. data: text
  171. });
  172. },
  173. }
  174. }
  175. </script>
  176. <style>
  177. .bgImg {
  178. display: block;
  179. width: 100%;
  180. height: 366rpx;
  181. }
  182. .mine {
  183. display: block;
  184. text-align: center;
  185. color: #333;
  186. margin-top: 44rpx;
  187. }
  188. .code {
  189. display: block;
  190. text-align: center;
  191. color: #333;
  192. font-size: 76rpx;
  193. font-weight: bold;
  194. margin-top: 30rpx;
  195. }
  196. .who {
  197. display: block;
  198. margin-top: 80rpx;
  199. font-size: 32rpx;
  200. color: #333;
  201. text-align: center;
  202. }
  203. .inputBox {
  204. text-align: center;
  205. margin-top: 44rpx;
  206. }
  207. .input {
  208. text-align: center;
  209. width: 440rpx;
  210. height: 88rpx;
  211. border-radius: 44rpx;
  212. background: #f5f5f5;
  213. font-size: 32rpx;
  214. display: inline-block;
  215. }
  216. .btn {
  217. width: 160rpx;
  218. height: 88rpx;
  219. border-radius: 44rpx;
  220. background: linear-gradient(90deg, rgba(255, 226, 0, 1), rgba(255, 200, 11, 1));
  221. box-shadow: 0px 4px 8px 0px rgba(255, 200, 11, 0.5);
  222. color: #333;
  223. font-size: 32rpx;
  224. display: inline-block;
  225. line-height: 88rpx;
  226. margin-left: 40rpx;
  227. }
  228. button[class="btn"]::after {
  229. border: 0;
  230. }
  231. .tishi {
  232. display: block;
  233. text-align: center;
  234. color: #999;
  235. margin-top: 30rpx;
  236. }
  237. .shareText {
  238. display: block;
  239. text-align: center;
  240. color: #333;
  241. font-size: 28rpx;
  242. margin-top: 100rpx;
  243. }
  244. .imgBox {
  245. text-align: center;
  246. width: 100%;
  247. margin-top: 60rpx;
  248. padding-bottom: 120rpx;
  249. }
  250. .img {
  251. display: inline-block;
  252. width: 100%;
  253. height: 100%;
  254. }
  255. .m_l {
  256. margin-left: 180rpx;
  257. }
  258. .zfbtn {
  259. display: inline-block;
  260. width: 120rpx;
  261. height: 120rpx;
  262. border-radius: 50%;
  263. background: transparent;
  264. outline: none;
  265. border: 0;
  266. padding: 0;
  267. }
  268. button[class="zfbtn"]::after {
  269. border: 0;
  270. }
  271. button[class="zfbtn m_l"]::after {
  272. border: 0;
  273. }
  274. .imagePathBox {
  275. width: 100%;
  276. height: 100%;
  277. background: rgba(0, 0, 0, 0.7);
  278. position: fixed;
  279. top: 0;
  280. left: 0;
  281. right: 0;
  282. bottom: 0;
  283. z-index: 10;
  284. }
  285. .shengcheng {
  286. width: 80%;
  287. height: 80%;
  288. position: fixed;
  289. top: 50rpx;
  290. left: 50%;
  291. margin-left: -40%;
  292. z-index: 10;
  293. }
  294. .baocun {
  295. display: block;
  296. width: 80%;
  297. height: 80rpx;
  298. padding: 0;
  299. line-height: 80rpx;
  300. text-align: center;
  301. position: fixed;
  302. bottom: 50rpx;
  303. left: 10%;
  304. background: #ffe200;
  305. color: #333;
  306. font-size: 32rpx;
  307. border-radius: 44rpx;
  308. }
  309. button[class="baocun"]::after {
  310. border: 0;
  311. }
  312. </style>