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.

462 lines
12 KiB

  1. // pages/spellGroup/mySpellGroup/index.js
  2. var config = require("../../../config/config.js");
  3. const Http = require("../../../utils/HttpBasics");
  4. const imgurl = require("../../../utils/imgurl");
  5. const utils = require("../../../utils/util.js")
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. teljpgUrl: imgurl.teljpg.url,
  12. couponChannelId:'',
  13. couponId:'',
  14. data:[],
  15. spellData:null,
  16. canSpell:true,
  17. canBuyIf:true,
  18. clock: "00",
  19. day: "00",
  20. hour: "00",
  21. min: "00",
  22. sec: "00",
  23. },
  24. /**
  25. * 生命周期函数--监听页面加载
  26. */
  27. onLoad: function (options) {
  28. this.setData({
  29. couponChannelId: options.couponChannelId,
  30. couponId: options.couponId
  31. })
  32. this.getDetail(options.couponChannelId);
  33. this.getOneSpell(options.couponId)
  34. },
  35. /**
  36. * 拨打电话
  37. */
  38. phone: function (e) {
  39. let that = this;
  40. wx.makePhoneCall({
  41. phoneNumber: e.target.dataset.merchantlinkphone
  42. });
  43. },
  44. /**
  45. * 直接购买
  46. */
  47. gotoBuy(){
  48. console.log(22222)
  49. this.setData({
  50. canBuyIf: false
  51. })
  52. this.orderFunc()
  53. },
  54. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  55. dateformat(micro_second) {
  56. // 总秒数
  57. var second = Math.floor(micro_second / 1000);
  58. // 天数
  59. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  60. // 小时
  61. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  62. // 分钟
  63. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  64. // 秒
  65. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60) : Math.floor(second % 60);
  66. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  67. return {
  68. a1: day,
  69. b1: hr,
  70. c1: min,
  71. d1: sec
  72. }
  73. },
  74. countdown(end_time) {
  75. let that = this;
  76. var EndTime = end_time;
  77. var NowTime = new Date().getTime();
  78. var total_micro_second = EndTime - NowTime || [];
  79. // 渲染倒计时时钟
  80. let obj = that.dateformat(total_micro_second);
  81. if (total_micro_second > 0) {
  82. that.setData({
  83. clock: obj,
  84. day: obj.a1,
  85. hour: obj.b1,
  86. min: obj.c1,
  87. sec: obj.d1,
  88. })
  89. } else {
  90. that.setData({
  91. // spellData:null,
  92. clock: "00",
  93. day: "00",
  94. hour: "00",
  95. min: "00",
  96. sec: "00",
  97. })
  98. // that.getOneSpell(that.data.couponId)
  99. }
  100. setTimeout(function () {
  101. total_micro_second -= 1000;
  102. that.countdown(end_time);
  103. }, 1000)
  104. },
  105. /**
  106. * 发起拼团
  107. */
  108. gotoSpell(){
  109. console.log(111111111)
  110. this.setData({
  111. canSpell:false
  112. })
  113. this.orderFunc(0)
  114. },
  115. //参与别人的拼团
  116. gotoPartner(){
  117. wx.navigateTo({
  118. url: `/pages/joinFrDpell/index?orderId=${this.data.spellData.orderId}&couponId=${this.data.spellData.couponId}&orderGroupId=${this.data.spellData.orderGroupId}&couponChannelId=${this.data.couponChannelId}`
  119. });
  120. },
  121. /**
  122. * 获取一个拼团信息
  123. */
  124. getOneSpell(couponId){
  125. let that = this;
  126. Http.get({
  127. url: config.api.queryRemainOne,
  128. data: {
  129. couponId: couponId
  130. }
  131. }).then(res => {
  132. if(res.data){
  133. that.countdown(res.data.expiredDate);
  134. that.setData({
  135. spellData: res.data
  136. });
  137. }
  138. });
  139. },
  140. /**
  141. * 获取券详情信息
  142. */
  143. getDetail(couponChannelId){
  144. let that = this;
  145. Http.get({
  146. url: config.api.couponDetail,
  147. data: {
  148. couponChannelId: couponChannelId
  149. }
  150. }).then(res => {
  151. let data = res.data;
  152. data.price = (data.price / 100).toFixed(2)
  153. data.salePrice = (data.salePrice / 100).toFixed(2);
  154. data.validStartDate = utils.formatTime(data.validStartDate,'yyyy-MM-dd')
  155. data.validEndDate = utils.formatTime(data.validEndDate, 'yyyy-MM-dd')
  156. that.setData({
  157. data
  158. });
  159. });
  160. },
  161. /**
  162. * 去拼团
  163. */
  164. goToOrderGroup(orderId, orderGroupId, _this){
  165. console.log(22222222)
  166. let that = this;
  167. // 支付成功
  168. Http.post({
  169. url: config.api.toOrderGroup,
  170. data: {
  171. id: orderGroupId,
  172. orderId,
  173. couponId: _this.data.data.couponId
  174. }
  175. })
  176. .then(res => {
  177. wx.navigateTo({
  178. url: `/pages/spellDetail/index?orderId=${orderId}&couponId=${_this.data.data.couponId}&orderGroupId=${res.data.orderGroupId}&couponChannelId=${_this.data.couponChannelId}`
  179. });
  180. })
  181. .catch(err => {
  182. console.log(err);
  183. })
  184. // return;
  185. },
  186. /**
  187. * 支付订单更新
  188. */
  189. payOrderUpdate: (orderId, payOrderId, status, reason, _this, orderGroupId) => {
  190. console.log(orderGroupId,7777)
  191. let that = this;
  192. // 支付成功
  193. Http.post({
  194. url: config.api.payOrderUpdate,
  195. data: {
  196. payOrderId: payOrderId,
  197. orderId: orderId,
  198. status: status,
  199. reason: reason
  200. }
  201. })
  202. .then(res => {
  203. wx.hideLoading()
  204. if (!orderGroupId){
  205. wx.navigateTo({
  206. url: '/pages/order/detail/index?orderId='+res.data.id,
  207. })
  208. }else{
  209. _this.goToOrderGroup(orderId, res.data.orderGroupId, _this)
  210. }
  211. })
  212. .catch(err => {
  213. console.log(err);
  214. if (err.code!=12002){
  215. setTimeout(function () {
  216. _this.payOrderUpdate(orderId, payOrderId, status, reason, _this, orderGroupId);
  217. }, 2000)
  218. }
  219. })
  220. },
  221. /**
  222. * 发起支付
  223. */
  224. orderFunc(orderGroupId) {
  225. let data = {
  226. couponChannelId: this.data.data.id,
  227. couponId: this.data.data.couponId
  228. }
  229. /**
  230. * 拼团订单
  231. */
  232. if (orderGroupId==0){
  233. data.orderGroupId = orderGroupId
  234. }
  235. var that = this;
  236. Http.post({
  237. url: config.api.checkPhoneStatus,
  238. data: {}
  239. })
  240. .then(res => {
  241. /**
  242. * orderSave 下单
  243. */
  244. return Http.post({
  245. url: config.api.orderSave,
  246. data: data
  247. });
  248. })
  249. .catch(err => {
  250. console.log(err);
  251. that.setData({
  252. showbutton: false,
  253. showbutton1: false
  254. })
  255. if (err.code == 2011) {
  256. wx.showToast({
  257. title: "商户信息没找到",
  258. image: './../../../assets/images/fail.png',
  259. duration: 2000,
  260. mask: false
  261. });
  262. } else if (err.code == 2013) {
  263. wx.showToast({
  264. title: "商户信息禁用",
  265. image: './../../../assets/images/fail.png',
  266. duration: 2000,
  267. mask: false
  268. });
  269. } else if (err.code == 3000) {
  270. wx.showToast({
  271. title: "库存不足",
  272. image: './../../../assets/images/fail.png',
  273. duration: 2000,
  274. mask: false
  275. });
  276. } else if (err.code == 3001) {
  277. wx.showToast({
  278. title: "超过限购条件",
  279. image: './../../../assets/images/fail.png',
  280. duration: 2000,
  281. mask: false
  282. });
  283. } else if (err.code == 3002) {
  284. wx.showToast({
  285. title: "订单失败",
  286. image: './../../../assets/images/fail.png',
  287. duration: 2000,
  288. mask: false
  289. });
  290. } else if (err.code == 3003) {
  291. wx.showToast({
  292. title: "订单不存在",
  293. image: './../../../assets/images/fail.png',
  294. duration: 2000,
  295. mask: false
  296. });
  297. } else if (err.code == 3004) {
  298. wx.showToast({
  299. title: "订单不存在",
  300. image: './../../../assets/images/fail.png',
  301. duration: 2000,
  302. mask: false
  303. });
  304. } else if (err.code == 4003) {
  305. wx.showToast({
  306. title: "卡券已作废",
  307. image: './../../../assets/images/fail.png',
  308. duration: 2000,
  309. mask: false
  310. });
  311. } else if (err.code == 11005) {
  312. /**
  313. * 将值传到用户手机号授权的页面
  314. *
  315. */
  316. wx.redirectTo({
  317. url: "/pages/getphoneInfo/index?path=spell&couponChannelId=" +
  318. that.data.couponChannelId + '&couponId=' + that.data.couponId
  319. });
  320. } else if (err.code == 11006) {
  321. // 用户手机已加密
  322. wx.redirectTo({
  323. url: "/pages/phoneinput/phoneinput?path=spell&couponChannelId=" +
  324. that.data.couponChannelId + '&couponId=' + that.data.couponId
  325. });
  326. } else {
  327. wx.showToast({
  328. title: err.message,
  329. icon: 'none',
  330. duration: 2000,
  331. mask: false
  332. });
  333. }
  334. })
  335. .then(res => {
  336. console.log(res)
  337. if (typeof (res) != "undefined") {
  338. let orderId = "" + res.data.id;
  339. that.setData({
  340. orderId: orderId
  341. });
  342. // 支付金额不为0
  343. /**
  344. * 支付订单创建
  345. */
  346. Http.post({
  347. url: config.api.payOrderCreate,
  348. data: {
  349. orderId: orderId
  350. }
  351. })
  352. .then(res => {
  353. var payOrderId = "" + res.data.payOrderId;
  354. wx.hideLoading();
  355. wx.requestPayment({
  356. timeStamp: res.data.timeStamp,
  357. nonceStr: res.data.nonceStr,
  358. package: res.data.package,
  359. signType: (res.data.signType) ? res.data.signType : "MD5",
  360. paySign: res.data.paySign,
  361. success: res => {
  362. wx.showLoading({
  363. title: '订单正在处理中...',
  364. })
  365. setTimeout(function () {
  366. wx.hideLoading()
  367. }, 5000)
  368. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  369. if (res.errMsg == "requestPayment:ok") {
  370. setTimeout(function () {
  371. wx.hideLoading();
  372. }, 2000);
  373. /**
  374. * 用户支付成功以后跳转到券包列表
  375. */
  376. if (that.data.cardType == 100) {
  377. wx.setStorage({
  378. key: 'couponNum2',
  379. data: "couponNum2"
  380. })
  381. } else if (that.data.data.type != 5) {
  382. wx.setStorage({
  383. key: 'couponNum',
  384. data: "couponNum"
  385. })
  386. }
  387. }
  388. },
  389. fail: res => {
  390. /**
  391. * 支付失败,需要更新订单的状态
  392. */
  393. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  394. that.setData({
  395. showbutton: false
  396. })
  397. return;
  398. },
  399. complete: res => { }
  400. });
  401. /// End payment --------
  402. })
  403. .catch(err => {
  404. wx.showToast({
  405. title: err.message,
  406. icon: 'none',
  407. duration: 2000,
  408. mask: false
  409. });
  410. })
  411. }
  412. })
  413. },
  414. /**
  415. * 生命周期函数--监听页面初次渲染完成
  416. */
  417. onReady: function () {
  418. },
  419. /**
  420. * 生命周期函数--监听页面显示
  421. */
  422. onShow: function () {
  423. this.setData({
  424. canSpell:true,
  425. canBuyIf:true
  426. })
  427. },
  428. /**
  429. * 生命周期函数--监听页面隐藏
  430. */
  431. onHide: function () {
  432. },
  433. /**
  434. * 生命周期函数--监听页面卸载
  435. */
  436. onUnload: function () {
  437. },
  438. /**
  439. * 页面相关事件处理函数--监听用户下拉动作
  440. */
  441. onPullDownRefresh: function () {
  442. },
  443. /**
  444. * 页面上拉触底事件的处理函数
  445. */
  446. onReachBottom: function () {
  447. }
  448. })