login.js 15 KB

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