detail copy.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <template>
  2. <view class="honour" :class="fromIndex ? '' : 'share'">
  3. <view class="container">
  4. <view class="certificate" @tap="previewImage">
  5. <canvas id="canvas" canvas-id="canvas"
  6. :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px' }"></canvas>
  7. </view>
  8. <view class="share">
  9. <!-- <button open-type="share" class="item" v-if="fromIndex">
  10. <image class="icon" src="../../static/icon/share1.png"></image>
  11. <view>分享好友</view>
  12. </button> -->
  13. <view class="item" @tap="saveImg">
  14. <!-- <image class="icon" src="../../static/icon/share2.png"></image> -->
  15. <view>长按保存图片</view>
  16. </view>
  17. </view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. // import { api_getHonourInfo } from '../../api.js';
  23. import * as filters from '../../common/util/filters.js';
  24. export default {
  25. // onShareAppMessage: function(res) {
  26. // //分享荣誉证书
  27. // uni.showLoading({ title: '加载中', mask: true }); //显示loading
  28. // if (res.from === 'button') {
  29. // //如果点击分享给好友按钮,则分享页面为当前页面
  30. // return {
  31. // title: '我的学位证书',
  32. // path: `/pages/honour/honour?name=${this.name}&season=${this.season}&grade=${this.grade}&year=${this.year}&month=${this.month}&day=${
  33. // this.day
  34. // }&header=${encodeURIComponent(this.header)}`
  35. // };
  36. // }
  37. // return {
  38. // //如果点击右上角分享按钮,则分享页面为小程序主页
  39. // title: '大卫博士商学院卖货争霸',
  40. // path: '/pages/index/index'
  41. // };
  42. // },
  43. data() {
  44. return {
  45. pageTitle: '荣耀殿堂', //页面名
  46. canvasWidth: 0, //canvas 宽
  47. canvasHeight: 0, //canvas 高
  48. name: '', //名字
  49. season: '',
  50. grade: '', //学位
  51. year: '', //年
  52. month: '', //月
  53. day: '', //日
  54. fromIndex: true, //是否非分享页面,
  55. header: '',
  56. headerImg: '',
  57. deg: ''
  58. };
  59. },
  60. computed: {
  61. userServerInfo() {
  62. return this.$store.state.userServerInfo;
  63. },
  64. userWeixinInfo() {
  65. return this.$store.state.userWeixinInfo;
  66. }
  67. },
  68. methods: {
  69. saveImg() {
  70. //保存证书到本地
  71. uni.canvasToTempFilePath({
  72. //首先将 canvas 转化为一个图片
  73. canvasId: 'canvas'
  74. })
  75. .then(([, res]) => {
  76. //获得图片的临时路径
  77. return uni.saveImageToPhotosAlbum({
  78. //将图片保存到系统相册
  79. filePath: res.tempFilePath
  80. });
  81. })
  82. .then(([err, res]) => {
  83. console.log(err, res);
  84. if (err && (err.errMsg === 'saveImageToPhotosAlbum:fail:auth denied' || err.errMsg ===
  85. 'saveImageToPhotosAlbum:fail auth deny')) {
  86. //如果用户拒绝授权保存图片到相册
  87. const that = this;
  88. uni.showModal({
  89. title: '"大卫博士学位争霸赛"需要保存图片或视频到你的相册',
  90. content: '',
  91. success(res) {
  92. if (res.confirm) {
  93. wx.openSetting({
  94. success(res) {
  95. that.saveImg();
  96. return;
  97. }
  98. });
  99. }
  100. }
  101. });
  102. } else if (err && err.errMsg === 'saveImageToPhotosAlbum:fail authorize no response') {
  103. uni.showModal({
  104. content: '保存图片失败,请手动截图保存',
  105. showCancel: false
  106. });
  107. }
  108. if (res && res.errMsg === 'saveImageToPhotosAlbum:ok') {
  109. uni.showModal({
  110. content: '图片已经保存成功',
  111. showCancel: false
  112. });
  113. }
  114. });
  115. },
  116. previewImage() {
  117. //点击 canvas 预览图片
  118. uni.showLoading({
  119. title: '加载中',
  120. mask: true
  121. }); //显示loading
  122. uni.canvasToTempFilePath({
  123. //首先将 canvas 转化为一个图片
  124. canvasId: 'canvas'
  125. }).then(([, res]) => {
  126. //获得图片的临时路径
  127. uni.hideLoading(); //app hide 时隐藏loading
  128. uni.previewImage({
  129. urls: [res.tempFilePath]
  130. });
  131. });
  132. },
  133. tohishonour() {
  134. //点击历史荣誉
  135. uni.navigateTo({
  136. url: '../hishonour/hishonour'
  137. });
  138. }
  139. },
  140. async onLoad() {
  141. //当进入页面之后判断是否带带有参数
  142. // const avatarUrl = this.userServerInfo.avatar
  143. // uni.hideShareMenu();
  144. // opt.time = opt.time || this.userServerInfo.start_time;
  145. // this.header = opt.name ? decodeURIComponent(opt.header) : avatarUrl;
  146. let opt = this.vuex_user
  147. this.header = opt ? decodeURIComponent(opt.headimg) : '';
  148. // opt.name="xvying"
  149. // opt.grade="学霸学位"
  150. this.header = this.vuex_user.headimg
  151. uni.showLoading({
  152. title: '加载中',
  153. mask: true
  154. });
  155. let _this = this;
  156. uni.downloadFile({
  157. // url:this.header,
  158. url: 'http://api.dwbszbs.dev.xmnk.cn/default/headimg.png"',
  159. success: ({
  160. statusCode,
  161. tempFilePath
  162. }) => {
  163. if (statusCode === 200) {
  164. this.headerImg = tempFilePath;
  165. }
  166. },
  167. fail: e => {
  168. console.log(e);
  169. uni.showToast({
  170. icon: 'none',
  171. title: '获取用户头像失败',
  172. duration: 3000
  173. });
  174. },
  175. complete: () => {
  176. console.log('执行这个地方')
  177. uni.hideLoading(); //停止 loading
  178. _this.canvasWidth = uni.upx2px(641);
  179. _this.canvasHeight = uni.upx2px(977);
  180. _this.fromIndex = opt.nickname ? false : true; //当前页面为分享页面
  181. _this.name = opt.nickname; //姓名
  182. _this.name = _this.name.slice(0, 6);
  183. _this.grade = opt.grade || '博士学位'; //学位
  184. console.log(_this.grade);
  185. const ctx = uni.createCanvasContext('canvas',this);
  186. console.log(ctx)
  187. // debugger
  188. ctx.drawImage(_this.grade === '大学毕业' ? '../../static/icon/zs2-min.jpg' :
  189. '../../static/icon/zs1-min.jpg', 0, 0, _this.canvasWidth, _this.canvasHeight);
  190. ctx.save();
  191. ctx.beginPath();
  192. ctx.arc(uni.upx2px(320), uni.upx2px(400), uni.upx2px(40), 0, 2 * Math.PI, false);
  193. ctx.clip();
  194. ctx.drawImage(_this.headerImg, uni.upx2px(280), uni.upx2px(360), uni.upx2px(80), uni
  195. .upx2px(80));
  196. ctx.restore();
  197. _this.$drawHonour(ctx, [
  198. [uni.upx2px(26), _this.grade === '学霸' || _this.grade === '结业' ? '' : _this
  199. .grade, uni.upx2px(320), uni.upx2px(555)
  200. ],
  201. [uni.upx2px(32), _this.name, uni.upx2px(315), uni.upx2px(495)],
  202. [uni.upx2px(28), _this.season, uni.upx2px(440), uni.upx2px(595)]
  203. // [uni.upx2px(24), _this.year, uni.upx2px(160), uni.upx2px(620)],
  204. // [uni.upx2px(24), this.month, uni.upx2px(245), uni.upx2px(620)],
  205. // [uni.upx2px(24), _this.day, uni.upx2px(330), uni.upx2px(620)],
  206. // [uni.upx2px(24), 21, uni.upx2px(458), uni.upx2px(620)]
  207. ]);
  208. // _this.$hideLoading();
  209. }
  210. });
  211. }
  212. };
  213. </script>
  214. <style lang="scss">
  215. .container {
  216. padding: 36rpx 54rpx;
  217. .certificate {
  218. width: 642rpx;
  219. height: 998rpx;
  220. .bg {
  221. width: 100%;
  222. height: 100%;
  223. }
  224. }
  225. .share {
  226. padding: 22rpx 90rpx;
  227. display: flex;
  228. justify-content: center;
  229. align-items: center;
  230. .item {
  231. display: flex;
  232. justify-content: center;
  233. flex-direction: column;
  234. align-items: center;
  235. background: none;
  236. padding: 0;
  237. margin: 0;
  238. line-height: 70rpx !important;
  239. &::after,
  240. &::before {
  241. border: none;
  242. }
  243. .icon {
  244. width: 64rpx;
  245. height: 64rpx;
  246. }
  247. color: #333333;
  248. font-size: 28rpx;
  249. }
  250. }
  251. }
  252. .honour {
  253. @include page();
  254. &.share {
  255. border-top-color: #493225;
  256. }
  257. .content {
  258. display: flex;
  259. flex-direction: column;
  260. .top {
  261. width: 100%;
  262. height: 680rpx;
  263. background: #493225;
  264. border-bottom-right-radius: 375rpx 123rpx;
  265. border-bottom-left-radius: 375rpx 123rpx;
  266. position: relative;
  267. &.share {
  268. height: 890rpx;
  269. }
  270. #canvas {
  271. position: absolute;
  272. top: 56rpx;
  273. left: 50%;
  274. transform: translateX(-50%);
  275. &.share {
  276. top: 0;
  277. }
  278. }
  279. .bg {
  280. position: absolute;
  281. width: 566rpx;
  282. height: 464rpx;
  283. top: 35rpx;
  284. left: 50%;
  285. z-index: 1;
  286. transform: translateX(-50%);
  287. }
  288. .all {
  289. position: absolute;
  290. right: 32rpx;
  291. bottom: -13rpx;
  292. font-size: 26rpx;
  293. color: #fa6342;
  294. }
  295. }
  296. .bot {
  297. flex: 1;
  298. box-sizing: border-box;
  299. padding: 100rpx;
  300. .title {
  301. text-align: center;
  302. height: 45rpx;
  303. display: flex;
  304. justify-content: center;
  305. align-items: center;
  306. font-size: 36rpx;
  307. color: #fa6342;
  308. text {
  309. margin: 0 40rpx;
  310. }
  311. image {
  312. width: 48rpx;
  313. height: 45rpx;
  314. }
  315. }
  316. .text {
  317. margin-top: 80rpx;
  318. font-size: 30rpx;
  319. color: #666666;
  320. }
  321. .share-save {
  322. height: 60rpx;
  323. margin-top: 80rpx;
  324. display: flex;
  325. justify-content: space-between;
  326. .button {
  327. height: 100%;
  328. width: 240rpx;
  329. box-sizing: border-box;
  330. border: 2rpx solid #fa6342;
  331. border-radius: 30rpx;
  332. line-height: 56rpx;
  333. text-align: center;
  334. color: #fa6342;
  335. background: #ffffff;
  336. padding: 0;
  337. font-size: 24rpx;
  338. text {
  339. margin: 0 6rpx;
  340. vertical-align: middle;
  341. &.cuIcon {
  342. font-size: 36rpx;
  343. }
  344. }
  345. }
  346. }
  347. }
  348. }
  349. }
  350. </style>