tool.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const util = require('../utils/util.js');
  2. const openidStatus = () => {
  3. return new Promise((resolve, reject) => {
  4. // if (wx.getStorageSync('openId')) {
  5. // resolve([wx.getStorageSync('openId'), wx.getStorageSync('sessionKey')]);
  6. // } else {
  7. wx.login({
  8. success: (res) => {
  9. if (res.code) {
  10. wx.request({
  11. url: util.globalData.publicUrl + '/wx/user/wxbfa171fdd4000e03/login',
  12. method: "GET",
  13. data: {
  14. code: res.code
  15. },
  16. success: res => {
  17. console.log(res.data.openid,'我是wx.login的openid')
  18. console.log(res.data.sessionKey,'我是wx.login的sessionKey')
  19. if (res.statusCode == 200) {
  20. wx.setStorageSync('openId', res.data.openid)
  21. wx.setStorageSync('sessionKey', res.data.sessionKey)
  22. resolve([res.data.openid, res.data.sessionKey]);
  23. }
  24. }
  25. });
  26. } else {
  27. reject(res.errMsg);
  28. }
  29. }
  30. })
  31. // }
  32. })
  33. };
  34. function formatTime(date) {
  35. var year = date.getFullYear()
  36. return year
  37. }
  38. const formDetails = (type) => {
  39. return new Promise((resolve, reject) => {
  40. wx.request({
  41. url: util.globalData.publicUrl + '/wx/member/' + wx.getStorageSync('openId') + '/options',
  42. method: "GET",
  43. data: {
  44. type
  45. },
  46. success: res => {
  47. // console.log(res)
  48. if (res.data.code == 0) {
  49. if (res.data.options) {
  50. let labelArr = res.data.options.map((obj, index) => {
  51. return obj.dictLabel;
  52. })
  53. let valueArr = res.data.options.map((obj, index) => {
  54. return obj.dictValue;
  55. })
  56. resolve([labelArr, valueArr])
  57. }
  58. } else {
  59. reject(res)
  60. }
  61. }
  62. })
  63. })
  64. };
  65. // 判断是否登录
  66. const isLogin = () => {
  67. return new Promise((resolve, reject) => {
  68. wx.request({
  69. url: util.globalData.publicUrl + '/wx/member/' + wx.getStorageSync('openId') + '/info',
  70. method: "get",
  71. success: (res) => {
  72. console.log(res)
  73. if (res.data.code == 0 && res.data.info.avatar) {
  74. resolve(res.data.info.avatar);
  75. } else {
  76. reject(res)
  77. }
  78. }
  79. })
  80. })
  81. };
  82. // 判断是否完善个人信息
  83. const isFinishInfo = () => {
  84. return new Promise((resolve, reject) => {
  85. wx.request({
  86. url: util.globalData.publicUrl + '/wx/member/' + wx.getStorageSync('openId') + '/info',
  87. method: "get",
  88. success: (res) => {
  89. console.log(res)
  90. if (res.data.code == 0) {
  91. resolve(res.data.info)
  92. } else {
  93. reject(res.data.code)
  94. }
  95. }
  96. })
  97. })
  98. };
  99. const isFinishTargetInfo = () => {
  100. return new Promise((resolve, reject) => {
  101. wx.request({
  102. url: util.globalData.publicUrl + '/wx/member/' + wx.getStorageSync('openId') + '/standard',
  103. method: "get",
  104. success: (res) => {
  105. console.log(res)
  106. if (res.data.code == 0) {
  107. resolve(res.data.standard);
  108. } else {
  109. reject(res.data.code)
  110. }
  111. }
  112. })
  113. })
  114. };
  115. module.exports = {
  116. openidStatus,
  117. formDetails,
  118. formatTime,
  119. isLogin,
  120. isFinishInfo,
  121. isFinishTargetInfo
  122. }