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.

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