login.js 14 KB

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