login.js 13 KB

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