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.

416 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. })
  141. .catch(err => {
  142. console.log(err);
  143. })
  144. // return;
  145. // wx.navigateTo({
  146. // url: `/pages/spellDetail/index?orderId=${orderId}&couponId=${_this.data.data.couponId}`
  147. // });
  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. setTimeout(function () {
  171. _this.payOrderUpdate(orderId, payOrderId, status, reason, _this, orderGroupId);
  172. }, 2000)
  173. })
  174. },
  175. /**
  176. * 发起支付
  177. */
  178. orderFunc(orderGroupId) {
  179. let data = {
  180. couponChannelId: this.data.data.id,
  181. couponId: this.data.data.couponId
  182. }
  183. /**
  184. * 拼团订单
  185. */
  186. if (orderGroupId==0){
  187. data.orderGroupId = orderGroupId
  188. }
  189. var that = this;
  190. Http.post({
  191. url: config.api.checkPhoneStatus,
  192. data: {}
  193. })
  194. .then(res => {
  195. /**
  196. * orderSave 下单
  197. */
  198. return Http.post({
  199. url: config.api.orderSave,
  200. data: data
  201. });
  202. })
  203. .catch(err => {
  204. console.log(err);
  205. that.setData({
  206. showbutton: false,
  207. showbutton1: false
  208. })
  209. if (err.code == 2011) {
  210. wx.showToast({
  211. title: "商户信息没找到",
  212. image: './../../../assets/images/fail.png',
  213. duration: 2000,
  214. mask: false
  215. });
  216. } else if (err.code == 2013) {
  217. wx.showToast({
  218. title: "商户信息禁用",
  219. image: './../../../assets/images/fail.png',
  220. duration: 2000,
  221. mask: false
  222. });
  223. } else if (err.code == 3000) {
  224. wx.showToast({
  225. title: "库存不足",
  226. image: './../../../assets/images/fail.png',
  227. duration: 2000,
  228. mask: false
  229. });
  230. } else if (err.code == 3001) {
  231. wx.showToast({
  232. title: "超过限购条件",
  233. image: './../../../assets/images/fail.png',
  234. duration: 2000,
  235. mask: false
  236. });
  237. } else if (err.code == 3002) {
  238. wx.showToast({
  239. title: "订单失败",
  240. image: './../../../assets/images/fail.png',
  241. duration: 2000,
  242. mask: false
  243. });
  244. } else if (err.code == 3003) {
  245. wx.showToast({
  246. title: "订单不存在",
  247. image: './../../../assets/images/fail.png',
  248. duration: 2000,
  249. mask: false
  250. });
  251. } else if (err.code == 3004) {
  252. wx.showToast({
  253. title: "订单不存在",
  254. image: './../../../assets/images/fail.png',
  255. duration: 2000,
  256. mask: false
  257. });
  258. } else if (err.code == 4003) {
  259. wx.showToast({
  260. title: "卡券已作废",
  261. image: './../../../assets/images/fail.png',
  262. duration: 2000,
  263. mask: false
  264. });
  265. } else if (err.code == 11005) {
  266. /**
  267. * 将值传到用户手机号授权的页面
  268. *
  269. */
  270. wx.redirectTo({
  271. url: "/pages/getphoneInfo/index?path=spell&couponChannelId=" +
  272. that.data.couponChannelId + '&couponId=' + that.data.couponId
  273. });
  274. } else if (err.code == 11006) {
  275. // 用户手机已加密
  276. wx.redirectTo({
  277. url: "/pages/phoneinput/phoneinput?path=spell&couponChannelId=" +
  278. that.data.couponChannelId + '&couponId=' + that.data.couponId
  279. });
  280. } else {
  281. wx.showToast({
  282. title: err.message,
  283. icon: 'none',
  284. duration: 2000,
  285. mask: false
  286. });
  287. }
  288. })
  289. .then(res => {
  290. console.log(res)
  291. if (typeof (res) != "undefined") {
  292. let orderId = "" + res.data.id;
  293. that.setData({
  294. orderId: orderId
  295. });
  296. // 支付金额不为0
  297. /**
  298. * 支付订单创建
  299. */
  300. Http.post({
  301. url: config.api.payOrderCreate,
  302. data: {
  303. orderId: orderId
  304. }
  305. })
  306. .then(res => {
  307. var payOrderId = "" + res.data.payOrderId;
  308. wx.hideLoading();
  309. wx.requestPayment({
  310. timeStamp: res.data.timeStamp,
  311. nonceStr: res.data.nonceStr,
  312. package: res.data.package,
  313. signType: (res.data.signType) ? res.data.signType : "MD5",
  314. paySign: res.data.paySign,
  315. success: res => {
  316. wx.showLoading({
  317. title: '订单正在处理中...',
  318. })
  319. setTimeout(function () {
  320. wx.hideLoading()
  321. }, 5000)
  322. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  323. if (res.errMsg == "requestPayment:ok") {
  324. setTimeout(function () {
  325. wx.hideLoading();
  326. }, 2000);
  327. /**
  328. * 用户支付成功以后跳转到券包列表
  329. */
  330. if (that.data.cardType == 100) {
  331. wx.setStorage({
  332. key: 'couponNum2',
  333. data: "couponNum2"
  334. })
  335. } else if (that.data.data.type != 5) {
  336. wx.setStorage({
  337. key: 'couponNum',
  338. data: "couponNum"
  339. })
  340. }
  341. }
  342. },
  343. fail: res => {
  344. /**
  345. * 支付失败,需要更新订单的状态
  346. */
  347. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  348. that.setData({
  349. showbutton: false
  350. })
  351. return;
  352. },
  353. complete: res => { }
  354. });
  355. /// End payment --------
  356. })
  357. .catch(err => {
  358. wx.showToast({
  359. title: err.message,
  360. icon: 'none',
  361. duration: 2000,
  362. mask: false
  363. });
  364. })
  365. }
  366. })
  367. },
  368. /**
  369. * 生命周期函数--监听页面初次渲染完成
  370. */
  371. onReady: function () {
  372. },
  373. /**
  374. * 生命周期函数--监听页面显示
  375. */
  376. onShow: function () {
  377. this.setData({
  378. canSpell:true,
  379. canBuyIf:true
  380. })
  381. },
  382. /**
  383. * 生命周期函数--监听页面隐藏
  384. */
  385. onHide: function () {
  386. },
  387. /**
  388. * 生命周期函数--监听页面卸载
  389. */
  390. onUnload: function () {
  391. },
  392. /**
  393. * 页面相关事件处理函数--监听用户下拉动作
  394. */
  395. onPullDownRefresh: function () {
  396. },
  397. /**
  398. * 页面上拉触底事件的处理函数
  399. */
  400. onReachBottom: function () {
  401. }
  402. })