敢为人鲜小程序前端代码仓库
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.

234 lines
5.4 KiB

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