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.

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