Newer
Older
miniCreditFactory / pages / piclist / piclist-sfzm.js
bello on 2 Jun 2020 10 KB add water text
//获取应用实例
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: [],              // 图片分组列表  
        // picListA: [],           // 身份证
        // picListB: [],           //户口本
        // picListC: [],           //结婚证
        // picListE: [],           //婚姻/单身状况声明
        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: "0101", typeSubId: "A01", picName: "身份证-正面", isMust: "1", isSingle: "1" },
            {
                pid: order['pid'], bussPk: order['bussPk'], typeId: "0101", typeSubId: "A02", picName: "身份证-反面", isMust: "1", isSingle: "1"
            },
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0101", typeSubId: "A03", picName: "共借人身份证", isMust: "0", isSingle: "0" },
        ]
        var list2 = [
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0101", typeSubId: "B01", picName: "户口本-首页", isMust: "1", isSingle: "1" },
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0101", typeSubId: "B02", picName: "户口本-户主页", isMust: "1", isSingle: "1" },
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0101", typeSubId: "B03", picName: "户口本-索引页", isMust: "1", isSingle: "1" },
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0101", typeSubId: "B04", picName: "户口本-个人页", isMust: "1", isSingle: "1" },
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0101", typeSubId: "B05", picName: "户口本-配偶页", isMust: "0", isSingle: "1" },
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0101", typeSubId: "B06", picName: "户口本-子女页", isMust: "0", isSingle: "0" },
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0101", typeSubId: "B07", picName: "户口本-其他", isMust: "0", isSingle: "0" },
        ]
        var list3 = [
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0101", typeSubId: "C01", picName: "结婚证", isMust: "0", isSingle: "0" },
        ]
        var list4 = [
            { pid: order['pid'], bussPk: order['bussPk'], typeId: "0101", typeSubId: "E01", picName: "婚姻-单身状况声明", isMust: "0", isSingle: "0" },
        ]

        // 组装数据,准备插入picList
        var list = []
        list.push(list1)
        list.push(list2)
        list.push(list3)
        list.push(list4)

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


        // 匹配已存在的图片到子列表中
        for (var i = 0; i < pics.length; i++) {
            var d = pics[i]
            if (pics[i]['typeSubId'] == 'A01') {
                this.data.picList[0][0] = pics[i]
            } else if (pics[i]['typeSubId'] == 'A02') {
                this.data.picList[0][1] = pics[i]
            } else if (pics[i]['typeSubId'] == 'A03') {
                this.data.picList[0].push(pics[i])
            } else if (pics[i]['typeSubId'] == 'B01') {
                this.data.picList[1][0] = pics[i]
            } else if (pics[i]['typeSubId'] == 'B02') {
                this.data.picList[1][1] = pics[i]
            } else if (pics[i]['typeSubId'] == 'B03') {
                this.data.picList[1][2] = pics[i]
            } else if (pics[i]['typeSubId'] == 'B04') {
                this.data.picList[1][3] = pics[i]
            } else if (pics[i]['typeSubId'] == 'B05') {
                this.data.picList[1][4] = pics[i]
            } else if (pics[i]['typeSubId'] == 'B06' || pics[i]['typeSubId'] == 'B07') {
                this.data.picList[1].push(pics[i])
            } else if (pics[i]['typeSubId'] == 'C01') {
                this.data.picList[2].push(pics[i])
            } else if (pics[i]['typeSubId'] == 'E01') {
                this.data.picList[3].push(pics[i])
            }

        }

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

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

    },

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

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

            // 获取选中的图片数据
            var selectData = this.data.picList[this.data.listNum][this.data.position]
            // 写入本地图片路径
            selectData['picPath'] = this.data.tmpPhotoPath
            // 清空缓存的图片路径
            this.data.tmpPhotoPath = ''

            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]
                })

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

                that.notifyPrePageRefresh()

            })


        }
    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    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!!!!!!!!!!!!!!! ')
        // 跳转拍照页面
        wx.navigateTo({
            url: '/pages/camera/camera?type=1',
        })
    },

    // 预览图片
    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,
        })

        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]
                        })

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

                        that.notifyPrePageRefresh()
                    })
                } else if (res.cancel) {

                }
            }
        })

    }
})