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.

499 lines
12 KiB

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