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.

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