qrshare.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <template>
  2. <view>
  3. <view class="canvas">
  4. <canvas class="canvasa" @longtap="saveCans(1)" :style="{ width: cansWidth + 'px', height: cansHeight + 'px' }" canvas-id="firstCanvas"></canvas>
  5. </view>
  6. <view class="imglist">
  7. <view :style="[{ width:widths+'upx'}]">
  8. <block v-for="(swiperLists,index) in swiperList" :key="index">
  9. <image style="padding: 4upx;" @click="selseimg(swiperLists.posterImg)" :src="swiperLists.posterImg" mode="aspectFill"></image>
  10. </block>
  11. </view>
  12. </view>
  13. <view class="bottom">
  14. <!-- #ifdef H5 -->
  15. <view class="bottom3" style="width: 80%; text-align: center;" @click="saveCans(2)">一键分享</view>
  16. <!-- #endif -->
  17. <!-- #ifdef APP-PLUS -->
  18. <view class="bottom3" style="width: 49%; text-align: center;" @click="saveCans(2)">一键分享</view>
  19. <view class="bottom1" style="width: 49%; text-align: center;" @click="saveCans(1)">保存海报</view>
  20. <!-- #endif -->
  21. <!-- <view class="bottom2" style="width: 32%;" @click="upbj()">上传背景</view> -->
  22. </view>
  23. <uni-popup ref="sharepopup" type="bottom">
  24. <share-btn :sharedataTemp="sharedata"></share-btn>
  25. </uni-popup>
  26. <avatar @upload="doUpload" @avtinit="doBefore" quality="1" ref="avatar"></avatar>
  27. </view>
  28. </template>
  29. <script>
  30. import avatar from "@/components/yq-avatar/yq-avatar.vue";
  31. import QR from '@/static/js/wxqrcode.js';
  32. import uniPopup from '@/components/uni-popup/uni-popup.vue';
  33. import shareBtn from '@/components/share-btn/share-btn.vue';
  34. import { mapMutations} from 'vuex';
  35. var that
  36. export default {
  37. components: {
  38. avatar,
  39. uniPopup,
  40. shareBtn
  41. },
  42. data() {
  43. return {
  44. sharedata: {
  45. type: 2,
  46. strShareUrl: "",
  47. strShareTitle: "分享标题",
  48. strShareSummary: "分享总结",
  49. strShareImageUrl: ""
  50. },
  51. uid:'',
  52. avatar:'',
  53. qrcodeImg:'',
  54. userinfo:[],
  55. cansWidth: 280, //海报宽度
  56. cansHeight: 500, //海报高度
  57. widths: "", //图片列表动态宽度
  58. imgsrc: "", //保存图片的路径
  59. userdata: {}, //个人数据
  60. swiperList: [] //海报数组
  61. };
  62. },
  63. onLoad: function() {
  64. this.show = true;
  65. this.userinfo=uni.getStorageSync("userinfo");
  66. this.avatar=uni.getStorageSync("avatar")
  67. var qrurl=this.configs.webUrl+'/h5/#/pages/login/reg?uid='+this.userinfo.id
  68. let img = QR.createQrCodeImg(qrurl, {
  69. size: 300//二维码大小
  70. })
  71. this.qrcodeImg = img;
  72. this.sharedata.strShareUrl=qrurl
  73. console.log(this.qrcodeImg)
  74. that = this
  75. that.huizhihaibao() //请求海报数据
  76. },
  77. methods: {
  78. upbj(){
  79. console.log(111)
  80. var index=0;
  81. var that = this
  82. this.$refs.avatar.fChooseImg(index,{
  83. selWidth: '500upx', selHeight: '893upx',
  84. expWidth: '800upx', expHeight: '1429upx',
  85. inner: index ? 'true' : 'false',
  86. canRotate:false,
  87. quality:1,
  88. });
  89. },
  90. doUpload(rsp) {
  91. //console.log(rsp);
  92. this.selseimg(rsp.path)
  93. },
  94. doBefore() {
  95. console.log('doBefore');
  96. },
  97. huizhihaibao() {
  98. var that = this
  99. //数据请求结束
  100. uni.showLoading({
  101. title: '加载中'
  102. });
  103. that.userdata = {
  104. img: "",
  105. text: "测试"
  106. }
  107. if(this.userinfo.config.fxhb){
  108. var swiperList = this.userinfo.config.fxhb
  109. let goodsData = [];
  110. swiperList.forEach(item=>{
  111. if(item.indexOf("http") != -1){
  112. item=item
  113. }else{
  114. item=that.configs.webUrl+item
  115. }
  116. goodsData.push({
  117. posterImg: item
  118. })
  119. })
  120. that.swiperList=goodsData
  121. }else{
  122. that.swiperList = []
  123. }
  124. uni.hideLoading();
  125. //数据请求结束
  126. that.selseimg(that.swiperList[0].posterImg)
  127. },
  128. selseimg(e) { //绘制海报
  129. if (that.userinfo.nickname > 6) { //昵称太长加省略号
  130. that.userinfo.nickname = that.userinfo.nickname.slice(0, 6) + "..."
  131. }
  132. uni.showLoading({
  133. title: '生成海报中'
  134. });
  135. if(e.indexOf("http") != -1){
  136. // #ifdef H5
  137. that.selseimging(e)
  138. // #endif
  139. // #ifdef APP-PLUS || MP-WEIXIN
  140. uni.downloadFile({
  141. url: e,
  142. success: function(res) {
  143. if(res.tempFilePath){
  144. that.selseimging(res.tempFilePath)
  145. }
  146. }
  147. })
  148. // #endif
  149. }else{
  150. that.selseimging(e)
  151. }
  152. },
  153. selseimging(hbbj){
  154. const context = uni.createCanvasContext('firstCanvas')
  155. context.drawImage(hbbj, 0, 0, 280, 500) //海报背景
  156. var yd=20;//控制y的距离
  157. setTimeout(function() { //定时器,海报方法不能立即执行,等页面渲染完成
  158. context.shadowColor = '#999999';
  159. context.shadowOffsetX = 0;// 设置阴影的横向偏移量
  160. context.shadowOffsetY = 0;
  161. context.shadowBlur = 5;// 设置模糊等级
  162. context.drawImage(that.$config.staticUrl+'zw.png',10, 350+yd, 260, 110) //个人信息背景
  163. context.fill()
  164. context.save()
  165. context.drawImage(that.qrcodeImg, 182, 374+yd, 75, 75) //二维码图片
  166. context.setFillStyle('#333333')
  167. context.setFontSize(14)
  168. context.fillText('我的邀请码:'+that.userinfo.id, 20, 373+yd)
  169. context.fill()
  170. context.setFillStyle('#333333')
  171. context.setFontSize(12)
  172. context.fillText('扫一扫二维码', 183, 368+yd)
  173. context.fill()
  174. context.setFillStyle('#333333')
  175. context.setFontSize(14)
  176. context.fillText(that.userinfo.nickname, 80, 405+yd) //昵称
  177. context.fill()
  178. context.setFillStyle('#666666')
  179. context.setFontSize(10)
  180. context.fillText('邀请你加入'+that.userinfo.config.name, 80, 425+yd)
  181. context.fillText('手机就能赚钱!', 80, 445+yd)
  182. context.fill()
  183. context.save()
  184. context.beginPath()
  185. context.arc(44, 420+yd, 25, 0, 2 * Math.PI)
  186. context.clip()
  187. context.drawImage(that.avatar, 17, 392+yd, 55, 55) //头像
  188. context.restore()
  189. context.draw()
  190. uni.hideLoading();
  191. }, 1000);
  192. },
  193. saveCans(e) { //保存海报
  194. console.log(222)
  195. if (e == 2) {
  196. that.$refs.sharepopup.open();
  197. }else{
  198. uni.showLoading({
  199. title: '保存海报中'
  200. });
  201. console.log('保存');
  202. let tempRatio = 1;
  203. // #ifdef H5 || APP-PLUS
  204. tempRatio = 1;
  205. // #endif
  206. uni.canvasToTempFilePath({
  207. x: 0,
  208. y: 0,
  209. width: that.cansWidth * tempRatio* 4,
  210. height: that.cansHeight * tempRatio* 4,
  211. destWidth: that.cansWidth * tempRatio * 4,
  212. destHeight: that.cansHeight * tempRatio * 4,
  213. canvasId: 'firstCanvas',
  214. success: function(res) {
  215. uni.hideLoading()
  216. uni.saveImageToPhotosAlbum({
  217. filePath: res.tempFilePath,
  218. success: function(red) {
  219. uni.showToast({
  220. title: '保存相册成功'
  221. })
  222. that.imgsrc = res.tempFilePath
  223. that.sharedata.strShareImageUrl= res.tempFilePath
  224. },
  225. fail(res) {
  226. if (res.errMsg == "saveImageToPhotosAlbum:fail auth deny") {
  227. uni.showModal({
  228. title: '您需要授权相册权限',
  229. success(res) {
  230. if (res.confirm) {
  231. uni.openSetting({
  232. success(res) {
  233. },
  234. fail(res) {
  235. console.log(res)
  236. }
  237. })
  238. }
  239. }
  240. })
  241. }
  242. }
  243. });
  244. },
  245. fail(res) {
  246. uni.hideLoading()
  247. }
  248. }, this)
  249. }
  250. },
  251. }
  252. }
  253. </script>
  254. <style>
  255. page {
  256. background-color: #F1F1F1;
  257. }
  258. .pageHeader {
  259. height: var(--status-bar-height);
  260. background-color: #FFFFFF;
  261. }
  262. .box-header {
  263. position: fixed;
  264. top: 0upx;
  265. width: 100%;
  266. z-index: 100;
  267. }
  268. .top-box {
  269. height: 100upx;
  270. width: 100%;
  271. }
  272. .canvas {
  273. display: flex;
  274. justify-content: center;
  275. margin-top: 20upx;
  276. border-radius: 16upx;
  277. }
  278. .bottom {
  279. padding: 20upx 0;
  280. width: 100%;
  281. display: flex;
  282. justify-content: space-around;
  283. position: fixed;
  284. bottom: 0;
  285. background-color: #ffffff;
  286. }
  287. .bottom1 {
  288. padding: 20upx 40upx;
  289. background: #FF8A00;
  290. border-radius: 44upx;
  291. font-size: 30upx;
  292. color: #FFFFFF;
  293. }
  294. .bottom2 {
  295. padding: 20upx 40upx;
  296. background: #FF5256;
  297. border-radius: 44upx;
  298. font-size: 30upx;
  299. color: #FFFFFF;
  300. }
  301. .bottom3 {
  302. padding: 20upx 40upx;
  303. background: #5E40FF;
  304. border-radius: 44upx;
  305. font-size: 30upx;
  306. color: #FFFFFF;
  307. }
  308. .imglist {
  309. width: 100%;
  310. padding: 20upx 0;
  311. overflow: auto;
  312. /* background-color: #ffffff; */
  313. }
  314. .imglist view {
  315. display: flex;
  316. justify-content: space-around;
  317. }
  318. .imglist image {
  319. width: 200upx;
  320. height: 200upx;
  321. }
  322. .tishi-box {
  323. display: flex;
  324. align-items: center;
  325. justify-content: space-around;
  326. background-color: #FFFFFF;
  327. position: fixed;
  328. bottom: 0;
  329. width: 100%;
  330. height: 260upx;
  331. background: #FFFFFF;
  332. border-radius: 20upx 20upx 0upx 0upx;
  333. text-align: center;
  334. }
  335. .tishi-box view {
  336. font-size: 28upx;
  337. font-family: PingFang SC;
  338. font-weight: 500;
  339. color: #333333;
  340. line-height: 50upx;
  341. }
  342. .tishi-box image {
  343. width: 100upx;
  344. height: 100upx;
  345. border-radius: 50%;
  346. }
  347. .canvasa{
  348. border-radius: 10upx;
  349. }
  350. </style>