share.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <template>
  2. <view class="bt-tip">
  3. <view class="" id="boxImage">
  4. </view>
  5. <view class="down" @click="saveImage" v-if="status==1">
  6. <view class="">
  7. 保存图片
  8. </view>
  9. </view>
  10. <view class="down" v-if="status==2" @click="shengcheng">
  11. <view class="">
  12. 点击生成图片
  13. </view>
  14. </view>
  15. <view class="down" v-if="status==3" @click="shengcheng">
  16. <view class="">
  17. 长按图片发送给好友
  18. </view>
  19. </view>
  20. <view class="" v-show="isShow">
  21. <view id="shareImage">
  22. <view class="border">
  23. <view class="content">
  24. <view class="title_1">
  25. <text>{{setCity}}</text>
  26. <image class="right_arrow" src="../../static/img/right.png" mode="" style="display: inline-block;vertical-align: center;height: 35upx;width: 80upx;margin: 0 10upx;"></image><text>{{reachCity}}</text>
  27. </view>
  28. <view class="title_2">
  29. 回{{reachCity}}过年需要隔离吗?
  30. </view>
  31. <view class="text_1">
  32. <p v-for="(item,index2) in policy" :key='index2'>
  33. {{item}}
  34. </p>
  35. </view>
  36. <view class="tip_share">
  37. <view class="left_share">
  38. <image src="../../static/img/erwei1.jpg" mode=""></image>
  39. </view>
  40. <view class="right_share">
  41. <view class="">
  42. 数据来源
  43. </view>
  44. <view class="">
  45. <p>国家卫生健康委提供</p>
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. <image class="btimg" src="../../static/img/image1.png" mode=""></image>
  51. <image src="../../static/img/icon6.png" mode="" class="icon"></image>
  52. </view>
  53. </view>
  54. </view>
  55. </view>
  56. </template>
  57. <script>
  58. // import html2canvas from '@/static/html/js/html2canvas.js';
  59. import html2canvas from '@/static/html/js/test.js';
  60. import parse from '../../uParse-master/src/wxParse.vue'
  61. export default {
  62. data() {
  63. return {
  64. policy: '',
  65. setCity: '',
  66. reachCity: '',
  67. result: "",
  68. Height: "",
  69. isShow: true
  70. }
  71. },
  72. onShow() {
  73. console.log('1212')
  74. this.policy = uni.getStorageSync('policy')
  75. this.setCity = uni.getStorageSync('setCity');
  76. this.reachCity = uni.getStorageSync('reachCity');
  77. },
  78. onLoad(options) {
  79. var that = this
  80. uni.getSystemInfo({
  81. success: (res) => {
  82. this.Height = res.windowHeight
  83. }
  84. })
  85. var ua = window.navigator.userAgent.toLowerCase();
  86. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  87. this.status = 2
  88. setTimeout(function() {
  89. }, 1000)
  90. // this.drawImage()
  91. // this.isShow = false
  92. } else {
  93. this.status = 1
  94. }
  95. // this.drawImage
  96. //自定义分享
  97. const script = document.createElement('script')
  98. script.style = 'text/javascript';
  99. script.src = 'http://res.wx.qq.com/open/js/jweixin-1.6.0.js';
  100. document.getElementsByTagName('head')[0].appendChild(script);
  101. script.onload = () => {
  102. uni.request({
  103. url: 'http://epidemic.site.ximengnaikang.com/api/v1/wechat/share', //仅为示例,并非真实接口地址。
  104. method: 'POST',
  105. data: {
  106. url: location.href,
  107. apis: 'updateAppMessageShareData'
  108. },
  109. success: (res) => {
  110. wx.config({
  111. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  112. appId: res.data.data.appId, // 必填,公众号的唯一标识
  113. timestamp: res.data.data.timestamp, // 必填,生成签名的时间戳
  114. nonceStr: res.data.data.nonceStr, // 必填,生成签名的随机串
  115. signature: res.data.data.signature, // 必填,签名
  116. jsApiList: res.data.data.jsApiList // 必填,需要使用的JS接口列表
  117. })
  118. setTimeout(function() {
  119. wx.ready(function() { //需在用户可能点击分享按钮前就先调用
  120. wx.updateAppMessageShareData({
  121. title: '疫情隔离政策查询', // 分享标题
  122. desc: '足不出户即可查询,隔离政策最新情况信息', // 分享描述
  123. link: location.href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  124. imgUrl: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3363295869,2467511306&fm=26&gp=0.jpg', // 分享图标网络图片
  125. success: function() {
  126. // 设置成功
  127. }
  128. })
  129. });
  130. }, 2000)
  131. }
  132. })
  133. }
  134. },
  135. components: {
  136. parse
  137. },
  138. methods: {
  139. shengcheng: function() {
  140. var that=this
  141. uni.pageScrollTo({
  142. scrollTop: 0,
  143. duration: 300,
  144. });
  145. uni.showLoading({
  146. title: '图片生成中',
  147. })
  148. setTimeout(function(){
  149. html2canvas(document.querySelector('#shareImage'), {
  150. scale: 2,
  151. x: 0, // x坐标
  152. y: 0, // y坐标
  153. foreignObjectRendering: true, // 是否在浏览器支持的情况下使用ForeignObject渲染
  154. async: false, // 是否异步解析和呈现元素
  155. background: "#ffffff", // 一定要添加背景颜色,否则出来的图片,背景全部都是透明的
  156. // dpi: 300, // 处理模糊问题
  157. useCORS: true,
  158. }).then(imgDom => {
  159. let previewFile = imgDom.toDataURL('image/png');
  160. var image = new Image();
  161. image.src = previewFile;
  162. image.style.width = '100%';
  163. document.getElementById('boxImage').appendChild(image);
  164. });
  165. uni.hideLoading()
  166. that.status=3
  167. that.isShow = false
  168. uni.showToast({
  169. title: '图片已生成'
  170. })
  171. },1000)
  172. },
  173. saveImage() {
  174. html2canvas(document.querySelector('#shareImage'), {
  175. useCORS: true,
  176. }).then(canvas => {
  177. let previewFile = canvas.toDataURL('image/png');
  178. this.download(previewFile)
  179. });
  180. },
  181. download(url) {
  182. var oA = document.createElement("a");
  183. oA.download = ''; // 设置下载的文件名,默认是'下载'
  184. oA.href = url;
  185. console.log(url, 'po')
  186. document.body.appendChild(oA);
  187. const event = document.createEvent('MouseEvents');
  188. oA.click();
  189. oA.remove(); // 下载之后把创建的元素删除
  190. }
  191. }
  192. }
  193. </script>
  194. <style>
  195. page {
  196. height: 100%;
  197. }
  198. #size {
  199. margin: 30px;
  200. background-color: #00BFFF;
  201. }
  202. .border {
  203. /* width: 100%; */
  204. position: relative;
  205. /* min-height:100vh; */
  206. padding: 0 15px;
  207. padding-top: 50px;
  208. padding-bottom: 40px;
  209. /* height: 600px; */
  210. background: linear-gradient(180deg, #1E37EB 0%, #0FC6A1 100%);
  211. }
  212. /* #shareImage{
  213. height: 100vh;
  214. width: 100%;
  215. } */
  216. .down {
  217. position: fixed;
  218. bottom: 0;
  219. height: 100upx;
  220. line-height: 100upx;
  221. width: 100%;
  222. text-align: center;
  223. z-index: 9999999999999;
  224. }
  225. .right_arrow {
  226. display: inline-block;
  227. vertical-align: center;
  228. height: 42upx;
  229. width: 100upx;
  230. margin: 0 10upx;
  231. }
  232. .down view {
  233. width: 40%;
  234. margin-left: 30%;
  235. text-align: center;
  236. font-size: 30upx;
  237. height: 80upx;
  238. line-height: 80upx;
  239. color: #FFFFFF;
  240. border-radius: 12upx;
  241. background-color: rgba(238, 82, 32, 0.8);
  242. }
  243. .content {
  244. padding: 60upx 34upx;
  245. background-color: #FFFFFF;
  246. border-radius: 12px;
  247. margin-bottom: 140px;
  248. }
  249. .btimg {
  250. position: absolute;
  251. left: 0;
  252. bottom: 0;
  253. width: 100%;
  254. height: 408upx;
  255. }
  256. .icon {
  257. position: absolute;
  258. top: 46upx;
  259. right: 30upx;
  260. height: 80px;
  261. width: 80px;
  262. }
  263. .title_1 {
  264. text-align: center;
  265. font-size: 52upx;
  266. font-weight: bold;
  267. height: 112upx;
  268. margin-top: 10upx;
  269. line-height: 112upx;
  270. color: #23459D;
  271. opacity: 1;
  272. }
  273. .title_2 {
  274. text-align: center;
  275. font-size: 20px;
  276. font-weight: bold;
  277. line-height: 34px;
  278. color: #333333;
  279. opacity: 1;
  280. margin-top: 30upx;
  281. margin-bottom: 20px;
  282. }
  283. .text_1 {
  284. font-size: 28upx;
  285. font-weight: 400;
  286. line-height: 21px;
  287. color: #333333;
  288. opacity: 1;
  289. }
  290. .tip_share {
  291. display: flex;
  292. margin-top: 10upx;
  293. }
  294. .left_share {
  295. flex: 0 0 80upx;
  296. margin-left: 5%;
  297. }
  298. .left_share image {
  299. height: 80px;
  300. width: 80px;
  301. /* margin-left: 10%; */
  302. /* right: 17px; */
  303. }
  304. .right_share {
  305. flex: 1;
  306. text-align: right;
  307. margin-top: 8px;
  308. font-size: 14px;
  309. font-weight: 400;
  310. line-height: 21px;
  311. color: #333333;
  312. opacity: 1;
  313. }
  314. </style>