personInfo.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. import WxValidate from '../../utils/WxValidate.js';
  2. const app = require('../../utils/util.js');
  3. const tool = require('../../utils/tool.js');
  4. const COS = require('../../utils/cos.js');
  5. Page({
  6. data: {
  7. myInfo: [],
  8. imgsrc: '../../images/cardModel.png',
  9. photo: '../../images/cardModel.png',
  10. questionList: [{
  11. name: '女',
  12. value: '0',
  13. checked: false
  14. }, {
  15. name: '男',
  16. value: '1',
  17. checked: false
  18. }],
  19. mzArr: [
  20. "汉族", "满族", "蒙古族", "回族", "藏族", "维吾尔族", "苗族", "彝族", "壮族", "布依族", "侗族", "瑶族", "白族", "土家族", "哈尼族", "哈萨克族", "傣族", "黎族", "傈僳族", "佤族", "畲族", "高山族", "拉祜族", "水族", "东乡族", "纳西族", "景颇族", "柯尔克孜族", "土族", "达斡尔族", "仫佬族", "羌族", "布朗族", "撒拉族", "毛南族", "仡佬族", "锡伯族", "阿昌族", "普米族", "朝鲜族", "塔吉克族", "怒族", "乌孜别克族", "俄罗斯族", "鄂温克族", "德昂族", "保安族", "裕固族", "京族", "塔塔尔族", "独龙族", "鄂伦春族", "赫哲族", "门巴族", "珞巴族", "基诺族", "其他"
  21. ],
  22. constellationArr: [],
  23. form: {
  24. idFront: '',
  25. idCard: '',
  26. name: '',
  27. sex: '',
  28. age: '',
  29. nativePlace: '',
  30. nation: '',
  31. birthday: '',
  32. constellation: '',
  33. photoUrl: ''
  34. }
  35. },
  36. change(e) {
  37. console.log(e)
  38. if (e.detail.value.length != 0) {
  39. this.setData({
  40. xyAgree: true
  41. })
  42. } else {
  43. this.setData({
  44. xyAgree: false
  45. })
  46. }
  47. console.log(this.data.xyAgree)
  48. },
  49. nationPicker(e) {
  50. let nation = 'form.nation'
  51. this.setData({
  52. [nation]: this.data.mzArr[e.detail.value],
  53. })
  54. },
  55. birthdayPicker(e) {
  56. let birthday = 'form.birthday'
  57. this.setData({
  58. [birthday]: e.detail.value
  59. })
  60. },
  61. constellationPicker(e) {
  62. let constellation = 'form.constellation'
  63. this.setData({
  64. [constellation]: this.data.constellationArrValue[e.detail.value],
  65. constellationLabel: this.data.constellationArr[e.detail.value],
  66. })
  67. },
  68. checkboxChange: function (e) {
  69. console.log('checkbox发生change事件,携带value值为:', e.detail.value)
  70. const items = this.data.questionList
  71. const values = e.detail.value
  72. for (let i = 0, lenI = items.length; i < lenI; ++i) {
  73. items[i].checked = false
  74. if (items[i].value == values) {
  75. items[i].checked = true
  76. }
  77. }
  78. let sex = 'form.sex';
  79. let sexValue = 0;
  80. if (e.detail.value == '男') {
  81. sexValue = 1;
  82. }
  83. this.setData({
  84. questionList: this.data.questionList,
  85. [sex]: sexValue
  86. })
  87. console.log(sexValue)
  88. },
  89. uploadCard(e) {
  90. let type = e.currentTarget.dataset.type
  91. wx.showLoading({
  92. title: '上传中',
  93. })
  94. wx.chooseImage({
  95. count: 1,
  96. sizeType: ['compressed'],
  97. sourceType: ['album', 'camera'],
  98. success: (res) => {
  99. let tempFilePaths = res.tempFilePaths[0]
  100. console.log(tempFilePaths, '图片的临时路径')
  101. this.toCos(tempFilePaths, type)
  102. },
  103. fail: () => {
  104. wx.hideLoading()
  105. }
  106. })
  107. },
  108. toCos(path, type) {
  109. var Bucket = 'yuehe-1257653330';
  110. var Region = 'ap-beijing';
  111. var ForcePathStyle = false;
  112. var prefix = 'https://' + Bucket + '.cos.' + Region + '.myqcloud.com/';
  113. if (ForcePathStyle) {
  114. prefix = 'https://cos.' + Region + '.myqcloud.com/' + Bucket + '/';
  115. }
  116. var stsCache;
  117. var getCredentials = (callback) => {
  118. if (stsCache && Date.now() / 1000 + 30 < stsCache.expiredTime) {
  119. callback(data.credentials);
  120. return;
  121. }
  122. wx.request({
  123. method: 'POST',
  124. url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/getCosSts',
  125. success: function (result) {
  126. var credentials = result.data.credential.credentials;
  127. if (credentials) {
  128. stsCache = result.data.credential
  129. } else {
  130. wx.hideLoading()
  131. wx.showModal({
  132. title: '临时密钥获取失败',
  133. content: JSON.stringify(data),
  134. showCancel: false
  135. });
  136. }
  137. callback(stsCache && stsCache.credentials);
  138. },
  139. error: function (err) {
  140. wx.hideLoading()
  141. wx.showModal({
  142. title: '临时密钥获取失败',
  143. content: JSON.stringify(err),
  144. showCancel: false
  145. });
  146. }
  147. });
  148. };
  149. // 计算签名
  150. var getAuthorization = function (options, callback) {
  151. getCredentials(function (credentials) {
  152. callback({
  153. XCosSecurityToken: credentials.sessionToken,
  154. Authorization: COS({
  155. SecretId: credentials.tmpSecretId,
  156. SecretKey: credentials.tmpSecretKey,
  157. Method: options.Method,
  158. Pathname: options.Pathname,
  159. })
  160. });
  161. });
  162. };
  163. // 上传文件
  164. console.log(path)
  165. var Key = path.substring(path.length - 20); // 这里指定上传的文件名
  166. console.log(Key, '我是名字')
  167. var signPathname = '/';
  168. if (ForcePathStyle) {
  169. signPathname = '/' + Bucket + '/';
  170. }
  171. getAuthorization({
  172. Method: 'POST',
  173. Pathname: signPathname
  174. }, (AuthData) => {
  175. var requestTask = wx.uploadFile({
  176. url: prefix,
  177. name: 'file',
  178. filePath: path,
  179. formData: {
  180. 'key': "face/" + Key,
  181. 'success_action_status': 200,
  182. 'Signature': AuthData.Authorization,
  183. 'x-cos-security-token': AuthData.XCosSecurityToken,
  184. 'Content-Type': '',
  185. },
  186. success: (res) => {
  187. console.log(res.header.Location)
  188. if (type == 'card') {
  189. this.setData({
  190. imgsrc: res.header.Location
  191. })
  192. this.toJx(res.header.Location)
  193. }
  194. if (type == 'photo') {
  195. let photoUrl = 'form.photoUrl';
  196. this.setData({
  197. photo: res.header.Location,
  198. [photoUrl]: res.header.Location
  199. })
  200. wx.hideLoading();
  201. }
  202. },
  203. fail: function () {
  204. wx.hideLoading()
  205. wx.showModal({
  206. title: '上传失败',
  207. showCancel: false
  208. });
  209. }
  210. });
  211. requestTask.onProgressUpdate(function (res) {
  212. console.log('进度:', res.progress);
  213. });
  214. });
  215. },
  216. toJx(location) {
  217. console.log(location)
  218. wx.request({
  219. url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/ocrCard',
  220. method: "POST",
  221. header: {
  222. 'content-type': 'application/x-www-form-urlencoded',
  223. },
  224. data: {
  225. path: location,
  226. side: 'front'
  227. },
  228. success: (res) => {
  229. console.log(res)
  230. if (res.data.code == 0) {
  231. let idFront = 'form.idFront'
  232. let idCard = 'form.idCard'
  233. let name = 'form.name'
  234. let sex = 'form.sex'
  235. let age = 'form.age'
  236. let nativePlace = 'form.nativePlace'
  237. let nation = 'form.nation'
  238. let birthday = 'form.birthday'
  239. let constellation = 'form.constellation'
  240. let res1 = JSON.parse(res.data.cardMessage)
  241. let sexTurn = 0;
  242. console.log(res1)
  243. if (res1.words_result) {
  244. console.log('我识别成功了')
  245. if (res1.words_result['性别'].words == '男') {
  246. sexTurn = 1;
  247. }
  248. let xz = this.getXingzuo(res1.words_result['出生'].words.substring(4, 6), res1.words_result['出生'].words.substring(6, 8));
  249. console.log(xz)
  250. for (let i = 0; i < this.data.constellationArr.length; i++) {
  251. console.log(this.data.constellationArr[i])
  252. if (this.data.constellationArr[i] == xz) {
  253. this.setData({
  254. constellationLabel: this.data.constellationArr[i],
  255. [constellation]: this.data.constellationArrValue[i]
  256. })
  257. }
  258. }
  259. this.setData({
  260. [idFront]: location,
  261. [idCard]: res1.words_result['公民身份号码'].words,
  262. [name]: res1.words_result['姓名'].words,
  263. [sex]: sexTurn,
  264. [age]: tool.formatTime(new Date()) - res1.words_result['出生'].words.substring(0, 4),
  265. [nativePlace]: res1.words_result['住址'].words,
  266. [nation]: res1.words_result['民族'].words,
  267. [birthday]: res1.words_result['出生'].words.substring(0, 4) + '-' + res1.words_result['出生'].words.substring(4, 6) + '-' + res1.words_result['出生'].words.substring(6, 8)
  268. })
  269. const items = this.data.questionList;
  270. const values = res1.words_result['性别'].words;
  271. for (let i = 0, lenI = items.length; i < lenI; ++i) {
  272. items[i].checked = false
  273. if (items[i].name == values) {
  274. items[i].checked = true
  275. }
  276. }
  277. this.setData({
  278. questionList: this.data.questionList
  279. })
  280. } else {
  281. console.log('我识别失败了')
  282. let idFront = 'form.idFront';
  283. this.setData({
  284. [idFront]: location
  285. })
  286. console.log(this.data.form)
  287. }
  288. }
  289. },
  290. // fail:(err)=>{
  291. // },
  292. complete: () => {
  293. wx.hideLoading()
  294. }
  295. })
  296. },
  297. getXingzuo(month, day) {
  298. const s = '魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯';
  299. const arr = [20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22];
  300. return s.substr(month * 2 - (day < arr[month - 1] ? 2 : 0), 2);
  301. },
  302. //报错
  303. showModal(error) {
  304. wx.showModal({
  305. content: error.msg,
  306. showCancel: false,
  307. })
  308. },
  309. initValidate() {
  310. const rules = {
  311. idFront: {
  312. required: true,
  313. },
  314. photoUrl: {
  315. required: true,
  316. },
  317. name: {
  318. required: true,
  319. minlength: 2,
  320. maxlength: 4
  321. },
  322. sex: {
  323. required: true
  324. },
  325. age: {
  326. required: true
  327. },
  328. nativePlace: {
  329. required: true
  330. },
  331. nation: {
  332. required: true
  333. },
  334. birthday: {
  335. required: true
  336. },
  337. constellation: {
  338. required: true
  339. }
  340. }
  341. const messages = {
  342. idFront: {
  343. required: '请上传身份证照片哟',
  344. },
  345. photoUrl: {
  346. required: '请上传本人照片哟',
  347. },
  348. name: {
  349. required: '请填写姓名哟',
  350. minlength: '请输入正确的名称哟',
  351. maxlength: '请输入正确的名称哟'
  352. },
  353. sex: {
  354. required: '请选择性别哟'
  355. },
  356. age: {
  357. required: '请填写年龄哟'
  358. },
  359. nativePlace: {
  360. required: '请填写户籍地址哟'
  361. },
  362. nation: {
  363. required: '请选择民族哟'
  364. },
  365. birthday: {
  366. required: '请填写生日哟'
  367. },
  368. constellation: {
  369. required: '请选择星座哟'
  370. }
  371. }
  372. this.WxValidate = new WxValidate(rules, messages)
  373. },
  374. getInfos() {
  375. wx.showLoading();
  376. wx.request({
  377. url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/info',
  378. method: "get",
  379. success: (res) => {
  380. console.log(res, '我是获取的信息')
  381. if (res.data.code == 0) {
  382. this.setData({
  383. myInfo: res.data.info
  384. })
  385. }
  386. let idFront = 'form.idFront'
  387. let photoUrl = 'form.photoUrl'
  388. let idCard = 'form.idCard'
  389. let name = 'form.name'
  390. let sex = 'form.sex'
  391. let age = 'form.age'
  392. let nativePlace = 'form.nativePlace'
  393. let nation = 'form.nation'
  394. let birthday = 'form.birthday'
  395. let constellation = 'form.constellation'
  396. tool.formDetails('he_astrology').then(result => {
  397. this.setData({
  398. constellationArr: result[0],
  399. constellationArrValue: result[1]
  400. })
  401. if (res.data.code == 0) {
  402. for (let i = 0; i < result[1].length; i++) {
  403. if (result[1][i] == res.data.info.constellation) {
  404. this.setData({
  405. [constellation]: result[1][i],
  406. constellationLabel: result[0][i],
  407. })
  408. }
  409. }
  410. }
  411. })
  412. if (res.data.code == 0) {
  413. const items = this.data.questionList
  414. const values = res.data.info.sex
  415. for (let i = 0, lenI = items.length; i < lenI; ++i) {
  416. if (items[i].value == values) {
  417. items[i].checked = true
  418. }
  419. }
  420. this.setData({
  421. imgsrc: res.data.info.idFront,
  422. [idFront]: res.data.info.idFront,
  423. photo: res.data.info.photoUrl,
  424. [photoUrl]: res.data.info.photoUrl,
  425. [idCard]: res.data.info.idCard,
  426. [name]: res.data.info.name,
  427. [sex]: res.data.info.sex,
  428. questionList: this.data.questionList,
  429. [age]: tool.formatTime(new Date()) - res.data.info.idCard.substring(6, 10),
  430. [nativePlace]: res.data.info.nativePlace,
  431. [nation]: res.data.info.nation,
  432. [birthday]: res.data.info.birthday
  433. })
  434. }
  435. },
  436. complete: () => {
  437. wx.hideLoading()
  438. }
  439. })
  440. },
  441. // 表单提交事件
  442. formSubmit(e) {
  443. console.log(e.detail.value)
  444. console.log(this.data.myInfo)
  445. const params = e.detail.value
  446. //校验表单
  447. if (!this.WxValidate.checkForm(params)) {
  448. const error = this.WxValidate.errorList[0]
  449. this.showModal(error)
  450. return false
  451. }
  452. // if (!this.data.xyAgree) {
  453. // wx.showToast({
  454. // title: '请仔细阅读协议并勾选呦',
  455. // icon: 'none',
  456. // duration: 3000,
  457. // })
  458. // return false
  459. // }
  460. wx.request({
  461. url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/info',
  462. method: "POST",
  463. header: {
  464. 'content-type': 'application/x-www-form-urlencoded',
  465. },
  466. data: params,
  467. success: (res) => {
  468. console.log(res)
  469. if (res.data.code == 0) {
  470. wx.showModal({
  471. title: '信息提交成功了呦',
  472. showCancel: false,
  473. success: (res) => {
  474. if (res.confirm) {
  475. if (this.data.myInfo && this.data.myInfo.height) {
  476. wx.redirectTo({
  477. url: '/pages/index/index',
  478. })
  479. } else {
  480. wx.redirectTo({
  481. url: '/pages/personInfoDetail/personInfoDetail',
  482. })
  483. }
  484. }
  485. }
  486. });
  487. }
  488. }
  489. })
  490. },
  491. goInfo(){
  492. wx.redirectTo({
  493. url: '/pages/InfoType/InfoType',
  494. })
  495. },
  496. goMy() {
  497. wx.redirectTo({
  498. url: '/pages/personal/personal',
  499. })
  500. },
  501. goIndex() {
  502. wx.redirectTo({
  503. url: '/pages/index/index',
  504. })
  505. },
  506. // goxy() {
  507. // wx.navigateTo({
  508. // url: '/pages/infoAgreement/infoAgreement',
  509. // })
  510. // },
  511. async onLoad(options) {
  512. this.initValidate()
  513. tool.openidStatus().then(result => {
  514. this.setData({
  515. openid: result[0],
  516. sessionkey: result[1]
  517. })
  518. this.getInfos();
  519. })
  520. }
  521. })