当前位置:   article > 正文

微信小程序分享生成海报_小程序海报分享邀请好友

小程序海报分享邀请好友

本文转自:微信小程序实现生成海报并且保存本地 - 张正-博客园 - 博客园

html:

  1. <view>
  2. <image src="{{img}}" class='bgImg'></image>
  3. <text class='mine'>我的邀请码</text>
  4. <text class='code'>{{code}}</text>
  5. <text class='who'>谁邀请你一起吃面?</text>
  6. <view class='inputBox'>
  7. <input placeholder='输入朋友的验证码' class='input' bindinput='bindKeyInput'/>
  8. <button bindtap='btnclick' class='btn'>提交</button>
  9. </view>
  10. <text class='tishi'>输入朋友的邀请码,朋友和你各自获得通用优惠券1张</text>
  11. <text class='shareText'>生成海报分享至</text>
  12. <view class='imgBox'>
  13. <button open-type="share" class='zfbtn'><image src="{{wechat}}" class='img'></image></button>
  14. <button class='zfbtn m_l' bindtap='formSubmit'><image src="{{quan}}" class='img'></image></button>
  15. </view>
  16. <!--生成海报 -->
  17. <!-- <view class="img-box">
  18. </view> -->
  19. <view class='imagePathBox' hidden="{{maskHidden == false}}">
  20. <image src="{{imagePath}}" class='shengcheng'></image>
  21. <button class='baocun' bindtap='baocun'>保存相册,分享到朋友圈</button>
  22. </view>
  23. <view hidden="{{maskHidden == false}}" class="mask"></view>
  24. <view class="canvas-box">
  25. <canvas style="width: 375px;height: 667px;position:fixed;top:9999px" canvas-id="mycanvas"/>
  26. </view>
  27. </view>

css部分:

  1. .bgImg{
  2. display: block;
  3. width: 100%;
  4. height: 366rpx;
  5. }
  6. .mine{
  7. display: block;
  8. text-align: center;
  9. color: #333;
  10. margin-top: 44rpx;
  11. }
  12. .code{
  13. display: block;
  14. text-align: center;
  15. color: #333;
  16. font-size: 76rpx;
  17. font-weight: bold;
  18. margin-top: 30rpx;
  19. }
  20. .who{
  21. display: block;
  22. margin-top: 80rpx;
  23. font-size: 32rpx;
  24. color: #333;
  25. text-align: center;
  26. }
  27. .inputBox{
  28. text-align: center;
  29. margin-top: 44rpx;
  30. }
  31. .input{
  32. text-align: center;
  33. width: 440rpx;
  34. height: 88rpx;
  35. border-radius: 44rpx;
  36. background: #f5f5f5;
  37. font-size: 32rpx;
  38. display: inline-block;
  39. }
  40. .btn{
  41. width: 160rpx;
  42. height: 88rpx;
  43. border-radius: 44rpx;
  44. background:linear-gradient(90deg,rgba(255,226,0,1),rgba(255,200,11,1));
  45. box-shadow: 0px 4px 8px 0px rgba(255,200,11,0.5);
  46. color:#333;
  47. font-size: 32rpx;
  48. display: inline-block;
  49. line-height: 88rpx;
  50. margin-left: 40rpx;
  51. }
  52. button[class="btn"]::after {
  53. border: 0;
  54. }
  55. .tishi{
  56. display: block;
  57. text-align: center;
  58. color: #999;
  59. margin-top: 30rpx;
  60. }
  61. .shareText{
  62. display: block;
  63. text-align: center;
  64. color: #333;
  65. font-size: 28rpx;
  66. margin-top: 100rpx;
  67. }
  68. .imgBox{
  69. text-align: center;
  70. width: 100%;
  71. margin-top:60rpx;
  72. padding-bottom: 120rpx;
  73. }
  74. .img{
  75. display: inline-block;
  76. width: 100%;
  77. height: 100%;
  78. }
  79. .m_l{
  80. margin-left: 180rpx;
  81. }
  82. .zfbtn{
  83. display: inline-block;
  84. width: 120rpx;
  85. height: 120rpx;
  86. border-radius: 50%;
  87. background: transparent;
  88. outline: none;
  89. border: 0;
  90. padding: 0;
  91. }
  92. button[class="zfbtn"]::after {
  93. border: 0;
  94. }
  95. button[class="zfbtn m_l"]::after {
  96. border: 0;
  97. }
  98. .imagePathBox{
  99. width: 100%;
  100. height: 100%;
  101. background: rgba(0,0,0,0.7);
  102. position: fixed;
  103. top: 0;
  104. left: 0;
  105. right: 0;
  106. bottom: 0;
  107. z-index: 10;
  108. }
  109. .shengcheng{
  110. width: 80%;
  111. height: 80%;
  112. position: fixed;
  113. top: 50rpx;
  114. left: 50%;
  115. margin-left: -40%;
  116. z-index: 10;
  117. }
  118. .baocun{
  119. display: block;
  120. width: 80%;
  121. height: 80rpx;
  122. padding: 0;
  123. line-height: 80rpx;
  124. text-align: center;
  125. position: fixed;
  126. bottom: 50rpx;
  127. left: 10%;
  128. background: #ffe200;
  129. color: #333;
  130. font-size: 32rpx;
  131. border-radius: 44rpx;
  132. }
  133. button[class="baocun"]::after{
  134. border: 0;
  135. }

