index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. // this.data.realname
  58. // this.data.idcard
  59. let checkName = /[\u4e00-\u9fa5]/.test(this.data.realname);
  60. if (!checkName) {
  61. wx.showToast({
  62. title: '请输入您的真实姓名',
  63. icon: 'none',
  64. duration: 2000
  65. })
  66. return false
  67. }
  68. let checkMobile = /^1[34578]\d{9}$/.test(this.data.mobile);
  69. if (!checkMobile) {
  70. wx.showToast({
  71. title: '手机号码格式有误',
  72. icon: 'none',
  73. duration: 2000
  74. })
  75. return false
  76. }
  77. let checkIdcard = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/gi.test(this.data.idcard)
  78. if (!checkIdcard) {
  79. wx.showToast({
  80. title: '请输入正确的身份证号',
  81. icon: 'none',
  82. duration: 2000
  83. })
  84. return false
  85. }
  86. return true;
  87. },
  88. verifyIdcardName(){
  89. return new Promise((resolve,reject)=>{
  90. wx.request({
  91. url: "https://server.fmode.cn/api/apig/idcard", //请求接口的url
  92. method: "POST", //请求方式
  93. data: {
  94. company: company,
  95. cardNo: this.data.idcard,
  96. realName: this.data.realname
  97. }, //请求参数
  98. header: {
  99. "content-type": "application/json", // 默认值
  100. },
  101. success: (res) => {
  102. if(res.statusCode != '200'){
  103. console.log(res);
  104. resolve(false)
  105. }
  106. console.log(res.data.data.result);
  107. if (res.data.data.result.isok) {
  108. resolve(true)
  109. } else {
  110. resolve(false)
  111. }
  112. },
  113. fail:(err)=>{
  114. console.log(err);
  115. wx.showToast({
  116. title: err,
  117. icon: 'none',
  118. duration: 2000
  119. })
  120. }
  121. })
  122. })
  123. },
  124. async save(){
  125. let checked = this.checkform()
  126. console.log(checked);
  127. if(!checked){
  128. return false
  129. }
  130. let verifyIdcardName = await this.verifyIdcardName()
  131. console.log(verifyIdcardName);
  132. if(!verifyIdcardName){
  133. wx.showToast({
  134. title: '身份校验失败,请输入真实姓名和身份证号',
  135. icon: 'none'
  136. })
  137. return false
  138. }
  139. wx.showToast({
  140. title: '添加成功',
  141. icon: 'none',
  142. duration: 1000
  143. })
  144. let formdata = {
  145. realname:this.data.realname,
  146. cardtype:this.data.cardtype,
  147. idcard:this.data.idcard,
  148. mobile:this.data.mobile
  149. };
  150. let customers = wx.getStorageSync("customers")
  151. if(customers){
  152. console.log(customers);
  153. customers.forEach((customer,index) =>{
  154. if(customer.realname == this.data.realname){
  155. customers[index] = formdata
  156. }
  157. })
  158. }else {
  159. customers = []
  160. customers.push(formdata)
  161. }
  162. wx.setStorageSync("customers",customers)
  163. setTimeout(()=>{
  164. wx.navigateBack({
  165. delta: 1,
  166. })
  167. },1000)
  168. },
  169. /**
  170. * 生命周期函数--监听页面加载
  171. */
  172. onLoad: function (options) {
  173. console.log(options);
  174. let {type,name}=options;
  175. if(type== 'edit' && name){
  176. this.initCustomerInfo(name)
  177. }
  178. },
  179. initCustomerInfo(name){
  180. let customers = wx.getStorageSync("customers")
  181. if(customers){
  182. console.log(customers);
  183. customers.forEach((customer,index) =>{
  184. if(customer.realname == name){
  185. this.setData({
  186. realname: customer.realname,
  187. cardtype: customer.cardtype,
  188. idcard: customer.idcard,
  189. mobile: customer.mobile,
  190. })
  191. }
  192. })
  193. }
  194. },
  195. /**
  196. * 生命周期函数--监听页面初次渲染完成
  197. */
  198. onReady: function () {
  199. },
  200. /**
  201. * 生命周期函数--监听页面显示
  202. */
  203. onShow: function () {
  204. },
  205. /**
  206. * 生命周期函数--监听页面隐藏
  207. */
  208. onHide: function () {
  209. },
  210. /**
  211. * 生命周期函数--监听页面卸载
  212. */
  213. onUnload: function () {
  214. },
  215. /**
  216. * 页面相关事件处理函数--监听用户下拉动作
  217. */
  218. onPullDownRefresh: function () {
  219. },
  220. /**
  221. * 页面上拉触底事件的处理函数
  222. */
  223. onReachBottom: function () {
  224. },
  225. /**
  226. * 用户点击右上角分享
  227. */
  228. onShareAppMessage: function () {
  229. }
  230. })