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.

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