tool.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.getSetting({
  69. success: res => {
  70. console.log(res)
  71. if (res.authSetting['scope.userInfo']) {
  72. wx.getUserInfo({
  73. success: res => {
  74. resolve(res)
  75. }
  76. })
  77. } else {
  78. reject(false)
  79. }
  80. }
  81. })
  82. // wx.request({
  83. // url: util.globalData.publicUrl + '/wx/member/' + wx.getStorageSync('openId') + '/info',
  84. // method: "get",
  85. // success: (res) => {
  86. // console.log(res)
  87. // if (res.data.code == 0 && res.data.info.avatar) {
  88. // resolve(res.data.info.avatar);
  89. // } else {
  90. // reject(res)
  91. // }
  92. // }
  93. // })
  94. })
  95. };
  96. // 判断是否完善个人信息
  97. const isFinishInfo = () => {
  98. return new Promise((resolve, reject) => {
  99. wx.request({
  100. url: util.globalData.publicUrl + '/wx/member/' + wx.getStorageSync('openId') + '/info',
  101. method: "get",
  102. success: (res) => {
  103. console.log(res)
  104. if (res.data.code == 0&&res.data.info.name&&res.data.info.tel) {
  105. resolve(res.data.info)
  106. } else {
  107. reject(res.data.code)
  108. }
  109. }
  110. })
  111. })
  112. };
  113. const isFinishTargetInfo = () => {
  114. return new Promise((resolve, reject) => {
  115. wx.request({
  116. url: util.globalData.publicUrl + '/wx/member/' + wx.getStorageSync('openId') + '/standard',
  117. method: "get",
  118. success: (res) => {
  119. console.log(res)
  120. if (res.data.code == 0) {
  121. resolve(res.data.standard);
  122. } else {
  123. reject(res.data.code)
  124. }
  125. }
  126. })
  127. })
  128. };
  129. module.exports = {
  130. openidStatus,
  131. formDetails,
  132. formatTime,
  133. isLogin,
  134. isFinishInfo,
  135. isFinishTargetInfo
  136. }