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

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