// pages/invoice/editInvoice/editInvoice.js const app = getApp(); const api = app.api; import WxValidate from '../../../utils/WxValidate' import {pointNum} from "../../../utils/util" Page({ /** * 页面的初始数据 */ data: { agreement:true, invoiceInfo:null, pickerInvoiceKindIndex:0, pickerInvoiceKind:[ { name:"个人", value:0, }, { name:"单位名称", value:1, } ], formData: { invoiceType:0, //写死普通发票类型 buyIdNumber:"", //购买方纳税人识别号 buyName:"", //购买方 invoiceKind:1, //0普通开票 1服务费开票 invoiceOrderId:null, //订单id /*receiverAddress:"", //收件人地址 receiverName:"", //收件人姓名 receiverPhoneNumber:"", //收件人电话*/ }, rules: { /*buyIdNumber: { required: true }, buyName: { required: true }, invoiceKind:{ required: true }, receiverAddress:{ required: true, }*/ }, messages: { /* buyIdNumber: { required: '请填写纳税人识别号' }, buyName: { required: '请填写发票抬头' }, receiverAddress:{ required: '请填写发票收货地址', }*/ }, isSubmit:false }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { const { rules, messages } = this.data; this.WxValidate = new WxValidate(rules, messages); console.log('WxValidate',this.WxValidate); this.setData({ id:options.id, 'formData.invoiceOrderId':options.id||null, 'formData.invoiceId':options.invoiceId||null }) wx.hideShareMenu(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { this.setData({ invoiceInfo: wx.getStorageSync('invoiceInfo') }) }, changeAgreement(){ this.setData({ agreement:!this.data.agreement }); }, /** 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, }) }, /** 保存数据 **/ saveData() { const {formData,pickerInvoiceKindIndex,agreement,isSubmit} = this.data; if(isSubmit){ return } // 传入表单数据,调用验证方法 if(pickerInvoiceKindIndex==0){ if(formData.buyName==''){ wx.showToast({ title: '请输入开票个人姓名', icon: 'none', duration: 2000 }); return false } }else{ if(formData.buyName==''){ wx.showToast({ title: '请输入开票单位名称', icon: 'none', duration: 2000 }); return false } if(formData.buyIdNumber==''){ wx.showToast({ title: '纳税人识别号', icon: 'none', duration: 2000 }); return false } } if (!this.WxValidate.checkForm(formData)) { const error = this.WxValidate.errorList[0]; wx.showToast({ title: error.msg, icon: 'none', duration: 2000 }) return false } if(!agreement){ wx.showToast({ title: '请勾选阅读法律申明和免责申明', icon: 'none', duration: 2000 }) return false } this.setData({ isSubmit:true }) app.post(api.addInvoice, formData).then(res => { wx.showToast({ title: '服务费开票成功', duration: 2000 }); // const {invoiceId,invoiceOrderId}=res.data; this.setData({ isSubmit:false, }); wx.navigateTo({ url:`/pages/invoice/myInvoice/myInvoice` }) //wx.navigateBack(); }).catch(()=>{ this.setData({ isSubmit:false }) }) }, /** 获取收货地址 **/ getAddress(){ const settingAddress=function(res){ this.setData({ 'formData.receiverName':res.userName, 'formData.receiverAddress':`${res.provinceName}${res.cityName}${res.countyName}${res.detailInfo}`, 'formData.receiverPhoneNumber':res.telNumber, }) }.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 }) } } }) } } }) } } }) }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })