editSale.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // pages/invoice/editSale/editSale.js
  2. const app = getApp();
  3. const api = app.api;
  4. import WxValidate from '../../../utils/WxValidate'
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. isShowSimple:false,
  11. saleId: null,
  12. formData: {
  13. identityNumber: "",
  14. name:"",
  15. phoneNumber: "",
  16. certifyFileId:""
  17. },
  18. upFileInfo:null,
  19. rules: {
  20. name: {
  21. required: true,
  22. },
  23. phoneNumber: {
  24. required: true,
  25. tel: true,
  26. },
  27. identityNumber: {
  28. required: true,
  29. idcard: true,
  30. },
  31. certifyFileId:{
  32. required: true,
  33. }
  34. },
  35. messages: {
  36. name: {
  37. required: '请输入姓名',
  38. },
  39. phoneNumber: {
  40. required: '请输入手机号',
  41. tel: '请输入正确的手机号',
  42. },
  43. identityNumber: {
  44. required: '请输入身份证号码',
  45. idcard: '请输入正确的身份证号码',
  46. },
  47. certifyFileId:{
  48. required: '请上传身份证',
  49. }
  50. },
  51. isSubmit:false,
  52. },
  53. /**
  54. * 生命周期函数--监听页面加载
  55. */
  56. onLoad: function (options) {
  57. console.log(options)
  58. this.setData({
  59. saleId: options.id || null
  60. });
  61. // if (this.data.saleId) {
  62. // this.getData(this.data.saleId)
  63. // }
  64. const { rules, messages } = this.data;
  65. this.WxValidate = new WxValidate(rules, messages);
  66. console.log('WxValidate',this.WxValidate)
  67. },
  68. changeInput(e){
  69. console.log(e);
  70. const id = e.currentTarget.id;
  71. const value = e.detail.value;
  72. this.setData({
  73. [id]: value
  74. });
  75. wx.hideShareMenu();
  76. },
  77. getData(id) {
  78. app.post(api.findSale, {
  79. saleId: id
  80. }).then(res => {
  81. let { saleId, name, phoneNumber, identityNumber,certifyFileInfo } = res.data;
  82. this.setData({
  83. saleId: saleId,
  84. upFileInfo:certifyFileInfo,
  85. formData: {
  86. name,
  87. phoneNumber,
  88. identityNumber,
  89. certifyFileId:certifyFileInfo.id
  90. }
  91. })
  92. })
  93. },
  94. saveData() {
  95. const {formData,saleId,upFileInfo,isSubmit} = this.data;
  96. if(isSubmit){
  97. return
  98. }
  99. if (saleId) {
  100. formData.saleId = saleId
  101. }
  102. // console.log(this.WxValidate.checkForm(formData));
  103. // 传入表单数据,调用验证方法
  104. if (!this.WxValidate.checkForm(formData)) {
  105. const error = this.WxValidate.errorList[0];
  106. wx.showToast({
  107. title: error.msg,
  108. icon: 'none',
  109. duration: 2000
  110. })
  111. return false
  112. }
  113. console.log(this.WxValidate);
  114. this.setData({
  115. isSubmit:true
  116. })
  117. app.post(api.updateSale, formData).then(res => {
  118. wx.showToast({
  119. title: '添加成功',
  120. duration: 2000
  121. })
  122. this.setData({
  123. isSubmit:false,
  124. });
  125. wx.navigateBack();
  126. }).catch(()=>{
  127. this.setData({
  128. isSubmit:false
  129. });
  130. })
  131. },
  132. chooseFile(){
  133. app.uploadFile().then(res=>{
  134. console.log(res)
  135. const data=JSON.parse(res.data);
  136. this.setData({
  137. upFileInfo:data,
  138. 'formData.certifyFileId':data.id
  139. })
  140. }).catch(error=>{
  141. console.log(error)
  142. });
  143. },
  144. removeFile(){
  145. this.setData({
  146. upFileInfo:null,
  147. 'formData.certifyFileId':'',
  148. })
  149. },
  150. showSimple(){
  151. this.setData({
  152. isShowSimple:true
  153. })
  154. },
  155. hideSimple(){
  156. this.setData({
  157. isShowSimple:false
  158. })
  159. },
  160. /**
  161. * 生命周期函数--监听页面初次渲染完成
  162. */
  163. onReady: function () {
  164. },
  165. /**
  166. * 生命周期函数--监听页面显示
  167. */
  168. onShow: function () {
  169. if (this.data.saleId) {
  170. this.getData(this.data.saleId)
  171. }
  172. },
  173. /**
  174. * 生命周期函数--监听页面隐藏
  175. */
  176. onHide: function () {
  177. },
  178. /**
  179. * 生命周期函数--监听页面卸载
  180. */
  181. onUnload: function () {
  182. },
  183. /**
  184. * 页面相关事件处理函数--监听用户下拉动作
  185. */
  186. onPullDownRefresh: function () {
  187. },
  188. /**
  189. * 页面上拉触底事件的处理函数
  190. */
  191. onReachBottom: function () {
  192. },
  193. /**
  194. * 用户点击右上角分享
  195. */
  196. onShareAppMessage: function () {
  197. }
  198. })