user.js 1.3 KB

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