tool.js 955 B

1234567891011121314151617181920212223242526272829303132333435
  1. const util = require('../utils/util.js');
  2. const openidStatus = () => {
  3. console.log(wx.getStorageSync('openId'),'缓存里是否有openid')
  4. if (wx.getStorageSync('openId')) {
  5. return wx.getStorageSync('openId')
  6. } else {
  7. return new Promise((resolve, reject) => {
  8. wx.login({
  9. success: (res) => {
  10. if (res.code) {
  11. wx.request({
  12. url: util.globalData.publicUrl + '/wx/getAppletOpenId',
  13. method: "GET",
  14. data: {
  15. code: res.code
  16. },
  17. success: res => {
  18. if (res.data.status == 200) {
  19. wx.setStorageSync('openId', res.data.data.openid)
  20. return res.data.data.openid;
  21. }
  22. }
  23. });
  24. } else {
  25. console.log('获取用户登录态失败!' + res.errMsg);
  26. }
  27. }
  28. })
  29. })
  30. }
  31. };
  32. module.exports = {
  33. openidStatus
  34. }