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

246 lines
5.9 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months 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 :src="userInfo.headImage" mode="aspectFill"
  20. class="headImage"></image>
  21. <image style="width: 100%;" :src="imagePath" mode="widthFix"></image>
  22. <!-- <view class="invitation-code">加油站: {{ title }}</view> -->
  23. <canvas id="myCanvas" type="2d" canvas-id="firstCanvas1"></canvas>
  24. <view class="uni-color-btn"
  25. @click="preservationImg(imagePath)">保存二维码</view>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. import { mapState } 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('getInviteCode', 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. // 绘制用户姓名
  100. // let nickName = this.userInfo.nickName || '绘制用户姓名'
  101. // ctx.fillText(nickName, canvas.width / Ratio - nickName.length * 11, 40);
  102. // ctx.font = '18px PingFangSC-regular';
  103. // 绘制欢迎语
  104. let s = this.title || ''
  105. ctx.fillText(s, canvas.width / Ratio - s.length * 11, 50);
  106. //二维码图片
  107. const coderImage = canvas.createImage()
  108. coderImage.src = this.baseUrl + this.url
  109. coderImage.onload = () => {
  110. ctx.drawImage(coderImage,
  111. canvas.width / Ratio - 240 / 2, 100, 240, 240)
  112. }
  113. // 绘制完成后存储路径
  114. setTimeout(() => {
  115. wx.canvasToTempFilePath({
  116. x: 0,
  117. y: 0,
  118. width: this.canvas.width,
  119. height: this.canvas.height,
  120. canvas,
  121. success: (res) => {
  122. var tempFilePath = res.tempFilePath;
  123. this.imagePath = tempFilePath
  124. uni.hideLoading()
  125. }
  126. });
  127. }, 600);
  128. })
  129. },
  130. back() {
  131. uni.navigateBack(-1)
  132. },
  133. preservationImg(img) {
  134. let that = this
  135. uni.authorize({
  136. /* scope.writePhotosAlbum 类型是保存到相册 */
  137. scope: 'scope.writePhotosAlbum',
  138. success() {
  139. /* 已授权进入 */
  140. /* 保存图片到相册方法方法 */
  141. that.imgApi(img);
  142. },
  143. complete(res) {
  144. /* 判断如果没有授权就打开设置选项让用户重新授权 */
  145. uni.getSetting({
  146. success(res) {
  147. if (!res.authSetting['scope.writePhotosAlbum']) {
  148. /* 打开设置的方法 */
  149. that.openInstall();
  150. }
  151. }
  152. });
  153. }
  154. });
  155. },
  156. imgApi(image) {
  157. /* 获取图片的信息 */
  158. uni.getImageInfo({
  159. src: image,
  160. success: function(image) {
  161. /* 保存图片到手机相册 */
  162. uni.saveImageToPhotosAlbum({
  163. filePath: image.path,
  164. success: function() {
  165. uni.showModal({
  166. title: '保存成功',
  167. content: '图片已成功保存到相册',
  168. showCancel: false
  169. });
  170. },
  171. complete(res) {
  172. console.log(res);
  173. }
  174. });
  175. }
  176. });
  177. },
  178. }
  179. }
  180. </script>
  181. <style lang="scss" scoped>
  182. .promotion {
  183. width: 100%;
  184. height: 100vh;
  185. background-color: $uni-color;
  186. .promotion-card {
  187. width: 90%;
  188. margin: 140rpx auto 0rpx auto;
  189. box-shadow: 0rpx 0rpx 15rpx rgba(0, 0, 0, .2);
  190. border-radius: 40rpx;
  191. padding: 40rpx 30rpx;
  192. box-sizing: border-box;
  193. background-color: #fff;
  194. position: relative;
  195. padding-top: 100rpx;
  196. .headImage{
  197. position: absolute;
  198. height: 180rpx;
  199. width: 180rpx;
  200. border-radius: 50%;
  201. top: -80rpx;
  202. left: 50%;
  203. transform: translate(-50%);
  204. border: 10rpx solid #fff;
  205. box-shadow: 0 0 10rpx 10rpx #00000013;
  206. }
  207. }
  208. }
  209. #myCanvas {
  210. position: fixed;
  211. left: 100%;
  212. /* visibility: hidden */
  213. /* visibility: hidden; */
  214. /* margin-top: 100rpx; */
  215. margin: 68rpx auto;
  216. width: 750rpx;
  217. height: 750rpx;
  218. /* line-height: 20px; */
  219. background-color: rgba(255, 255, 255, 1);
  220. text-align: center;
  221. }
  222. </style>