editServiceInvoice.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // pages/invoice/editInvoice/editInvoice.js
  2. const app = getApp();
  3. const api = app.api;
  4. import WxValidate from '../../../utils/WxValidate'
  5. import {pointNum} from "../../../utils/util"
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. agreement:true,
  12. invoiceInfo:null,
  13. pickerInvoiceKindIndex:0,
  14. pickerInvoiceKind:[
  15. {
  16. name:"个人",
  17. value:0,
  18. },
  19. {
  20. name:"单位名称",
  21. value:1,
  22. }
  23. ],
  24. formData: {
  25. invoiceType:0, //写死普通发票类型
  26. buyIdNumber:"", //购买方纳税人识别号
  27. buyName:"", //购买方
  28. invoiceKind:1, //0普通开票 1服务费开票
  29. invoiceOrderId:null, //订单id
  30. /*receiverAddress:"", //收件人地址
  31. receiverName:"", //收件人姓名
  32. receiverPhoneNumber:"", //收件人电话*/
  33. },
  34. rules: {
  35. /*buyIdNumber: {
  36. required: true
  37. },
  38. buyName: {
  39. required: true
  40. },
  41. invoiceKind:{
  42. required: true
  43. },
  44. receiverAddress:{
  45. required: true,
  46. }*/
  47. },
  48. messages: {
  49. /* buyIdNumber: {
  50. required: '请填写纳税人识别号'
  51. },
  52. buyName: {
  53. required: '请填写发票抬头'
  54. },
  55. receiverAddress:{
  56. required: '请填写发票收货地址',
  57. }*/
  58. },
  59. isSubmit:false
  60. },
  61. /**
  62. * 生命周期函数--监听页面加载
  63. */
  64. onLoad: function (options) {
  65. const { rules, messages } = this.data;
  66. this.WxValidate = new WxValidate(rules, messages);
  67. console.log('WxValidate',this.WxValidate);
  68. this.setData({
  69. id:options.id,
  70. 'formData.invoiceOrderId':options.id||null,
  71. 'formData.invoiceId':options.invoiceId||null
  72. })
  73. wx.hideShareMenu();
  74. },
  75. /**
  76. * 生命周期函数--监听页面初次渲染完成
  77. */
  78. onReady: function () {
  79. },
  80. /**
  81. * 生命周期函数--监听页面显示
  82. */
  83. onShow: function () {
  84. this.setData({
  85. invoiceInfo: wx.getStorageSync('invoiceInfo')
  86. })
  87. },
  88. changeAgreement(){
  89. this.setData({
  90. agreement:!this.data.agreement
  91. });
  92. },
  93. /** input change value**/
  94. changeInput(e){
  95. console.log(e);
  96. const id = e.currentTarget.id;
  97. const number = e.currentTarget.dataset.number;
  98. let value = e.detail.value;
  99. if(number=='pointNum'){
  100. value=pointNum(value)
  101. }
  102. this.setData({
  103. [id]: value
  104. });
  105. },
  106. /** pick value**/
  107. bindPickerChangeKind: function(e) {
  108. console.log('picker发送选择改变,携带值为', e.detail.value)
  109. this.setData({
  110. pickerInvoiceKindIndex: e.detail.value,
  111. })
  112. },
  113. /** 保存数据 **/
  114. saveData() {
  115. const {formData,pickerInvoiceKindIndex,agreement,isSubmit} = this.data;
  116. if(isSubmit){
  117. return
  118. }
  119. // 传入表单数据,调用验证方法
  120. if(pickerInvoiceKindIndex==0){
  121. if(formData.buyName==''){
  122. wx.showToast({
  123. title: '请输入开票个人姓名',
  124. icon: 'none',
  125. duration: 2000
  126. });
  127. return false
  128. }
  129. }else{
  130. if(formData.buyName==''){
  131. wx.showToast({
  132. title: '请输入开票单位名称',
  133. icon: 'none',
  134. duration: 2000
  135. });
  136. return false
  137. }
  138. if(formData.buyIdNumber==''){
  139. wx.showToast({
  140. title: '纳税人识别号',
  141. icon: 'none',
  142. duration: 2000
  143. });
  144. return false
  145. }
  146. }
  147. if (!this.WxValidate.checkForm(formData)) {
  148. const error = this.WxValidate.errorList[0];
  149. wx.showToast({
  150. title: error.msg,
  151. icon: 'none',
  152. duration: 2000
  153. })
  154. return false
  155. }
  156. if(!agreement){
  157. wx.showToast({
  158. title: '请勾选阅读法律申明和免责申明',
  159. icon: 'none',
  160. duration: 2000
  161. })
  162. return false
  163. }
  164. this.setData({
  165. isSubmit:true
  166. })
  167. app.post(api.addInvoice, formData).then(res => {
  168. wx.showToast({
  169. title: '服务费开票成功',
  170. duration: 2000
  171. });
  172. // const {invoiceId,invoiceOrderId}=res.data;
  173. this.setData({
  174. isSubmit:false,
  175. });
  176. wx.navigateTo({
  177. url:`/pages/invoice/myInvoice/myInvoice`
  178. })
  179. //wx.navigateBack();
  180. }).catch(()=>{
  181. this.setData({
  182. isSubmit:false
  183. })
  184. })
  185. },
  186. /** 获取收货地址 **/
  187. getAddress(){
  188. const settingAddress=function(res){
  189. this.setData({
  190. 'formData.receiverName':res.userName,
  191. 'formData.receiverAddress':`${res.provinceName}${res.cityName}${res.countyName}${res.detailInfo}`,
  192. 'formData.receiverPhoneNumber':res.telNumber,
  193. })
  194. }.bind(this);
  195. wx.chooseAddress({
  196. success:(res)=>{
  197. // console.log(res);
  198. settingAddress(res);
  199. },
  200. fail:(error)=>{
  201. //console.log(error);
  202. if(error.errMsg=="chooseAddress:fail auth deny"){
  203. wx.showModal({
  204. title: '是否授权通讯地址',
  205. content: '需要获取您通讯地址,请确认授权,否则获取地址功能将无法使用',
  206. success: (tip)=> {
  207. if (tip.confirm) {
  208. wx.openSetting({
  209. success: function (data) {
  210. //console.log(data);
  211. if (data.authSetting["scope.address"] === true) {
  212. wx.showToast({
  213. title: '授权成功',
  214. icon: 'success',
  215. duration: 1000
  216. });
  217. //授权成功之后,再调用chooseLocation选择
  218. wx.chooseAddress({
  219. success: (res)=> {
  220. settingAddress(res);
  221. },
  222. })
  223. } else {
  224. wx.showToast({
  225. title: '授权失败',
  226. icon: 'success',
  227. duration: 1000
  228. })
  229. }
  230. }
  231. })
  232. }
  233. }
  234. })
  235. }
  236. }
  237. })
  238. },
  239. /**
  240. * 生命周期函数--监听页面隐藏
  241. */
  242. onHide: function () {
  243. },
  244. /**
  245. * 生命周期函数--监听页面卸载
  246. */
  247. onUnload: function () {
  248. },
  249. /**
  250. * 页面相关事件处理函数--监听用户下拉动作
  251. */
  252. onPullDownRefresh: function () {
  253. },
  254. /**
  255. * 页面上拉触底事件的处理函数
  256. */
  257. onReachBottom: function () {
  258. },
  259. /**
  260. * 用户点击右上角分享
  261. */
  262. onShareAppMessage: function () {
  263. }
  264. })