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

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