httpUtil.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. let appLaunch = wx.getLaunchOptionsSync();
  52. let userSourseType = '';
  53. console.log(appLaunch.scene)
  54. switch (appLaunch.scene) {
  55. case 1020:
  56. userSourseType = 'profile';
  57. break;
  58. case 1058:
  59. userSourseType = 'article';
  60. break;
  61. default:
  62. userSourseType = wx.getStorageSync('userSourseType');
  63. break;
  64. }
  65. const userData = {
  66. encryptedData: res.encryptedData,
  67. sessionKey: sessionKey,
  68. iv: res.iv,
  69. sourceType: userSourseType
  70. }
  71. request.getInstance().url(url).header({
  72. "content-type": "application/x-www-form-urlencoded; charset=UTF-8"
  73. }).data(userData).method('POST').send().success(res => {
  74. wx.hideToast()
  75. wx.showToast({
  76. title: '登录成功',
  77. icon: 'success',
  78. duration: 1500,
  79. mask: true
  80. })
  81. wx.setStorageSync('uid', res.data.data.data.uid)
  82. wx.setStorageSync('user', res.data.data.data)
  83. userLoginRecord()
  84. successcallback(res)
  85. });
  86. },
  87. fail: function (res) {
  88. failcallback(res)
  89. }
  90. })
  91. }
  92. })
  93. }
  94. // 获取原来的 openid 和 session_key
  95. function getOpenidSessionKey(successcallback, failcallback) {
  96. // if (wx.getStorageSync('user')) {
  97. // successcallback(
  98. // wx.getStorageSync('user'),
  99. // //分享小程序按钮
  100. // wx.showShareMenu({
  101. // withShareTicket: true
  102. // })
  103. // );
  104. // return;
  105. // }
  106. wxLogin(function (res) {
  107. console.log('wxlogin123123123123',res)
  108. if (res.data.code == 200) {
  109. let sessionKey = res.data.data.session_key;
  110. let openId = res.data.data.openid;
  111. getLoginSchedule(sessionKey, openId, successcallback, failcallback);
  112. }
  113. }, function (res) {
  114. failcallback(res);
  115. })
  116. }
  117. module.exports = {
  118. getOpenidSessionKey
  119. }