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.

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