C端小程序
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.3 KiB

  1. let config = require('./config/config.js')
  2. const Http = require("./utils/HttpBasics");
  3. App({
  4. data: {},
  5. onLaunch: function(options) {
  6. var that = this;
  7. this.globalData.sceneAddress = options.scene;
  8. this.getLocation()
  9. this.getUserInfo()
  10. // 登录
  11. wx.login({
  12. success: ({
  13. code
  14. }) => {
  15. Http.post({
  16. url: config.api.login,
  17. data: {
  18. appId: config.weapp.AppId,
  19. code: code,
  20. sceneAddress: this.globalData.sceneAddress,
  21. }
  22. }).then(res => {
  23. // console.log("req",res);
  24. this.globalData.token = res.data.token;
  25. Http.setToken(res.data.token)
  26. return Http.post({
  27. url: config.api.checkUserStatus,
  28. data: {}
  29. })
  30. }).then(res => {
  31. }).catch(err => {
  32. console.log(err)
  33. if (err.code == 11004) {
  34. // 用户昵称未授权
  35. wx.redirectTo({
  36. url: '../getuserinfo/index',
  37. })
  38. }
  39. })
  40. }
  41. })
  42. },
  43. /**
  44. * 获取地址位置信息
  45. */
  46. getLocation: function() {
  47. wx.getLocation({
  48. type: 'wgs84',
  49. success: function(res) {
  50. console.log("getLocation", res);
  51. },
  52. fail: error => {
  53. console.log(error);
  54. }
  55. })
  56. },
  57. /**
  58. * 获取用户信息
  59. */
  60. getUserInfo: function() {
  61. // 获取用户信息
  62. wx.getSetting({
  63. success: res => {
  64. if (res.authSetting['scope.userInfo']) {
  65. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  66. wx.getUserInfo({
  67. success: res => {
  68. // 可以将 res 发送给后台解码出 unionId
  69. this.globalData.userInfo = res.userInfo
  70. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  71. // 所以此处加入 callback 以防止这种情况
  72. if (this.userInfoReadyCallback) {
  73. this.userInfoReadyCallback(res)
  74. }
  75. }
  76. })
  77. }
  78. }
  79. })
  80. },
  81. globalData: {
  82. // token
  83. token: null,
  84. // 渠道
  85. sceneAddress: null,
  86. // 二维码参数
  87. scene: null,
  88. // 当前商场信息
  89. market: {
  90. name: "陕西大悦城"
  91. }
  92. }
  93. })