// pages/invoice/editInvoice/editInvoice.js const app = getApp(); const api = app.api; import WxValidate from '../../../utils/WxValidate' import {pointNum} from "../../../utils/util" Page({ /** * 页面的初始数据 */ data: { saleInfo:null, pickerInvoiceKindIndex:0, pickerInvoiceKind:[ { name:"增值税普通发票", value:0, }, // { // name:"增值税开票", // value:1, // } ], /* pickerInvoiceKind:[ { name:"普通开票", value:0, }, { name:"服务费开票", value:1, } ],*/ pickerServeIndex:"", // pickerServe:[ // '会务服务', // '展览展示服务', // '文化艺术交流策划', // '企业管理咨询', // '商务信息咨询', // '市场信息咨询与调查', // '企业营销策划', // '企业形象策划' // ], pickerServe:[ '绘画作品', '雕塑作品', '装置作品', '综合作品', '摄影作品', '新媒体作品', '影像作品', '陶瓷作品' ], invoiceBuySearchValue:'', invoiceBuyFocus:false, invoiceBuyList:[], isShowInvoiceBuyList:false, searchTimer:null, formData: { amount:"", //合计开票金额 buyIdNumber:"", //购买方纳税人识别号 buyName:"", //购买方 invoiceKind:0, //0普通开票 1服务费开票 invoiceOrderId:20, //订单id invoiceType:0, //发票类型 provinceName:"",// cityName:"", expAreaName:"", address:"", // receiverAddress:"", //收件人地址 /* receiverName:"", //收件人姓名 receiverPhoneNumber:"", //收件人电话*/ name:"",//收件人姓名 mobile:"",//收件人姓名 serviceContentInfo:{ serverName:"", number:1, }, saleId:"", //销售方id saleIdNumber:"", //销售方身份证 saleName:"", //销售方信息 salePhoneNumber:"", //销售方电话 }, rules: { invoiceType:{ required: true, }, myServerName:{ required: true, }, buyName: { required: true }, buyIdNumber: { required: true, egAndNumber: true, }, amount: { required: true, number:true, }, invoiceKind:{ required: true }, address:{ required: true, }, saleId:{ required: true } }, messages: { invoiceType:{ required: '请选择发票类型', }, myServerName:{ required: '请选择服务内容', }, amount: { required: '请填写开票金额', number:"请输入正确的开票金额" }, buyIdNumber: { required: '请填写纳税人识别号', egAndNumber: '税号只能输入数字和英文' }, buyName: { required: '请填写发票抬头' }, invoiceKind:{ required: '请填写开票种类', }, address:{ required: '请填写发票收货地址', }, saleId:{ required: '请选择销售方' } }, isValidate:false, isSubmit:false }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { const { rules, messages } = this.data; this.WxValidate = new WxValidate(rules, messages); console.log('WxValidate',this.WxValidate); this.setData({ invoiceId:options.invoiceId||'', invoiceOrderId:options.id||'', 'formData.invoiceId':options.invoiceId||'', 'formData.invoiceOrderId':options.id||'' },()=>{ //有提交过发票 未更改状态获取之前记录 if(options.invoiceId&&options.invoiceId!=""){ this.getData(); } }); wx.hideShareMenu(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { this.getInvoiceBuy(null); this.getSaleList(); }, /** input change value**/ changeInput(e){ console.log(e); const id = e.currentTarget.id; const number = e.currentTarget.dataset.number; let value = e.detail.value; if(number=='pointNum'){ value=pointNum(value) } this.setData({ [id]: value }); }, /** pick value**/ bindPickerChangeKind: function(e) { console.log('picker发送选择改变,携带值为', e.detail.value) this.setData({ pickerInvoiceKindIndex: e.detail.value, 'formData.invoiceType':e.detail.value },()=>{ this.validateForm() }) }, bindPickerChangeServe: function(e) { console.log('picker发送选择改变,携带值为', e.detail.value) this.setData({ pickerServeIndex: e.detail.value, 'formData.myServerName':e.detail.value, 'formData.serviceContentInfo.serverName':this.data.pickerServe[e.detail.value] },()=>{ this.validateForm() }) }, /**已提交获取数据**/ getData(){ const {invoiceId,invoiceOrderId}=this.data; app.post(api.queryOrderInvoiceInfo,{ id:invoiceId, invoiceOrderId }).then(res=>{ const { invoiceType, serviceContentInfo, saleId, saleIdNumber, saleName, salePhoneNumber, buyName, buyIdNumber, invoiceAmount, provinceName, cityName, expAreaName, address, mobile, name, }=res.data; this.setData({ pickerInvoiceKindIndex:invoiceType, 'formData.invoiceType':invoiceType, 'formData.serviceContentInfo.':{ serverName:serviceContentInfo.serverName, number:1, }, 'formData.myServerName':serviceContentInfo.serverName, 'formData.provinceName': provinceName, 'formData.cityName': cityName, 'formData.expAreaName': expAreaName, 'formData.address': address, 'formData.mobile':mobile, 'formData.name':name, 'formData.amount':invoiceAmount/100, 'formData.buyName':buyName, 'formData.buyIdNumber':buyIdNumber, 'formData.saleId':saleId, //销售方id 'formData.saleIdNumber':saleIdNumber, //销售方身份证 'formData.saleName':saleName, //销售方信息 'formData.salePhoneNumber':salePhoneNumber, //销售方电话 },()=>{ this.validateForm() }) }) }, validateForm(showInfo=false){ const {formData} = this.data; const submitFormData=Object.assign({},formData); let flag=true; // 传入表单数据,调用验证方法 if (!this.WxValidate.checkForm(submitFormData)) { flag=false if(showInfo===true){ const error = this.WxValidate.errorList[0]; wx.showToast({ title: error.msg, icon: 'none', duration: 2000 }) } } if (submitFormData.amount< 5000){ flag=false if(showInfo===true) { wx.showToast({ title: '开票金额不能小于5000元', icon: 'none', duration: 2000 }) } } this.setData({ isValidate:flag }); return flag }, /** 保存数据 **/ saveData() { const {formData,isSubmit} = this.data; const submitFormData=Object.assign({},formData) if(isSubmit){ return } // // 传入表单数据,调用验证方法 // if (!this.WxValidate.checkForm(submitFormData)) { // const error = this.WxValidate.errorList[0]; // wx.showToast({ // title: error.msg, // icon: 'none', // duration: 2000 // }) // return false // } // if (submitFormData.amount< 5000){ // wx.showToast({ // title: '开票金额不能小于5000元', // icon: 'none', // duration: 2000 // }) // return false // } // //console.log(this.WxValidate); let flag=this.validateForm(true); if(!flag){ return; } //元转成分单位 submitFormData.amount= parseInt(submitFormData.amount*100); console.log('转成分',submitFormData.amount) this.setData({ isSubmit:true }); app.post(api.addInvoice, submitFormData).then(res => { wx.showToast({ title: '添加成功', duration: 2000 }); const {invoiceId,invoiceOrderId}=res.data; this.setData({ isSubmit:false, }); wx.navigateTo({ url:`/pages/invoice/preview/preview?invoiceId=${invoiceId}&invoiceOrderId=${invoiceOrderId}` }) //wx.navigateBack(); }).catch(()=>{ this.setData({ isSubmit:false }) }) }, /** 获取收货地址 **/ getAddress(){ const settingAddress=function(res){ this.setData({ 'formData.name':res.userName, 'formData.provinceName':res.provinceName, 'formData.cityName': res.cityName, 'formData.expAreaName':res.countyName, 'formData.address':res.detailInfo, 'formData.mobile':res.telNumber, },()=>{ this.validateForm() }) }.bind(this); wx.chooseAddress({ success:(res)=>{ // console.log(res); settingAddress(res); }, fail:(error)=>{ //console.log(error); if(error.errMsg=="chooseAddress:fail auth deny"){ wx.showModal({ title: '是否授权通讯地址', content: '需要获取您通讯地址,请确认授权,否则获取地址功能将无法使用', success: (tip)=> { if (tip.confirm) { wx.openSetting({ success: function (data) { //console.log(data); if (data.authSetting["scope.address"] === true) { wx.showToast({ title: '授权成功', icon: 'success', duration: 1000 }); //授权成功之后,再调用chooseLocation选择 wx.chooseAddress({ success: (res)=> { settingAddress(res); }, }) } else { wx.showToast({ title: '授权失败', icon: 'success', duration: 1000 }) } } }) } } }) } } }) }, /** 跳转销售方 **/ goSaleList(){ wx.navigateTo({ url:`/pages/invoice/saleList/saleList?id=${this.data.formData.saleId}` }) }, /**获取销售方信息**/ getSaleList(){ const saleInfo=wx.getStorageSync('saleInfo'); if(saleInfo){ let {saleId,identityNumber,name,phoneNumber}=saleInfo; this.setData({ 'formData.saleId':saleId, //销售方id 'formData.saleIdNumber':identityNumber, //销售方身份证 'formData.saleName':name, //销售方信息 'formData.salePhoneNumber':phoneNumber, //销售方电话 },()=>{ this.validateForm() }) } wx.removeStorageSync('saleInfo') }, /** * 触发发票抬头列表input * */ changeInputInvoiceBuy(e){ const {value}=e.detail; let {searchTimer}=this.data; this.setData({ 'formData.buyName':value },()=>{ clearInterval(searchTimer) searchTimer=setTimeout(()=>{ this.getInvoiceBuy(value); },500); this.setData({ searchTimer:searchTimer }) }) }, setInvoiceBuyValue(e){ const {name,code}=e.currentTarget.dataset; console.info(name) console.info(code) // todo: 税号有时带出不出来 先加一个延时测试一下 setTimeout(()=>{ this.setData({ //invoiceBuySearchValue:value, 'formData.buyName':name, 'formData.buyIdNumber':code },()=>{ this.validateForm() }); },30) }, showInvoiceBuyList() { this.setData({ isShowInvoiceBuyList:true, invoiceBuyFocus:true }); }, hideInvoiceBuyList() { this.setData({ isShowInvoiceBuyList:false, invoiceBuyFocus:false },()=>{ this.validateForm() }); }, /** * 获取发票抬头列表 * **/ getInvoiceBuy(name){ app.post(api.queryInvoiceBuy,{ buyLike:name, //购买方纳税人识别号 }).then(res=>{ this.setData({ invoiceBuyList:res.data }); }) }, /** * 返回上一级 */ back(){ wx.navigateBack(); }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })