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.

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