user.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * 用户相关服务
  3. */
  4. const util = require('../utils/util.js');
  5. const api = require('../config/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. * 判断是否实名注册过
  23. */
  24. function checkRegisted(){
  25. return new Promise(function(resolve, reject) {
  26. //是否实名
  27. util.request(api.AuthIsCert).then(function (res) {
  28. resolve(res.data.isnid);
  29. }).catch(()=>{
  30. reject(false);
  31. });
  32. });
  33. }
  34. /**
  35. * Promise封装wx.login
  36. */
  37. function login() {
  38. return new Promise(function(resolve, reject) {
  39. wx.login({
  40. success: function(res) {
  41. if (res.code) {
  42. resolve(res);
  43. } else {
  44. reject(res);
  45. }
  46. },
  47. fail: function(err) {
  48. reject(err);
  49. }
  50. });
  51. });
  52. }
  53. /**
  54. * 返回用户购物车的数量
  55. */
  56. function cartcount(){
  57. util.request(api.CartGoodsCount, 'POST').then(res => {
  58. if (res.errno === 0) {
  59. wx.setTabBarBadge({
  60. index: 2,
  61. text: ''+res.data+''
  62. })
  63. wx.setStorageSync('cartcount', res.data);
  64. }
  65. })
  66. }
  67. /**
  68. *
  69. *初始化购物车数量
  70. */
  71. function cartshow(){
  72. // let count=wx.getStorageSync('cartcount');
  73. // if(count==0){
  74. // wx.hideTabBarRedDot({
  75. // index: 2,
  76. // });
  77. // }else{
  78. // wx.setTabBarBadge({
  79. // index: 2,
  80. // text: ''+count+''
  81. // })
  82. // }
  83. }
  84. function cartadd(num){
  85. let count=wx.getStorageSync('cartcount');
  86. console.info('count'+count);
  87. if(count==null) count=0;
  88. count=count+num;
  89. wx.setStorageSync('cartcount',count);
  90. wx.setTabBarBadge({
  91. index: 2,
  92. text: ''+count+''
  93. })
  94. }
  95. /**
  96. * 调用微信登录
  97. */
  98. function loginByWeixin(userInfo) {
  99. return new Promise(function(resolve, reject) {
  100. console.info(userInfo);
  101. //登录远程服务器
  102. util.request(api.AuthLoginByWeixin, userInfo
  103. , 'POST').then(res => {
  104. if (res.errno === 0) {
  105. //存储用户信息
  106. wx.setStorageSync('userInfo', res.data.userInfo);
  107. resolve(res);
  108. } else {
  109. reject(res);
  110. }
  111. }).catch((err) => {
  112. reject(err);
  113. });
  114. });
  115. }
  116. /**
  117. * 判断用户是否登录
  118. */
  119. function checkLogin() {
  120. return new Promise(function(resolve, reject) {
  121. if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
  122. checkSession().then(() => {
  123. resolve(true);
  124. }).catch(() => {
  125. reject(false);
  126. });
  127. } else {
  128. reject(false);
  129. }
  130. });
  131. }
  132. /**微信手机号码解密 */
  133. function denPhone(e){
  134. return new Promise(function(resolve, reject) {
  135. //登录远程服务器
  136. util.request(api.AuthDenPhone, {
  137. iv: e.iv,
  138. encryptedData:e.encryptedData
  139. }, 'POST').then(res => {
  140. if (res.errno === 0) {
  141. resolve(res);
  142. } else {
  143. reject(res);
  144. console.log(res);
  145. }
  146. }).catch((err) => {
  147. reject(err);
  148. });
  149. })
  150. }
  151. module.exports = {
  152. loginByWeixin,
  153. checkLogin,
  154. denPhone,
  155. login,
  156. checkRegisted,
  157. cartcount,
  158. cartadd,
  159. cartshow
  160. };