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

279 lines
6.5 KiB

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