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.

212 rivejä
5.3 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: ({ code }) => {
  43. var usrdata = {
  44. appId: config.weapp.AppId,
  45. code: code,
  46. sceneAddress: app.globalData.sceneAddress
  47. };
  48. if (app.globalData.locationInfo) {
  49. usrdata = {
  50. appId: config.weapp.AppId,
  51. code: code,
  52. sceneAddress: app.globalData.sceneAddress,
  53. latitude: "" + app.globalData.locationInfo.latitude,
  54. longitude: "" + app.globalData.locationInfo.longitude
  55. };
  56. }
  57. Http.post({
  58. url: config.api.login,
  59. data: usrdata
  60. })
  61. .then(res => {
  62. console.log("userlogin:app", res);
  63. app.globalData.token = res.data.token;
  64. Http.setToken(res.data.token);
  65. that.checkUserCarStatus();
  66. that.getUserInfo();
  67. that.getBannerlist();
  68. if (app.couponChannelListCallback) {
  69. app.couponChannelListCallback(app.globalData.token);
  70. }
  71. if (app.couponListCallback) {
  72. app.couponListCallback(app.globalData.token);
  73. }
  74. if (app.businessListCallback) {
  75. app.businessListCallback(app.globalData.token);
  76. }
  77. return Http.post({
  78. url: config.api.checkUserStatus,
  79. data: {}
  80. });
  81. })
  82. .then(res => {
  83. console.log("checkUserStatus:res", res);
  84. })
  85. .catch(err => {
  86. console.log("checkUserStatus:err", err);
  87. if (err.code == 11004) {
  88. // 用户昵称未授权
  89. wx.redirectTo({
  90. url: "/pages/getuserinfo/index"
  91. });
  92. }
  93. });
  94. }
  95. });
  96. },
  97. /**
  98. * banner
  99. */
  100. getBannerlist: function() {
  101. let that = this;
  102. Http.get({
  103. url: config.api.bannerlist,
  104. data: {
  105. pageNum: 1,
  106. pageSize: 10
  107. }
  108. }).then(res => {
  109. console.log(res);
  110. that.setData({
  111. list: res.data.list
  112. });
  113. console.log(res.data.list);
  114. });
  115. },
  116. /**
  117. * 检查用户是否有车
  118. */
  119. checkUserCarStatus: function() {
  120. var that = this;
  121. Http.get({
  122. url: config.api.userCarCount,
  123. data: {}
  124. }).then(res => {
  125. if (res.data > 0) {
  126. // 用户名下有车
  127. app.globalData.phone = res.data.phone;
  128. app.globalData.supportCar = true;
  129. // 共同登录
  130. that.userCarLogin();
  131. }
  132. });
  133. },
  134. /**
  135. * car共同登录
  136. */
  137. userCarLogin: function() {
  138. var that = this;
  139. if (!app.globalData.carLogin) {
  140. // 共同登录
  141. Http.post({
  142. url: config.api.carInit,
  143. data: {
  144. phone: app.globalData.phone
  145. }
  146. }).then(res => {
  147. app.globalData.carLogin = true;
  148. app.globalData.parkVendor = res.data.vendor;
  149. if (res.data.token != "undefined") {
  150. app.globalData.etcpToken = res.data.token;
  151. console.log("etcpToken", app.globalData.etcpToken);
  152. }
  153. });
  154. }
  155. },
  156. /**
  157. * 获取用户信息
  158. */
  159. getUserInfo: function() {
  160. // 获取用户信息
  161. wx.getSetting({
  162. success: res => {
  163. console.log("getSetting", res);
  164. if (res.authSetting["scope.userInfo"]) {
  165. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  166. wx.getUserInfo({
  167. success: res => {
  168. // 可以将 res 发送给后台解码出 unionId
  169. console.log("getUserInfo", res);
  170. }
  171. });
  172. }
  173. }
  174. });
  175. },
  176. //下拉加载更多
  177. onReachBottom: function() {
  178. let that = this;
  179. console.info("before++ " + that.data.page);
  180. that.data.page++;
  181. console.info("after++ " + that.data.page);
  182. that.setData({
  183. page: that.data.page
  184. });
  185. //父组件获得子组件的方法
  186. //如果code == 0
  187. if (that.data.code == 0 || that.data.code == undefined) {
  188. that.selectComponent("#lists").getList(0, that.data.page);
  189. } else {
  190. that.selectComponent("#lists").getList(that.data.code, that.data.page);
  191. }
  192. },
  193. // 用户点击右上角分享
  194. onShareAppMessage: function() {
  195. return {
  196. title: "富茂链客",
  197. desc: "分享个小程序,希望你喜欢",
  198. success: function(res) {
  199. wx.showToast({
  200. title: "分享成功",
  201. duration: 1000,
  202. icon: "success"
  203. });
  204. }
  205. };
  206. }
  207. });