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.

306 lines
7.6 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. title:'',
  10. desc:'',
  11. scrollTop: 0,
  12. showGame:false,
  13. gamedata:{},
  14. couponId:'',//游戏返回时传回的字段
  15. played:false,//从游戏页面跳回首页返回true
  16. havePlayEd: app.globalData.havePlayEd,
  17. page: 1 // 刷新进入页面时已经加载了第一页数据,onReachBottom时 page++,从第2页开始加载
  18. },
  19. swiperChange: function(e) {
  20. this.setData({
  21. swiperCurrent: e.detail.current
  22. });
  23. },
  24. gotogame:function(){
  25. wx.navigateTo({
  26. url: '../game/index',
  27. })
  28. },
  29. /**
  30. * 生命周期函数--监听页面初次渲染完成
  31. */
  32. onLoad: function(options) {
  33. if(options.played=="true"){
  34. this.setData({
  35. played:true
  36. })
  37. }
  38. var that = this;
  39. var scene = decodeURIComponent(options.scene);
  40. that.setData({
  41. scene:scene
  42. });
  43. app.getLocation();
  44. if (options.couponChannelId && options.couponId){
  45. that.userLogin(options.couponChannelId, options.couponId);
  46. }else{
  47. that.userLogin()
  48. }
  49. Http.get({
  50. url: config.api.getWeapNote,
  51. data: {
  52. appId: config.weapp.AppId,
  53. }
  54. })
  55. .then(res=>{
  56. let weapNote = JSON.parse(res.data.weapNote);
  57. that.setData({
  58. desc: weapNote.firstpage.desc,
  59. title: weapNote.firstpage.title
  60. })
  61. })
  62. },
  63. onShow:function(){
  64. this.setData({
  65. havePlayEd: app.globalData.havePlayEd ? app.globalData.havePlayEd:false
  66. })
  67. let num = wx.getStorageSync('couponNum');
  68. if (num =='couponNum'){
  69. wx.showTabBarRedDot({
  70. index: 2
  71. })
  72. };
  73. },
  74. /**
  75. * 下拉刷新
  76. */
  77. onPullDownRefresh: function(e) {
  78. let that = this;
  79. that.userLogin();
  80. // console.log(this.data.code);
  81. if (that.data.code == 0 || that.data.code == undefined) {
  82. that.selectComponent("#lists").getList(0, 1);
  83. wx.stopPullDownRefresh();
  84. // console.log("下拉刷新");
  85. } else {
  86. that.selectComponent("#lists").getList(that.data.code, 1);
  87. wx.stopPullDownRefresh();
  88. };
  89. },
  90. onGetCode: function(e) {
  91. //子组件传递给父组件的值
  92. this.setData({
  93. code: e.detail.val,
  94. page: e.detail.pageNum
  95. });
  96. },
  97. /**
  98. * 用户登录
  99. */
  100. userLogin: function (couponChannelId,couponId) {
  101. var that = this;
  102. // 登录
  103. wx.login({
  104. success: ({
  105. code
  106. }) => {
  107. wx.getSystemInfo({
  108. success: function (res) {
  109. that.setData({
  110. systemInfo: JSON.stringify(res)
  111. })
  112. }
  113. })
  114. var usrdata = {
  115. appId: config.weapp.AppId,
  116. code: code,
  117. sceneAddress: app.globalData.sceneAddress,
  118. scene: that.data.scene,
  119. systemInfo: that.data.systemInfo
  120. };
  121. if (app.globalData.locationInfo) {
  122. usrdata = {
  123. appId: config.weapp.AppId,
  124. code: code,
  125. sceneAddress: app.globalData.sceneAddress,
  126. latitude: "" + app.globalData.locationInfo.latitude,
  127. longitude: "" + app.globalData.locationInfo.longitude,
  128. scene: that.data.scene,
  129. systemInfo: that.data.systemInfo
  130. };
  131. }
  132. Http.post({
  133. url: config.api.login,
  134. data: usrdata
  135. })
  136. .then(res => {
  137. app.globalData.token = res.data.token;
  138. if (res.data.token && couponChannelId && couponId){
  139. wx.navigateTo({
  140. url: `/pages/coupon/detail/index?couponChannelId=${couponChannelId}&couponId=${couponId}`,
  141. })
  142. }
  143. if (res.data.token){
  144. that.setData({
  145. showGame: true
  146. })
  147. that.getGameOne(res.data.token)
  148. }
  149. Http.setToken(res.data.token);
  150. that.checkUserCarStatus();
  151. that.getUserInfo();
  152. that.getBannerlist();
  153. if (app.couponChannelListCallback) {
  154. app.couponChannelListCallback(app.globalData.token);
  155. }
  156. if (app.couponListCallback) {
  157. app.couponListCallback(app.globalData.token);
  158. }
  159. if (app.businessListCallback) {
  160. app.businessListCallback(app.globalData.token);
  161. }
  162. return Http.post({
  163. url: config.api.checkUserStatus,
  164. data: {}
  165. });
  166. })
  167. .then(res => {
  168. console.log("checkUserStatus:res", res);
  169. })
  170. .catch(err => {
  171. console.log("checkUserStatus:err", err);
  172. if (err.code == 11004) {
  173. // 用户昵称未授权
  174. wx.redirectTo({
  175. url: "/pages/getuserinfo/index"
  176. });
  177. }
  178. });
  179. }
  180. });
  181. },
  182. getGameOne:function(token){
  183. let _this=this;
  184. Http.get({
  185. url: config.api.getGame,
  186. data:{
  187. token: token
  188. }
  189. }).then(res => {
  190. console.log(res.data,77777)
  191. _this.setData({
  192. gamedata:res.data
  193. })
  194. })
  195. },
  196. /**
  197. * banner
  198. */
  199. getBannerlist: function() {
  200. let that = this;
  201. Http.get({
  202. url: config.api.bannerlist,
  203. data: {
  204. pageNum: 1,
  205. pageSize: 7
  206. }
  207. }).then(res => {
  208. that.setData({
  209. list: res.data.list
  210. });
  211. });
  212. },
  213. /**
  214. * 检查用户是否有车
  215. */
  216. checkUserCarStatus: function() {
  217. var that = this;
  218. Http.get({
  219. url: config.api.userCarCount,
  220. data: {}
  221. }).then(res => {
  222. if (res.data > 0) {
  223. // 用户名下有车
  224. app.globalData.phone = res.data.phone;
  225. app.globalData.supportCar = true;
  226. // 共同登录
  227. that.userCarLogin();
  228. }
  229. });
  230. },
  231. /**
  232. * car共同登录
  233. */
  234. userCarLogin: function() {
  235. var that = this;
  236. if (!app.globalData.carLogin) {
  237. // 共同登录
  238. Http.post({
  239. url: config.api.carInit,
  240. data: {
  241. phone: app.globalData.phone
  242. }
  243. }).then(res => {
  244. app.globalData.carLogin = true;
  245. app.globalData.parkVendor = res.data.vendor;
  246. if (res.data.token != "undefined") {
  247. app.globalData.etcpToken = res.data.token;
  248. }
  249. });
  250. }
  251. },
  252. /**
  253. * 获取用户信息
  254. */
  255. getUserInfo: function() {
  256. // 获取用户信息
  257. wx.getSetting({
  258. success: res => {
  259. if (res.authSetting["scope.userInfo"]) {
  260. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  261. wx.getUserInfo({
  262. success: res => {
  263. // 可以将 res 发送给后台解码出 unionId
  264. console.log("getUserInfo", res);
  265. }
  266. });
  267. }
  268. }
  269. });
  270. },
  271. //下拉加载更多
  272. onReachBottom: function() {
  273. let that = this;
  274. that.data.page++;
  275. that.setData({
  276. page: that.data.page
  277. });
  278. // console.log("我是第" + that.data.page + "几页")
  279. //父组件获得子组件的方法
  280. //如果code == 0
  281. if (that.data.code == 0 || that.data.code == undefined) {
  282. that.selectComponent("#lists").getList(0, that.data.page);
  283. } else {
  284. that.selectComponent("#lists").getList(that.data.code, that.data.page);
  285. }
  286. },
  287. // 用户点击右上角分享
  288. onShareAppMessage: function() {
  289. return {
  290. title: this.data.title,
  291. desc: this.data.desc,
  292. success: function(res) {
  293. wx.showToast({
  294. title: "分享成功",
  295. duration: 1000,
  296. icon: "success"
  297. });
  298. }
  299. };
  300. }
  301. });