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.

409 lines
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. console.log("AppId:" + config.weapp.AppId)
  79. console.log("-------------------Yesyyy----------------------------------")
  80. wx.setStorageSync('imgurl', imgurl)
  81. if (options.played == "true") {
  82. this.setData({
  83. played: true
  84. })
  85. }
  86. var that = this;
  87. if (decodeURIComponent(options.scene) == "undefined") {
  88. that.setData({
  89. scene: 0
  90. });
  91. } else {
  92. that.setData({
  93. scene: decodeURIComponent(options.scene)
  94. });
  95. }
  96. app.getLocation();
  97. if (options.couponChannelId && options.couponId) {
  98. that.userLogin(options.couponChannelId, options.couponId);
  99. } else {
  100. that.userLogin()
  101. }
  102. Http.get({
  103. url: config.api.getWeapNote,
  104. data: {
  105. appId: config.weapp.AppId,
  106. }
  107. })
  108. .then(res => {
  109. let weapNote = JSON.parse(res.data.weapNote);
  110. that.setData({
  111. desc: weapNote.firstpage.desc,
  112. title: weapNote.firstpage.title
  113. })
  114. })
  115. .catch(err => {
  116. wx.showModal({
  117. title: "提示",
  118. content:err.errMsg,
  119. showCancel:false
  120. });
  121. });
  122. },
  123. onShow: function () {
  124. this.setData({
  125. havePlayEd: app.globalData.havePlayEd ? app.globalData.havePlayEd : false
  126. })
  127. let num = wx.getStorageSync('couponNum');
  128. if (num == 'couponNum') {
  129. wx.showTabBarRedDot({
  130. index: 2
  131. })
  132. };
  133. },
  134. /**
  135. *
  136. * @param {code,page}
  137. * 子组件向父组件传值
  138. */
  139. onGetCode: function (e) {
  140. this.setData({
  141. code: e.detail.val,
  142. page: e.detail.pageNum,
  143. });
  144. console.log(e.detail.val)
  145. console.log(e.detail.pageNum)
  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){
  189. that.getGameOne(res.data.token)
  190. that.getStaticGame(res.data.token)
  191. }
  192. Http.setToken(res.data.token);
  193. that.checkUserCarStatus();
  194. that.getUserInfo();
  195. that.getBannerlist();
  196. if (app.couponChannelListCallback) {
  197. app.couponChannelListCallback(app.globalData.token);
  198. }
  199. if (app.couponListCallback) {
  200. app.couponListCallback(app.globalData.token);
  201. }
  202. if (app.businessListCallback) {
  203. app.businessListCallback(app.globalData.token);
  204. }
  205. that.checkuerstatus(couponChannelId, couponId);
  206. })
  207. .catch(err => {
  208. });
  209. }
  210. });
  211. },
  212. checkuerstatus(couponChannelId, couponId) {
  213. Http.post({
  214. url: config.api.checkUserStatus,
  215. data: {}
  216. })
  217. .then(res => {
  218. if (couponChannelId && couponId) {
  219. wx.navigateTo({
  220. url: `/pages/coupon/detail/index?couponChannelId=${couponChannelId}&couponId=${couponId}`,
  221. })
  222. }
  223. })
  224. .catch(err => {
  225. if (err.code == 11004) {
  226. // 用户昵称未授权
  227. if (couponChannelId && couponId) {
  228. wx.redirectTo({
  229. url: `/pages/getuserinfo/index?couponChannelId=${couponChannelId}&couponId=${couponId}`
  230. });
  231. } else {
  232. wx.redirectTo({
  233. url: '/pages/getuserinfo/index'
  234. });
  235. }
  236. }
  237. });
  238. },
  239. getStaticGame(token) {
  240. let _this = this;
  241. Http.get({
  242. url: config.api.getGame,
  243. data: {
  244. triggleAction: 2,
  245. token: token
  246. }
  247. }).then(res => {
  248. if (res.data.id) {
  249. _this.setData({
  250. showIf:true
  251. })
  252. }
  253. _this.setData({
  254. staticGamedata: res.data
  255. })
  256. })
  257. .catch(err => {
  258. console.log(err)
  259. })
  260. },
  261. getGameOne: function (token) {
  262. let _this = this;
  263. Http.get({
  264. url: config.api.getGame,
  265. data:{
  266. triggleAction: 1,
  267. token: token
  268. }
  269. }).then(res => {
  270. if (res.data.id) {
  271. _this.setData({
  272. showGame: true
  273. })
  274. }
  275. _this.setData({
  276. gamedata: res.data
  277. })
  278. })
  279. .catch(err => {
  280. console.log(err)
  281. })
  282. },
  283. /**
  284. * banner
  285. */
  286. getBannerlist: function () {
  287. let that = this;
  288. Http.get({
  289. url: config.api.bannerlist,
  290. data: {
  291. pageNum: 1,
  292. pageSize: 7
  293. }
  294. }).then(res => {
  295. that.setData({
  296. list: res.data.list
  297. });
  298. });
  299. },
  300. /**
  301. * 检查用户是否有车
  302. */
  303. checkUserCarStatus: function () {
  304. var that = this;
  305. Http.get({
  306. url: config.api.userCarCount,
  307. data: {}
  308. }).then(res => {
  309. if (res.data > 0) {
  310. // 用户名下有车
  311. app.globalData.phone = res.data.phone;
  312. app.globalData.supportCar = true;
  313. // 共同登录
  314. that.userCarLogin();
  315. }
  316. });
  317. },
  318. /**
  319. * car共同登录
  320. */
  321. userCarLogin: function () {
  322. var that = this;
  323. if (!app.globalData.carLogin) {
  324. // 共同登录
  325. Http.post({
  326. url: config.api.carInit,
  327. data: {
  328. phone: app.globalData.phone
  329. }
  330. }).then(res => {
  331. app.globalData.carLogin = true;
  332. app.globalData.parkVendor = res.data.vendor;
  333. if (res.data.token != "undefined") {
  334. app.globalData.etcpToken = res.data.token;
  335. }
  336. });
  337. }
  338. },
  339. /**
  340. * 获取用户信息
  341. */
  342. getUserInfo: function () {
  343. // 获取用户信息
  344. wx.getSetting({
  345. success: res => {
  346. if (res.authSetting["scope.userInfo"]) {
  347. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  348. wx.getUserInfo({
  349. success: res => {
  350. // 可以将 res 发送给后台解码出 unionId
  351. }
  352. });
  353. }
  354. }
  355. });
  356. },
  357. /**
  358. * 刷新
  359. */
  360. onPullDownRefresh: function (e) {
  361. let that = this;
  362. that.userLogin();
  363. that.setData({
  364. page:1
  365. })
  366. if (that.data.code == 0 || that.data.code == undefined) {
  367. that.selectComponent("#lists").getList(0, 1, "refresh");
  368. wx.stopPullDownRefresh();
  369. } else {
  370. that.selectComponent("#lists").getList(that.data.code, 1, "refresh");
  371. wx.stopPullDownRefresh();
  372. };
  373. },
  374. //加载更多
  375. onReachBottom: function () {
  376. let that = this;
  377. that.data.page++;
  378. that.setData({
  379. page: that.data.page
  380. });
  381. console.log("加载更多页数"+that.data.page);
  382. console.log("加载更多key"+that.data.code);
  383. //父组件获得子组件的方法
  384. //如果code == 0
  385. if (that.data.code == 0 || that.data.code == undefined) {
  386. that.selectComponent("#lists").getList(0, that.data.page);
  387. } else {
  388. that.selectComponent("#lists").getList(that.data.code, that.data.page);
  389. }
  390. },
  391. // 用户点击右上角分享
  392. onShareAppMessage: function () {
  393. return {
  394. title: this.data.title,
  395. desc: this.data.desc,
  396. success: function (res) {
  397. wx.showToast({
  398. title: "分享成功",
  399. duration: 1000,
  400. icon: "success"
  401. });
  402. }
  403. };
  404. }
  405. });