user.js 1.4 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.isAdmin = res.data.isAdmin
  22. wx.setStorageSync('userInfo', res.data.userInfo);
  23. wx.setStorageSync('token', res.data.token);
  24. resolve(res);
  25. } else {
  26. util.showErrorToast(res.errmsg)
  27. reject(res);
  28. }
  29. }).catch((err) => {
  30. reject(err);
  31. });
  32. }).catch((err) => {
  33. reject(err);
  34. })
  35. });
  36. }
  37. /**
  38. * 判断用户是否登录
  39. */
  40. function checkLogin() {
  41. return new Promise(function (resolve, reject) {
  42. if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
  43. util.checkSession().then(() => {
  44. resolve(true);
  45. }).catch(() => {
  46. reject(false);
  47. });
  48. } else {
  49. reject(false);
  50. }
  51. });
  52. }
  53. module.exports = {
  54. loginByWeixin,
  55. checkLogin,
  56. };