login.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. if (res.yicLogin && res.yicLogin.token) wx.setStorageSync("token", res.yicLogin.token);
  286. wx.setStorageSync("USER", res.memberinfo);
  287. that.setData({
  288. "params.id": res.memberinfo.id,
  289. });
  290. that.addScore();
  291. if (that.data.meetId != "") {
  292. //跳到我的约见详情页
  293. wx.reLaunch({
  294. url: `/meet/meet/addmeet?meetId=${this.data.meetId}&expertId=${this.data.expertId}&memberId=${this.data.memberId}`,
  295. success() { },
  296. });
  297. } else {
  298. wx.reLaunch({
  299. url: "/pages/home/index/index?login=1",
  300. success() {
  301. let pages = getCurrentPages();
  302. let prevpage = pages[pages.length - 1];
  303. if (prevpage.hasOwnProperty("loadCity")) {
  304. prevpage.loadCity();
  305. }
  306. },
  307. });
  308. }
  309. return;
  310. }
  311. that.setData({
  312. loginPopup: true
  313. })
  314. return false
  315. }
  316. },
  317. function (err) {
  318. wx.showToast({
  319. title: "请求失败",
  320. icon: "none",
  321. duration: 3500,
  322. });
  323. return;
  324. }
  325. );
  326. },
  327. closePopup() {
  328. this.setData({
  329. loginPopup: false
  330. })
  331. },
  332. handleRegister() {
  333. // if (!this.data.params.imgCode) {
  334. // return util.alert("请输入图形验证码");
  335. // }
  336. // if (!this.data.params.code && !this.data.disabled) {
  337. // return util.alert("请输入短信证码");
  338. // }
  339. // if (!this.data.isCheck) {
  340. // return util.alert("请勾选隐私协议");
  341. // }
  342. // let userInfo = wx.getStorageSync("userInfo");
  343. let form = {
  344. // captcha: this.data.imgCode,
  345. inviteCode: this.data.inviteCode,
  346. memberPhoto: this.data.params.memberphoto,
  347. mobile: this.data.params.mobile,
  348. name: this.data.params.vipname,
  349. openid: wx.getStorageSync("openid"),
  350. // shortMessageCode: this.data.code,
  351. };
  352. App._post_form(
  353. "auth/appletsRegistered",
  354. "application/json;charset=UTF-8",
  355. form,
  356. (res) => {
  357. if (res.code === 0) {
  358. if (res.yicLogin && res.yicLogin.token) wx.setStorageSync("token", res.yicLogin.token);
  359. wx.setStorageSync("USER", res.memberinfo);
  360. // console.log(this.data.meetId, 11111111)
  361. if (this.data.meetId != "") {
  362. wx.reLaunch({
  363. url: `/meet/meet/addmeet?meetId=${this.data.meetId}&expertId=${this.data.expertId}&memberId=${this.data.memberId}`,
  364. });
  365. } else {
  366. wx.reLaunch({
  367. url: "/pages/home/index/index?login=1",
  368. });
  369. }
  370. }
  371. }
  372. );
  373. },
  374. register() {
  375. if (!this.data.params.memberphoto) {
  376. return tools.alert("请先获取头像信息");
  377. }
  378. if (!this.data.params.vipname) {
  379. return tools.alert("请先获取昵称信息");
  380. }
  381. if (!this.data.params.mobile) {
  382. return tools.alert("请先绑定手机号");
  383. }
  384. // wx.setStorageSync("userInfo", {
  385. // avatarUrl: this.data.params.memberphoto,
  386. // nickName: this.data.params.vipname
  387. // });
  388. this.handleRegister();
  389. return
  390. wx.navigateTo({
  391. url: "/pages/register/register?inviteCode=" +
  392. this.data.inviteCode +
  393. "&openid=" +
  394. this.data.openid +
  395. "&meetId=" +
  396. this.data.meetId +
  397. "&expertId=" +
  398. this.data.expertId +
  399. "&memberId=" +
  400. this.data.memberId,
  401. });
  402. return;
  403. },
  404. //获取头像
  405. getUserImg(res) {
  406. let that = this;
  407. wx.showLoading({
  408. title: '上传中',
  409. });
  410. let apiUrl = App.apiRoot + 'file/upload';
  411. let a = wx.uploadFile({
  412. url: apiUrl,
  413. filePath: res.detail.avatarUrl,
  414. name: 'file',
  415. success: (res) => {
  416. // console.log("上传成功", res)
  417. const newData = JSON.parse(res.data)
  418. if (newData.code == '000000') {
  419. that.setData({
  420. ['params.memberphoto']: newData.data
  421. });
  422. wx.hideLoading();
  423. }
  424. },
  425. fail(err) {
  426. // console.log("上传失败", err)
  427. wx.hideLoading();
  428. },
  429. });
  430. },
  431. getNickname(e) {
  432. let name = e.detail.value.trim();
  433. this.setData({
  434. ['params.vipname']: name
  435. });
  436. console.log('this.params', this.data.params);
  437. },
  438. formSubmit(e) {
  439. let _this = this;
  440. if (e.detail.value) {
  441. let value = e.detail.value;
  442. if (!value.tel) {
  443. _this.setData({
  444. pass: false,
  445. });
  446. return;
  447. } else {
  448. if (!mobile.test(_this.data.telInput)) {
  449. _this.setData({
  450. pass: false,
  451. });
  452. return;
  453. }
  454. }
  455. if (!value.code) {
  456. _this.setData({
  457. cpass: false,
  458. cfocus: false,
  459. });
  460. return;
  461. }
  462. wx.login({
  463. success(res) {
  464. if (res.code) {
  465. // 发起网络请求
  466. let parm = {
  467. tel: value.tel,
  468. code: res.code,
  469. authCode: value.code,
  470. };
  471. App._post_form(
  472. "member/appLogin",
  473. "application/json",
  474. JSON.stringify(parm),
  475. function (res) {
  476. if (res.code === 0) {
  477. if (res.yicLogin && res.yicLogin.token) wx.setStorageSync("token", res.yicLogin.token);
  478. wx.setStorageSync("USER", res.memberinfo);
  479. wx.reLaunch({
  480. url: "/pages/home/index/index",
  481. });
  482. } else {
  483. wx.showModal({
  484. title: "温馨提示",
  485. content: res.msg,
  486. showCancel: false,
  487. });
  488. }
  489. }
  490. );
  491. } else {
  492. console.log("登录失败!" + res.errMsg);
  493. }
  494. },
  495. });
  496. }
  497. },
  498. sendCode() {
  499. let _this = this;
  500. if (_this.data.telInput) {
  501. if (!mobile.test(_this.data.telInput)) {
  502. _this.setData({
  503. pass: false,
  504. cfocus: true,
  505. });
  506. return;
  507. }
  508. if (_this.data.countstatus === false) {
  509. if (_this.data.imgCode) {
  510. App._post_form(
  511. "verification/verKey",
  512. "", {
  513. phone: _this.data.telInput,
  514. code: _this.data.imgCode,
  515. uuid: _this.data.uuid,
  516. },
  517. function (res) {
  518. if (res.code === 0) {
  519. setTimeout(() => {
  520. wx.showToast({
  521. title: "发送成功,请查收!",
  522. icon: "none",
  523. duration: 2000,
  524. });
  525. }, 200);
  526. // _this.data.code = res.key;
  527. } else {
  528. console.log(res);
  529. wx.showToast({
  530. title: res.msg,
  531. icon: "none",
  532. duration: 2000,
  533. });
  534. }
  535. }
  536. );
  537. } else {
  538. wx.showToast({
  539. title: "请输入图形验证码后获取短信验证码",
  540. icon: "none",
  541. duration: 3500,
  542. });
  543. return;
  544. }
  545. }
  546. util.getPhoneCode(60, _this, _this.data.countstatus);
  547. } else {
  548. _this.setData({
  549. pass: false,
  550. });
  551. }
  552. },
  553. focus() {
  554. this.setData({
  555. focus: true,
  556. pass: true,
  557. });
  558. },
  559. blur(e) {
  560. if (e.detail.value) {
  561. this.data.telInput = e.detail.value;
  562. }
  563. this.setData({
  564. focus: false,
  565. pass: true,
  566. });
  567. },
  568. cfocus() {
  569. this.setData({
  570. cfocus: true,
  571. cpass: true,
  572. });
  573. },
  574. cblur() {
  575. this.setData({
  576. cfocus: false,
  577. });
  578. },
  579. get_uuid() {
  580. var s = [];
  581. var hexDigits = "0123456789abcdef";
  582. for (var i = 0; i < 36; i++) {
  583. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  584. }
  585. s[14] = "4";
  586. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
  587. s[8] = s[13] = s[18] = s[23] = "-";
  588. var uuid = s.join("");
  589. return uuid;
  590. },
  591. /**
  592. * 生命周期函数--监听页面初次渲染完成
  593. */
  594. onReady: function () { },
  595. /**
  596. * 生命周期函数--监听页面显示
  597. */
  598. onShow: function () { },
  599. /**
  600. * 生命周期函数--监听页面隐藏
  601. */
  602. onHide: function () { },
  603. /**
  604. * 生命周期函数--监听页面卸载
  605. */
  606. onUnload: function () { },
  607. /**
  608. * 页面相关事件处理函数--监听用户下拉动作
  609. */
  610. onPullDownRefresh: function () { },
  611. /**
  612. * 页面上拉触底事件的处理函数
  613. */
  614. onReachBottom: function () { },
  615. /**
  616. * 用户点击右上角分享
  617. */
  618. onShareAppMessage: function () { },
  619. inviteCodeInput(e) {
  620. this.setData({
  621. inviteCode: e.detail.value,
  622. });
  623. },
  624. });