青蛙卖大米小程序2024-11-24
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.

276 lines
6.0 KiB

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