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.

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