personInfo.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. name: {
  358. required: '请填写姓名哟',
  359. minlength: '请输入正确的名称哟',
  360. maxlength: '请输入正确的名称哟'
  361. },
  362. sex: {
  363. required: '请选择性别哟'
  364. },
  365. age: {
  366. required: '请填写年龄哟'
  367. },
  368. nativePlace: {
  369. required: '请填写户籍地址哟'
  370. },
  371. nation: {
  372. required: '请选择民族哟'
  373. },
  374. birthday: {
  375. required: '请填写生日哟'
  376. },
  377. constellation: {
  378. required: '请选择星座哟'
  379. }
  380. }
  381. this.WxValidate = new WxValidate(rules, messages)
  382. },
  383. getInfos() {
  384. wx.showLoading();
  385. wx.request({
  386. url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/info',
  387. method: "get",
  388. success: (res) => {
  389. console.log(res, '我是获取的信息')
  390. if (res.data.code == 0) {
  391. let config='ysf.config';
  392. let sex1 = '';
  393. if(res.data.info.sex==0){
  394. sex1='女'
  395. }else{
  396. sex1='男'
  397. }
  398. console.log(sex1)
  399. this.setData({
  400. myInfo: res.data.info,
  401. [config]:JSON.stringify({
  402. "uid": res.data.id, // 用户唯一标识
  403. "level": 1, // vip等级
  404. "data": JSON.stringify([{
  405. "index": 0,
  406. "key": "name",
  407. "label": "姓名",
  408. "value": res.data.info.name
  409. },
  410. {
  411. "index": 1,
  412. "key": "createTime",
  413. "label": "创建时间",
  414. "value": res.data.info.createTime
  415. },
  416. {
  417. "index": 2,
  418. "key": "sex",
  419. "label": "性别",
  420. "value": sex1
  421. },
  422. {
  423. "index": 3,
  424. "key": "tel",
  425. "label": "电话",
  426. "value": res.data.info.tel
  427. }
  428. ])
  429. })
  430. })
  431. console.log(this.data.ysf)
  432. }
  433. let idFront = 'form.idFront'
  434. let photoUrl = 'form.photoUrl'
  435. let idCard = 'form.idCard'
  436. let name = 'form.name'
  437. let tel = 'form.tel'
  438. let sex = 'form.sex'
  439. let age = 'form.age'
  440. let nativePlace = 'form.nativePlace'
  441. let nation = 'form.nation'
  442. let birthday = 'form.birthday'
  443. let constellation = 'form.constellation'
  444. tool.formDetails('he_astrology').then(result => {
  445. this.setData({
  446. constellationArr: result[0],
  447. constellationArrValue: result[1]
  448. })
  449. if (res.data.code == 0) {
  450. for (let i = 0; i < result[1].length; i++) {
  451. if (result[1][i] == res.data.info.constellation) {
  452. this.setData({
  453. [constellation]: result[1][i],
  454. constellationLabel: result[0][i],
  455. })
  456. }
  457. }
  458. }
  459. })
  460. if (res.data.code == 0) {
  461. const items = this.data.questionList
  462. const values = res.data.info.sex
  463. for (let i = 0, lenI = items.length; i < lenI; ++i) {
  464. if (items[i].value == values) {
  465. items[i].checked = true
  466. }
  467. }
  468. this.setData({
  469. imgsrc: res.data.info.idFront,
  470. [idFront]: res.data.info.idFront,
  471. photo: res.data.info.photoUrl,
  472. [tel]:res.data.info.tel,
  473. [photoUrl]: res.data.info.photoUrl,
  474. [idCard]: res.data.info.idCard,
  475. [name]: res.data.info.name,
  476. [sex]: res.data.info.sex,
  477. questionList: this.data.questionList,
  478. [age]: tool.formatTime(new Date()) - res.data.info.idCard.substring(6, 10),
  479. [nativePlace]: res.data.info.nativePlace,
  480. [nation]: res.data.info.nation,
  481. [birthday]: res.data.info.birthday
  482. })
  483. }
  484. },
  485. complete: () => {
  486. wx.hideLoading()
  487. }
  488. })
  489. },
  490. getPhoneNumber(e) {
  491. tool.isLogin().then(res => {
  492. console.log(res)
  493. if (e.detail.errMsg == "getPhoneNumber:ok") {
  494. wx.request({
  495. url: app.globalData.publicUrl + '/wx/user/wxbfa171fdd4000e03/phone',
  496. header: {
  497. 'content-type': 'application/x-www-form-urlencoded',
  498. },
  499. data: {
  500. sessionKey: this.data.sessionkey,
  501. signature: res.signature,
  502. rawData: res.rawData,
  503. encryptedData: e.detail.encryptedData,
  504. iv: e.detail.iv
  505. },
  506. method: "POST",
  507. success: (res) => {
  508. console.log(res, '我是解析的手号');
  509. let tel = 'form.tel'
  510. if (res.data.phoneNumber) {
  511. this.setData({
  512. [tel]: res.data.phoneNumber
  513. })
  514. } else {
  515. this.setData({
  516. [tel]: 0
  517. })
  518. }
  519. }
  520. })
  521. }
  522. }).catch(err => {
  523. wx.showModal({
  524. title: '您还未登录',
  525. showCancel: false,
  526. success(res) {
  527. if (res.confirm) {
  528. wx.redirectTo({
  529. url: '/pages/personal/personal',
  530. })
  531. }
  532. }
  533. });
  534. })
  535. },
  536. // 表单提交事件
  537. formSubmit(e) {
  538. console.log(e.detail.value)
  539. console.log(this.data.myInfo)
  540. const params = e.detail.value
  541. //校验表单
  542. if (!this.WxValidate.checkForm(params)) {
  543. const error = this.WxValidate.errorList[0]
  544. this.showModal(error)
  545. return false
  546. }
  547. // if (!this.data.xyAgree) {
  548. // wx.showToast({
  549. // title: '请仔细阅读协议并勾选呦',
  550. // icon: 'none',
  551. // duration: 3000,
  552. // })
  553. // return false
  554. // }
  555. wx.request({
  556. url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/info',
  557. method: "POST",
  558. header: {
  559. 'content-type': 'application/x-www-form-urlencoded',
  560. },
  561. data: params,
  562. success: (res) => {
  563. console.log(res)
  564. if (res.data.code == 0) {
  565. wx.showModal({
  566. title: '信息提交成功了呦',
  567. showCancel: false,
  568. success: (res) => {
  569. if (res.confirm) {
  570. if (this.data.myInfo && this.data.myInfo.height) {
  571. wx.redirectTo({
  572. url: '/pages/index/index',
  573. })
  574. } else {
  575. wx.redirectTo({
  576. url: '/pages/personInfoDetail/personInfoDetail',
  577. })
  578. }
  579. }
  580. }
  581. });
  582. }else{
  583. wx.showModal({
  584. title: '不好意思啦',
  585. content:'信息提交失败了,请联系客服呦',
  586. showCancel: false
  587. })
  588. }
  589. }
  590. })
  591. },
  592. goInfo(){
  593. wx.redirectTo({
  594. url: '/pages/InfoType/InfoType',
  595. })
  596. },
  597. goMy() {
  598. wx.redirectTo({
  599. url: '/pages/personal/personal',
  600. })
  601. },
  602. goIndex() {
  603. wx.redirectTo({
  604. url: '/pages/index/index',
  605. })
  606. },
  607. async onLoad(options) {
  608. this.initValidate()
  609. tool.openidStatus().then(result => {
  610. this.setData({
  611. openid: result[0],
  612. sessionkey: result[1]
  613. })
  614. this.getInfos();
  615. })
  616. },
  617. onShow() {
  618. this.getInfos()
  619. tool.isLogin().then(res => {
  620. this.setData({
  621. userInfo: JSON.parse(res.rawData)
  622. })
  623. console.log(this.data.userInfo,'11111111')
  624. }).catch(err => {
  625. console.log(err,'0000000000')
  626. })
  627. }
  628. })