C端小程序
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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