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.

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