C端小程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

478 строки
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. })
  271. if (err.code == 2011) {
  272. wx.showToast({
  273. title: "商户信息没找到",
  274. image: './../../../assets/images/fail.png',
  275. duration: 2000,
  276. mask: false
  277. });
  278. } else if (err.code == 2013) {
  279. wx.showToast({
  280. title: "商户信息禁用",
  281. image: './../../../assets/images/fail.png',
  282. duration: 2000,
  283. mask: false
  284. });
  285. } else if (err.code == 3000) {
  286. wx.showToast({
  287. title: "库存不足",
  288. image: './../../../assets/images/fail.png',
  289. duration: 2000,
  290. mask: false
  291. });
  292. } else if (err.code == 3001) {
  293. wx.showToast({
  294. title: "超过限购条件",
  295. image: './../../../assets/images/fail.png',
  296. duration: 2000,
  297. mask: false
  298. });
  299. } else if (err.code == 3002) {
  300. wx.showToast({
  301. title: "订单失败",
  302. image: './../../../assets/images/fail.png',
  303. duration: 2000,
  304. mask: false
  305. });
  306. } else if (err.code == 3003) {
  307. wx.showToast({
  308. title: "订单不存在",
  309. image: './../../../assets/images/fail.png',
  310. duration: 2000,
  311. mask: false
  312. });
  313. } else if (err.code == 3004) {
  314. wx.showToast({
  315. title: "订单不存在",
  316. image: './../../../assets/images/fail.png',
  317. duration: 2000,
  318. mask: false
  319. });
  320. } else if (err.code == 4003) {
  321. wx.showToast({
  322. title: "卡券已作废",
  323. image: './../../../assets/images/fail.png',
  324. duration: 2000,
  325. mask: false
  326. });
  327. } else if (err.code == 11005) {
  328. /**
  329. * 将值传到用户手机号授权的页面
  330. *
  331. */
  332. wx.redirectTo({
  333. url: "/pages/getphoneInfo/index?path=spell&couponChannelId=" +
  334. that.data.couponChannelId + '&couponId=' + that.data.couponId
  335. });
  336. } else if (err.code == 11006) {
  337. // 用户手机已加密
  338. wx.redirectTo({
  339. url: "/pages/phoneinput/phoneinput?path=spell&couponChannelId=" +
  340. that.data.couponChannelId + '&couponId=' + that.data.couponId
  341. });
  342. } else {
  343. wx.showToast({
  344. title: err.message,
  345. icon: 'none',
  346. duration: 2000,
  347. mask: false
  348. });
  349. }
  350. })
  351. .then(res => {
  352. console.log(res)
  353. if (typeof (res) != "undefined") {
  354. let orderId = "" + res.data.id;
  355. that.setData({
  356. orderId: orderId
  357. });
  358. // 支付金额不为0
  359. /**
  360. * 支付订单创建
  361. */
  362. Http.post({
  363. url: config.api.payOrderCreate,
  364. data: {
  365. orderId: orderId
  366. }
  367. })
  368. .then(res => {
  369. var payOrderId = "" + res.data.payOrderId;
  370. wx.hideLoading();
  371. wx.requestPayment({
  372. timeStamp: res.data.timeStamp,
  373. nonceStr: res.data.nonceStr,
  374. package: res.data.package,
  375. signType: (res.data.signType) ? res.data.signType : "MD5",
  376. paySign: res.data.paySign,
  377. success: res => {
  378. wx.showLoading({
  379. title: '订单正在处理中...',
  380. })
  381. setTimeout(function () {
  382. wx.hideLoading()
  383. }, 5000)
  384. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  385. if (res.errMsg == "requestPayment:ok") {
  386. setTimeout(function () {
  387. wx.hideLoading();
  388. }, 2000);
  389. /**
  390. * 用户支付成功以后跳转到券包列表
  391. */
  392. if (that.data.cardType == 100) {
  393. wx.setStorage({
  394. key: 'couponNum2',
  395. data: "couponNum2"
  396. })
  397. } else if (that.data.data.type != 5) {
  398. wx.setStorage({
  399. key: 'couponNum',
  400. data: "couponNum"
  401. })
  402. }
  403. }
  404. },
  405. fail: res => {
  406. /**
  407. * 支付失败,需要更新订单的状态
  408. */
  409. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  410. that.setData({
  411. showbutton: false
  412. })
  413. return;
  414. },
  415. complete: res => { }
  416. });
  417. /// End payment --------
  418. })
  419. .catch(err => {
  420. wx.showToast({
  421. title: err.message,
  422. icon: 'none',
  423. duration: 2000,
  424. mask: false
  425. });
  426. })
  427. }
  428. })
  429. },
  430. /**
  431. * 生命周期函数--监听页面初次渲染完成
  432. */
  433. onReady: function () {
  434. },
  435. /**
  436. * 生命周期函数--监听页面显示
  437. */
  438. onShow: function () {
  439. this.setData({
  440. canSpell:true,
  441. canBuyIf:true
  442. })
  443. },
  444. /**
  445. * 生命周期函数--监听页面隐藏
  446. */
  447. onHide: function () {
  448. },
  449. /**
  450. * 生命周期函数--监听页面卸载
  451. */
  452. onUnload: function () {
  453. },
  454. /**
  455. * 页面相关事件处理函数--监听用户下拉动作
  456. */
  457. onPullDownRefresh: function () {
  458. },
  459. /**
  460. * 页面上拉触底事件的处理函数
  461. */
  462. onReachBottom: function () {
  463. }
  464. })