C端小程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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