httpUtil.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const request = require('./WXHttpRequest.js');
  2. const url = require('./const.js');
  3. const util = require('./util.js');
  4. const httpRequestApi = require('./APIClient');
  5. const HOST = url.baseApi;
  6. function genAPIUrl(action) {
  7. return HOST + action;
  8. }
  9. // 登录
  10. function wxLogin(successcallback, failcallback) {
  11. wx.showToast({
  12. title: '登录中...',
  13. icon: 'loading',
  14. duration: 1500,
  15. mask: true
  16. })
  17. wx.login({
  18. success: function (res) {
  19. if (res.code) {
  20. // 获取openid
  21. console.log('openId', res.code)
  22. let url = genAPIUrl('wx/user/openId')
  23. let data = {
  24. code: res.code
  25. }
  26. return request.getInstance().url(url).data(data).send().success(successcallback).fail(failcallback);
  27. } else {
  28. console.log('获取用户登录态失败!' + res.errMsg)
  29. }
  30. }
  31. })
  32. }
  33. function userLoginRecord(uid) {
  34. if (wx.getStorageSync('uid')) {
  35. console.log('调用方法')
  36. let url = genAPIUrl('wx/loginLog');
  37. return request.getInstance().header({
  38. uid: wx.getStorageSync('uid')
  39. }).method('POST').url(url).send().success(()=>{}).fail(()=>{});
  40. }
  41. }
  42. //登录
  43. function getLoginSchedule(sessionKey, openId, successcallback, failcallback) {
  44. wx.getSetting({
  45. success: function (res) {
  46. // 已经授权,可以直接调用 getUserInfo 获取头像昵称
  47. wx.getUserInfo({
  48. withCredentials: true,
  49. success: function (res) {
  50. let url = genAPIUrl('wx/user/loginRegist');
  51. const userData = {
  52. encryptedData: res.encryptedData,
  53. sessionKey: sessionKey,
  54. iv: res.iv
  55. }
  56. request.getInstance().url(url).header({
  57. "content-type": "application/x-www-form-urlencoded; charset=UTF-8"
  58. }).data(userData).method('POST').send().success(res => {
  59. wx.hideToast()
  60. wx.showToast({
  61. title: '登录成功',
  62. icon: 'success',
  63. duration: 1500,
  64. mask: true
  65. })
  66. wx.setStorageSync('uid', res.data.data.data.uid)
  67. wx.setStorageSync('user', res.data.data.data)
  68. userLoginRecord()
  69. successcallback(res)
  70. });
  71. },
  72. fail: function (res) {
  73. failcallback(res)
  74. }
  75. })
  76. }
  77. })
  78. }
  79. // 获取原来的 openid 和 session_key
  80. function getOpenidSessionKey(successcallback, failcallback) {
  81. if (wx.getStorageSync('user')) {
  82. successcallback(
  83. wx.getStorageSync('user'),
  84. //分享小程序按钮
  85. wx.showShareMenu({
  86. withShareTicket: true
  87. })
  88. );
  89. return;
  90. }
  91. wxLogin(function (res) {
  92. if (res.data.code == 200) {
  93. let sessionKey = res.data.data.session_key;
  94. let openId = res.data.data.openid;
  95. getLoginSchedule(sessionKey, openId, successcallback, failcallback);
  96. }
  97. }, function (res) {
  98. failcallback(res);
  99. })
  100. }
  101. module.exports = {
  102. getOpenidSessionKey
  103. }