index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. var company = getApp().globalData.company;
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. realname: '',
  8. // cardtype: '身份证', // 注释掉证件类型
  9. // idcard: '', // 注释掉身份证号码
  10. mobile: '',
  11. formctrl: {
  12. realname: '姓名',
  13. // cardtype: '证件类型', // 注释掉证件类型
  14. // idcard: '证件号码', // 注释掉身份证号码
  15. mobile: '手机号码'
  16. },
  17. showTypePicker: false,
  18. columns: ['身份证', '护照', '港澳通行证', '台胞证'], // 注释掉证件类型选择
  19. },
  20. selectCardtype() {
  21. // this.setData({
  22. // showTypePicker: true // 注释掉证件类型选择
  23. // })
  24. },
  25. typePickerEnter(event) {
  26. // const { picker, value, index } = event.detail;
  27. // console.log(picker, value, index);
  28. // console.log(`当前值:${value}, 当前索引:${index}`);
  29. // this.setData({
  30. // cardtype: value,
  31. // showTypePicker: false
  32. // })
  33. },
  34. typePickerCancel() {
  35. // console.log('取消');
  36. // this.setData({
  37. // showTypePicker: false
  38. // })
  39. },
  40. onChange() {
  41. console.log(this.data.realname);
  42. },
  43. checkform() {
  44. let formctrl = this.data.formctrl;
  45. let keys = Object.keys(formctrl);
  46. for (let index = 0; index < keys.length; index++) {
  47. let key = keys[index];
  48. if (this.data[key].trim() == '') {
  49. console.log(key, this.data[key]);
  50. wx.showToast({
  51. title: `请输入${formctrl[key]}`,
  52. icon: 'error'
  53. });
  54. return false;
  55. }
  56. }
  57. // 只检查姓名和手机号码
  58. let checkName = /[\u4e00-\u9fa5]/.test(this.data.realname);
  59. if (!checkName) {
  60. wx.showToast({
  61. title: '请输入正确姓名',
  62. icon: 'none',
  63. duration: 2000
  64. });
  65. return false;
  66. }
  67. let checkMobile = /^1[34578]\d{9}$/.test(this.data.mobile);
  68. if (!checkMobile) {
  69. wx.showToast({
  70. title: '手机号码格式有误',
  71. icon: 'none',
  72. duration: 2000
  73. });
  74. return false;
  75. }
  76. return true;
  77. },
  78. async save() {
  79. let checked = this.checkform();
  80. if (!checked) {
  81. return false;
  82. }
  83. wx.showToast({
  84. title: '添加成功',
  85. icon: 'none',
  86. duration: 1000
  87. });
  88. let formdata = {
  89. realname: this.data.realname,
  90. // cardtype: this.data.cardtype, // 注释掉证件类型
  91. // idcard: this.data.idcard, // 注释掉身份证号码
  92. mobile: this.data.mobile
  93. };
  94. let customers = wx.getStorageSync("customers") || []; // 确保 customers 存在
  95. const existingCustomerIndex = customers.findIndex(customer => customer.realname === this.data.realname);
  96. if (existingCustomerIndex !== -1) {
  97. // 如果客户已经存在,则更新客户信息
  98. customers[existingCustomerIndex] = formdata;
  99. } else {
  100. // 如果客户不存在,则添加新的客户信息
  101. customers.push(formdata);
  102. }
  103. wx.setStorageSync("customers", customers);
  104. setTimeout(() => {
  105. wx.navigateBack({
  106. delta: 1,
  107. });
  108. }, 1000);
  109. },
  110. /**
  111. * 生命周期函数--监听页面加载
  112. */
  113. onLoad: function (options) {
  114. console.log(options);
  115. let { type, name } = options;
  116. if (type == 'edit' && name) {
  117. this.initCustomerInfo(name);
  118. }
  119. },
  120. initCustomerInfo(name) {
  121. let customers = wx.getStorageSync("customers");
  122. if (customers) {
  123. console.log(customers);
  124. customers.forEach((customer, index) => {
  125. if (customer.realname == name) {
  126. this.setData({
  127. realname: customer.realname,
  128. // cardtype: customer.cardtype, // 注释掉证件类型
  129. // idcard: customer.idcard, // 注释掉身份证号码
  130. mobile: customer.mobile,
  131. });
  132. }
  133. });
  134. }
  135. },
  136. /**
  137. * 生命周期函数--监听页面初次渲染完成
  138. */
  139. onReady: function () {},
  140. /**
  141. * 生命周期函数--监听页面显示
  142. */
  143. onShow: function () {},
  144. /**
  145. * 生命周期函数--监听页面隐藏
  146. */
  147. onHide: function () {},
  148. /**
  149. * 生命周期函数--监听页面卸载
  150. */
  151. onUnload: function () {},
  152. /**
  153. * 页面相关事件处理函数--监听用户下拉动作
  154. */
  155. onPullDownRefresh: function () {},
  156. /**
  157. * 页面上拉触底事件的处理函数
  158. */
  159. onReachBottom: function () {},
  160. /**
  161. * 用户点击右上角分享
  162. */
  163. onShareAppMessage: function () {}
  164. });