C端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.6 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("req", 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. .catch(err => {
  60. console.log(err);
  61. if (err.code == 11004) {
  62. // 用户昵称未授权
  63. wx.redirectTo({
  64. url: "../getuserinfo/index"
  65. });
  66. }
  67. });
  68. }
  69. });
  70. },
  71. /**
  72. * 获取用户信息
  73. */
  74. getUserInfo: function() {
  75. // 获取用户信息
  76. wx.getSetting({
  77. success: res => {
  78. console.log("getSetting", res);
  79. if (res.authSetting["scope.userInfo"]) {
  80. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  81. wx.getUserInfo({
  82. success: res => {
  83. // 可以将 res 发送给后台解码出 unionId
  84. console.log("getUserInfo", res);
  85. }
  86. });
  87. }
  88. }
  89. });
  90. },
  91. /**
  92. * 检查用户是否有车
  93. */
  94. checkUserCarStatus: function() {
  95. var that = this;
  96. Http.get({
  97. url: config.api.userCarCount,
  98. data: {}
  99. }).then(res => {
  100. if (res.data > 0) {
  101. // 用户名下有车
  102. that.globalData.phone = res.data.phone;
  103. that.globalData.supportCar = true;
  104. // 共同登录
  105. that.userCarLogin();
  106. }
  107. });
  108. },
  109. /**
  110. * car共同登录
  111. */
  112. userCarLogin: function() {
  113. var that = this;
  114. if (!that.globalData.carLogin) {
  115. // 共同登录
  116. Http.post({
  117. url: config.api.carInit,
  118. data: {
  119. phone: that.globalData.phone
  120. }
  121. }).then(res => {
  122. that.globalData.carLogin = true;
  123. that.globalData.parkVendor = res.data.vendor;
  124. if (res.data.token != "undefined") {
  125. that.globalData.etcpToken = res.data.token;
  126. console.log("etcpToken", that.globalData.etcpToken);
  127. }
  128. });
  129. }
  130. },
  131. globalData: {
  132. // token
  133. token: null,
  134. // 渠道
  135. sceneAddress: null,
  136. // 二维码参数
  137. scene: null,
  138. // 支持智慧停车, 用户名下有车
  139. phone: null,
  140. supportCar: false,
  141. parkVendor: 1, // 1: ETCP, 2: TJD
  142. // ETCP token
  143. etcpToken: null,
  144. carLogin: false,
  145. // 当前商场信息
  146. market: {
  147. name: "陕西大悦城"
  148. }
  149. }
  150. });