tool.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. })
  83. };
  84. // 判断是否完善个人信息
  85. const isFinishInfo = () => {
  86. return new Promise((resolve, reject) => {
  87. wx.request({
  88. url: util.globalData.publicUrl + '/wx/member/' + wx.getStorageSync('openId') + '/info',
  89. method: "get",
  90. success: (res) => {
  91. console.log(res)
  92. if (res.data.code == 0&&res.data.info.name&&res.data.info.tel) {
  93. resolve(res.data.info)
  94. } else {
  95. reject(res.data.code)
  96. }
  97. }
  98. })
  99. })
  100. };
  101. const isFinishTargetInfo = () => {
  102. return new Promise((resolve, reject) => {
  103. wx.request({
  104. url: util.globalData.publicUrl + '/wx/member/' + wx.getStorageSync('openId') + '/standard',
  105. method: "get",
  106. success: (res) => {
  107. console.log(res)
  108. if (res.data.code == 0) {
  109. resolve(res.data.standard);
  110. } else {
  111. reject(res.data.code)
  112. }
  113. }
  114. })
  115. })
  116. };
  117. module.exports = {
  118. openidStatus,
  119. formDetails,
  120. formatTime,
  121. isLogin,
  122. isFinishInfo,
  123. isFinishTargetInfo
  124. }