httpUtilNoLogin.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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, shareUid) {
  11. wx.login({
  12. success: function(res) {
  13. if (res.code) {
  14. // 获取openid
  15. console.log('登录登录登录登录code', res.code)
  16. let url = genAPIUrl('wx/user/openId')
  17. let data = {
  18. code: res.code,
  19. shareUid
  20. }
  21. return request.getInstance().url(url).data(data).send().success(successcallback).fail(failcallback);
  22. } else {
  23. console.log('获取用户登录态失败!' + res.errMsg)
  24. }
  25. }
  26. })
  27. }
  28. // 获取原来的 openid 和 session_key
  29. /* 新增参数grade 需要给游客添加grade参数 */
  30. function getOpenidNoLogin(successcallback, failcallback, shareUid) {
  31. wxLogin(function(res) {
  32. if (res.data.code == 200) {
  33. console.log(res);
  34. // let openId = res.data.data.openid;
  35. // let url = genAPIUrl('wx/user/register');
  36. // request.getInstance().method('POST').url(url).header({
  37. // 'content-type': 'application/json'
  38. // }).data({
  39. // openId,
  40. // grade: grade ? grade : null
  41. // }).send().success((res) => {
  42. if (res.data.code === 200) {
  43. wx.setStorageSync('uid', res.data.data.data.uid)
  44. wx.setStorageSync('grade', res.data.data.data.grade)
  45. wx.setStorageSync('user', res.data.data.data)
  46. successcallback(res.data)
  47. }
  48. // }).fail(() => {});
  49. }
  50. }, function(res) {
  51. failcallback(res);
  52. }, shareUid)
  53. }
  54. module.exports = {
  55. getOpenidNoLogin
  56. }