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.

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