login.js 15 KB

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