real_name.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // pages/real_name/real_name.js
  2. const app = getApp();
  3. const util = require('../../utils/utils.js');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. frameShow: false,
  10. cardVal: '',//身份证号
  11. nameVal: '',//姓名
  12. mobile:''
  13. },
  14. onLoad: function (options) {
  15. this.setData({
  16. mobile: my.getStorageSync({'key':'userInfo'}).data.mobile
  17. })
  18. },
  19. onReady: function () {
  20. },
  21. onShow: function () {
  22. },
  23. onHide: function () {
  24. },
  25. onUnload: function () {
  26. },
  27. onPullDownRefresh: function () {
  28. },
  29. onReachBottom: function () {
  30. },
  31. onShareAppMessage: function () {
  32. },
  33. frameClose: function () {
  34. let that = this
  35. that.setData({
  36. frameShow: false
  37. })
  38. },
  39. //验证名字
  40. ckName(e) {
  41. this.setData({
  42. nameVal: e.detail.value
  43. })
  44. },
  45. name(e) {
  46. this.setData({
  47. nameVal: e.detail.value
  48. })
  49. },
  50. namefirm(e) {
  51. this.setData({
  52. nameVal: e.detail.value
  53. })
  54. },
  55. //验证身份证号
  56. ckCard(e) {
  57. this.setData({
  58. cardVal: e.detail.value
  59. })
  60. },
  61. input(e) {
  62. this.setData({
  63. cardVal: e.detail.value
  64. })
  65. },
  66. valuefirm(e) {
  67. this.setData({
  68. cardVal: e.detail.value
  69. })
  70. },
  71. // sureSubmit:function(){
  72. // my.reLaunch({
  73. // url: '/pages/index/index',
  74. // })
  75. // },
  76. changeMobile: function () {
  77. my.navigateTo({
  78. url: '/pages/my_account/my_account',
  79. })
  80. },
  81. //提交申请按钮
  82. sureSubmit: util.throttle(function () {
  83. let reg = /(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/;
  84. let reg1 = /^[\u4E00-\u9FA5]{2,4}$/;
  85. var that = this;
  86. if (!reg1.test(this.data.nameVal)) {
  87. my.showToast({
  88. content: '请输入正确的姓名',
  89. icon: 'none',
  90. duration: 2000
  91. })
  92. } else if (!reg.test(this.data.cardVal)) {
  93. my.showToast({
  94. content: '请输入正确的身份证号',
  95. icon: 'none',
  96. duration: 2000
  97. })
  98. } else {
  99. that.checkIDCard(that.data.cardVal)
  100. }
  101. }, 1000),
  102. // 一键获取手机号
  103. checkIDCard(idcode) {
  104. // 加权因子
  105. var that = this;
  106. var weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
  107. // 校验码
  108. var check_code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
  109. var code = idcode + ''
  110. var last = idcode[17] //最后一位
  111. var seventeen = code.substring(0, 17)
  112. // ISO 7064:1983.MOD 11-2
  113. // 判断最后一位校验码是否正确
  114. var arr = seventeen.split('')
  115. var len = arr.length
  116. var num = 0
  117. for (var i = 0; i < len; i++) {
  118. num = num + arr[i] * weight_factor[i]
  119. }
  120. // 获取余数
  121. var resisue = num % 11
  122. var last_no = check_code[resisue]
  123. var idcard_patter = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/
  124. // 判断格式是否正确
  125. var format = idcard_patter.test(idcode)
  126. console.log(format)
  127. console.log(last)
  128. console.log(last_no)
  129. // 返回验证结果,校验码和格式同时正确才算是合法的身份证号码
  130. if (last == last_no && format == true) {
  131. my.showLoading({
  132. content: '认证中...',
  133. mask: true
  134. })
  135. if (app.globalData.req) {
  136. let data = {
  137. card_id: that.data.cardVal,
  138. name: that.data.nameVal
  139. }
  140. app.request('/user/real-name-authentication', data, 'POST', app.globalData.req).then(res => {
  141. console.log(res)
  142. if (res.status == 200) {
  143. my.hideLoading()
  144. my.alert({
  145. title:'提示',
  146. content:'实名认证成功,祝您用车愉快',
  147. showCancel:false,
  148. success (res) {
  149. my.navigateBack()
  150. }
  151. })
  152. }
  153. }).catch(err => {
  154. console.log(err)
  155. })
  156. } else {
  157. my.showToast({
  158. content: '您的操作过于频繁,请稍后再试~',
  159. icon: 'none'
  160. })
  161. }
  162. } else {
  163. my.showToast({
  164. content: '身份证号输入有误~',
  165. icon: 'none'
  166. })
  167. }
  168. },
  169. })