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.

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