tool.js 3.7 KB

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