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.

407 line
10 KiB

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