user.js 3.9 KB

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