裂变星小程序-25.03.04
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.

282 lines
6.1 KiB

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