detail.js 23.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
// pages/activity/detail/detail.js
const app = getApp();
var t = null;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        current_swiper:0,
        slide: [],
        peopleList: [],
        num:1,
        actEndTimeList: [],
        is_zan:false,
        display:false,
        content:'',
        is_ask:false,
        is_reply:false,
        placeholder:'输入您的留言',
        is_join:false,
        img_heights: [], //图片宽度
        img_width: 750, //默认
        show_canvas: false,
        can_save:false,
    },
    //获取图片真实宽度
    imageLoad: function (e) {
        var img_width = e.detail.width,
           img_height = e.detail.height,
            //宽高比
            ratio = img_width / img_height;
        console.log(img_width, img_height);
        //计算的高度值
        var viewHeight = 750 / ratio;
        var img_height = viewHeight;
        var img_heights = this.data.img_heights;
        //把每一张图片的对应的高度记录到数组里
        img_heights[e.target.dataset.id] = img_height;
        console.log('img_heights',img_heights);
        this.setData({
            img_heights: img_heights
        })
    },
    //查看我的订单
    goMyRelease() {
        wx.redirectTo({url:'/pages/order/order-detail/order-detail?id=' + this.data.order_id})
    },
    //完成按钮
    goBack() {
        wx.redirectTo({url:'/pages/spell-list/spell-list?current=' + 0})
    },
    // 问一问
    ask(e) {
        console.log('前问一问-display', this.data.display);
        this.setData({
            display: !this.data.display,
            is_ask:true,is_reply:false,
            content:'' ,
            placeholder:'输入您的留言',
        });
        console.log('后问一问-display', this.data.display);
        // wx.navigateTo({
        //     url: '/pages/comment/comment?activity_id=' + this.data.detail_id
        //         + '&common_id=' + 0
        // })
        // console.log('问',e.detail.value);
        // this.setData({focus:true})
    },
    //输入评论内容
    inputComment(e) {
        this.setData({ content: e.detail.value });
    },
    //发送评论/回复
    clickComment(e) {
        this.setData({ display: false });
        if(this.data.content === '' ) {
            wx.showToast({title:'请输入内容',icon:'none'})
        }else {
            let url = '/portal/Active/common';
            let params = {
                token:wx.getStorageSync('token'),
                activeId:this.data.detail_id,
                commonId: this.data.is_ask?0:this.data.commet_id,
                content:this.data.content,
                formId:e.detail.formId,
            };
            app.post(url,params,{}).then((res)=>{
                console.log('提交评论',res);
                if(+res.code === 1) {
                    wx.showToast({title:this.data.is_ask?'留言成功':'回复成功',icon:'none',});
                    this.getDetail();
                }
            })
        }
    },
    //评论input失去焦点
    blur() {
        this.setData({ display: false });
    },
    //点击键盘上的完成
    confirm() {
        this.setData({ display: false });
        this.clickComment();
    },
    //回复
    reply(e) {
        const id = e.currentTarget.dataset.id;
        this.setData({
            display: !this.data.display,
            is_ask:false,is_reply:true,
            commet_id:id,content:'' ,
            placeholder:'输入您的回复'
        });
        // wx.navigateTo({
        //     url: '/pages/comment/comment?is_reply=' + true
        //         + '&activity_id=' + this.data.detail_id + '&common_id=' + id
        // })
    },
    //收藏/取消收藏
    collect() {
        console.log('收藏');
        let self = this;
        let url = '/portal/Active/collection';
        let url_del = '/portal/Active/delCollection';
        let header = {
            "XX-Token": wx.getStorageSync('token')
        };
        let params = {
            token: wx.getStorageSync('token'),
            activeId: self.data.detail_id,
        };
        if(self.data.is_collect) {
            app.post(url_del, params, header).then((res) => {
                console.log('取消收藏',res);
                if(+res.code === 1) {
                    wx.showToast({title:res.message,icon:'none'});
                    self.setData({
                        is_collect: 0
                    });
                }
            })
        }else {
            app.post(url, params, header).then((res) => {
                console.log('收藏',res);
                if(+res.code === 1) {
                    wx.showToast({title:res.message,icon:'none'});
                    self.setData({
                        is_collect: 1
                    });
                }
            })
        }

    },
    //点赞
    clickZan(e) {
        let self = this;
        const id = e.currentTarget.dataset.id;
        const index = e.currentTarget.dataset.index;
        let url = '/portal/Active/perfect';
        let header = {
            "XX-Token": wx.getStorageSync('token')
        };
        let params = {
            token: wx.getStorageSync('token'),
            commonId: id,
        };
        app.post(url, params, {}).then((res) => {
            console.log('点赞', res);
            if (+res.code === 1) {  //
                wx.showToast({title:res.message,icon:'none'});
                self.setData({
                    [`commentList[${index}].is_prefect`]: true,
                    [`commentList[${index}].prefect`]: res.message == '该评论已赞'?+self.data.commentList[index].prefect:+self.data.commentList[index].prefect + 1,
                });
                // console.log('paise_user', paise_user);
            }
        })
    },
    swiperChange(e) {
        // console.log(e);
        this.setData({current_swiper: e.detail.current})
    },
    onPageScroll(e) {
        // console.log('页面滚动', e);
        if(e.scrollTop > 140) {
            this.setData({hidden_top:true})
        }else if(e.scrollTop <=140){
            this.setData({hidden_top:false})
        }
    },
    // 增加数量
    addCount(e) {
        // let k = e.currentTarget.dataset.idx;
        // let t = e.currentTarget.dataset.children_idx;
        // let cartslist = this.data.cartslist;
        // let num = cartslist[k].children[t].num;
        // let product_id = cartslist[k].children[t].product_id;
        let num = this.data.num;
        num ++;
        this.setData({
            num: num
        });
    },
    // 减少数量
    minusCount(e) {
        let num = this.data.num;
        if (num <= 1) {
            return false;
        }
        num --;
        this.setData({
            num: num
        });
    },
    //获取详情
    getDetail() {
        const self = this;
        console.log('city',self.data.city);
        let url = '/portal/Active/mealDetail';
        let params = {
            token:wx.getStorageSync('token'),
            activeId: self.data.detail_id,
        };
        let header = {
            "token": wx.getStorageSync('token')
        };
        app.post(url, params, {}).then((res) => {
            console.log('获取详情', res);
            if (+res.code === 1) {
                self.setData({
                    detail: res.data.active,
                    'detail.time': app.initTime(res.data.active.surplus),
                    end_time: res.data.active.time,
                    over_hours:res.data.active.over_time,//几小时截止
                    join_people: res.data.son,
                    commentList:res.data.common,
                    is_collect:res.data.is_collect,
                    count:res.data.count,
                    is_send:res.data.is_send,//能否参加1:不能2:能
                });
                this.getLocalImage();
                // self.countDown();


                // let newTime = new Date().getTime();//当前时间戳
                // let end_time = app.nextTime(this.data.end_time, this.data.over_hours);//截止时间
                // let endTime = new Date(end_time).getTime(); //截止时间戳
                // if (endTime - newTime > 0) {}
                // (0全部,2待拼成,3人数不足未拼成,4已拼成,5已取消,6已完成,7已评价,8已拼成(活动未结束),9删除
                const status = res.data.active.status;
                if(status === 2 || status === 6 || status === 7 || status === 8) {
                    console.log('调用倒计时');
                    self.countDown();
                }else {
                    console.log('不调用倒计时');
                    let countDownArr = [];
                    let obj = null;
                    obj = {
                        day: '00',
                        hou: '00',
                        min: '00',
                        sec: '00'
                    };
                    countDownArr.push(obj);
                    this.setData({
                        'detail.time': countDownArr,
                    });
                }
                // this.drawCard();
            }
            // wx.hideLoading()
        })

    },
    //确认拼餐
    confirmJoin(e) {
        const self = this;
        if(+self.data.is_send === 2){ //1:不能参与2:可参与
            let url = '/portal/Active/join';
            let params = {
                token: wx.getStorageSync('token'),
                number: self.data.num,
                activeId: self.data.detail_id,
                formId: e.detail.formId,
            };
            let header = {
                "token": wx.getStorageSync('token')
            };
            app.post(url, params, {}).then((res) => {
                console.log('确认拼餐', res);
                if (+res.code === 1) {
                    // wx.showToast({title:'拼餐成功',icon:'success'});
                    self.setData({is_showRelease_modal:true,order_id:+res.data.orderId,is_join:true});
                    self.getDetail();
                }else if(+res.code === 0) { //参与失败按钮还是确认拼活动
                    self.setData({is_join:false});
                }
            })
        }
    },
    timeFormat(param) { //小于10的格式化函数
        return param < 10 ? '0' + param : param;
    },
    time() {
        const self = this;
        self.data.detail.surplus--;
        //定時器,每秒重新計算
        // setTimeout(function () {
        //     self.time();
        // },1000)
    },
    countDown() { //倒计时函数
        t = setTimeout(this.countDown, 1000);
        let countDownArr = [];
        let obj = null;
        if (this.data.detail.surplus > 0) { //未结束
            this.time();
            // console.log('surplus', this.data.detail.surplus);
            // t = setTimeout(this.countDown, 1000);
            const time = app.initTime(this.data.detail.surplus);
            // console.log('time', time);
            obj = {
                day: time[0].day,
                hou: time[0].hou,
                min: time[0].min,
                sec: time[0].sec
            };
            t;
            // this.setData({'detail.time': app.initTime(this.data.detail.surplus),})
        } else {
            obj = {
                day: '00',
                hou: '00',
                min: '00',
                sec: '00'
            };
            clearTimeout(t);
            this.end();
        }
        countDownArr.push(obj);
        // 渲染,然后每隔一秒执行一次倒计时函数
        this.setData({
            // countDownList: countDownArr,
            'detail.time': countDownArr,
        });
        // console.log('time',this.data.detail.time);
    },
    //倒计时结束
    end() {
        const self = this;
        let url = '/portal/Active/activity';
        let params = {
            activeId: self.data.detail_id,
        };
        let header = {
            "token": wx.getStorageSync('token')
        };
        app.post(url, params, {}).then((res) => {
            console.log('倒计时结束', res);
            if (+res.code === 1) {
                // wx.showToast({title:'拼活动结束',icon:'success'})
            }
        })
    },
    //导航
    getNavigate() {
        const self = this;
        const arr = self.data.detail.latng.split(',');
        console.log('arr',arr,arr[0],arr[1]);

        wx.openLocation({//​使用微信内置地图查看位置。
            latitude: +arr[0],//要去的纬度-地址
            longitude: +arr[1],//要去的经度-地址
            // lat:39.086437,
            // lng:117.122583,
            name: self.data.detail.address,
            address: self.data.detail.address
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log('详情options', options);
        this.setData({detail_id:+options.id?+options.id:''});
    },
    //点击分享
    share() {
        // this.drawCard();
        console.log(this.data.can_save);
        this.setData({can_save: false});
        // wx.showLoading({title:'加载中',mask:true});
        this.drawCard();
        console.log(this.data.can_save);
        // if(this.data.can_save) {
        //     wx.hideLoading({});
            this.setData({show_canvas: true});
        // }
    },
    hideCanvasMask() {
        // this.setData({show_canvas: true});
    },
    //关闭分享卡片
    closeCanvas() {
        this.setData({show_canvas: false});
        // wx.navigateBack({delta:1})
    },
    //保存图片到手机相册
    saveToPhone() {
        this.getPower();
    },
    //图片链接转成本地路径
    getLocalImage() {
        const self = this;
        wx.getImageInfo({
            src: self.data.detail.banner[0],
            success(res) {
                console.log('图片链接转成本地路径',res);
                self.setData({local_img:res.path})
                self.drawCard()
                console.log(res.width)
                console.log(res.height)
            }
        })
    },
    drawCard() {
        console.log('开始画');
        const self = this;
        //先创建一个画布
        const ctx = wx.createCanvasContext("canvas");
        //填充背景色
        ctx.fillStyle = '#fff';
        ctx.fillRect(0, 0, 300, 150);
        console.log(self.data.local_img);
        //将图片转化为画布
        ctx.drawImage(self.data.local_img, 0, 0, 330, 150);
        console.log(111);
        // ctx.drawImage("../../../../images/tou.png", 20, 166, 30, 30);//头像
        // ctx.drawImage("../../../../images/code.jpg", 220, 209, 60, 60);//头像
        //用户名字
        ctx.setFontSize(13);
        ctx.setFillStyle('#333333');
        ctx.textAlign = "left";
        console.log(111);
        ctx.fillText(self.data.detail.userName, 69, 187);
        ctx.restore();
        //签到天数
        ctx.setFontSize(17);
        ctx.setFillStyle('#2C82E6');
        ctx.fillText('连续', 25, 229);
        ctx.restore();
        ctx.setFillStyle('#2C82E6');
        ctx.font = 'bold 22px sans-serif';
        ctx.fillText(self.data.count, 63, 229);
        ctx.restore();
        ctx.setFontSize(17);
        ctx.setFillStyle('#2C82E6');
        ctx.font = 'normal 17px sans-serif';
        ctx.fillText('天签到', 80, 229);
        ctx.restore();

        const text = self.data.detail.content;
        // ctx.fillText('此处放文案此处放文案此处放文案此处放文案此处放文案此处放文案此处放文案', 25, 252,200);
        // ctx.restore();
        var chr = text.split("");//这个方法是将一个字符串分割成字符串数组
        var temp = "";
        var row = [];
        ctx.setFontSize(13);
        ctx.setFillStyle('#333333');
        for (var a = 0; a < chr.length; a++) {
            if (ctx.measureText(temp).width < 170) {
                temp += chr[a];
            }
            else {
                a--; //这里添加了a-- 是为了防止字符丢失,效果图中有对比
                row.push(temp);
                temp = "";
            }
        }
        row.push(temp);

        //如果数组长度大于2 则截取前两个
        if (row.length > 2) {
            var rowCut = row.slice(0, 2);
            var rowPart = rowCut[1];
            var test = "";
            var empty = [];
            for (var a = 0; a < rowPart.length; a++) {
                if (ctx.measureText(test).width < 170) {
                    test += rowPart[a];
                }
                else {
                    break;
                }
            }
            empty.push(test);
            var group = empty[0] + "...";//这里只显示两行,超出的用...表示
            rowCut.splice(1, 1, group);
            row = rowCut;
        }
        for (var b = 0; b < row.length; b++) {
            ctx.fillText(row[b], 25, 252 + b * 16, 170);
        }
        ctx.restore();

        // ctx.drawImage("../../../../images/home-banner.png", 0, 0, 250, 150)
        //成功执行,draw方法中进行回调
        ctx.draw(true, function () {
                // wx.hideLoading();
            console.log("draw callback success");
            self.setData({can_save: true,show_canvas: true});
            console.log(self.data.can_save);
        })
    },
    //获取保存到相册的权限
    getPower() {
        const that = this;
        //保存临时图片
        wx.getSetting({
            success(res) {
                if (!res.authSetting['scope.writePhotosAlbum']) {
                    wx.authorize({
                        scope: 'scope.writePhotosAlbum',
                        success() { //这里是用户同意授权后的回调
                            that.savePic();
                        },
                        fail() { //这里是用户拒绝授权后的回调
                            wx.showModal({
                                title: '提示',
                                content: '若不打开授权,则无法将图片保存在相册中!',
                                showCancel: true,
                                cancelText: '暂不授权',
                                cancelColor: '#000000',
                                confirmText: '去授权',
                                confirmColor: '#3CC51F',
                                success: function (res) {
                                    if (res.confirm) {
                                        wx.openSetting({
                                            //调起客户端小程序设置界面,返回用户设置的操作结果。
                                            success: function (ret) {
                                                if (ret.authSetting["scope.writePhotosAlbum"] == true) {
                                                    wx.showToast({
                                                        title: '授权成功',
                                                        icon: 'none',
                                                        duration: 1000
                                                    });
                                                    //再次授权,保存到相册
                                                    that.savePic();
                                                } else {
                                                    wx.showToast({
                                                        title: '授权失败',
                                                        icon: 'none',
                                                        duration: 1000
                                                    })
                                                }
                                            }
                                        })
                                    } else {
                                        wx.showToast({
                                            title: '授权失败',
                                            icon: 'none',
                                            duration: 1000
                                        })
                                        // console.log('用户点击取消')
                                    }
                                }
                            })
                        }
                    })
                } else { //用户已经授权过了
                    console.log('用户已经授权过了')
                    that.savePic();
                }
            }
        });

    },
    savePic() {
        if(this.data.can_save) {
            wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: 300,
                height: 300,
                destWidth: 300,
                destHeight: 300,
                canvasId: 'canvas',
                fileType: 'jpg',
                quality:1,
                success: function (res) {
                    console.log("get tempfilepath(success) is:", res.tempFilePath);
                    //将图片保存在系统相册中(应先获取权限,)
                    wx.saveImageToPhotosAlbum({
                        filePath: res.tempFilePath,
                        success(res) {
                            wx.showToast({title:'保存成功',icon:'none'});
                            console.log("save photo is success")
                        },
                        fail: function () {
                            wx.showToast({title:'保存失败',icon:'none'});
                            console.log("save photo is fail")
                        }
                    })
                },
                fail: function () {
                    console.log('get tempfilepath is fail')
                }
            })
        }else {
            wx.showLoading({title:'加载中',icon:'loading',duration:1000})
        }

    },
    onShareAppMessage: function() {
        let self = this;
        // 设置菜单中的转发按钮触发转发事件时的转发内容
        var shareObj = {
            title: '火柴西路', // 默认是小程序的名称(可以写slogan等)
            path: '', // 默认是当前页面,必须是以‘/’开头的完整路径
            imageUrl: '', //自定义图片路径,可以是本地文件路径、代码包文件路径或者网络图片路径,支持PNG及JPG,不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
            success: function(res) {
                if (res.errMsg == 'shareAppMessage:ok') {}
            },
            fail: function() {
                // 转发失败之后的回调
                if (res.errMsg == 'shareAppMessage:fail cancel') {
                    // 用户取消转发
                } else if (res.errMsg == 'shareAppMessage:fail') {
                    // 转发失败,其中 detail message 为详细失败信息
                }
            },
            complete: function() {
                // 转发结束之后的回调(转发成不成功都会执行)
            }
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getDetail();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        clearTimeout(t)
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
})