特易招,招聘小程序
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.

258 lines
5.3 KiB

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