httpUtil.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. if (res.data.code == 200) {
  108. let sessionKey = res.data.data.session_key;
  109. let openId = res.data.data.openid;
  110. getLoginSchedule(sessionKey, openId, successcallback, failcallback);
  111. }
  112. }, function (res) {
  113. failcallback(res);
  114. })
  115. }
  116. module.exports = {
  117. getOpenidSessionKey
  118. }