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.

194 lines
5.0 KiB

  1. const Http = require("../../utils/HttpBasics");
  2. const config = require("../../config/config");
  3. let app = getApp();
  4. Page({
  5. data: {
  6. market: app.globalData.market,
  7. list: [],
  8. swiperCurrent: 0,
  9. scrollTop: 0,
  10. page: 1 // 刷新进入页面时已经加载了第一页数据,onReachBottom时 page++,从第2页开始加载
  11. },
  12. swiperChange: function(e) {
  13. this.setData({
  14. swiperCurrent: e.detail.current
  15. });
  16. },
  17. /**
  18. * 生命周期函数--监听页面初次渲染完成
  19. */
  20. onLoad: function(options) {
  21. var that = this;
  22. var scene = decodeURIComponent(options.scene);
  23. app.getLocation();
  24. },
  25. onShow: function() {
  26. this.userLogin()
  27. },
  28. onGetCode: function(e) {
  29. //子组件传递给父组件的值
  30. this.setData({
  31. code: e.detail.val,
  32. page: e.detail.pageNum
  33. });
  34. },
  35. /**
  36. * 用户登录
  37. */
  38. userLogin: function() {
  39. var that = this;
  40. // 登录
  41. wx.login({
  42. success: ({
  43. code
  44. }) => {
  45. var usrdata = {
  46. appId: config.weapp.AppId,
  47. code: code,
  48. sceneAddress: app.globalData.sceneAddress,
  49. }
  50. if (app.globalData.locationInfo) {
  51. usrdata = {
  52. appId: config.weapp.AppId,
  53. code: code,
  54. sceneAddress: app.globalData.sceneAddress,
  55. latitude: '' + app.globalData.locationInfo.latitude,
  56. longitude: '' + app.globalData.locationInfo.longitude,
  57. }
  58. }
  59. Http.post({
  60. url: config.api.login,
  61. data: usrdata
  62. })
  63. .then(res => {
  64. console.log("userlogin:app", res);
  65. app.globalData.token = res.data.token;
  66. Http.setToken(res.data.token);
  67. that.checkUserCarStatus();
  68. that.getUserInfo();
  69. if (app.couponChannelListCallback) {
  70. app.couponChannelListCallback(app.globalData.token);
  71. }
  72. if (app.couponListCallback) {
  73. app.couponListCallback(app.globalData.token);
  74. }
  75. if (app.businessListCallback) {
  76. app.businessListCallback(app.globalData.token);
  77. }
  78. return Http.post({
  79. url: config.api.checkUserStatus,
  80. data: {}
  81. });
  82. })
  83. .then(res => {
  84. console.log("checkUserStatus:res", res);
  85. })
  86. .catch(err => {
  87. console.log("checkUserStatus:err", err);
  88. if (err.code == 11004) {
  89. // 用户昵称未授权
  90. wx.redirectTo({
  91. url: "/pages/getuserinfo/index"
  92. });
  93. }
  94. });
  95. }
  96. });
  97. },
  98. /**
  99. * 检查用户是否有车
  100. */
  101. checkUserCarStatus: function() {
  102. var that = this;
  103. Http.get({
  104. url: config.api.userCarCount,
  105. data: {}
  106. }).then(res => {
  107. if (res.data > 0) {
  108. // 用户名下有车
  109. app.globalData.phone = res.data.phone;
  110. app.globalData.supportCar = true;
  111. // 共同登录
  112. that.userCarLogin();
  113. }
  114. });
  115. },
  116. /**
  117. * car共同登录
  118. */
  119. userCarLogin: function() {
  120. var that = this;
  121. if (!app.globalData.carLogin) {
  122. // 共同登录
  123. Http.post({
  124. url: config.api.carInit,
  125. data: {
  126. phone: app.globalData.phone
  127. }
  128. }).then(res => {
  129. app.globalData.carLogin = true;
  130. app.globalData.parkVendor = res.data.vendor;
  131. if (res.data.token != "undefined") {
  132. app.globalData.etcpToken = res.data.token;
  133. console.log("etcpToken", app.globalData.etcpToken);
  134. }
  135. });
  136. }
  137. },
  138. /**
  139. * 获取用户信息
  140. */
  141. getUserInfo: function() {
  142. // 获取用户信息
  143. wx.getSetting({
  144. success: res => {
  145. console.log("getSetting", res);
  146. if (res.authSetting["scope.userInfo"]) {
  147. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  148. wx.getUserInfo({
  149. success: res => {
  150. // 可以将 res 发送给后台解码出 unionId
  151. console.log("getUserInfo", res);
  152. }
  153. });
  154. }
  155. }
  156. });
  157. },
  158. //下拉加载更多
  159. onReachBottom: function() {
  160. let that = this;
  161. console.info("before++ " + that.data.page);
  162. that.data.page++;
  163. console.info("after++ " + that.data.page);
  164. that.setData({
  165. page: that.data.page
  166. });
  167. //父组件获得子组件的方法
  168. //如果code == 0
  169. if (that.data.code == 0 || that.data.code == undefined) {
  170. that.selectComponent("#lists").getList(0, that.data.page);
  171. } else {
  172. that.selectComponent("#lists").getList(that.data.code, that.data.page);
  173. }
  174. },
  175. // 用户点击右上角分享
  176. onShareAppMessage: function() {
  177. return {
  178. title: "富茂链客",
  179. desc: "分享个小程序,希望你喜欢",
  180. success: function(res) {
  181. wx.showToast({
  182. title: "分享成功",
  183. duration: 1000,
  184. icon: "success"
  185. });
  186. }
  187. };
  188. }
  189. });