js部分:

  1. // pages/prize/prize.js
  2. const app = getApp()
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. img:"../../images/gobg.png",
  9. wechat:"../../images/wechat.png",
  10. quan:"../../images/quan.png",
  11. code:"E7AI98",
  12. inputValue:"",
  13. maskHidden: false,
  14. name:"",
  15. touxiang:"",
  16. code: "E7A93C"
  17. },
  18. //获取输入框的值
  19. bindKeyInput:function(e){
  20. this.setData({
  21. inputValue: e.detail.value
  22. })
  23. },
  24. //点击提交按钮
  25. btnclick:function(){
  26. var text = this.data.inputValue
  27. wx.showToast({
  28. title: text,
  29. icon: 'none',
  30. duration: 2000
  31. })
  32. },
  33. /**
  34. * 生命周期函数--监听页面加载
  35. */
  36. onLoad: function (options) {
  37. var that = this;
  38. wx.getUserInfo({
  39. success: res => {
  40. console.log(res.userInfo,"huoqudao le ")
  41. this.setData({
  42. name: res.userInfo.nickName,
  43. })
  44. wx.downloadFile({
  45. url: res.userInfo.avatarUrl, //仅为示例,并非真实的资源
  46. success: function (res) {
  47. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  48. if (res.statusCode === 200) {
  49. console.log(res, "reererererer")
  50. that.setData({
  51. touxiang: res.tempFilePath
  52. })
  53. }
  54. }
  55. })
  56. }
  57. })
  58. },
  59. //将canvas转换为图片保存到本地,然后将图片路径传给image图片的src
  60. createNewImg: function () {
  61. var that = this;
  62. var context = wx.createCanvasContext('mycanvas');
  63. context.setFillStyle("#ffe200")
  64. context.fillRect(0, 0, 375, 667)
  65. var path = "/images/gobg.png";
  66. //将模板图片绘制到canvas,在开发工具中drawImage()函数有问题,不显示图片
  67. //不知道是什么原因,手机环境能正常显示
  68. context.drawImage(path, 0, 0, 375, 183);
  69. var path1 = that.data.touxiang;
  70. console.log(path1,"path1")
  71. //将模板图片绘制到canvas,在开发工具中drawImage()函数有问题,不显示图片
  72. var path2 = "/images/txquan.png";
  73. var path3 = "/images/heise.png";
  74. var path4 = "/images/wenziBg.png";
  75. var path5 = "/images/wenxin.png";
  76. context.drawImage(path2, 126, 186, 120, 120);
  77. //不知道是什么原因,手机环境能正常显示
  78. // context.save(); // 保存当前context的状态
  79. var name = that.data.name;
  80. //绘制名字
  81. context.setFontSize(24);
  82. context.setFillStyle('#333333');
  83. context.setTextAlign('center');
  84. context.fillText(name, 185, 340);
  85. context.stroke();
  86. //绘制一起吃面标语
  87. context.setFontSize(14);
  88. context.setFillStyle('#333333');
  89. context.setTextAlign('center');
  90. context.fillText("邀请你一起去吃面", 185, 370);
  91. context.stroke();
  92. //绘制验证码背景
  93. context.drawImage(path3, 48, 390, 280, 84);
  94. //绘制code码
  95. context.setFontSize(40);
  96. context.setFillStyle('#ffe200');
  97. context.setTextAlign('center');
  98. context.fillText(that.data.code, 185, 435);
  99. context.stroke();
  100. //绘制左下角文字背景图
  101. context.drawImage(path4, 25, 520, 184, 82);
  102. context.setFontSize(12);
  103. context.setFillStyle('#333');
  104. context.setTextAlign('left');
  105. context.fillText("进入小程序输入朋友的邀请", 35, 540);
  106. context.stroke();
  107. context.setFontSize(12);
  108. context.setFillStyle('#333');
  109. context.setTextAlign('left');
  110. context.fillText("码,朋友和你各自获得通用", 35, 560);
  111. context.stroke();
  112. context.setFontSize(12);
  113. context.setFillStyle('#333');
  114. context.setTextAlign('left');
  115. context.fillText("优惠券1张哦~", 35, 580);
  116. context.stroke();
  117. //绘制右下角扫码提示语
  118. context.drawImage(path5, 248, 578, 90, 25);
  119. //绘制头像
  120. context.arc(186, 246, 50, 0, 2 * Math.PI) //画出圆
  121. context.strokeStyle = "#ffe200";
  122. context.clip(); //裁剪上面的圆形
  123. context.drawImage(path1, 136, 196, 100, 100); // 在刚刚裁剪的园上画图
  124. context.draw();
  125. //将生成好的图片保存到本地,需要延迟一会,绘制期间耗时
  126. setTimeout(function () {
  127. wx.canvasToTempFilePath({
  128. canvasId: 'mycanvas',
  129. success: function (res) {
  130. var tempFilePath = res.tempFilePath;
  131. that.setData({
  132. imagePath: tempFilePath,
  133. canvasHidden:true
  134. });
  135. },
  136. fail: function (res) {
  137. console.log(res);
  138. }
  139. });
  140. }, 200);
  141. },
  142. //点击保存到相册
  143. baocun:function(){
  144. var that = this
  145. wx.saveImageToPhotosAlbum({
  146. filePath: that.data.imagePath,
  147. success(res) {
  148. wx.showModal({
  149. content: '图片已保存到相册,赶紧晒一下吧~',
  150. showCancel: false,
  151. confirmText: '好的',
  152. confirmColor: '#333',
  153. success: function (res) {
  154. if (res.confirm) {
  155. console.log('用户点击确定');
  156. /* 该隐藏的隐藏 */
  157. that.setData({
  158. maskHidden: false
  159. })
  160. }
  161. },fail:function(res){
  162. console.log(11111)
  163. }
  164. })
  165. }
  166. })
  167. },
  168. //点击生成
  169. formSubmit: function (e) {
  170. var that = this;
  171. this.setData({
  172. maskHidden: false
  173. });
  174. wx.showToast({
  175. title: '装逼中...',
  176. icon: 'loading',
  177. duration: 1000
  178. });
  179. setTimeout(function () {
  180. wx.hideToast()
  181. that.createNewImg();
  182. that.setData({
  183. maskHidden: true
  184. });
  185. }, 1000)
  186. },
  187. /**
  188. * 生命周期函数--监听页面初次渲染完成
  189. */
  190. onReady: function () {
  191. },
  192. /**
  193. * 生命周期函数--监听页面显示
  194. */
  195. onShow: function () {
  196. var that = this;
  197. wx.getUserInfo({
  198. success: res => {
  199. console.log(res.userInfo, "huoqudao le ")
  200. this.setData({
  201. name: res.userInfo.nickName,
  202. })
  203. wx.downloadFile({
  204. url: res.userInfo.avatarUrl, //仅为示例,并非真实的资源
  205. success: function (res) {
  206. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  207. if (res.statusCode === 200) {
  208. console.log(res, "reererererer")
  209. that.setData({
  210. touxiang: res.tempFilePath
  211. })
  212. }
  213. }
  214. })
  215. }
  216. })
  217. },
  218. /**
  219. * 生命周期函数--监听页面隐藏
  220. */
  221. onHide: function () {
  222. },
  223. /**
  224. * 生命周期函数--监听页面卸载
  225. */
  226. onUnload: function () {
  227. },
  228. /**
  229. * 页面相关事件处理函数--监听用户下拉动作
  230. */
  231. onPullDownRefresh: function () {
  232. },
  233. /**
  234. * 页面上拉触底事件的处理函数
  235. */
  236. onReachBottom: function () {
  237. },
  238. /**
  239. * 用户点击右上角分享
  240. */
  241. onShareAppMessage: function (res) {
  242. return {
  243. title:"这个是我分享出来的东西",
  244. success:function(res){
  245. console.log(res,"转发成功")
  246. },
  247. fail:function(res){
  248. console.log(res,"转发失败")
  249. }
  250. }
  251. }
  252. })

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/639620
推荐阅读
相关标签
  

闽ICP备14008679号