user.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * 用户相关服务
  3. */
  4. const util = require('../utils/util.js');
  5. const api = require('../config/api.js');
  6. /**
  7. * 调用微信登录
  8. */
  9. function loginByWeixin(userInfo) {
  10. debugger;
  11. let code = null;
  12. return new Promise(function (resolve, reject) {
  13. return util.login().then((res) => {
  14. code = res.code;
  15. return userInfo;
  16. }).then((userInfo) => {
  17. //登录远程服务器
  18. util.request(api.AuthLoginByWeixin, { code: code, userInfo: userInfo }, 'POST', 'application/json').then(res => {
  19. if (res.errno === 0) {
  20. //存储用户信息
  21. res.data.userInfo.userId = res.data.userId;
  22. res.data.userInfo.sessionKey = res.data.sessionKey;
  23. res.data.userInfo.isAdmin = res.data.isAdmin;
  24. wx.setStorageSync('userInfo', res.data.userInfo);
  25. wx.setStorageSync('token', res.data.token);
  26. resolve(res);
  27. } else {
  28. util.showErrorToast(res.errmsg)
  29. reject(res);
  30. }
  31. }).catch((err) => {
  32. reject(err);
  33. });
  34. }).catch((err) => {
  35. reject(err);
  36. })
  37. });
  38. }
  39. /**
  40. * 判断用户是否登录
  41. */
  42. function checkLogin() {
  43. return new Promise(function (resolve, reject) {
  44. if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
  45. util.checkSession().then(() => {
  46. resolve(true);
  47. }).catch(() => {
  48. reject(false);
  49. });
  50. } else {
  51. reject(false);
  52. }
  53. });
  54. }
  55. module.exports = {
  56. loginByWeixin,
  57. checkLogin,
  58. };