抖音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.

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