123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- <template>
- <view class="honour" :class="fromIndex ? '' : 'share'">
- <!-- <custom-nav :title="pageTitle" v-if="fromIndex"></custom-nav> -->
- <custom-toast ref="toast"></custom-toast>
- <view class="container">
- <view class="certificate" @tap="previewImage">
- <canvas style="position: absolute;top: 10%;left: 8%" id="canvas" canvas-id="canvas"
- :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px' }"></canvas>
- </view>
- </view>
- </view>
- </template>
- <script>
- export default {
- onShareAppMessage: function(res) {
- //分享荣誉证书
- uni.showLoading({
- title: '加载中',
- mask: true
- }); //显示loading
- if (res.from === 'button') {
- //如果点击分享给好友按钮,则分享页面为当前页面
- return {
- title: '我的学位证书',
- path: `/pages/honour/honour?name=${this.name}&season=${this.season}&grade=${this.grade}&year=${this.year}&month=${this.month}&day=${
- this.day
- }&header=${encodeURIComponent(this.header)}`
- };
- }
- return {
- //如果点击右上角分享按钮,则分享页面为小程序主页
- title: '大卫博士商学院卖货争霸',
- path: '/pages/zbsIndex/index'
- };
- },
- data() {
- return {
- pageTitle: '荣耀殿堂', //页面名
- canvasWidth: 0, //canvas 宽
- canvasHeight: 0, //canvas 高
- name: '', //名字
- season: '',
- grade: '', //学位
- year: '', //年
- month: '', //月
- day: '', //日
- fromIndex: true, //是否非分享页面,
- header: '',
- headerImg: '',
- deg: ''
- };
- },
- computed: {
- userServerInfo() {
- return uni.getStorageSync('newUserInfo');
- },
- },
- methods: {
- saveImg() {
- //保存证书到本地
- uni.canvasToTempFilePath({
- //首先将 canvas 转化为一个图片
- canvasId: 'canvas'
- })
- .then(([, res]) => {
- //获得图片的临时路径
- return uni.saveImageToPhotosAlbum({
- //将图片保存到系统相册
- filePath: res.tempFilePath
- });
- })
- .then(([err, res]) => {
- console.log(err, res);
- if (err && (err.errMsg === 'saveImageToPhotosAlbum:fail:auth denied' || err.errMsg ===
- 'saveImageToPhotosAlbum:fail auth deny')) {
- //如果用户拒绝授权保存图片到相册
- const that = this;
- uni.showModal({
- title: '"大卫博士学位争霸赛"需要保存图片或视频到你的相册',
- content: '',
- success(res) {
- if (res.confirm) {
- wx.openSetting({
- success(res) {
- that.saveImg();
- return;
- }
- });
- }
- }
- });
- } else if (err && err.errMsg === 'saveImageToPhotosAlbum:fail authorize no response') {
- uni.showModal({
- content: '保存图片失败,请手动截图保存',
- showCancel: false
- });
- }
- if (res && res.errMsg === 'saveImageToPhotosAlbum:ok') {
- uni.showModal({
- content: '图片已经保存成功',
- showCancel: false
- });
- }
- });
- },
- previewImage() {
- //点击 canvas 预览图片
- uni.showLoading({
- title: '加载中',
- mask: true
- }); //显示loading
- uni.canvasToTempFilePath({
- //首先将 canvas 转化为一个图片
- canvasId: 'canvas'
- }).then(([, res]) => {
- //获得图片的临时路径
- uni.hideLoading(); //app hide 时隐藏loading
- uni.previewImage({
- urls: [res.tempFilePath]
- });
- });
- },
- tohishonour() {
- //点击历史荣誉
- uni.navigateTo({
- url: '../hishonour/hishonour'
- });
- }
- },
- async onLoad(opt) {
- //当进入页面之后判断是否带带有参数
- const avatarUrl = this.userServerInfo.avatar
- opt.time = opt.time || this.userServerInfo.start_time;
- this.header = opt.name ? decodeURIComponent(opt.header) : avatarUrl;
- uni.showLoading({
- title: '加载中',
- mask: true
- });
- let _this = this;
- uni.downloadFile({
- url: this.header,
- success: ({
- statusCode,
- tempFilePath
- }) => {
- if (statusCode === 200) {
- this.headerImg = tempFilePath;
- }
- },
- fail: e => {
- console.log(e);
- uni.showToast({
- icon: 'none',
- title: '获取用户头像失败',
- duration: 3000
- });
- },
- complete: () => {
- uni.showLoading(); //停止 loading
- _this.canvasWidth = uni.upx2px(641);
- _this.canvasHeight = uni.upx2px(977);
- _this.fromIndex = opt.name ? false : true; //当前页面为分享页面
- _this.name = opt.name || _this.userServerInfo.nickname; //姓名
- _this.name = _this.name.slice(0, 6);
- _this.season = opt.season || _this.userServerInfo.season; // 赛季季数
- _this.grade = opt.grade || ''; //学位
- console.log(_this.grade);
- // _this.year = opt.year || filters.getYear(+opt.time) //年
- // _this.month = opt.month || filters.getMonth(+opt.time) //月
- // _this.day = opt.day || filters.getDate(+opt.time) //日
- const ctx = uni.createCanvasContext('canvas');
- ctx.drawImage(_this.grade === '大学毕业' ? '../../static/icon/zs2-min.jpg' :
- '../../static/icon/zs1-min.jpg', 0, 0, _this.canvasWidth, _this.canvasHeight);
- ctx.save();
- ctx.beginPath();
- ctx.arc(uni.upx2px(320), uni.upx2px(400), uni.upx2px(40), 0, 2 * Math.PI, false);
- ctx.clip();
- ctx.drawImage(_this.headerImg, uni.upx2px(280), uni.upx2px(360), uni.upx2px(80), uni
- .upx2px(80));
- ctx.restore();
- ctx.setTextAlign('center')
- ctx.setFontSize(uni.upx2px(26))
- ctx.fillText(_this.grade === '学霸' || _this.grade === '结业' ? '' : _this
- .grade, uni.upx2px(320), uni.upx2px(555))
- ctx.setFontSize(uni.upx2px(32))
- ctx.fillText(_this.name, uni.upx2px(315), uni.upx2px(495))
- ctx.setFontSize(uni.upx2px(28))
- ctx.fillText(_this.season, uni.upx2px(440), uni.upx2px(595))
- ctx.draw()
- _this.$hideLoading();
- uni.hideLoading()
- }
- });
- }
- };
- </script>
- <style scoped lang="scss">
- .container {
- padding: 36rpx 54rpx;
- .certificate {
- width: 642rpx;
- height: 998rpx;
- .bg {
- width: 100%;
- height: 100%;
- }
- }
- .share {
- padding: 22rpx 90rpx;
- display: flex;
- justify-content: center;
- align-items: center;
- .item {
- display: flex;
- justify-content: center;
- flex-direction: column;
- align-items: center;
- background: none;
- padding: 0;
- margin: 0;
- line-height: 70rpx !important;
- &::after,
- &::before {
- border: none;
- }
- .icon {
- width: 64rpx;
- height: 64rpx;
- }
- color: #333333;
- font-size: 28rpx;
- }
- }
- }
- .honour {
- @include page();
- &.share {
- border-top-color: #493225;
- }
- .content {
- display: flex;
- flex-direction: column;
- .top {
- width: 100%;
- height: 680rpx;
- background: #493225;
- border-bottom-right-radius: 375rpx 123rpx;
- border-bottom-left-radius: 375rpx 123rpx;
- position: relative;
- &.share {
- height: 890rpx;
- }
- #canvas {
- position: absolute;
- top: 100px;
- left: 50%;
- transform: translateX(-50%);
- &.share {
- top: 0;
- }
- }
- .bg {
- position: absolute;
- width: 566rpx;
- height: 464rpx;
- top: 100px;
- left: 50%;
- z-index: 1;
- transform: translateX(-50%);
- }
- .all {
- position: absolute;
- right: 32rpx;
- bottom: -13rpx;
- font-size: 26rpx;
- color: #fa6342;
- }
- }
- .bot {
- flex: 1;
- box-sizing: border-box;
- padding: 100rpx;
- .title {
- text-align: center;
- height: 45rpx;
- display: flex;
- justify-content: center;
- align-items: center;
- font-size: 36rpx;
- color: #fa6342;
- text {
- margin: 0 40rpx;
- }
- image {
- width: 48rpx;
- height: 45rpx;
- }
- }
- .text {
- margin-top: 80rpx;
- font-size: 30rpx;
- color: #666666;
- }
- .share-save {
- height: 60rpx;
- margin-top: 80rpx;
- display: flex;
- justify-content: space-between;
- .button {
- height: 100%;
- width: 240rpx;
- box-sizing: border-box;
- border: 2rpx solid #fa6342;
- border-radius: 30rpx;
- line-height: 56rpx;
- text-align: center;
- color: #fa6342;
- background: #ffffff;
- padding: 0;
- font-size: 24rpx;
- text {
- margin: 0 6rpx;
- vertical-align: middle;
- &.cuIcon {
- font-size: 36rpx;
- }
- }
- }
- }
- }
- }
- }
- </style>
|