C端小程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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