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

154 lines
3.7 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. that.globalData.sceneAddress = options.scene;
  8. that.getLocation();
  9. that.userLogin();
  10. },
  11. /**
  12. * 获取地址位置信息
  13. */
  14. getLocation: function() {
  15. wx.getLocation({
  16. type: "wgs84",
  17. success: function(res) {
  18. console.log("getLocation", res);
  19. },
  20. fail: error => {
  21. console.log(error);
  22. }
  23. });
  24. },
  25. /**
  26. * 用户登录
  27. */
  28. userLogin: function() {
  29. var that = this;
  30. // 登录
  31. wx.login({
  32. success: ({ code }) => {
  33. Http.post({
  34. url: config.api.login,
  35. data: {
  36. appId: config.weapp.AppId,
  37. code: code,
  38. sceneAddress: that.globalData.sceneAddress
  39. }
  40. })
  41. .then(res => {
  42. console.log("userlogin", res);
  43. that.globalData.token = res.data.token;
  44. Http.setToken(res.data.token);
  45. that.checkUserCarStatus();
  46. that.getUserInfo();
  47. if (that.couponChannelListCallback) {
  48. that.couponChannelListCallback(that.globalData.token);
  49. }
  50. if (that.couponListCallback) {
  51. that.couponListCallback(that.globalData.token);
  52. }
  53. return Http.post({
  54. url: config.api.checkUserStatus,
  55. data: {}
  56. });
  57. })
  58. .then(res => {
  59. console.log("checkUserStatus", res);
  60. })
  61. .catch(err => {
  62. console.log("checkUserStatus:err", err);
  63. if (err.code == 11004) {
  64. // 用户昵称未授权
  65. wx.redirectTo({
  66. url: "../getuserinfo/index"
  67. });
  68. }
  69. });
  70. }
  71. });
  72. },
  73. /**
  74. * 获取用户信息
  75. */
  76. getUserInfo: function() {
  77. // 获取用户信息
  78. wx.getSetting({
  79. success: res => {
  80. console.log("getSetting", res);
  81. if (res.authSetting["scope.userInfo"]) {
  82. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  83. wx.getUserInfo({
  84. success: res => {
  85. // 可以将 res 发送给后台解码出 unionId
  86. console.log("getUserInfo", res);
  87. }
  88. });
  89. }
  90. }
  91. });
  92. },
  93. /**
  94. * 检查用户是否有车
  95. */
  96. checkUserCarStatus: function() {
  97. var that = this;
  98. Http.get({
  99. url: config.api.userCarCount,
  100. data: {}
  101. }).then(res => {
  102. if (res.data > 0) {
  103. // 用户名下有车
  104. that.globalData.phone = res.data.phone;
  105. that.globalData.supportCar = true;
  106. // 共同登录
  107. that.userCarLogin();
  108. }
  109. });
  110. },
  111. /**
  112. * car共同登录
  113. */
  114. userCarLogin: function() {
  115. var that = this;
  116. if (!that.globalData.carLogin) {
  117. // 共同登录
  118. Http.post({
  119. url: config.api.carInit,
  120. data: {
  121. phone: that.globalData.phone
  122. }
  123. }).then(res => {
  124. that.globalData.carLogin = true;
  125. that.globalData.parkVendor = res.data.vendor;
  126. if (res.data.token != "undefined") {
  127. that.globalData.etcpToken = res.data.token;
  128. console.log("etcpToken", that.globalData.etcpToken);
  129. }
  130. });
  131. }
  132. },
  133. globalData: {
  134. // token
  135. token: null,
  136. // 渠道
  137. sceneAddress: null,
  138. // 二维码参数
  139. scene: null,
  140. // 支持智慧停车, 用户名下有车
  141. phone: null,
  142. supportCar: false,
  143. parkVendor: 1, // 1: ETCP, 2: TJD
  144. // ETCP token
  145. etcpToken: null,
  146. carLogin: false,
  147. // 当前商场信息
  148. market: {
  149. name: "陕西大悦城"
  150. }
  151. }
  152. });