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.

513 lines
14 KiB

  1. var config = require("../../../config/config.js");
  2. var app = getApp();
  3. const Http = require("../../../utils/HttpBasics");
  4. const util = require("../../../utils/util");
  5. Page({
  6. data: {
  7. data: {},
  8. questions1:null,
  9. questions2:null,
  10. display:"none",
  11. carList: [],
  12. couponChannelId: null,
  13. couponId: null,
  14. orderId: "",
  15. hour: null,
  16. minute: "",
  17. tempFilePaths: null,
  18. userInfo: {},
  19. hasUserInfo: false,
  20. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  21. id: null,
  22. result: [],
  23. end_time: null,
  24. clock: "已经截止",
  25. questionnaire: {},
  26. questionId:null
  27. },
  28. onShow:function(){
  29. this.setData({
  30. display:"none",
  31. })
  32. },
  33. phone: function () {
  34. let that = this;
  35. console.log(that.data);
  36. if (that.data.data.merchantLinkPhone) {
  37. wx.makePhoneCall({
  38. phoneNumber: that.data.data.merchantLinkPhone
  39. });
  40. }
  41. },
  42. /**
  43. * @Meo
  44. * 问卷调查组件传递的值
  45. */
  46. close: function (e) {
  47. console.log(e.detail);
  48. let that = this;
  49. that.setData({
  50. questions1: e.detail,
  51. display:"none"
  52. })
  53. },
  54. formSubmit:function(e){
  55. console.log(e.detail);
  56. let that = this;
  57. that.setData({
  58. questions2: e.detail,
  59. display:"none"
  60. })
  61. },
  62. /**
  63. * gotopay
  64. */
  65. gotopay:function(){
  66. let that = this;
  67. console.log(that.data.questions1);
  68. if (that.data.questions1 || that.data.questions2){
  69. that.setData({
  70. display: "none",
  71. })
  72. that.orderFunc();
  73. }else{
  74. Http.get({
  75. url: config.api.getQuestion,
  76. data: {
  77. couponType: JSON.stringify(that.data.data.type)
  78. }
  79. })
  80. .then(res => {
  81. if (res.data == undefined) {
  82. that.setData({
  83. display: "none",
  84. })
  85. that.orderFunc();
  86. }
  87. else if (res.data) {
  88. that.setData({
  89. display: "block",
  90. questionnaire: JSON.parse(res.data.content),
  91. questionId:res.data.id
  92. });
  93. console.log(that.data.questionnaire);
  94. }
  95. })
  96. .catch(err => {
  97. wx.showToast({
  98. title: err.errMsg,
  99. icon: 'none',
  100. duration: 2000,
  101. mask: false
  102. });
  103. })
  104. }
  105. },
  106. countdown(end_time) {
  107. let that = this;
  108. var EndTime = end_time;
  109. var NowTime = new Date().getTime();
  110. var total_micro_second = EndTime - NowTime || [];
  111. // 渲染倒计时时钟
  112. let obj = that.dateformat(total_micro_second);
  113. if (total_micro_second>0){
  114. that.setData({
  115. clock: obj,
  116. day: obj.a1,
  117. hour: obj.b1,
  118. min: obj.c1,
  119. sec: obj.d1,
  120. })
  121. }else{
  122. that.setData({
  123. clock: "00",
  124. day: "00",
  125. hour: "00",
  126. min: "00",
  127. sec: "00",
  128. })
  129. }
  130. setTimeout(function () {
  131. total_micro_second -= 1000;
  132. that.countdown(end_time);
  133. }, 1000)
  134. },
  135. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  136. dateformat(micro_second) {
  137. // 总秒数
  138. var second = Math.floor(micro_second / 1000);
  139. // 天数
  140. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  141. // 小时
  142. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  143. // 分钟
  144. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  145. // 秒
  146. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60): Math.floor(second % 60);
  147. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  148. return { a1: day, b1: hr, c1: min, d1: sec}
  149. },
  150. onLoad(options) {
  151. let that = this;
  152. wx.showLoading({
  153. title: "加载中..."
  154. });
  155. /**
  156. * 暂时注销
  157. * this.orderFunc()
  158. */
  159. that.setData({
  160. couponChannelId: options.couponChannelId,
  161. couponId: options.couponId
  162. });
  163. var parmer = {
  164. url: config.api.couponDetail,
  165. data: {
  166. couponChannelId: options.couponChannelId
  167. }
  168. };
  169. Http.get(parmer).then(res => {
  170. console.log(res.data);
  171. if (res.data.endTime){
  172. that.countdown(res.data.endTime);
  173. //当前时间与优惠券下架时间做计算
  174. var endTime = util.formatTime(res.data.endTime, "yyyy-MM-dd hh:mm:ss");
  175. if (util.timechuo(endTime).indexOf('-') == 0) {
  176. that.setData({
  177. endtime: "活动已结束",
  178. });
  179. } else {
  180. that.setData({
  181. endtime: util.timechuo(endTime)
  182. });
  183. }
  184. }
  185. wx.hideLoading();
  186. that.setData({
  187. data: res.data
  188. });
  189. if (res.data.validType == 1) {
  190. that.setData({
  191. validStartDate: util.formatTime(res.data.validStartDate, "yyyy-MM-dd"),
  192. validEndDate: util.formatTime(res.data.validEndDate, "yyyy-MM-dd"),
  193. });
  194. } else {
  195. that.setData({
  196. validDays: res.data.validDays
  197. });
  198. }
  199. }).catch(err => {
  200. wx.showToast({
  201. title: err.errMsg,
  202. icon: 'none',
  203. duration: 2000,
  204. mask: false
  205. });
  206. })
  207. },
  208. /**
  209. * 支付订单更新
  210. */
  211. payOrderUpdate: (orderId, payOrderId, status, reason) => {
  212. Http.post({
  213. url: config.api.payOrderUpdate,
  214. data: {
  215. payOrderId: payOrderId,
  216. orderId: orderId,
  217. status: status,
  218. reason: reason
  219. }
  220. })
  221. .then(res => {
  222. console.log(res);
  223. console.log("payOrderUpdate then", res);
  224. })
  225. .catch(err => {
  226. wx.showToast({
  227. title: err.errMsg,
  228. icon: 'none',
  229. duration: 2000,
  230. mask: false
  231. });
  232. })
  233. },
  234. /**
  235. * 发起支付
  236. */
  237. orderFunc(flag) {
  238. var that = this;
  239. wx.showLoading({
  240. title: "加载中..."
  241. });
  242. if (that.data.data.type == 6) { } else {
  243. Http.post({
  244. url: config.api.checkPhoneStatus,
  245. data: {}
  246. })
  247. .then(res => {
  248. console.log(res);
  249. var data = {
  250. couponChannelId: "" + that.data.couponChannelId,
  251. couponId: "" + that.data.couponId
  252. };
  253. if (that.data.couponChannelId == null) {
  254. var data = {
  255. couponId: "" + that.data.couponId
  256. };
  257. }
  258. /**
  259. * orderSave 下单
  260. */
  261. return Http.post({
  262. url: config.api.orderSave,
  263. data: data
  264. });
  265. })
  266. .catch(err => {
  267. console.log(err);
  268. if (err.code == "2011") {
  269. wx.showToast({
  270. title: "商户信息没找到",
  271. image: "./../../../assets/img/fail.png",
  272. duration: 2000,
  273. mask: false
  274. });
  275. } else if (err.code == "2013") {
  276. wx.showToast({
  277. title: "商户信息禁用",
  278. image: "./../../../assets/img/fail.png",
  279. duration: 2000,
  280. mask: false
  281. });
  282. } else if (err.code == "3000") {
  283. wx.showToast({
  284. title: "库存不足",
  285. image: "./../../../assets/img/fail.png",
  286. duration: 2000,
  287. mask: false
  288. });
  289. } else if (err.code == "3001") {
  290. wx.showToast({
  291. title: "领取达到上限",
  292. image: "./../../../assets/img/fail.png",
  293. duration: 2000,
  294. mask: false
  295. });
  296. } else if (err.code == "3002") {
  297. wx.showToast({
  298. title: "订单失败",
  299. image: "./../../../assets/img/fail.png",
  300. duration: 2000,
  301. mask: false
  302. });
  303. } else if (err.code == "3003") {
  304. wx.showToast({
  305. title: "订单不存在",
  306. image: "./../../../assets/img/fail.png",
  307. duration: 2000,
  308. mask: false
  309. });
  310. } else if (err.code == "3004") {
  311. wx.showToast({
  312. title: "订单不存在",
  313. image: "./../../../assets/img/fail.png",
  314. duration: 2000,
  315. mask: false
  316. });
  317. } else if (err.code == "4003") {
  318. wx.showToast({
  319. title: "卡券已作废",
  320. image: "./../../../assets/img/fail.png",
  321. duration: 2000,
  322. mask: false
  323. });
  324. } else if (err.code == 11005) {
  325. // 用户手机未授权
  326. /**
  327. * 将值传到用户手机号授权的页面
  328. *
  329. */
  330. wx.redirectTo({
  331. url: "/pages/getphoneInfo/index?couponChannelId=" +
  332. that.data.couponChannelId +
  333. "&couponId=" +
  334. that.data.couponId
  335. });
  336. } else if (err.code == 11006) {
  337. // 用户手机已加密
  338. wx.redirectTo({
  339. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  340. that.data.couponChannelId +
  341. "&couponId=" +
  342. that.data.couponId
  343. });
  344. } else {
  345. wx.showToast({
  346. title: err.errMsg,
  347. icon: 'none',
  348. duration: 2000,
  349. mask: false
  350. });
  351. }
  352. })
  353. .then(res => {
  354. if (typeof (res) != "undefined") {
  355. console.log("", res);
  356. let orderId = "" + res.data.id;
  357. that.setData({
  358. orderId: orderId
  359. });
  360. if (res.data.payment > 0) {
  361. // 支付金额不为0
  362. /**
  363. * 支付订单创建
  364. */
  365. Http.post({
  366. url: config.api.payOrderCreate,
  367. data: {
  368. orderId: orderId
  369. }
  370. }).then(res => {
  371. console.log(res);
  372. /// Begin payment ----
  373. var payOrderId = "" + res.data.payOrderId;
  374. wx.hideLoading();
  375. wx.requestPayment({
  376. timeStamp: res.data.timeStamp,
  377. nonceStr: res.data.nonceStr,
  378. package: res.data.package,
  379. signType: (res.data.signType) ? res.data.signType : "MD5",
  380. paySign: res.data.paySign,
  381. success: res => {
  382. that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  383. console.log(res);
  384. if (res.errMsg == "requestPayment:ok") {
  385. setTimeout(function () {
  386. wx.hideLoading();
  387. }, 2000);
  388. setTimeout(() => {
  389. wx.navigateTo({
  390. url: `/pages/order/detail/index?orderId=${
  391. that.data.orderId
  392. }`
  393. });
  394. }, 1000)
  395. /**
  396. * 用户支付成功以后跳转到券包列表
  397. */
  398. wx.setStorage({
  399. key: 'couponNum',
  400. data: "couponNum",
  401. })
  402. if (that.data.data.type == 5) {
  403. setTimeout(() => {
  404. wx.switchTab({
  405. url: '/pages/passCar/passCar'
  406. });
  407. }, 1600)
  408. }
  409. }
  410. },
  411. fail: res => {
  412. that.payOrderUpdate(that.data.orderId, payOrderId, 2); // 支付失败
  413. console.log(res);
  414. return;
  415. },
  416. complete: res => {
  417. }
  418. });
  419. /// End payment --------
  420. ///// virtual pay
  421. //var payOrderId = "" + res.data.out_trade_no;
  422. //that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  423. ///// end virtual pay
  424. })
  425. .catch(err => {
  426. wx.showToast({
  427. title: err.errMsg,
  428. icon: 'none',
  429. duration: 2000,
  430. mask: false
  431. });
  432. })
  433. } else {
  434. /**
  435. * @Meo
  436. * 功能:用户问卷调查功能
  437. */
  438. console.log(that.data.data.type);
  439. that.payOrderUpdate(orderId, "0", 1);
  440. wx.setStorage({
  441. key: 'couponNum',
  442. data: "couponNum"
  443. })
  444. setTimeout(function () {
  445. wx.navigateTo({
  446. url: `/pages/order/detail/index?orderId=${
  447. that.data.orderId
  448. }`
  449. });
  450. }, 1000)
  451. /**
  452. * @Meo
  453. * type
  454. * 5 停车券
  455. */
  456. if (that.data.data.type == 5) {
  457. setTimeout(() => {
  458. wx.switchTab({
  459. url: '/pages/passCar/passCar'
  460. });
  461. }, 1600);
  462. }
  463. }
  464. }
  465. })
  466. .catch(err => {
  467. wx.showToast({
  468. title: err.errMsg,
  469. icon: 'none',
  470. duration: 2000,
  471. mask: false
  472. });
  473. })
  474. }
  475. },
  476. onShareAppMessage: function (options) {
  477. var that = this;
  478. var shareObj = {
  479. title: options.target.dataset.title,
  480. path: '/pages/index/index',
  481. success: function (res) {
  482. if (res.errMsg == 'shareAppMessage:ok') {
  483. console.log(res)
  484. }
  485. },
  486. fail: function (error) {
  487. if (res.errMsg == 'shareAppMessage:fail cancel') {
  488. console.log(error)
  489. } else if (res.errMsg == 'shareAppMessage:fail') {
  490. console.log(error)
  491. }
  492. }
  493. };
  494. // 来自页面内的按钮的转发
  495. if (options.from == 'button') {
  496. console.log(options)
  497. var eData = options.target.dataset.id;
  498. var couponId = options.target.dataset.couponid;
  499. // shareObj.path = `/pages/index/index?couponChannelId=${eData}&couponId=${couponId}`;
  500. shareObj.path = `/pages/index/index?couponChannelId=${eData}&couponId=${couponId}`;
  501. }
  502. // 返回shareObj
  503. return shareObj;
  504. }
  505. });