finishInfo.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. if (_this.data.userName == result['姓名'].words) {
  142. _this.setData({
  143. userName: result['姓名'].words,
  144. idCardNumber: result['公民身份号码'].words,
  145. parameter: _this.data.parameter,
  146. age: tYear - aa
  147. })
  148. } else {
  149. wx.showModal({
  150. showCancel: false,
  151. content: '登录巡访员名字和扫描身份证名字不一致,请重新扫描'
  152. })
  153. }
  154. } else {
  155. wx.showToast({
  156. title: "身份证OCR识别失败",
  157. icon: 'none',
  158. duration: 2000,
  159. })
  160. }
  161. },
  162. complete: (e) => {
  163. wx.hideLoading();
  164. }
  165. })
  166. },
  167. // 错误信息
  168. fail: console.error
  169. })
  170. },
  171. fail: function () {
  172. wx.hideLoading();
  173. }
  174. })
  175. } else {
  176. wx.showToast({
  177. title: '身份证识别失败,token失效',
  178. icon: 'none',
  179. duration: 2000,
  180. })
  181. }
  182. }
  183. },
  184. })
  185. },
  186. postListChange: function (e) {
  187. this.setData({
  188. postindex: e.detail.value,
  189. posttext: '',
  190. })
  191. },
  192. changeIdCard(e) {
  193. console.log(e.detail.value, "121212");
  194. var myDate = new Date();
  195. var tYear = myDate.getFullYear();
  196. let aa = parseInt(e.detail.value.slice(6, 10));
  197. console.log(aa);
  198. this.setData({
  199. age: tYear - aa
  200. })
  201. },
  202. incomeListChange: function (e) {
  203. this.setData({
  204. incomeindex: e.detail.value,
  205. incometext: '',
  206. })
  207. },
  208. educationListChange: function (e) {
  209. this.setData({
  210. educationindex: e.detail.value,
  211. educationtext: '',
  212. })
  213. },
  214. initValidate() {
  215. const rules = {
  216. userName: {
  217. required: true,
  218. },
  219. sex: {
  220. required: true,
  221. },
  222. idCardNumber: {
  223. required: true,
  224. idcard: true,
  225. },
  226. age: {
  227. required: true,
  228. },
  229. job: {
  230. required: true,
  231. },
  232. income: {
  233. required: true,
  234. },
  235. education: {
  236. required: true,
  237. },
  238. };
  239. const messages = {
  240. userName: {
  241. required: '请输入您的姓名',
  242. },
  243. sex: {
  244. required: '请输入您的性别',
  245. },
  246. idCardNumber: {
  247. required: '请输入您的身份证号',
  248. idcard: '身份证号格式错误 ',
  249. },
  250. age: {
  251. required: '请输入您的年龄',
  252. },
  253. job: {
  254. required: '请输入您的职务',
  255. },
  256. income: {
  257. required: '请输入您的收入',
  258. },
  259. education: {
  260. required: '请输入您的学历',
  261. },
  262. };
  263. // 创建实例对象
  264. this.WxValidate = new WxValidate(rules, messages)
  265. },
  266. showModal(error) {
  267. wx.showToast({
  268. title: error.msg,
  269. icon: 'none',
  270. duration: 2000,
  271. })
  272. },
  273. formSubmit(e) {
  274. console.log(e.detail, "daying");
  275. /***4-3(表单提交校验)*/
  276. const params = e.detail.value
  277. if (!this.WxValidate.checkForm(params)) {
  278. const error = this.WxValidate.errorList[0]
  279. this.showModal(error)
  280. return false
  281. }
  282. /*** 这里添写验证成功以后的逻辑**/
  283. //验证通过以后->
  284. this.submitInfo(params);
  285. },
  286. //修改信息接口
  287. submitInfo(params) {
  288. wx.request({
  289. url: app.globalData.publicUrl + '/applet/suppleUserInfo',
  290. method: "POST",
  291. data: params,
  292. header: {
  293. appletsId: wx.getStorageSync('openId')
  294. },
  295. success: (res) => {
  296. if (res.data.code == 0) {
  297. console.log(res, "0000");
  298. wx.showModal({
  299. showCancel: false,
  300. content: '提交成功',
  301. success() {
  302. wx.switchTab({
  303. url: '/pages/index/index',
  304. })
  305. }
  306. })
  307. }
  308. }
  309. })
  310. },
  311. //获得当前登陆人的信息
  312. userInfo() {
  313. wx.request({
  314. url: app.globalData.publicUrl + '/applet/findUserInfo',
  315. method: "GET",
  316. header: {
  317. appletsId: wx.getStorageSync('openId')
  318. },
  319. success: (res) => {
  320. console.log(res.data.data, "000");
  321. this.data.parameter.forEach(item => {
  322. if (item.name == res.data.data.sex) {
  323. item.checked = true;
  324. }
  325. })
  326. //赋值问题
  327. let jonIndex = this.data.postList.findIndex(item => {
  328. return item.name == res.data.data.job
  329. })
  330. if (jonIndex != -1) {
  331. this.setData({
  332. posttext: '',
  333. postindex: jonIndex,
  334. })
  335. } else {
  336. this.setData({
  337. posttext: '请选择',
  338. })
  339. }
  340. let incomeindex = this.data.incomeList.findIndex(item => {
  341. return item.name == res.data.data.income
  342. })
  343. if (incomeindex != -1) {
  344. this.setData({
  345. incometext: '',
  346. incomeindex: incomeindex,
  347. })
  348. } else {
  349. this.setData({
  350. incometext: '请选择',
  351. })
  352. }
  353. let educationindex = this.data.educationList.findIndex(item => {
  354. return item.name == res.data.data.education
  355. })
  356. if (educationindex != -1) {
  357. this.setData({
  358. educationindextext: '',
  359. educationindex: educationindex,
  360. })
  361. } else {
  362. this.setData({
  363. educationtext: '请选择',
  364. })
  365. }
  366. this.setData({
  367. userName: res.data.data.userName,
  368. parameter: this.data.parameter,
  369. idCardNumber: res.data.data.idCardNumber,
  370. age: res.data.data.age,
  371. // posttext: res.data.data.job,
  372. // incometext: res.data.data.income,
  373. // educationtext: res.data.data.education,
  374. })
  375. console.log(this.data.postList, "赋值上了么");
  376. }
  377. })
  378. },
  379. onLoad: function () {
  380. this.initValidate();
  381. this.userInfo();
  382. },
  383. })