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

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