抖音c端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

674 行
22 KiB

  1. const navigationBarHeight = (getApp().statusBarHeight + 50) + 'px'
  2. var config = require("../../../config/config.js");
  3. var app = getApp();
  4. const Http = require("../../../utils/HttpBasics");
  5. const util = require("../../../utils/util");
  6. const imgurl = require("../../../utils/imgurl");
  7. Page({
  8. data: {
  9. couponIds:"",
  10. couponChannelId:"",
  11. showPage:false,
  12. data:{},
  13. spuIdObj:{},
  14. priceAndStockObj:{}
  15. },
  16. onLoad: function (options) {
  17. console.log(options);
  18. tt.showLoading({
  19. title: "加载中..."
  20. });
  21. let that = this
  22. if (app.globalData.token){
  23. if (options && (options.couponChannelId || options.couponIds)) {
  24. if (options.couponIds) {
  25. this.setData({
  26. couponIds: options.couponIds
  27. })
  28. that.getDetail(options.couponIds, 'notendclock');
  29. } else {
  30. that.getDetail(options.couponChannelId, 'notendclock');
  31. that.getSupId(options.couponChannelId)
  32. }
  33. }
  34. }else{
  35. app.tokenCallback = token => {
  36. if (options && (options.couponChannelId || options.couponIds)) {
  37. if (options.couponIds) {
  38. this.setData({
  39. couponIds: options.couponIds
  40. })
  41. that.getDetail(options.couponIds, 'notendclock');
  42. } else {
  43. that.getDetail(options.couponChannelId, 'notendclock');
  44. // that.getSupId(options.couponChannelId)
  45. }
  46. }
  47. }
  48. }
  49. },
  50. getSupId(id) {
  51. Http.get({
  52. url: config.api.getSpuid,
  53. data: { id: id }
  54. }).then(res => {
  55. if (res.data) {
  56. this.setData({
  57. spuIdObj: res.data
  58. })
  59. }
  60. }).catch(err => {
  61. // tt.showToast({
  62. // title: err.message,
  63. // icon: 'none',
  64. // duration: 2000,
  65. // mask: false
  66. // });
  67. })
  68. },
  69. getCouponPriceAndStock(couponChannelId) {//获取券价格与库存
  70. Http.get({
  71. url: config.api.couponPriceAndStock,
  72. data: {
  73. couponChannelId
  74. }
  75. }).then(res => {
  76. let tempBoj = JSON.parse(res.data)
  77. console.log(tempBoj, "$");
  78. this.setData({
  79. priceAndStockObj: tempBoj
  80. })
  81. })
  82. },
  83. getDetail: function (couponChannelId, flag) {
  84. let that = this;
  85. let data = {};
  86. that.getCouponPriceAndStock(couponChannelId);//获取券价格和库存
  87. if (that.data.couponIds) {
  88. data.couponId = couponChannelId
  89. } else {
  90. data.couponChannelId = couponChannelId
  91. }
  92. var parmer = {
  93. url: config.api.newCouponDetail,
  94. data: data
  95. };
  96. Http.get(parmer).then(res => {
  97. that.setData({
  98. couponId: res.data.couponId
  99. })
  100. /**
  101. * activityStatus==0 活动未开始
  102. * activityStatus==1 活动已开始
  103. * flag == endclock 说明倒计时已经结束
  104. */
  105. if (res.data.endTime && res.data.beginTime) {
  106. that.setData({
  107. begin_time: res.data.beginTime,
  108. end_time: res.data.endTime,
  109. activityStatus: res.data.actStatus ? res.data.actStatus : ''
  110. })
  111. if (res.data.actStatus == 0 && flag != 'endclock') {
  112. that.countdown(res.data.beginTime);
  113. } else if (res.data.actStatus != 0 && flag != 'endclock') {
  114. that.countdown(res.data.endTime);
  115. } else {
  116. clearInterval(that.data.setInterval)
  117. }
  118. if (res.data.actStatus == 0) {
  119. var beginTime = util.formatTime(res.data.beginTime, "yyyy-MM-dd hh:mm:ss");
  120. if (util.timechuo(beginTime).indexOf('-') == 0) {
  121. that.setData({
  122. beginTime: "活动已结束",
  123. });
  124. } else {
  125. that.setData({
  126. beginTime: util.timechuo(beginTime)
  127. });
  128. }
  129. } else {
  130. var endTime = util.formatTime(res.data.endTime, "yyyy-MM-dd hh:mm:ss");
  131. if (util.timechuo(endTime).indexOf('-') == 0) {
  132. that.setData({
  133. endtime: "活动已结束",
  134. });
  135. } else {
  136. that.setData({
  137. endtime: util.timechuo(endTime)
  138. });
  139. }
  140. }
  141. }
  142. tt.hideLoading();
  143. that.setData({
  144. data: res.data,
  145. });
  146. if (res.data.type == 10) {
  147. that.setData({
  148. salePriceStr: res.data.salePriceStr,
  149. pickEndDate: util.formatTime(res.data.pickEndDate, "yyyy-MM-dd"),
  150. pickStartDate: util.formatTime(res.data.pickStartDate, "yyyy-MM-dd"),
  151. priceStr: res.data.priceStr,
  152. tailPriceStr: res.data.tailPriceStr,
  153. origPriceStr: res.data.origPriceStr
  154. })
  155. }
  156. if (res.data.validType == 1) {
  157. that.setData({
  158. validStartDate: util.formatTime(res.data.validStartDate, "yyyy-MM-dd"),
  159. validEndDate: util.formatTime(res.data.validEndDate, "yyyy-MM-dd"),
  160. pickStartDate: util.formatTime(res.data.pickStartDate, "yyyy-MM-dd"),
  161. pickEndDate: util.formatTime(res.data.pickEndDate, "yyyy-MM-dd"),
  162. });
  163. } else {
  164. if (res.data.validDays) {
  165. that.setData({
  166. validDays: res.data.validDays
  167. });
  168. }
  169. }
  170. if (res.data.itemGroup) {
  171. let tempObj = this.data.data
  172. tempObj.itemGroup = JSON.parse(tempObj.itemGroup)
  173. this.setData({
  174. data: tempObj
  175. })
  176. console.log(this.data.data, "data");
  177. }
  178. if (res.data.productAttrs && res.data.skuAttrs) {
  179. let tempObj = this.data.data
  180. tempObj.productAttrs = JSON.parse(tempObj.productAttrs)
  181. tempObj.skuAttrs = JSON.parse(tempObj.skuAttrs)
  182. tempObj.productAttrs.map(item => {
  183. if (item.key == "Notification") {
  184. if (item.data != '') {
  185. tempObj.curLsit = JSON.parse(item.data)
  186. }
  187. }
  188. if (item.key == "bring_out_meal") {
  189. tempObj.besides = item.data
  190. }
  191. if (item.key == "free_pack") {
  192. tempObj.pack = item.data
  193. }
  194. if (item.key == "superimposed_discounts") {
  195. tempObj.superimposed_discounts = item.data
  196. }
  197. if (item.key == "private_room") {
  198. tempObj.private_room = item.data
  199. }
  200. if (item.key == "rec_person_num_max") {
  201. tempObj.rec_person_num_max = item.data
  202. }
  203. if (item.key == 'Description') {
  204. tempObj.Description = JSON.parse(item.data)
  205. }
  206. if (item.key == "can_no_use_date") {
  207. tempObj.can_no_use_date = JSON.parse(item.data)
  208. }
  209. })
  210. tempObj.skuAttrs.map(item => {
  211. if (item.key == "commodity") {
  212. if (item.data != '') {
  213. tempObj.itemGroup = JSON.parse(item.data)
  214. }
  215. }
  216. })
  217. this.setData({
  218. data: tempObj
  219. })
  220. console.log(this.data.data, "data");
  221. }
  222. }).catch(err => {
  223. tt.showToast({
  224. title: err.message,
  225. icon: 'none',
  226. duration: 2000,
  227. mask: false
  228. });
  229. })
  230. },
  231. /**
  232. * 支付订单更新
  233. */
  234. payOrderUpdate: (orderId, payOrderId, status, reason, type, _this, composeOrderType) => {
  235. let that = this;
  236. // 支付成功
  237. Http.post({
  238. url: config.api.payOrderUpdate,
  239. data: {
  240. payOrderId: payOrderId,
  241. composeOrderId: orderId,
  242. status: status,
  243. reason: reason
  244. }
  245. })
  246. .then(res => {
  247. tt.hideLoading()
  248. // 有价券
  249. if (!type && type != 'free') {
  250. tt.navigateTo({
  251. url: `/pages/order/detail/index?orderId=${orderId
  252. }`
  253. });
  254. } else if (type == 'free') {
  255. tt.navigateTo({
  256. url: `/pages/order/detail/index?orderId=${orderId
  257. }`
  258. });
  259. }
  260. })
  261. .catch(err => {
  262. console.log(err);
  263. if (!type) {
  264. setTimeout(function () {
  265. _this.payOrderUpdate(orderId, payOrderId, status, reason, type, _this, composeOrderType);
  266. }, 2000)
  267. }
  268. })
  269. },
  270. /**
  271. * 发起支付
  272. */
  273. orderFunc(discount) {
  274. if(this.data.showbutton)return;
  275. let that = this;
  276. tt.showLoading({
  277. title: "加载中..."
  278. });
  279. that.setData({
  280. showbutton: true,
  281. })
  282. Http.get({
  283. url: config.api.checkUserStatus,
  284. data: {
  285. token: app.globalData.token
  286. }
  287. }).then(res => {
  288. Http.get({
  289. url: config.api.checkPhoneStatus,
  290. data: {}
  291. }).then(res => {
  292. if ((!tt.canIUse('createOrder')) || that.data.data.type == 50 || that.data.priceAndStockObj.salePrice == 0) {
  293. //积分券和免费券
  294. // 发起砍价
  295. if (discount == 'discount') {
  296. var data = {
  297. couponChannelId: "" + that.data.couponChannelId,
  298. couponId: "" + that.data.couponId,
  299. formId: "" + that.data.formId,
  300. press: true
  301. }
  302. } else if (discount == 'discount1') {
  303. var data = {
  304. couponChannelId: "" + that.data.couponChannelId,
  305. couponId: "" + that.data.couponId,
  306. formId: "" + that.data.formId,
  307. press: false
  308. }
  309. } else if (that.data.couponChannelId == null) {
  310. var data = {
  311. couponId: "" + that.data.couponId,
  312. formId: "" + that.data.formId,
  313. };
  314. } else {
  315. var data = {
  316. couponChannelId: "" + that.data.couponChannelId,
  317. couponId: "" + that.data.couponId,
  318. formId: "" + that.data.formId,
  319. }
  320. }
  321. console.log(data, "data")
  322. let url = ""
  323. let tempObj;
  324. let tempArr = []
  325. if (that.data.data.type == 12) {
  326. url = config.api.couponPackageSave
  327. tempObj = {
  328. signleOrder: {
  329. couponChannelId: "" + that.data.couponChannelId,
  330. couponId: "" + that.data.couponId,
  331. formId: "" + that.data.formId,
  332. },
  333. count: "1"
  334. }
  335. tempArr.push(tempObj)
  336. } else {
  337. url = config.api.orderSave
  338. }
  339. /**
  340. * orderSave 下单
  341. */
  342. Http.post({
  343. url: url,
  344. data: that.data.data.type == 12 ? JSON.stringify(tempArr) : data
  345. }).then(res => {
  346. if (discount != 'discount') {
  347. if (typeof (res) != "undefined") {
  348. let orderId = "" + res.data.mainOrderId;
  349. that.setData({
  350. orderId: orderId,
  351. composeOrderType: res.data.composeOrderType
  352. });
  353. if (res.data.payment > 0) {
  354. // 支付金额不为0
  355. /**
  356. * 支付订单创建
  357. */
  358. Http.post({
  359. url: config.api.payOrderCreate,
  360. data: {
  361. orderId: orderId,
  362. composeOrderType: res.data.composeOrderType
  363. }
  364. }).then(res => {
  365. var payOrderId = "" + res.data.outOrderNo;
  366. tt.hideLoading();
  367. tt.pay({
  368. service: 5,
  369. orderInfo: {
  370. order_id: res.data.orderId,
  371. order_token: res.data.token,
  372. },
  373. success: res => {
  374. tt.showLoading({
  375. title: '订单正在处理中...',
  376. })
  377. if (res.code === 0) {
  378. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that, that.data.composeOrderType);
  379. if (res.errMsg == "requestPayment:ok") {
  380. /**
  381. * 用户支付成功以后跳转到券包列表
  382. */
  383. if (that.data.cardType == 100) {
  384. tt.setStorage({
  385. key: 'couponNum2',
  386. data: "couponNum2"
  387. })
  388. } else if (that.data.data.type != 5 && that.data.cardType != 100) {
  389. tt.setStorage({
  390. key: 'couponNum',
  391. data: "couponNum"
  392. })
  393. }
  394. }
  395. }
  396. else if (res.code === 1) {
  397. that.setData({
  398. showbutton: false
  399. })
  400. tt.hideLoading();
  401. tt.showToast({
  402. title: '支付超时', // 内容
  403. icon: "none"
  404. });
  405. } else if (res.code === 2) {
  406. that.setData({
  407. showbutton: false
  408. })
  409. tt.hideLoading();
  410. tt.showToast({
  411. title: '支付失败', // 内容
  412. icon: "none"
  413. });
  414. } else if (res.code === 3) {
  415. that.setData({
  416. showbutton: false
  417. })
  418. tt.hideLoading();
  419. tt.showToast({
  420. title: '支付关闭', // 内容
  421. icon: "none"
  422. });
  423. } else if (res.code === 4) {
  424. that.setData({
  425. showbutton: false
  426. })
  427. tt.hideLoading();
  428. tt.showToast({
  429. title: '支付取消', // 内容
  430. icon: "none"
  431. });
  432. } else if (res.code === 9) {
  433. that.setData({
  434. showbutton: false
  435. })
  436. tt.hideLoading();
  437. tt.showToast({
  438. title: '订单状态开发者自行获取', // 内容
  439. icon: "none"
  440. });
  441. }
  442. },
  443. fail: res => {
  444. tt.hideLoading();
  445. /**
  446. * 支付失败,需要更新订单的状态
  447. */
  448. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that, that.data.composeOrderType);
  449. that.setData({
  450. showbutton: false
  451. })
  452. return;
  453. },
  454. complete: res => { }
  455. });
  456. /// End payment --------
  457. })
  458. .catch(err => {
  459. tt.hideLoading();
  460. tt.showToast({
  461. title: err.message,
  462. icon: 'none',
  463. duration: 2000,
  464. mask: false
  465. });
  466. })
  467. } else {
  468. // 免费券
  469. that.payOrderUpdate(orderId, "0", 1, '', 'free', that, that.data.composeOrderType);
  470. if (that.data.cardType == 100) {
  471. tt.setStorage({
  472. key: 'couponNum2',
  473. data: "couponNum2"
  474. })
  475. } else if (that.data.data.type != "5" && that.data.cardType != 100) {
  476. tt.setStorage({
  477. key: 'couponNum',
  478. data: "couponNum"
  479. })
  480. }
  481. }
  482. }
  483. } else {
  484. if (res) {
  485. tt.navigateTo({
  486. url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${res.data.mainOrderId}&composeOrderType=${res.data.composeOrderType}`,
  487. })
  488. }
  489. }
  490. })
  491. } else {
  492. //有价券
  493. console.log(tt.canIUse('createOrder'), "是否支持2.0js-api");
  494. if (tt.canIUse('createOrder')) {
  495. //支持 支付2.0 js-api
  496. let tempCallbackData = {}
  497. tempCallbackData[that.data.data.couponChannel.ttSpuId] = that.data.data.couponChannel.id
  498. let options = {
  499. callbackData: tempCallbackData,
  500. goodsList: [
  501. {
  502. quantity: 1, // 购买数量 必填
  503. price: that.data.priceAndStockObj.salePrice, // 商品价格 必填
  504. goodsName: that.data.data.title, // 商品名称 必填
  505. goodsPhoto: that.data.data.coverImg, // 商品图片链接 必填
  506. goodsId: that.data.data.couponChannel.ttSpuId, // 商品ID 必填
  507. goodsType: 1, // 商品类型 必填
  508. goodsLabels: ['过期退', '随时退', '免预约'], // 商品标签 非必填
  509. dateRule: '', // 使用规则 非必填
  510. },
  511. ],
  512. payment: {
  513. totalAmount: that.data.priceAndStockObj.salePrice, // 订单总价 必填
  514. },
  515. success: res => {
  516. tt.hideLoading();
  517. that.setData({
  518. showbutton: false,
  519. })
  520. const { orderId, outOrderNo } = res;
  521. that.setData({ orderId, outOrderNo });
  522. that.payOrderUpdate(outOrderNo, outOrderNo, 1, '', '', that, '');
  523. },
  524. fail: res => {
  525. const { orderId, outOrderNo, errNo, errMsg, errLogId } = res;
  526. if (errLogId) {
  527. tt.hideLoading();
  528. that.setData({
  529. showbutton: false,
  530. })
  531. console.log('预下单失败', errNo, errMsg, errLogId);
  532. }
  533. if (orderId || outOrderNo) {
  534. tt.hideLoading();
  535. that.setData({
  536. showbutton: false,
  537. })
  538. console.log('支付失败', errNo, errMsg, orderId, outOrderNo);
  539. console.log(that.payOrderUpdate, "payOrderUpdate");
  540. that.payOrderUpdate(outOrderNo,outOrderNo,2,'','fail')
  541. }
  542. },
  543. }
  544. console.log(options, "options");
  545. tt.createOrder(options)
  546. } else {
  547. //不支持 支付2.0 js -api
  548. }
  549. }
  550. }).catch(err => {
  551. tt.hideLoading()
  552. that.setData({
  553. showbutton: false,
  554. })
  555. if (err.code == 2011) {
  556. tt.showToast({
  557. title: "商户信息没找到",
  558. image: './../../../assets/images/fail.png',
  559. duration: 2000,
  560. mask: false
  561. });
  562. } else if (err.code == 2013) {
  563. tt.showToast({
  564. title: "商户信息禁用",
  565. image: './../../../assets/images/fail.png',
  566. duration: 2000,
  567. mask: false
  568. });
  569. } else if (err.code == 3000) {
  570. tt.showToast({
  571. title: "库存不足",
  572. image: './../../../assets/images/fail.png',
  573. duration: 2000,
  574. mask: false
  575. });
  576. } else if (err.code == 3001) {
  577. tt.showToast({
  578. title: "您已超过限购",
  579. image: './../../../assets/images/fail.png',
  580. duration: 2000,
  581. mask: false
  582. });
  583. } else if (err.code == 3002) {
  584. tt.showToast({
  585. title: "订单失败",
  586. image: './../../../assets/images/fail.png',
  587. duration: 2000,
  588. mask: false
  589. });
  590. } else if (err.code == 3003) {
  591. tt.showToast({
  592. title: "订单不存在",
  593. image: './../../../assets/images/fail.png',
  594. duration: 2000,
  595. mask: false
  596. });
  597. } else if (err.code == 3004) {
  598. tt.showToast({
  599. title: "订单不存在",
  600. image: './../../../assets/images/fail.png',
  601. duration: 2000,
  602. mask: false
  603. });
  604. } else if (err.code == 4003) {
  605. tt.showToast({
  606. title: "卡券已作废",
  607. image: './../../../assets/images/fail.png',
  608. duration: 2000,
  609. mask: false
  610. });
  611. } else if (err.code == 3012) {
  612. that.getUnPaidOrder(that.data.couponId);
  613. } else if (err.code == 11005) {
  614. /**
  615. * 将值传到用户手机号授权的页面
  616. *
  617. */
  618. tt.redirectTo({
  619. url: "/pages/getphoneInfo/index?couponChannelId=" +
  620. that.data.couponChannelId
  621. });
  622. } else if (err.code == 11006) {
  623. // 用户手机已加密
  624. tt.redirectTo({
  625. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  626. that.data.couponChannelId
  627. });
  628. } else {
  629. tt.showToast({
  630. title: err.message,
  631. icon: 'none',
  632. duration: 2000,
  633. mask: false
  634. });
  635. }
  636. })
  637. }).catch(err => {
  638. console.log(err)
  639. if (err.code == 11004) {
  640. tt.redirectTo({
  641. url: `/pages/getuserinfo/index?fromflag=coupondetail&couponChannelIdflag=${that.data.couponChannelId}`,
  642. })
  643. } else {
  644. tt.showToast({
  645. title: err.message,
  646. icon: "none",
  647. duration: 2500
  648. })
  649. }
  650. })
  651. },
  652. })