user.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. * 根据客户id 获取登录信息 让客户端登录
  23. * @param {}} userId
  24. */
  25. function getLoginInfo(orderSn) {
  26. return new Promise(function (resolve, reject) {
  27. //是否实名
  28. util.request(api.GetLoginInfo, {
  29. orderSn: orderSn
  30. })
  31. .then(function (res) {
  32. resolve(res.data);
  33. })
  34. .catch(() => {
  35. reject(false);
  36. });
  37. });
  38. }
  39. /**
  40. * 判断是否实名注册过
  41. */
  42. function checkRegisted() {
  43. return new Promise(function (resolve, reject) {
  44. //是否实名
  45. util.request(api.AuthIsCert)
  46. .then(function (res) {
  47. resolve(res.data.isnid);
  48. })
  49. .catch(() => {
  50. reject(false);
  51. });
  52. });
  53. }
  54. /**
  55. * Promise封装wx.login
  56. */
  57. function login() {
  58. return new Promise(function (resolve, reject) {
  59. uni.login({
  60. success: function (res) {
  61. if (res.code) {
  62. resolve(res);
  63. } else {
  64. reject(res);
  65. }
  66. },
  67. fail: function (err) {
  68. reject(err);
  69. }
  70. });
  71. });
  72. }
  73. /**
  74. * 获取当前登录人的openid
  75. * 静默授权时下单用
  76. */
  77. function userOpenId() {
  78. return new Promise(function (resolve, reject) {
  79. uni.login({
  80. success: function (res) {
  81. if (res.code) {
  82. util.request(api.AuthOpenId, {
  83. code: res.code
  84. })
  85. .then((response) => {
  86. resolve(response);
  87. })
  88. .catch((ress) => {
  89. reject(ress);
  90. });
  91. } else {
  92. reject(res);
  93. }
  94. },
  95. fail: function (err) {
  96. reject(err);
  97. }
  98. });
  99. });
  100. }
  101. /**
  102. * 返回用户购物车的数量
  103. */
  104. /**
  105. *
  106. *初始化购物车数量
  107. */
  108. /**
  109. * 调用微信登录
  110. */
  111. function loginByWeixin(userInfo) {
  112. return new Promise(function (resolve, reject) {
  113. console.info(userInfo);
  114. //登录远程服务器
  115. util.request(api.AuthLoginByWeixin, userInfo, 'POST')
  116. .then((res) => {
  117. if (res.errno === 0) {
  118. //存储用户信息
  119. uni.setStorageSync('userInfo', res.data.userInfo);
  120. resolve(res);
  121. } else {
  122. reject(res);
  123. }
  124. })
  125. .catch((err) => {
  126. reject(err);
  127. });
  128. });
  129. }
  130. /**
  131. * 判断用户是否登录
  132. */
  133. function checkLogin() {
  134. return new Promise(function (resolve, reject) {
  135. if (uni.getStorageSync('userInfo') && uni.getStorageSync('token')) {
  136. checkSession()
  137. .then(() => {
  138. resolve(true);
  139. })
  140. .catch(() => {
  141. reject(false);
  142. });
  143. } else {
  144. reject(false);
  145. }
  146. });
  147. }
  148. /**微信手机号码解密 */
  149. function denPhone(e) {
  150. return new Promise(function (resolve, reject) {
  151. //登录远程服务器
  152. util.request(
  153. api.AuthDenPhone,
  154. {
  155. iv: e.iv,
  156. encryptedData: e.encryptedData
  157. },
  158. 'POST'
  159. )
  160. .then((res) => {
  161. if (res.errno === 0) {
  162. resolve(res);
  163. } else {
  164. reject(res);
  165. console.log(res);
  166. }
  167. })
  168. .catch((err) => {
  169. reject(err);
  170. });
  171. });
  172. }
  173. module.exports = {
  174. loginByWeixin,
  175. checkLogin,
  176. denPhone,
  177. login,
  178. checkRegisted,
  179. getLoginInfo,
  180. userOpenId
  181. };