finishInfo.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. const app = require('../../utils/util.js');
  2. var util = require('../../utils/md5.js')
  3. import WxValidate from '../../utils/WxValidate';
  4. Page({
  5. data: {
  6. postindex: null,
  7. incomeindex: null,
  8. educationindex: null,
  9. posttext: '请选择',
  10. incometext: '请选择',
  11. educationtext: '请选择',
  12. userName: '', //姓名
  13. idCardNumber: '', //身份证号码
  14. age: '', //年龄
  15. parameter: [{
  16. name: '男'
  17. }, {
  18. name: '女',
  19. }],
  20. postList: [{
  21. name: '在职人员'
  22. },
  23. {
  24. name: '两委人员'
  25. },
  26. {
  27. name: '社会组织人员'
  28. },
  29. {
  30. name: '志愿者'
  31. },
  32. {
  33. name: '学生'
  34. },
  35. {
  36. name: '其他'
  37. },
  38. ],
  39. incomeList: [{
  40. name: '暂无'
  41. },
  42. {
  43. name: '500元以下'
  44. },
  45. {
  46. name: '500-1000元'
  47. },
  48. {
  49. name: '1000-2000元'
  50. },
  51. {
  52. name: '2000-5000元'
  53. },
  54. {
  55. name: '5000元-1万元'
  56. },
  57. {
  58. name: '1-2万元'
  59. },
  60. {
  61. name: '2万元以上'
  62. },
  63. ],
  64. educationList: [{
  65. name: '小学'
  66. },
  67. {
  68. name: '初中'
  69. },
  70. {
  71. name: '高中'
  72. },
  73. {
  74. name: '大专'
  75. },
  76. {
  77. name: '本科'
  78. },
  79. {
  80. name: '本科以上'
  81. },
  82. ],
  83. },
  84. // 百度身份证OCR接口 首先去后台获得token
  85. goOcr() {
  86. wx.request({
  87. url: app.globalData.publicUrl + '/applet/getToken',
  88. method: "GET",
  89. header: {
  90. appletsId: wx.getStorageSync('openId')
  91. },
  92. success: (res) => {
  93. console.log(res.data, "0000");
  94. if (res.data.data.status == 200) {
  95. let token = res.data.data.token;
  96. if (token) {
  97. wx.showLoading({
  98. title: '加载中',
  99. })
  100. var _this = this;
  101. wx.chooseImage({
  102. count: 1, // 默认9
  103. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  104. sourceType: ['camera'], // 可以指定来源是相册还是相机,默认二者都有
  105. success: function (res) {
  106. wx.showLoading({
  107. title: '加载中',
  108. })
  109. // 拿文件转换base64
  110. wx.getFileSystemManager().readFile({
  111. filePath: res.tempFilePaths[0],
  112. encoding: 'base64',
  113. success: res1 => {
  114. //返回临时文件路径
  115. console.log('读取本地文件basema ', res1.data)
  116. let img = 'data:image/png;base64,' + res1.data
  117. wx.request({
  118. url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' + token,
  119. method: 'POST',
  120. header: {
  121. 'content-type': 'application/x-www-form-urlencoded'
  122. },
  123. data: {
  124. id_card_side: 'front',
  125. image: img
  126. },
  127. dataType: 'json',
  128. success: (e) => {
  129. // 扫描后赋值
  130. if (e.data.idcard_number_type == 1) {
  131. let result = e.data.words_result
  132. console.log(result, "00000000");
  133. _this.data.parameter.forEach(item => {
  134. if (item.name == result['性别'].words) {
  135. item.checked = true;
  136. }
  137. })
  138. var myDate = new Date();
  139. var tYear = myDate.getFullYear();
  140. let aa = parseInt(result['公民身份号码'].words.slice(6, 10));
  141. _this.setData({
  142. userName: result['姓名'].words,
  143. idCardNumber: result['公民身份号码'].words,
  144. parameter: _this.data.parameter,
  145. age: tYear - aa
  146. })
  147. } else {
  148. wx.showToast({
  149. title: "身份证OCR识别失败",
  150. icon: 'none',
  151. duration: 2000,
  152. })
  153. }
  154. },
  155. complete: (e) => {
  156. wx.hideLoading();
  157. }
  158. })
  159. },
  160. // 错误信息
  161. fail: console.error
  162. })
  163. },
  164. fail: function () {
  165. wx.hideLoading();
  166. }
  167. })
  168. } else {
  169. wx.showToast({
  170. title: '身份证识别失败,token失效',
  171. icon: 'none',
  172. duration: 2000,
  173. })
  174. }
  175. }
  176. },
  177. })
  178. },
  179. postListChange: function (e) {
  180. this.setData({
  181. postindex: e.detail.value,
  182. posttext: '',
  183. })
  184. },
  185. changeIdCard(e) {
  186. console.log(e.detail.value, "121212");
  187. var myDate = new Date();
  188. var tYear = myDate.getFullYear();
  189. let aa = parseInt(e.detail.value.slice(6, 10));
  190. console.log(aa);
  191. this.setData({
  192. age: tYear - aa
  193. })
  194. },
  195. incomeListChange: function (e) {
  196. this.setData({
  197. incomeindex: e.detail.value,
  198. incometext: '',
  199. })
  200. },
  201. educationListChange: function (e) {
  202. this.setData({
  203. educationindex: e.detail.value,
  204. educationtext: '',
  205. })
  206. },
  207. initValidate() {
  208. const rules = {
  209. userName: {
  210. required: true,
  211. },
  212. sex: {
  213. required: true,
  214. },
  215. idCardNumber: {
  216. required: true,
  217. idcard: true,
  218. },
  219. age: {
  220. required: true,
  221. },
  222. job: {
  223. required: true,
  224. },
  225. income: {
  226. required: true,
  227. },
  228. education: {
  229. required: true,
  230. },
  231. };
  232. const messages = {
  233. userName: {
  234. required: '请输入您的姓名',
  235. },
  236. sex: {
  237. required: '请输入您的性别',
  238. },
  239. idCardNumber: {
  240. required: '请输入您的身份证号',
  241. idcard: '身份证号格式错误 ',
  242. },
  243. age: {
  244. required: '请输入您的年龄',
  245. },
  246. job: {
  247. required: '请输入您的职务',
  248. },
  249. income: {
  250. required: '请输入您的收入',
  251. },
  252. education: {
  253. required: '请输入您的学历',
  254. },
  255. };
  256. // 创建实例对象
  257. this.WxValidate = new WxValidate(rules, messages)
  258. },
  259. showModal(error) {
  260. wx.showToast({
  261. title: error.msg,
  262. icon: 'none',
  263. duration: 2000,
  264. })
  265. },
  266. formSubmit(e) {
  267. console.log(e.detail, "daying");
  268. /***4-3(表单提交校验)*/
  269. const params = e.detail.value
  270. if (!this.WxValidate.checkForm(params)) {
  271. const error = this.WxValidate.errorList[0]
  272. this.showModal(error)
  273. return false
  274. }
  275. /*** 这里添写验证成功以后的逻辑**/
  276. //验证通过以后->
  277. this.submitInfo(params);
  278. },
  279. //修改信息接口
  280. submitInfo(params) {
  281. wx.request({
  282. url: app.globalData.publicUrl + '/applet/suppleUserInfo',
  283. method: "POST",
  284. data: params,
  285. header: {
  286. appletsId: wx.getStorageSync('openId')
  287. },
  288. success: (res) => {
  289. if (res.data.code == 0) {
  290. console.log(res, "0000");
  291. wx.showModal({
  292. showCancel: false,
  293. content: '提交成功',
  294. success() {
  295. wx.switchTab({
  296. url: '/pages/index/index',
  297. })
  298. }
  299. })
  300. }
  301. }
  302. })
  303. },
  304. //获得当前登陆人的信息
  305. userInfo() {
  306. wx.request({
  307. url: app.globalData.publicUrl + '/applet/findUserInfo',
  308. method: "GET",
  309. header: {
  310. appletsId: wx.getStorageSync('openId')
  311. },
  312. success: (res) => {
  313. console.log(res.data.data, "000");
  314. this.data.parameter.forEach(item => {
  315. if (item.name == res.data.data.sex) {
  316. item.checked = true;
  317. }
  318. })
  319. //赋值问题
  320. let jonIndex = this.data.postList.findIndex(item => {
  321. return item.name == res.data.data.job
  322. })
  323. if (jonIndex != -1) {
  324. this.setData({
  325. posttext: '',
  326. postindex: jonIndex,
  327. })
  328. } else {
  329. this.setData({
  330. posttext: '请选择',
  331. })
  332. }
  333. let incomeindex = this.data.incomeList.findIndex(item => {
  334. return item.name == res.data.data.income
  335. })
  336. if (incomeindex != -1) {
  337. this.setData({
  338. incometext: '',
  339. incomeindex: incomeindex,
  340. })
  341. } else {
  342. this.setData({
  343. incometext: '请选择',
  344. })
  345. }
  346. let educationindex = this.data.educationList.findIndex(item => {
  347. return item.name == res.data.data.education
  348. })
  349. if (educationindex != -1) {
  350. this.setData({
  351. educationindextext: '',
  352. educationindex: educationindex,
  353. })
  354. } else {
  355. this.setData({
  356. educationtext: '请选择',
  357. })
  358. }
  359. this.setData({
  360. userName: res.data.data.userName,
  361. parameter: this.data.parameter,
  362. idCardNumber: res.data.data.idCardNumber,
  363. age: res.data.data.age,
  364. // posttext: res.data.data.job,
  365. // incometext: res.data.data.income,
  366. // educationtext: res.data.data.education,
  367. })
  368. console.log(this.data.postList, "赋值上了么");
  369. }
  370. })
  371. },
  372. onLoad: function () {
  373. this.initValidate();
  374. this.userInfo();
  375. },
  376. })