user.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * 用户相关服务
  3. */
  4. const util = require('../utils/util.js');
  5. const api = require('../api/api.js');
  6. /**
  7. * Promise封装wx.checkSession
  8. */
  9. function checkSession() {
  10. return new Promise(function(resolve, reject) {
  11. wx.checkSession({
  12. success: function() {
  13. resolve(true);
  14. },
  15. fail: function() {
  16. reject(false);
  17. }
  18. })
  19. });
  20. }
  21. /**
  22. * Promise封装wx.login
  23. */
  24. function login() {
  25. return new Promise(function(resolve, reject) {
  26. wx.login({
  27. success: function(res) {
  28. if (res.code) {
  29. resolve(res);
  30. } else {
  31. reject(res);
  32. }
  33. },
  34. fail: function(err) {
  35. reject(err);
  36. }
  37. });
  38. });
  39. }
  40. /**
  41. * 调用微信登录
  42. */
  43. function loginByWeixin(userInfo) {
  44. return new Promise(function(resolve, reject) {
  45. console.info(userInfo);
  46. //登录远程服务器
  47. util.request(api.AuthLoginByWeixin, userInfo
  48. , 'POST').then(res => {
  49. if (res.errno === 0) {
  50. //存储用户信息
  51. wx.setStorageSync('userInfo', res.data.userInfo);
  52. resolve(res);
  53. } else {
  54. reject(res);
  55. }
  56. }).catch((err) => {
  57. reject(err);
  58. });
  59. });
  60. }
  61. /**
  62. * 判断用户是否登录
  63. */
  64. function checkLogin() {
  65. return new Promise(function(resolve, reject) {
  66. if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
  67. resolve(true);
  68. } else {
  69. reject(false);
  70. }
  71. });
  72. }
  73. function cartadd(num){
  74. let count=wx.getStorageSync('cartcount');
  75. console.info('count'+count);
  76. if(count==null) count=0;
  77. count=count+num;
  78. wx.setStorageSync('cartcount',count);
  79. // wx.setTabBarBadge({
  80. // index: 2,
  81. // text: ''+count+''
  82. // })
  83. }
  84. module.exports = {
  85. loginByWeixin,
  86. checkLogin,
  87. login,
  88. cartadd
  89. };