const util = require('../../utils/util.js'); const app = getApp() Page({ data: { index: 0, userList: [], userNum: 0, name: "", phone: "", content: "", showImg: [], images: [], fileList: [], files: [], timeArray: [ "请选择有效时间" , "5分钟" , "10分钟" , "15分钟" ], totalFee: "", isCheck: false, click: true }, onLoad: function () { var that = this , userInfo = wx.getStorageSync("userInfo"); that.setData({ name: userInfo.userName, phone: userInfo.mobile }) //第一次进来加载律师 wx.request({ url: app.globalData.serverUrl + '/cloudcard/addLawyer', method: "GET", data: { id : userInfo.id }, success(res) { var lawyerList = res.data.data.lawyerList; for(var i in lawyerList){ var str = app.globalData.fileUrl + lawyerList[i].headPortrait; lawyerList[i].headPortrait = str; } that.setData({ userList: lawyerList, userNum: res.data.data.lawyerCount, totalFee: res.data.data.totalFee }) }, fail: function (res) { wx.showToast({ title: '获取失败!', icon: 'none', duration: 2000 }) } }) }, //添加律师 addLawyer: function(){ if(this.data.userNum <= this.data.userList.length){ wx.showToast({ title: '已达到律师添加上限!', icon: 'none', duration: 2000 }) } else { var that = this; var str = "" , list = that.data.userList; for(var i in list){ str += list[i].id + "," } str = str.substring( 0 , str.length -1); wx.request({ url: app.globalData.serverUrl + '/cloudcard/addLawyer', method: "GET", data: { lawyerIds: str }, success(res) { that.data.userList.push(res.data.data); that.setData({ userList: that.data.userList }) }, fail: function (res) { wx.showToast({ title: '获取失败!', icon: 'none', duration: 2000 }) } }) } }, //移除律师 removeUser: function(e){ const userList = this.data.userList , i = e.target.dataset.index; userList.splice(i,1); this.setData({ userList: userList }) }, //获取接收人 getNameValue: function(e){ this.setData({ name: e.detail.value }) }, //获取手机号 getPhoneValue: function(e){ this.setData({ phone: e.detail.value }) }, //获取合意内容 getContentValue: function(e){ this.setData({ content: e.detail.value }) }, //图片上传 chooseImage: function(e){ var that = this; wx.chooseImage({ count: 9, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success: res => { var list = res.tempFilePaths; wx.showLoading({ title: '上传中...', }) var num = 0; for(var i in list){ if(that.data.images.length + (parseInt(i)+1) > 9){ wx.hideLoading(); wx.showToast({ title: '上传图片不能超过9张!', icon: 'none', duration: 1000 }) return false; } else { var showArray = that.data.showImg; showArray.push(res.tempFilePaths[i]); var array = that.data.images; wx.uploadFile({ url: app.globalData.serverUrl + '/cloudcard/upload', filePath: res.tempFilePaths[i], name: 'file', formData: { 'flag': 0 }, success: function (res) { num ++; var data = JSON.parse(res.data); var msg = { fileUrl: data.data.fileUrl, fileSize: data.data.fileSize, fileName: data.data.fileName, fileType: 0 } array.push(msg); that.setData({ showImg: showArray, images: array }) if(i == list.length-1){ wx.showToast({ title: '上传成功!', icon: 'success', duration: 1000 }) } }, fail: function(res){ num ++; wx.showToast({ title: '上传失败!', icon: 'none', duration: 1000 }) } }); } } if(num == list.length-1){ wx.hideLoading(); } } }) }, //图片预览 handleImagePreview: function(e){ const i = e.target.dataset.index; const images = this.data.showImg; wx.previewImage({ current: images[i], urls: images, }) }, //移除图片 removeImage: function(e){ const showImg = this.data.showImg , images = this.data.images , i = e.target.dataset.index; showImg.splice(i,1); images.splice(i,1); this.setData({ showImg: showImg, images: images }) }, //文件上传 chooseFile: function(e){ var that = this; wx.chooseMessageFile({ count: 10, type: 'file', success: function(res) { var list = res.tempFiles; wx.showLoading({ title: '上传中...', }) var num = 0; for(var i in list){ if(list[i].size < (1024*1024*50)){ var fileList = that.data.fileList; fileList.push(list[i]); var array = that.data.images; wx.uploadFile({ url: app.globalData.serverUrl + '/cloudcard/upload', filePath: list[i].path, name: 'file', formData: { "originalFileName": list[i].name, 'flag': 1 }, success: function(res){ num ++; var data = JSON.parse(res.data); var msg = { fileUrl: data.data.fileUrl, fileSize: data.data.fileSize, fileName: data.data.fileName, fileOld: list[i].path, fileType: 1 } array.push(msg); fileList[i].url = data.data.fileUrl, that.setData({ fileList: fileList, images: that.data.images }) if(i == list.length-1){ wx.showToast({ title: '上传成功!', icon: 'success', duration: 1000 }) } }, fail: function(res){ num++; wx.showToast({ title: '上传失败!', icon: 'none', duration: 1000 }) } }) } else { wx.showToast({ title: '上传文件不得超过50M!', icon: 'none', duration: 1000 }) } } if(num == list.length-1){ wx.hideLoading(); } } }) }, //文件预览 showFile: function(e){ const i = e.target.dataset.index; var that = this , url = that.data.fileList[i].url; // wx.getSystemInfo({ // success: function (res) { // if(res.system.indexOf("iOS") > -1){ // wx.navigateTo({ // url: '../book/pdf?url=' + url // }) // } else { wx.showLoading({ title: '正在打开请稍候...', }) wx.downloadFile({ url: app.globalData.fileUrl + url, success: function (res) { wx.hideLoading() var Path = res.tempFilePath //返回的文件临时地址,用于后面打开本地预览所用 wx.openDocument({ filePath: Path, success: function (res) { console.log('打开成功'); } }) }, fail: function (res) { wx.hideLoading() wx.showToast({ title: '文件打开失败!', icon: 'none', duration: 1000 }) } }) // } // } // }) }, //移除文件 removeFile: function(e){ const fileList = this.data.fileList , i = e.currentTarget.dataset.index; var images = this.data.images; for(var j in images){ if(images[j].fileOld == fileList[i].path){ images.splice(j,1); break; } } fileList.splice(i,1); this.setData({ fileList: fileList, images: images }) }, //时间选择 bindPickerChange: function(e){ this.setData({ index: e.detail.value }) }, //查看确认书 book: function(){ wx.navigateTo({ url: '../book/book' }) }, //阅读确认书 checkboxChange: function(e){ var ischeck; if(e.detail.value.length == 0){ ischeck = false; } else { ischeck = true; } this.setData({ isCheck: ischeck }) }, //保存并支付 // toPay formSubmit: function(e){ var myreg = /^1[0-9]{10,11}$/ , openId = wx.getStorageSync("openId"); var users = "",time = 0; if(this.data.userList.length == 0){ wx.showToast({ title: '云证律师不能为空', icon: 'none', duration: 1000 }) return false; } else { for(var i in this.data.userList){ users += this.data.userList[i].id + "," } users = users.substring( 0 , users.length -1); } if(this.data.index == 0){ time = 0; } else if(this.data.index == 1){ time = 5; } else if(this.data.index == 2){ time = 10; } else if(this.data.index == 3){ time = 15; } if (this.data.name == "") { wx.showToast({ title: '姓名不能为空', icon: 'none', duration: 1000 }) return false; } else if (this.data.phone == "") { wx.showToast({ title: '手机号不能为空', icon: 'none', duration: 1000 }) return false; } else if (!myreg.test(this.data.phone)) { wx.showToast({ title: '请输入正确的手机号', icon: 'none', duration: 1000 }) return false; } else if(time == 0){ wx.showToast({ title: '请选择有效时间', icon: 'none', duration: 1000 }) return false; } else if(!this.data.isCheck){ wx.showToast({ title: '请阅读《网律云证用户确认书》', icon: 'none', duration: 1000 }) return false; } else { var that = this; if(that.data.click){ that.setData({ click: false }) wx.request({ url: app.globalData.serverUrl + "/cloudcard/create", method: "POST", data: { openId: openId, lawyerIds: users, initiatorName: this.data.name, initiatorMobile: this.data.phone, content: this.data.content, validTime: time, fileUploads: JSON.stringify(this.data.images), formId: e.detail.formId }, header: { "Content-Type": "application/x-www-form-urlencoded" }, success: function (res) { if(res.data.code == "ok"){ wx.showToast({ title: '创建成功!', icon: 'success', duration: 2000 }) var item = res.data.data; wx.requestPayment({ timeStamp: item.timeStamp, nonceStr: item.nonceStr, package: item.package, signType: item.signType, paySign: item.paySign, success (res) { console.log("成功") that.setData({ click: true }) console.log(item) setTimeout(function() { wx.reLaunch({ url: '../share/share?id=' + item.cloudCardOrder.id }) }, 200) }, fail (res) { console.log("失败") that.setData({ click: true }) wx.showToast({ title: "支付失败", icon: 'none', duration: 2000 }) setTimeout(function() { wx.reLaunch({ url: '../cloud/cloud' }) }, 200) } }) } else { that.setData({ click: true }) wx.showToast({ title: res.data.msg, icon: 'none', duration: 2000 }) } }, fail: function (res) { that.setData({ click: true }) wx.showToast({ title: '提交失败!', icon: 'none', duration: 2000 }) } }) } else { wx.showToast({ title: '保存中,请勿重复提交', icon: 'none', duration: 1000 }) } } } })