瑶都万能墙
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.

278 lines
6.0 KiB

4 months ago
  1. <template>
  2. <view class="promotion">
  3. <navbar title="二维码" leftClick @leftClick="$utils.navigateBack" />
  4. <view class="promotion-card">
  5. <!-- <view class="user-info"> -->
  6. <!-- <image class="image" :src="userInfo.headImage" mode="widthFix"></image> -->
  7. <!-- <image class="image" src="../../static/logo.png" mode="widthFix"></image> -->
  8. <!-- <view class="user-name">{{ userInfo.nickName }}</view> -->
  9. <!-- <view class="user-name">湖南第一深情</view> -->
  10. <!-- </view> -->
  11. <!-- <view class="invitation-code-img">
  12. <image
  13. style="width: 400rpx;"
  14. :src="imagePath" mode="widthFix"></image>
  15. </view> -->
  16. <image style="width: 100%;" :src="imagePath" mode="widthFix"></image>
  17. <!-- <view class="invitation-code">加油站: {{ title }}</view> -->
  18. <canvas id="myCanvas" type="2d" canvas-id="firstCanvas1"></canvas>
  19. </view>
  20. <view class="btns">
  21. <view class="btn"
  22. @click="preservationImg(imagePath)">保存</view>
  23. <!-- <view class="btn">立即邀请</view> -->
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. import {
  29. mapState
  30. } from 'vuex'
  31. export default {
  32. name: 'Promotion',
  33. computed: {
  34. ...mapState(['userInfo']),
  35. },
  36. data() {
  37. return {
  38. url: '',
  39. title: '123123',
  40. baseUrl: 'https://dianpin-img.xzaiyp.top/',
  41. canvas: {},
  42. imagePath: '',
  43. }
  44. },
  45. onShow() {
  46. this.getQrCode()
  47. this.$store.commit('getUserInfo')
  48. },
  49. methods: {
  50. getQrCode() {
  51. this.$api('getQrCode', res => {
  52. if (res.code == 200) {
  53. this.url = res.result.url
  54. this.title = res.result.name
  55. this.draw()
  56. }
  57. })
  58. },
  59. draw() {
  60. uni.showLoading({
  61. title: "拼命绘画中..."
  62. })
  63. wx.createSelectorQuery()
  64. .select('#myCanvas') // 绘制的canvas的id
  65. .fields({
  66. node: true,
  67. size: true
  68. })
  69. .exec((res) => {
  70. const canvas = res[0].node
  71. // 渲染上下文
  72. const ctx = canvas.getContext('2d')
  73. // Canvas 画布的实际绘制宽高
  74. const width = res[0].width
  75. const height = res[0].height
  76. // 初始化画布大小
  77. const dpr = wx.getWindowInfo().pixelRatio
  78. //根据dpr调整
  79. // dpr 2 4
  80. // 3 6
  81. let Ratio = dpr * 2
  82. console.log("bug", dpr)
  83. canvas.width = width * dpr
  84. canvas.height = height * dpr
  85. this.canvas = canvas
  86. ctx.scale(dpr, dpr)
  87. ctx.clearRect(0, 0, width, height)
  88. ctx.fillStyle = '#fff'
  89. ctx.fillRect(0, 0, canvas.width, canvas.height)
  90. //用户图片
  91. // const image = canvas.createImage()
  92. // image.onload = () => {
  93. // ctx.drawImage(image, 30, 18, 40, 40)
  94. // }
  95. // image.src = '/public/img/wechar_1.png'
  96. // image.src = this.userInfo.headImage
  97. ctx.fillStyle = 'black'
  98. ctx.font = '22px PingFangSC-regular';
  99. let s = this.title || '加油站'
  100. ctx.fillText(s, canvas.width / Ratio - s.length * 11, 50);
  101. //二维码图片
  102. const coderImage = canvas.createImage()
  103. coderImage.src = this.baseUrl + this.url
  104. coderImage.onload = () => {
  105. ctx.drawImage(coderImage,
  106. canvas.width / Ratio - 240 / 2, 100, 240, 240)
  107. }
  108. // 绘制完成后存储路径
  109. setTimeout(() => {
  110. wx.canvasToTempFilePath({
  111. x: 0,
  112. y: 0,
  113. width: this.canvas.width,
  114. height: this.canvas.height,
  115. canvas,
  116. success: (res) => {
  117. var tempFilePath = res.tempFilePath;
  118. this.imagePath = tempFilePath
  119. uni.hideLoading()
  120. }
  121. });
  122. }, 600);
  123. })
  124. },
  125. back() {
  126. uni.navigateBack(-1)
  127. },
  128. preservationImg(img) {
  129. let that = this
  130. uni.authorize({
  131. /* scope.writePhotosAlbum 类型是保存到相册 */
  132. scope: 'scope.writePhotosAlbum',
  133. success() {
  134. /* 已授权进入 */
  135. /* 保存图片到相册方法方法 */
  136. that.imgApi(img);
  137. },
  138. complete(res) {
  139. /* 判断如果没有授权就打开设置选项让用户重新授权 */
  140. uni.getSetting({
  141. success(res) {
  142. if (!res.authSetting['scope.writePhotosAlbum']) {
  143. /* 打开设置的方法 */
  144. that.openInstall();
  145. }
  146. }
  147. });
  148. }
  149. });
  150. },
  151. imgApi(image) {
  152. /* 获取图片的信息 */
  153. uni.getImageInfo({
  154. src: image,
  155. success: function(image) {
  156. /* 保存图片到手机相册 */
  157. uni.saveImageToPhotosAlbum({
  158. filePath: image.path,
  159. success: function() {
  160. uni.showModal({
  161. title: '保存成功',
  162. content: '图片已成功保存到相册',
  163. showCancel: false
  164. });
  165. },
  166. complete(res) {
  167. console.log(res);
  168. }
  169. });
  170. }
  171. });
  172. },
  173. }
  174. }
  175. </script>
  176. <style lang="scss" scoped>
  177. .promotion {
  178. .promotion-card {
  179. width: 90%;
  180. margin: 100rpx auto 0rpx auto;
  181. box-shadow: 0rpx 0rpx 15rpx rgba(0, 0, 0, .2);
  182. border-radius: 15rpx;
  183. padding: 40rpx 30rpx;
  184. box-sizing: border-box;
  185. .user-info {
  186. display: flex;
  187. align-items: center;
  188. .image {
  189. width: 80rpx;
  190. height: 80rpx;
  191. border-radius: 50%;
  192. margin-right: 10rpx;
  193. }
  194. .user-name {
  195. font-size: 30rpx;
  196. }
  197. }
  198. .invitation-code-img {
  199. display: flex;
  200. align-items: center;
  201. justify-content: center;
  202. margin: 100rpx 0rpx;
  203. .image {
  204. width: 30%;
  205. border-radius: 50%;
  206. }
  207. }
  208. .invitation-code {
  209. text-align: center;
  210. color: #818181;
  211. font-size: 28rpx;
  212. }
  213. }
  214. .btns {
  215. position: fixed;
  216. left: 0;
  217. bottom: 0;
  218. width: 100%;
  219. height: 120rpx;
  220. display: flex;
  221. background: #00aaff;
  222. color: white;
  223. font-size: 28rpx;
  224. .btn {
  225. flex: 1;
  226. display: flex;
  227. align-items: center;
  228. justify-content: center;
  229. &:nth-child(2) {
  230. background: orange;
  231. }
  232. }
  233. }
  234. }
  235. #myCanvas {
  236. position: fixed;
  237. left: 100%;
  238. /* visibility: hidden */
  239. /* visibility: hidden; */
  240. /* margin-top: 100rpx; */
  241. margin: 68rpx auto;
  242. width: 750rpx;
  243. height: 750rpx;
  244. /* line-height: 20px; */
  245. background-color: rgba(255, 255, 255, 1);
  246. text-align: center;
  247. }
  248. </style>