user.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * 用户相关服务
  3. */
  4. const util = require('../utils/util.js');
  5. const api = require('../config/api.js');
  6. var app = getApp();
  7. /**
  8. * 调用微信登录
  9. */
  10. function loginByWeixin() {
  11. let code = null;
  12. return new Promise(function(resolve, reject) {
  13. return util.login().then((res) => {
  14. code = res.code;
  15. return util.getUserInfo(); //接口改了,用不了
  16. }).then((userInfo) => {
  17. //登录远程服务器
  18. util.request(api.AuthLoginByWeixin, {
  19. code: code,
  20. userInfo: userInfo
  21. }, 'POST').then(res => {
  22. if (res.errno === 0) {
  23. //存储用户信息
  24. wx.setStorageSync('userInfo', res.data.userInfo);
  25. wx.setStorageSync('token', res.data.token);
  26. resolve(res);
  27. } else {
  28. reject(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. resolve(true);
  45. // util.checkSession().then(() => {
  46. // resolve(true);
  47. // }).catch(() => {
  48. // reject(false); //session过期
  49. // });
  50. } else {
  51. reject(false); //没有登陆过
  52. }
  53. });
  54. }
  55. /**
  56. * 判断用户是否已授权获取userInfo
  57. */
  58. function checkUserAuth() {
  59. return new Promise(function(resolve, reject) {
  60. wx.getSetting({
  61. success: res => {
  62. if (res.authSetting['scope.userInfo']) {
  63. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  64. resolve(true);
  65. }else{
  66. reject(false);
  67. }
  68. }
  69. })
  70. })
  71. }
  72. module.exports = {
  73. loginByWeixin,
  74. checkLogin,
  75. checkUserAuth,
  76. };