C端小程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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