C端小程序
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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