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.

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