personInfo.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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. tel:'',
  28. sex: '',
  29. age: '',
  30. nativePlace: '',
  31. nation: '',
  32. birthday: '',
  33. constellation: '',
  34. photoUrl: ''
  35. },
  36. userInfo: '',
  37. ysf: {
  38. title: '注册页',
  39. config: ''
  40. }
  41. },
  42. change(e) {
  43. console.log(e)
  44. if (e.detail.value.length != 0) {
  45. this.setData({
  46. xyAgree: true
  47. })
  48. } else {
  49. this.setData({
  50. xyAgree: false
  51. })
  52. }
  53. console.log(this.data.xyAgree)
  54. },
  55. nationPicker(e) {
  56. let nation = 'form.nation'
  57. this.setData({
  58. [nation]: this.data.mzArr[e.detail.value],
  59. })
  60. },
  61. birthdayPicker(e) {
  62. let birthday = 'form.birthday'
  63. this.setData({
  64. [birthday]: e.detail.value
  65. })
  66. },
  67. constellationPicker(e) {
  68. let constellation = 'form.constellation'
  69. this.setData({
  70. [constellation]: this.data.constellationArrValue[e.detail.value],
  71. constellationLabel: this.data.constellationArr[e.detail.value],
  72. })
  73. },
  74. checkboxChange: function (e) {
  75. console.log('checkbox发生change事件,携带value值为:', e.detail.value)
  76. const items = this.data.questionList
  77. const values = e.detail.value
  78. for (let i = 0, lenI = items.length; i < lenI; ++i) {
  79. items[i].checked = false
  80. if (items[i].value == values) {
  81. items[i].checked = true
  82. }
  83. }
  84. let sex = 'form.sex';
  85. let sexValue = 0;
  86. if (e.detail.value == '男') {
  87. sexValue = 1;
  88. }
  89. this.setData({
  90. questionList: this.data.questionList,
  91. [sex]: sexValue
  92. })
  93. console.log(sexValue)
  94. },
  95. uploadCard(e) {
  96. let type = e.currentTarget.dataset.type
  97. wx.showLoading({
  98. title: '上传中',
  99. })
  100. wx.chooseImage({
  101. count: 9,
  102. sizeType: ['compressed'],
  103. sourceType: ['album', 'camera'],
  104. success: (res) => {
  105. let tempFilePaths = res.tempFilePaths[0]
  106. console.log(tempFilePaths, '图片的临时路径')
  107. this.toCos(tempFilePaths, type)
  108. },
  109. fail: () => {
  110. wx.hideLoading()
  111. }
  112. })
  113. },
  114. toCos(path, type) {
  115. var Bucket = 'yuehe-1257653330';
  116. var Region = 'ap-beijing';
  117. var ForcePathStyle = false;
  118. var prefix = 'https://' + Bucket + '.cos.' + Region + '.myqcloud.com/';
  119. if (ForcePathStyle) {
  120. prefix = 'https://cos.' + Region + '.myqcloud.com/' + Bucket + '/';
  121. }
  122. var stsCache;
  123. var getCredentials = (callback) => {
  124. if (stsCache && Date.now() / 1000 + 30 < stsCache.expiredTime) {
  125. callback(data.credentials);
  126. return;
  127. }
  128. wx.request({
  129. method: 'POST',
  130. url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/getCosSts',
  131. success: function (result) {
  132. var credentials = result.data.credential.credentials;
  133. if (credentials) {
  134. stsCache = result.data.credential
  135. } else {
  136. wx.hideLoading()
  137. wx.showModal({
  138. title: '临时密钥获取失败',
  139. content: JSON.stringify(data),
  140. showCancel: false
  141. });
  142. }
  143. callback(stsCache && stsCache.credentials);
  144. },
  145. error: function (err) {
  146. wx.hideLoading()
  147. wx.showModal({
  148. title: '临时密钥获取失败',
  149. content: JSON.stringify(err),
  150. showCancel: false
  151. });
  152. }
  153. });
  154. };
  155. // 计算签名
  156. var getAuthorization = function (options, callback) {
  157. getCredentials(function (credentials) {
  158. callback({
  159. XCosSecurityToken: credentials.sessionToken,
  160. Authorization: COS({
  161. SecretId: credentials.tmpSecretId,
  162. SecretKey: credentials.tmpSecretKey,
  163. Method: options.Method,
  164. Pathname: options.Pathname,
  165. })
  166. });
  167. });
  168. };
  169. // 上传文件
  170. console.log(path)
  171. var Key = path.substring(path.length - 20); // 这里指定上传的文件名
  172. console.log(Key, '我是名字')
  173. var signPathname = '/';
  174. if (ForcePathStyle) {
  175. signPathname = '/' + Bucket + '/';
  176. }
  177. getAuthorization({
  178. Method: 'POST',
  179. Pathname: signPathname
  180. }, (AuthData) => {
  181. var requestTask = wx.uploadFile({
  182. url: prefix,
  183. name: 'file',
  184. filePath: path,
  185. formData: {
  186. 'key': "face/" + Key,
  187. 'success_action_status': 200,
  188. 'Signature': AuthData.Authorization,
  189. 'x-cos-security-token': AuthData.XCosSecurityToken,
  190. 'Content-Type': '',
  191. },
  192. success: (res) => {
  193. console.log(res.header.Location)
  194. if (type == 'card') {
  195. this.setData({
  196. imgsrc: res.header.Location
  197. })
  198. this.toJx(res.header.Location)
  199. }
  200. if (type == 'photo') {
  201. let photoUrl = 'form.photoUrl';
  202. this.setData({
  203. photo: res.header.Location,
  204. [photoUrl]: res.header.Location
  205. })
  206. wx.hideLoading();
  207. }
  208. },
  209. fail: function () {
  210. wx.hideLoading()
  211. wx.showModal({
  212. title: '上传失败',
  213. showCancel: false
  214. });
  215. }
  216. });
  217. requestTask.onProgressUpdate(function (res) {
  218. console.log('进度:', res.progress);
  219. });
  220. });
  221. },
  222. toJx(location) {
  223. console.log(location)
  224. wx.request({
  225. url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/ocrCard',
  226. method: "POST",
  227. header: {
  228. 'content-type': 'application/x-www-form-urlencoded',
  229. },
  230. data: {
  231. path: location,
  232. side: 'front'
  233. },
  234. success: (res) => {
  235. console.log(res)
  236. if (res.data.code == 0) {
  237. let idFront = 'form.idFront'
  238. let idCard = 'form.idCard'
  239. let name = 'form.name'
  240. let sex = 'form.sex'
  241. let age = 'form.age'
  242. let nativePlace = 'form.nativePlace'
  243. let nation = 'form.nation'
  244. let birthday = 'form.birthday'
  245. let constellation = 'form.constellation'
  246. let res1 = JSON.parse(res.data.cardMessage)
  247. let sexTurn = 0;
  248. console.log(res1)
  249. if (res1.words_result) {
  250. console.log('我识别成功了')
  251. if (res1.words_result['性别'].words == '男') {
  252. sexTurn = 1;
  253. }
  254. let xz = this.getXingzuo(res1.words_result['出生'].words.substring(4, 6), res1.words_result['出生'].words.substring(6, 8));
  255. console.log(xz)
  256. for (let i = 0; i < this.data.constellationArr.length; i++) {
  257. console.log(this.data.constellationArr[i])
  258. if (this.data.constellationArr[i] == xz) {
  259. this.setData({
  260. constellationLabel: this.data.constellationArr[i],
  261. [constellation]: this.data.constellationArrValue[i]
  262. })
  263. }
  264. }
  265. this.setData({
  266. [idFront]: location,
  267. [idCard]: res1.words_result['公民身份号码'].words,
  268. [name]: res1.words_result['姓名'].words,
  269. [sex]: sexTurn,
  270. [age]: tool.formatTime(new Date()) - res1.words_result['出生'].words.substring(0, 4),
  271. [nativePlace]: res1.words_result['住址'].words,
  272. [nation]: res1.words_result['民族'].words,
  273. [birthday]: res1.words_result['出生'].words.substring(0, 4) + '-' + res1.words_result['出生'].words.substring(4, 6) + '-' + res1.words_result['出生'].words.substring(6, 8)
  274. })
  275. const items = this.data.questionList;
  276. const values = res1.words_result['性别'].words;
  277. for (let i = 0, lenI = items.length; i < lenI; ++i) {
  278. items[i].checked = false
  279. if (items[i].name == values) {
  280. items[i].checked = true
  281. }
  282. }
  283. this.setData({
  284. questionList: this.data.questionList
  285. })
  286. } else {
  287. console.log('我识别失败了')
  288. let idFront = 'form.idFront';
  289. this.setData({
  290. [idFront]: location
  291. })
  292. console.log(this.data.form)
  293. }
  294. }
  295. },
  296. // fail:(err)=>{
  297. // },
  298. complete: () => {
  299. wx.hideLoading()
  300. }
  301. })
  302. },
  303. getXingzuo(month, day) {
  304. const s = '魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯';
  305. const arr = [20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22];
  306. return s.substr(month * 2 - (day < arr[month - 1] ? 2 : 0), 2);
  307. },
  308. //报错
  309. showModal(error) {
  310. wx.showModal({
  311. content: error.msg,
  312. showCancel: false,
  313. })
  314. },
  315. initValidate() {
  316. const rules = {
  317. idFront: {
  318. required: true,
  319. },
  320. photoUrl: {
  321. required: true,
  322. },
  323. name: {
  324. required: true,
  325. minlength: 2,
  326. maxlength: 4
  327. },
  328. sex: {
  329. required: true
  330. },
  331. tel: {
  332. required: true
  333. },
  334. age: {
  335. required: true
  336. },
  337. nativePlace: {
  338. required: true
  339. },
  340. nation: {
  341. required: true
  342. },
  343. birthday: {
  344. required: true
  345. },
  346. constellation: {
  347. required: true
  348. }
  349. }
  350. const messages = {
  351. idFront: {
  352. required: '请上传身份证照片哟',
  353. },
  354. photoUrl: {
  355. required: '请上传本人照片哟',
  356. },
  357. tel: {
  358. required: '请上传手机号'
  359. },
  360. name: {
  361. required: '请填写姓名哟',
  362. minlength: '请输入正确的名称哟',
  363. maxlength: '请输入正确的名称哟'
  364. },
  365. sex: {
  366. required: '请选择性别哟'
  367. },
  368. age: {
  369. required: '请填写年龄哟'
  370. },
  371. nativePlace: {
  372. required: '请填写户籍地址哟'
  373. },
  374. nation: {
  375. required: '请选择民族哟'
  376. },
  377. birthday: {
  378. required: '请填写生日哟'
  379. },
  380. constellation: {
  381. required: '请选择星座哟'
  382. }
  383. }
  384. this.WxValidate = new WxValidate(rules, messages)
  385. },
  386. getInfos() {
  387. wx.showLoading();
  388. wx.request({
  389. url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/info',
  390. method: "get",
  391. success: (res) => {
  392. console.log(res, '我是获取的信息')
  393. if (res.data.code == 0) {
  394. let config='ysf.config';
  395. let sex1 = '';
  396. if(res.data.info.sex==0){
  397. sex1='女'
  398. }else{
  399. sex1='男'
  400. }
  401. console.log(sex1)
  402. this.setData({
  403. myInfo: res.data.info,
  404. [config]:JSON.stringify({
  405. "uid": res.data.id, // 用户唯一标识
  406. "level": 1, // vip等级
  407. "data": JSON.stringify([{
  408. "index": 0,
  409. "key": "name",
  410. "label": "姓名",
  411. "value": res.data.info.name
  412. },
  413. {
  414. "index": 1,
  415. "key": "createTime",
  416. "label": "创建时间",
  417. "value": res.data.info.createTime
  418. },
  419. {
  420. "index": 2,
  421. "key": "sex",
  422. "label": "性别",
  423. "value": sex1
  424. },
  425. {
  426. "index": 3,
  427. "key": "tel",
  428. "label": "电话",
  429. "value": res.data.info.tel
  430. }
  431. ])
  432. })
  433. })
  434. console.log(this.data.ysf)
  435. }
  436. let idFront = 'form.idFront'
  437. let photoUrl = 'form.photoUrl'
  438. let idCard = 'form.idCard'
  439. let name = 'form.name'
  440. let tel = 'form.tel'
  441. let sex = 'form.sex'
  442. let age = 'form.age'
  443. let nativePlace = 'form.nativePlace'
  444. let nation = 'form.nation'
  445. let birthday = 'form.birthday'
  446. let constellation = 'form.constellation'
  447. tool.formDetails('he_astrology').then(result => {
  448. this.setData({
  449. constellationArr: result[0],
  450. constellationArrValue: result[1]
  451. })
  452. if (res.data.code == 0) {
  453. for (let i = 0; i < result[1].length; i++) {
  454. if (result[1][i] == res.data.info.constellation) {
  455. this.setData({
  456. [constellation]: result[1][i],
  457. constellationLabel: result[0][i],
  458. })
  459. }
  460. }
  461. }
  462. })
  463. if (res.data.code == 0) {
  464. const items = this.data.questionList
  465. const values = res.data.info.sex
  466. for (let i = 0, lenI = items.length; i < lenI; ++i) {
  467. if (items[i].value == values) {
  468. items[i].checked = true
  469. }
  470. }
  471. this.setData({
  472. imgsrc: res.data.info.idFront,
  473. [idFront]: res.data.info.idFront,
  474. photo: res.data.info.photoUrl,
  475. [tel]:res.data.info.tel,
  476. [photoUrl]: res.data.info.photoUrl,
  477. [idCard]: res.data.info.idCard,
  478. [name]: res.data.info.name,
  479. [sex]: res.data.info.sex,
  480. questionList: this.data.questionList,
  481. [age]: tool.formatTime(new Date()) - res.data.info.idCard.substring(6, 10),
  482. [nativePlace]: res.data.info.nativePlace,
  483. [nation]: res.data.info.nation,
  484. [birthday]: res.data.info.birthday
  485. })
  486. }
  487. },
  488. complete: () => {
  489. wx.hideLoading()
  490. }
  491. })
  492. },
  493. getPhoneNumber(e) {
  494. console.log(e,'000000000')
  495. if (e.detail.errMsg == "getPhoneNumber:ok") {
  496. wx.getSetting({
  497. success: res => {
  498. console.log(res,'111111')
  499. if (res.authSetting['scope.userInfo']) {
  500. wx.getUserInfo({
  501. success: res2 => {
  502. console.log(JSON.parse(res2.rawData),'222222222222')
  503. wx.request({
  504. url: app.globalData.publicUrl + '/wx/user/wxbfa171fdd4000e03/phone',
  505. header: {
  506. 'content-type': 'application/x-www-form-urlencoded',
  507. },
  508. data: {
  509. sessionKey: this.data.sessionkey,
  510. signature: res2.signature,
  511. rawData: res2.rawData,
  512. encryptedData: e.detail.encryptedData,
  513. iv: e.detail.iv
  514. },
  515. method: "POST",
  516. success: (res) => {
  517. console.log(res, '我是解析的手号');
  518. let tel = 'form.tel'
  519. if (res.data.phoneNumber) {
  520. this.setData({
  521. [tel]: res.data.phoneNumber
  522. })
  523. } else {
  524. this.setData({
  525. [tel]: 0
  526. })
  527. }
  528. }
  529. })
  530. // }
  531. }
  532. })
  533. }
  534. }
  535. })
  536. }
  537. },
  538. // 表单提交事件
  539. formSubmit(e) {
  540. console.log(e.detail.value)
  541. console.log(this.data.myInfo)
  542. const params = e.detail.value
  543. //校验表单
  544. if (!this.WxValidate.checkForm(params)) {
  545. const error = this.WxValidate.errorList[0]
  546. this.showModal(error)
  547. return false
  548. }
  549. // if (!this.data.xyAgree) {
  550. // wx.showToast({
  551. // title: '请仔细阅读协议并勾选呦',
  552. // icon: 'none',
  553. // duration: 3000,
  554. // })
  555. // return false
  556. // }
  557. wx.request({
  558. url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/info',
  559. method: "POST",
  560. header: {
  561. 'content-type': 'application/x-www-form-urlencoded',
  562. },
  563. data: params,
  564. success: (res) => {
  565. console.log(res)
  566. if (res.data.code == 0) {
  567. wx.showModal({
  568. title: '信息提交成功了呦',
  569. showCancel: false,
  570. success: (res) => {
  571. if (res.confirm) {
  572. if (this.data.myInfo && this.data.myInfo.height) {
  573. wx.redirectTo({
  574. url: '/pages/index/index',
  575. })
  576. } else {
  577. wx.redirectTo({
  578. url: '/pages/personInfoDetail/personInfoDetail',
  579. })
  580. }
  581. }
  582. }
  583. });
  584. }else{
  585. wx.showModal({
  586. title: '不好意思啦',
  587. content:'信息提交失败了,请联系客服呦',
  588. showCancel: false
  589. })
  590. }
  591. }
  592. })
  593. },
  594. goInfo(){
  595. wx.redirectTo({
  596. url: '/pages/InfoType/InfoType',
  597. })
  598. },
  599. goMy() {
  600. wx.redirectTo({
  601. url: '/pages/personal/personal',
  602. })
  603. },
  604. goIndex() {
  605. wx.redirectTo({
  606. url: '/pages/index/index',
  607. })
  608. },
  609. async onLoad(options) {
  610. this.initValidate()
  611. tool.openidStatus().then(result => {
  612. this.setData({
  613. openid: result[0],
  614. sessionkey: result[1]
  615. })
  616. this.getInfos();
  617. })
  618. },
  619. onShow() {
  620. tool.isLogin().then(res => {
  621. // this.setData({
  622. // userInfo: JSON.parse(res.rawData)
  623. // })
  624. // wx.getSetting({
  625. // success: res => {
  626. // if (res.authSetting['scope.userInfo']) {
  627. // wx.getUserInfo({
  628. // success: res => {
  629. // this.setData({
  630. // userInfo:JSON.parse(res.rawData)
  631. // })
  632. // }
  633. // })
  634. // }
  635. // }
  636. // })
  637. }).catch(err => {
  638. wx.showModal({
  639. title: '您还未登录',
  640. showCancel: false,
  641. success(res) {
  642. if (res.confirm) {
  643. wx.redirectTo({
  644. url: '/pages/personal/personal',
  645. })
  646. }
  647. }
  648. });
  649. // console.log(err,'0000000000')
  650. })
  651. }
  652. })