user.js 1.5 KB

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