Newer
Older
miniCreditFactory / pages / piclist / piclist-dywzp.js
bello on 29 May 2020 11 KB init
//获取应用实例
const app = getApp()

var U = require('../../utils/Urls.js')
var Api = require('../../utils/api.js')
var Time = require('../../utils/time.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        order: {},
        tmpPhotoPath: '', //拍照返回的路径
        tmpThumbPath: '', //录像返回的路径
        tmpVideoPath: '',  //录像返回的路径
        tmpRecordPath: '', //录音返回的路径 
        picList: [],              // 图片分组列表  
        listNum: -1,            //选中图片在哪个picList
        position: -1,           // 选中图片在picList的哪个pistion
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log('~~~~~~~~~~~~~~')
        console.log(options.type)
        var pics = JSON.parse(decodeURIComponent(options.model))
        var order = JSON.parse(decodeURIComponent(options.order))
        console.log(pics)

        var list1 = [
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0105", typeSubId: "Z01", picName: "外访小视频", isMust: "0", isSingle: "0" }
        ]
        var list2 = [
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0105", typeSubId: "A01", picName: "小区大门", isMust: "1", isSingle: "1" },
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0105", typeSubId: "A02", picName: "楼宇外观(含楼牌号)", isMust: "1", isSingle: "1" },
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0105", typeSubId: "A03", picName: "单元门牌", isMust: "1", isSingle: "1" },
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0105", typeSubId: "A04", picName: "房屋门牌", isMust: "1", isSingle: "1" },
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0105", typeSubId: "A05", picName: "尽调人员与房屋门牌合影", isMust: "1", isSingle: "1" },
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0105", typeSubId: "A06", picName: "其他房产信息", isMust: "1", isSingle: "0" }
        ]
        var list3 = [
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0105", typeSubId: "B01", picName: "客厅", isMust: "0", isSingle: "0" },
        ]
        var list4 = [
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0105", typeSubId: "C01", picName: "餐厅", isMust: "0", isSingle: "0" },
        ]
        var list5 = [
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0105", typeSubId: "D01", picName: "主卧室", isMust: "0", isSingle: "0" },
        ]
        var list6 = [
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0105", typeSubId: "E01", picName: "次卧室", isMust: "0", isSingle: "0" },
        ]
        var list7 = [
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0105", typeSubId: "F01", picName: "其他卧室", isMust: "0", isSingle: "0" },
        ]
        var list8 = [
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0105", typeSubId: "G01", picName: "书房", isMust: "0", isSingle: "0" },
        ]
        var list9 = [
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0105", typeSubId: "I01", picName: "厨房", isMust: "0", isSingle: "0" },
        ]
        var list10 = [
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0105", typeSubId: "J01", picName: "卫生间", isMust: "0", isSingle: "0" },
        ]
        var list11 = [
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0105", typeSubId: "K01", picName: "其他房间", isMust: "0", isSingle: "0" },
        ]

        // 组装数据,准备插入picList
        var list = []
        list.push(list1)
        list.push(list2)
        list.push(list3)
        list.push(list4)
        list.push(list5)
        list.push(list6)
        list.push(list7)
        list.push(list8)
        list.push(list9)
        list.push(list10)
        list.push(list11)

        this.setData({
            order: order,
            picList: list,
        })


        // 匹配已存在的图片到子列表中
        for (var i = 0; i < pics.length; i++) {
            var d = pics[i]
            if (pics[i]['typeSubId'] == 'Z01') {
                this.data.picList[0].push(pics[i])
            } else if (pics[i]['typeSubId'] == 'A01') {
                this.data.picList[1][0] = pics[i]
            } else if (pics[i]['typeSubId'] == 'A02') {
                this.data.picList[1][1] = pics[i]
            } else if (pics[i]['typeSubId'] == 'A03') {
                this.data.picList[1][2] = pics[i]
            } else if (pics[i]['typeSubId'] == 'A04') {
                this.data.picList[1][3] = pics[i]
            } else if (pics[i]['typeSubId'] == 'A05') {
                this.data.picList[1][4] = pics[i]
            } else if (pics[i]['typeSubId'] == 'A06') {
                this.data.picList[1].push(pics[i])
            } else if (pics[i]['typeSubId'] == 'B01') {
                this.data.picList[2].push(pics[i])
            } else if (pics[i]['typeSubId'] == 'C01') {
                this.data.picList[3].push(pics[i])
            } else if (pics[i]['typeSubId'] == 'D01') {
                this.data.picList[4].push(pics[i])
            } else if (pics[i]['typeSubId'] == 'E01') {
                this.data.picList[5].push(pics[i])
            } else if (pics[i]['typeSubId'] == 'F01') {
                this.data.picList[6].push(pics[i])
            } else if (pics[i]['typeSubId'] == 'G01') {
                this.data.picList[7].push(pics[i])
            } else if (pics[i]['typeSubId'] == 'I01') {
                this.data.picList[8].push(pics[i])
            } else if (pics[i]['typeSubId'] == 'J01') {
                this.data.picList[9].push(pics[i])
            } else if (pics[i]['typeSubId'] == 'K01') {
                this.data.picList[10].push(pics[i])
            } 

        }

        this.setData({
            picList: this.data.picList
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        console.log('img => ' + this.data.tmpPhotoPath)
        console.log('img => ' + this.data.tmpVideoPath)
        // 拍照返回后会刷新页面,在这里处理图片的上传
        if (this.data.tmpPhotoPath || this.data.tmpVideoPath) {

            // 显示加载框
            wx.showLoading({
                title: 'loading...',
            })

            // 获取选中的图片数据
            var selectData = this.data.picList[this.data.listNum][this.data.position]
            if (this.data.listNum == 0){
                // 写入本地录像路径
                selectData['picPath'] = this.data.tmpVideoPath
            } else {
                //写入本地图片路径
                selectData['picPath'] = this.data.tmpPhotoPath
            }
            
            // 清空缓存的图片路径
            this.data.tmpPhotoPath = ''
            this.data.tmpVideoPath = ''

            var that = this

            Api.UploadOSS(this.data.order['pid'], selectData, function (r, sData) {
                // 新增模式, 判断是否需要新增一个空图片
                if (sData['isSingle'] != 1 && r != null) {
                    var addPicData = JSON.parse(JSON.stringify(sData))
                    addPicData['id'] = ''
                    addPicData['picUrl'] = ''
                    addPicData['picId'] = ''
                    addPicData['picPath'] = ''
                    addPicData['picName'] = sData['picName'].split('_')[0]
                    that.data.picList[that.data.listNum].push(addPicData)

                }

                //局部刷新 
                that.setData({
                    ['picList[' + that.data.listNum + ']']: that.data.picList[that.data.listNum]
                })

                that.notifyPrePageRefresh()
                
                console.log('----- result -----')
                console.log(that.data.picList)
            })

        }
    },


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

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        this.setData({
            order: {},
            tmpPhotoPath: '', //拍照返回的路径
            tmpThumbPath: '', //录像返回的路径
            tmpVideoPath: '',  //录像返回的路径
            tmpRecordPath: '', //录音返回的路径 
            picList: [],              // 图片分组列表  
            listNum: -1,            //选中图片在哪个picList
            position: -1,           // 选中图片在picList的哪个pistion
        })
    },

    /**
         * 通知前一页需要刷新数据
         */
    notifyPrePageRefresh() {
        var pages = getCurrentPages();
        var prePage = pages[pages.length - 2]
        prePage.setData({
            enableRefresh: true
        })
    },

    // 打开拍照
    openCamera: function (e) {
        var listNum = e.currentTarget.dataset.listno
        var position = e.currentTarget.dataset.position
        console.log('num => ' + listNum)
        console.log('position=> ' + position)
        // 保存点击的图片位置
        this.setData({
            listNum: listNum,
            position: position,
        })

        console.log('open camera!!!!!!!!!!!!!!! ')
        // 跳转拍照页面
        var type = 1
        // 若是第一个列表,则跳转录像
        if (listNum == 0){
            type = 2
        }
        wx.navigateTo({
            url: '/pages/camera/camera?type='+type,
        })
    },

    // 预览图片
    previewPic: function (e) {
        console.log('preview picture...................')
        var listNum = e.currentTarget.dataset.listno
        var position = e.currentTarget.dataset.position
        console.log('num => ' + listNum)
        console.log('position=> ' + position)
        // 保存点击的图片位置
        this.setData({
            listNum: listNum,
            position: position,
        })
        if (listNum == 0){
            wx.navigateTo({
                url: '../preview/previewphoto?url=&record=&video=' + encodeURIComponent(this.data.picList[listNum][position]['picUrl']),
            })
        } else {
            wx.navigateTo({
                url: '../preview/previewphoto?video=&record=&url=' + encodeURIComponent(this.data.picList[listNum][position]['picUrl']),
            })
        }
      
    },

    // 删除图片
    deletePic: function (e) {
        console.log('delete picture..............')
        var listNum = e.currentTarget.dataset.listno
        var position = e.currentTarget.dataset.position
        console.log('num => ' + listNum)
        console.log('position=> ' + position)
        // 保存点击的图片位置
        this.setData({
            listNum: listNum,
            position: position,
        })

        var that = this;
        // 获取选中的图片数据
        var selectData = this.data.picList[listNum][position]

        wx.showModal({
            title: '提示',
            content: '是否要删除当前图片',
            success(res) {
                if (res.confirm) {
                    console.log('确定')
                    Api.SynOnlinePic(null, selectData, function (r, sData) {
                        // 删除模式
                        if (sData['isSingle'] != 1) {
                            (that.data.picList[that.data.listNum]).splice(that.data.position, 1)
                        }

                        //局部刷新 
                        that.setData({
                            ['picList[' + that.data.listNum + ']']: that.data.picList[that.data.listNum]
                        })

                        that.notifyPrePageRefresh()

                        console.log('----- result -----')
                        console.log(that.data.picList)
                    })
                } else if (res.cancel) {

                }
            }
        })

    }
})