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.

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