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

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