login.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. // pages/login.js
  2. const util = require("../utils/util");
  3. const tools = require("../utils/tool");
  4. const App = getApp();
  5. var mobile = /^(13[0-9]|14[4579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[1589])\d{8}$/;
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. appAssetsUrl: App.appAssetsUrl,
  12. appAssetsUrl2: App.appAssetsUrl2,
  13. pass: true,
  14. focus: false,
  15. cpass: true,
  16. cfocus: false,
  17. btntxt: "获取验证码",
  18. settimeoutName: "",
  19. countstatus: false,
  20. telInput: "",
  21. uuid: "",
  22. imgCode: "",
  23. code: "",
  24. params: {
  25. id: "",
  26. vipname: "",
  27. memberphoto: "",
  28. mobile: ""
  29. },
  30. inviteCode: "",
  31. showUserBtn: false,
  32. isCheck: false,
  33. openid: "",
  34. meetId: "",
  35. expertId: "",
  36. memberId: "",
  37. loginPopup: false,
  38. code: null,
  39. },
  40. /**
  41. * 生命周期函数--监听页面加载
  42. */
  43. onLoad: function (options) {
  44. wx.setNavigationBarTitle({
  45. title: "青雲慧",
  46. });
  47. let taht = this;
  48. if (options.meetId && options.expertId && options.memberId) {
  49. this.setData({
  50. meetId: options.meetId,
  51. expertId: options.expertId,
  52. memberId: options.memberId,
  53. });
  54. }
  55. wx.showLoading({
  56. title: "加载中...",
  57. mobile: true,
  58. });
  59. wx.login({
  60. success(res) {
  61. App._post_form(
  62. "member/authorization",
  63. "application/json;charset=UTF-8", {
  64. code: res.code,
  65. },
  66. (data) => {
  67. wx.hideLoading();
  68. if (data.code == 0) {
  69. wx.setStorageSync("openid", data.data.openid);
  70. wx.setStorageSync("session_key", data.data.session_key);
  71. taht.setData({
  72. openid: data.data.openid,
  73. });
  74. }
  75. }
  76. );
  77. },
  78. });
  79. // if (options.inviteCode) {
  80. // let sceneStr = decodeURIComponent(options.scene);
  81. // const params = new URLSearchParams(sceneStr);
  82. // const inviteCode = params.get('inviteCode');
  83. // // let inviteCode = options.inviteCode;
  84. // this.setData({
  85. // inviteCode,
  86. // });
  87. // }
  88. try {
  89. if (options.scene) {
  90. // 解码scene参数
  91. const sceneStr = decodeURIComponent(options.scene);
  92. console.log('解码后的scene:', sceneStr);
  93. // 解析参数格式:inviteCode=xxx
  94. const params = new URLSearchParams(sceneStr);
  95. const inviteCode = params.get('inviteCode');
  96. if (inviteCode) {
  97. console.log('获取到邀请码:', inviteCode);
  98. // 在这里处理邀请码逻辑
  99. this.setData({
  100. inviteCode,
  101. });
  102. } else {
  103. console.log('未找到邀请码参数');
  104. }
  105. }
  106. } catch (error) {
  107. console.log('错误:', error);
  108. }
  109. // console.log(options, "options");
  110. },
  111. reCode() {
  112. // this.getUrl();
  113. },
  114. toPrivacy() {
  115. wx.navigateTo({
  116. url: "/pages/privacy/privacy",
  117. });
  118. },
  119. toHome() {
  120. wx.switchTab({
  121. url: "/pages/home/index/index",
  122. });
  123. },
  124. goxy() {
  125. wx.navigateTo({
  126. url: "/pages/volunteer/worldXyi/worldXyi",
  127. });
  128. },
  129. getUrl() {
  130. this.data.uuid = this.get_uuid();
  131. this.setData({
  132. url: App.apiRoot + "captcha.jpg?uuid=" + this.data.uuid,
  133. });
  134. },
  135. change(e) {
  136. console.log(e);
  137. if (e.detail.value.length > 0) {
  138. this.setData({
  139. isCheck: true,
  140. });
  141. } else {
  142. this.setData({
  143. isCheck: false,
  144. });
  145. }
  146. },
  147. updateUserInfo(e) {
  148. let _this = this;
  149. wx.getUserProfile({
  150. desc: "用于完善会员资料", // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  151. success: (res) => {
  152. var userinfo = res.userInfo;
  153. _this.data.params.memberphoto = userinfo.avatarUrl;
  154. _this.data.params.vipname = userinfo.nickName;
  155. },
  156. });
  157. return;
  158. App._post_form(
  159. "member/apiUpdateMeberInfo",
  160. "application/json",
  161. JSON.stringify(_this.data.allData),
  162. function (res) {
  163. if (res.code === 0) {
  164. wx.showLoading({
  165. title: "保存中...",
  166. duration: 1000,
  167. });
  168. _this.PushVipInfo();
  169. if (_this.data.nolink === true) {
  170. wx.switchTab({
  171. url: "/pages/my/index/index",
  172. });
  173. } else if (_this.data.nolink === false) {
  174. wx.navigateBack({
  175. delta: 1,
  176. });
  177. }
  178. }
  179. }
  180. );
  181. },
  182. getImgCode(e) {
  183. this.data.imgCode = e.detail.value;
  184. },
  185. //统计积分(每日登录)
  186. addScore: function () {
  187. if (!util.getUserId()) {
  188. return;
  189. }
  190. wx.showLoading({
  191. title: "努力加载中...",
  192. });
  193. App._post_form(
  194. "scoreStu/dailyLogin",
  195. "", {
  196. stuId: util.getUserId(),
  197. },
  198. function (res) {
  199. wx.hideToast();
  200. }
  201. );
  202. },
  203. getPhoneNumber(e) {
  204. const that = this;
  205. if (e.detail.errMsg != "getPhoneNumber:ok") {
  206. return;
  207. }
  208. var params = {
  209. encryptedData: e.detail.encryptedData,
  210. iv: e.detail.iv,
  211. sessionKey: wx.getStorageSync("session_key"),
  212. };
  213. App._post_form(
  214. "member/decodeUserInfo?encryptedData=" +
  215. params.encryptedData +
  216. "&iv=" +
  217. params.iv +
  218. "&sessionKey=" +
  219. params.sessionKey,
  220. "application/json;charset=UTF-8",
  221. params,
  222. function (data) {
  223. if (data.code == 0) {
  224. that.setData({
  225. ['params.mobile']: data.msg,
  226. disabled: true
  227. });
  228. }
  229. },
  230. function (err) {
  231. wx.showToast({
  232. title: "请求失败",
  233. icon: "none",
  234. duration: 3500,
  235. });
  236. return;
  237. }
  238. );
  239. },
  240. handleAgreePrivacyAuthorization() { },
  241. getUserInfo() {
  242. if (!this.data.isCheck) {
  243. wx.showToast({
  244. title: "请勾选隐私协议",
  245. icon: "none",
  246. duration: 3500,
  247. });
  248. return;
  249. }
  250. let that = this;
  251. that.login();
  252. // wx.getUserProfile({
  253. // desc: "用于确认用户身份",
  254. // success: (res) => {
  255. // // wx.setStorageSync("userInfo", res.userInfo);
  256. // // that.setData({
  257. // // ["params.memberphoto"]: res.userInfo.avatarUrl,
  258. // // ["params.vipname"]: res.userInfo.nickName,
  259. // // });
  260. // that.login();
  261. // },
  262. // fail(err) {
  263. // wx.showToast({
  264. // title: "获取用户信息失败",
  265. // icon: "none",
  266. // });
  267. // },
  268. // });
  269. },
  270. login() {
  271. let that = this;
  272. var pr = {
  273. openid: wx.getStorageSync("openid"),
  274. // tel: this.data.telInput,
  275. // inviteCode: this.data.inviteCode,
  276. // memberPhoto: this.data.params.memberphoto,
  277. // vipname: this.data.params.vipname,
  278. };
  279. wx.showLoading({
  280. title: "提交中...",
  281. });
  282. App._post_form(
  283. "auth/appletsLogin",
  284. "application/json;charset=UTF-8",
  285. pr,
  286. function (res) {
  287. if (res.code === 0) {
  288. if (res.memberinfo) {
  289. wx.setStorageSync("USER", res.memberinfo);
  290. that.setData({
  291. "params.id": res.memberinfo.id,
  292. });
  293. that.addScore();
  294. if (that.data.meetId != "") {
  295. //跳到我的约见详情页
  296. wx.reLaunch({
  297. url: `/meet/meet/addmeet?meetId=${this.data.meetId}&expertId=${this.data.expertId}&memberId=${this.data.memberId}`,
  298. success() { },
  299. });
  300. } else {
  301. wx.reLaunch({
  302. url: "/pages/home/index/index?login=1",
  303. success() {
  304. let pages = getCurrentPages();
  305. let prevpage = pages[pages.length - 1];
  306. if (prevpage.hasOwnProperty("loadCity")) {
  307. prevpage.loadCity();
  308. }
  309. },
  310. });
  311. }
  312. return;
  313. }
  314. that.setData({
  315. loginPopup: true
  316. })
  317. return false
  318. }
  319. },
  320. function (err) {
  321. wx.showToast({
  322. title: "请求失败",
  323. icon: "none",
  324. duration: 3500,
  325. });
  326. return;
  327. }
  328. );
  329. },
  330. closePopup() {
  331. this.setData({
  332. loginPopup: false
  333. })
  334. },
  335. handleRegister() {
  336. // if (!this.data.params.imgCode) {
  337. // return util.alert("请输入图形验证码");
  338. // }
  339. // if (!this.data.params.code && !this.data.disabled) {
  340. // return util.alert("请输入短信证码");
  341. // }
  342. // if (!this.data.isCheck) {
  343. // return util.alert("请勾选隐私协议");
  344. // }
  345. // let userInfo = wx.getStorageSync("userInfo");
  346. let form = {
  347. // captcha: this.data.imgCode,
  348. inviteCode: this.data.inviteCode,
  349. memberPhoto: this.data.params.memberphoto,
  350. mobile: this.data.params.mobile,
  351. name: this.data.params.vipname,
  352. openid: wx.getStorageSync("openid"),
  353. // shortMessageCode: this.data.code,
  354. };
  355. App._post_form(
  356. "auth/appletsRegistered",
  357. "application/json;charset=UTF-8",
  358. form,
  359. (res) => {
  360. if (res.code === 0) {
  361. wx.setStorageSync("USER", res.memberinfo);
  362. console.log(this.data.meetId, 11111111)
  363. if (this.data.meetId != "") {
  364. wx.reLaunch({
  365. url: `/meet/meet/addmeet?meetId=${this.data.meetId}&expertId=${this.data.expertId}&memberId=${this.data.memberId}`,
  366. });
  367. } else {
  368. wx.reLaunch({
  369. url: "/pages/home/index/index?login=1",
  370. });
  371. }
  372. }
  373. }
  374. );
  375. },
  376. register() {
  377. if (!this.data.params.memberphoto) {
  378. return tools.alert("请先获取头像信息");
  379. }
  380. if (!this.data.params.vipname) {
  381. return tools.alert("请先获取昵称信息");
  382. }
  383. if (!this.data.params.mobile) {
  384. return tools.alert("请先绑定手机号");
  385. }
  386. // wx.setStorageSync("userInfo", {
  387. // avatarUrl: this.data.params.memberphoto,
  388. // nickName: this.data.params.vipname
  389. // });
  390. this.handleRegister();
  391. return
  392. wx.navigateTo({
  393. url: "/pages/register/register?inviteCode=" +
  394. this.data.inviteCode +
  395. "&openid=" +
  396. this.data.openid +
  397. "&meetId=" +
  398. this.data.meetId +
  399. "&expertId=" +
  400. this.data.expertId +
  401. "&memberId=" +
  402. this.data.memberId,
  403. });
  404. return;
  405. },
  406. //获取头像
  407. getUserImg(res) {
  408. let that = this;
  409. wx.showLoading({
  410. title: '上传中',
  411. });
  412. let apiUrl = App.apiRoot + 'file/upload';
  413. let a = wx.uploadFile({
  414. url: apiUrl,
  415. filePath: res.detail.avatarUrl,
  416. name: 'file',
  417. success: (res) => {
  418. // console.log("上传成功", res)
  419. const newData = JSON.parse(res.data)
  420. if (newData.code == '000000') {
  421. that.setData({
  422. ['params.memberphoto']: newData.data
  423. });
  424. wx.hideLoading();
  425. }
  426. },
  427. fail(err) {
  428. // console.log("上传失败", err)
  429. wx.hideLoading();
  430. },
  431. });
  432. },
  433. getNickname(e) {
  434. let name = e.detail.value.trim();
  435. this.setData({
  436. ['params.vipname']: name
  437. });
  438. console.log('this.params', this.data.params);
  439. },
  440. formSubmit(e) {
  441. let _this = this;
  442. if (e.detail.value) {
  443. let value = e.detail.value;
  444. if (!value.tel) {
  445. _this.setData({
  446. pass: false,
  447. });
  448. return;
  449. } else {
  450. if (!mobile.test(_this.data.telInput)) {
  451. _this.setData({
  452. pass: false,
  453. });
  454. return;
  455. }
  456. }
  457. if (!value.code) {
  458. _this.setData({
  459. cpass: false,
  460. cfocus: false,
  461. });
  462. return;
  463. }
  464. wx.login({
  465. success(res) {
  466. if (res.code) {
  467. // 发起网络请求
  468. let parm = {
  469. tel: value.tel,
  470. code: res.code,
  471. authCode: value.code,
  472. };
  473. App._post_form(
  474. "member/appLogin",
  475. "application/json",
  476. JSON.stringify(parm),
  477. function (res) {
  478. if (res.code === 0) {
  479. wx.setStorageSync("USER", res.memberinfo);
  480. wx.reLaunch({
  481. url: "/pages/home/index/index",
  482. });
  483. } else {
  484. wx.showModal({
  485. title: "温馨提示",
  486. content: res.msg,
  487. showCancel: false,
  488. });
  489. }
  490. }
  491. );
  492. } else {
  493. console.log("登录失败!" + res.errMsg);
  494. }
  495. },
  496. });
  497. }
  498. },
  499. sendCode() {
  500. let _this = this;
  501. if (_this.data.telInput) {
  502. if (!mobile.test(_this.data.telInput)) {
  503. _this.setData({
  504. pass: false,
  505. cfocus: true,
  506. });
  507. return;
  508. }
  509. if (_this.data.countstatus === false) {
  510. if (_this.data.imgCode) {
  511. App._post_form(
  512. "verification/verKey",
  513. "", {
  514. phone: _this.data.telInput,
  515. code: _this.data.imgCode,
  516. uuid: _this.data.uuid,
  517. },
  518. function (res) {
  519. if (res.code === 0) {
  520. setTimeout(() => {
  521. wx.showToast({
  522. title: "发送成功,请查收!",
  523. icon: "none",
  524. duration: 2000,
  525. });
  526. }, 200);
  527. // _this.data.code = res.key;
  528. } else {
  529. console.log(res);
  530. wx.showToast({
  531. title: res.msg,
  532. icon: "none",
  533. duration: 2000,
  534. });
  535. }
  536. }
  537. );
  538. } else {
  539. wx.showToast({
  540. title: "请输入图形验证码后获取短信验证码",
  541. icon: "none",
  542. duration: 3500,
  543. });
  544. return;
  545. }
  546. }
  547. util.getPhoneCode(60, _this, _this.data.countstatus);
  548. } else {
  549. _this.setData({
  550. pass: false,
  551. });
  552. }
  553. },
  554. focus() {
  555. this.setData({
  556. focus: true,
  557. pass: true,
  558. });
  559. },
  560. blur(e) {
  561. if (e.detail.value) {
  562. this.data.telInput = e.detail.value;
  563. }
  564. this.setData({
  565. focus: false,
  566. pass: true,
  567. });
  568. },
  569. cfocus() {
  570. this.setData({
  571. cfocus: true,
  572. cpass: true,
  573. });
  574. },
  575. cblur() {
  576. this.setData({
  577. cfocus: false,
  578. });
  579. },
  580. get_uuid() {
  581. var s = [];
  582. var hexDigits = "0123456789abcdef";
  583. for (var i = 0; i < 36; i++) {
  584. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  585. }
  586. s[14] = "4";
  587. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
  588. s[8] = s[13] = s[18] = s[23] = "-";
  589. var uuid = s.join("");
  590. return uuid;
  591. },
  592. /**
  593. * 生命周期函数--监听页面初次渲染完成
  594. */
  595. onReady: function () { },
  596. /**
  597. * 生命周期函数--监听页面显示
  598. */
  599. onShow: function () { },
  600. /**
  601. * 生命周期函数--监听页面隐藏
  602. */
  603. onHide: function () { },
  604. /**
  605. * 生命周期函数--监听页面卸载
  606. */
  607. onUnload: function () { },
  608. /**
  609. * 页面相关事件处理函数--监听用户下拉动作
  610. */
  611. onPullDownRefresh: function () { },
  612. /**
  613. * 页面上拉触底事件的处理函数
  614. */
  615. onReachBottom: function () { },
  616. /**
  617. * 用户点击右上角分享
  618. */
  619. onShareAppMessage: function () { },
  620. inviteCodeInput(e) {
  621. this.setData({
  622. inviteCode: e.detail.value,
  623. });
  624. },
  625. });