金诚优选前端代码
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.

189 lines
6.2 KiB

  1. <template>
  2. <!-- <view id="author">
  3. <u-parse :content="info.content" v-if="info.content" />
  4. </view> -->
  5. <view>
  6. <view class="personal_li"
  7. @click="shareClick">
  8. <image src="../../../static/card-r.png"
  9. mode="widthFix"
  10. class="iconImage"></image>
  11. <text class="font14">分享生成图片</text>
  12. <image src="../../../static/home-r.png"
  13. mode="widthFix"
  14. class="jtIcon"></image>
  15. </view>
  16. <view class="canvasContent" v-if="canvasShow">
  17. <canvas canvas-id="shareCanvas" class="canvasName"></canvas>
  18. <view class="canvasText">图片已保存到相册可分享给好友</view>
  19. <image src="../../../static/error.png" class="errorImage" @click="canvasShow = false"></image>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';
  25. import uParse from '@/components/gaoyia-parse/parse.vue';
  26. export default {
  27. data() {
  28. return {
  29. info: '',
  30. canvasShow: false
  31. };
  32. },
  33. components:{
  34. uParse
  35. },
  36. onLoad() {
  37. /* this.getAuthor(); */
  38. },
  39. methods: {
  40. getAuthor() {
  41. wx.showLoading({
  42. title: "加载中",
  43. mask: true
  44. });
  45. this.$http.get({
  46. api: 'api/ibrand'
  47. }).then(res => {
  48. if (res.statusCode == 200) {
  49. res = res.data;
  50. if (res.status) {
  51. this.setData({
  52. info: res.data
  53. });
  54. } else {
  55. wx.showModal({
  56. content: res.message || '请求失败',
  57. showCancel: false
  58. });
  59. }
  60. } else {
  61. wx.showModal({
  62. content: res.message || '请求失败',
  63. showCancel: false
  64. });
  65. }
  66. wx.hideLoading();
  67. }).catch(err => {
  68. wx.hideLoading();
  69. wx.showModal({
  70. content: '请求失败',
  71. showCancel: false
  72. });
  73. });
  74. },
  75. setData: function (obj) {
  76. let that = this;
  77. let keys = [];
  78. let val, data;
  79. Object.keys(obj).forEach(function (key) {
  80. keys = key.split('.');
  81. val = obj[key];
  82. data = that.$data;
  83. keys.forEach(function (key2, index) {
  84. if (index + 1 == keys.length) {
  85. that.$set(data, key2, val);
  86. } else {
  87. if (!data[key2]) {
  88. that.$set(data, key2, {});
  89. }
  90. }
  91. data = data[key2];
  92. });
  93. });
  94. },
  95. //这是一个封装好的方法
  96. promisify: api => {
  97. return (options, ...params) => {
  98. return new Promise((resolve, reject) => {
  99. const extras = {
  100. success: resolve,
  101. fail: reject
  102. }
  103. api({ ...options, ...extras }, ...params)
  104. })
  105. }
  106. },
  107. shareClick() {
  108. const wxGetImageInfo = this.promisify(uni.getImageInfo)
  109. Promise.all([
  110. // 图片目前只随机找了几张图片,后期可自行替换
  111. wxGetImageInfo({
  112. src: 'http://pics.ksudi.com/pic/2019/soms/companycard/jd2.png' // 背景图片
  113. }),
  114. wxGetImageInfo({
  115. src: 'http://pics.ksudi.com/pic/2019/soms/companycard/st2.png' // 二维码图片,二维码图片如需要携带参数,可根据接口将需要扫码进入页面的路径+参数传入后端,后端可根据生产小程序二维码路径,将路径放入这里就ok了,<a href="https://www.jianshu.com/p/5f96a4f91b9c" target="_blank">可参考</a>
  116. })
  117. ]).then(res => {
  118. console.log(3454)
  119. const ctx = wx.createCanvasContext('shareCanvas')
  120. console.log(ctx)
  121. // 底图
  122. ctx.drawImage(res[0].path, 0, 0, 600, 700)
  123. // 作者名称
  124. ctx.setTextAlign('center') // 文字居中
  125. ctx.setFillStyle('#000000') // 文字颜色:黑色
  126. ctx.setFontSize(22) // 文字字号:22px
  127. ctx.fillText('作者:张杰', 300 / 2, 100)
  128. // 小程序码
  129. const qrImgSize = 150
  130. ctx.drawImage(res[1].path, (340 - qrImgSize) / 2, 230, qrImgSize, qrImgSize)
  131. ctx.stroke()
  132. // 绘图生成临时图片
  133. console.log('res', res)
  134. ctx.draw(false,() => {
  135. this.tempFileImage()
  136. })
  137. this.canvasShow = true
  138. })
  139. },
  140. tempFileImage() {
  141. let that = this
  142. uni.canvasToTempFilePath({
  143. canvasId: 'shareCanvas',
  144. success: (res) => {
  145. uni.hideLoading()
  146. that.savePic(res.tempFilePath)
  147. },
  148. fail:function () {
  149. //TODO
  150. }
  151. })
  152. },
  153. //保存
  154. savePic (filePath) {
  155. console.log('filePath', filePath)
  156. uni.showLoading({
  157. title: '正在保存'
  158. });
  159. uni.saveImageToPhotosAlbum({
  160. filePath: filePath,
  161. success: function () {
  162. uni.showToast({
  163. title: '图片保存成功~'
  164. });
  165. },
  166. fail: function (e) {
  167. //TODO
  168. },
  169. complete: function (){
  170. uni.hideLoading()
  171. }
  172. });
  173. }
  174. }
  175. };
  176. </script>
  177. <style rel="stylesheet/less" lang="less">
  178. @import "author";
  179. </style>