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.

273 line
7.8 KiB

  1. var config = require("../../../config/config.js");
  2. var app = getApp();
  3. const Http = require("../../../utils/HttpBasics");
  4. const util = require("../../../utils/util");
  5. Page({
  6. data: {
  7. data: {},
  8. couponChannelId: null,
  9. couponId: null,
  10. orderId: "",
  11. day: "",
  12. hour: "",
  13. minute: ""
  14. },
  15. onLoad(options) {
  16. let that = this;
  17. console.log(options.couponChannelId);
  18. console.log(options.couponId);
  19. /**
  20. * 首页banner detail couponlist传过来的值
  21. * iambannercoupon
  22. */
  23. that.setData({
  24. couponChannelId: options.couponChannelId,
  25. couponId: options.couponId
  26. });
  27. wx.showLoading({
  28. title: "加载中..."
  29. });
  30. if (options.flag) {
  31. this.orderFunc();
  32. } else {
  33. if(options.couponChannelId=='iambannercoupon'){
  34. /**
  35. * 待解决
  36. */
  37. var details ={
  38. couponChannelId: options.couponId
  39. }
  40. }else{
  41. var details ={
  42. couponChannelId: options.couponChannelId
  43. }
  44. }
  45. Http.get({
  46. url: config.api.couponDetail,
  47. data: details
  48. }).then(res => {
  49. console.log(res);
  50. console.log("details")
  51. //当前时间与优惠券下架时间做计算
  52. var startTime = util.fmtDate(res.data.endTime);
  53. console.log(startTime);
  54. var s1 = new Date(startTime.replace(/-/g, "/"));
  55. var s2 = new Date();
  56. var runTime = parseInt((s1.getTime() - s2.getTime()) / 1000);
  57. var year = Math.floor(runTime / 86400 / 365);
  58. var runTime = runTime % (86400 * 365);
  59. var month = Math.floor(runTime / 86400 / 30);
  60. var runTime = runTime % (86400 * 30);
  61. var day = Math.floor(runTime / 86400);
  62. var runTime = runTime % 86400;
  63. var hour = Math.floor(runTime / 3600);
  64. var runTime = runTime % 3600;
  65. var minute = Math.floor(runTime / 60);
  66. var runTime = runTime % 60;
  67. var second = runTime;
  68. console.log(year, month, day, hour, minute, second);
  69. that.setData({
  70. year: year,
  71. month: month,
  72. day: day,
  73. hour: hour,
  74. minute: minute
  75. });
  76. console.log(that.data.day);
  77. wx.hideLoading();
  78. that.setData({
  79. data: res.data
  80. });
  81. });
  82. }
  83. },
  84. /**
  85. * 支付订单更新
  86. */
  87. payOrderUpdate: (orderId, payOrderId, status, reason) => {
  88. // 支付成功
  89. Http.post({
  90. url: config.api.payOrderUpdate,
  91. data: {
  92. payOrderId: payOrderId,
  93. orderId: orderId,
  94. status: status,
  95. reason: reason
  96. }
  97. })
  98. .then(res => {
  99. console.log("payOrderUpdate then", res);
  100. // wx.showToast({
  101. // title: "购买成功",
  102. // duration: 2500
  103. // });
  104. })
  105. .catch(err => {
  106. console.log("payOrderUpdate catch", err);
  107. });
  108. },
  109. /**
  110. * 发起支付
  111. */
  112. orderFunc() {
  113. var that = this;
  114. wx.showLoading({
  115. title: "加载中..."
  116. });
  117. if (that.data.type == 5) {
  118. // 领取 5.停车券
  119. // TODO 选取用户下的车牌
  120. } else {
  121. Http.post({
  122. url: config.api.checkPhoneStatus,
  123. data: {}
  124. })
  125. .then(res => {
  126. console.log(res);
  127. /**
  128. * orderSave 下单
  129. */
  130. return Http.post({
  131. url: config.api.orderSave,
  132. data: {
  133. couponChannelId: "" + that.data.couponChannelId,
  134. couponId: "" + that.data.couponId
  135. }
  136. });
  137. })
  138. .catch(err => {
  139. console.log(err);
  140. if (err.code == 11005) {
  141. // 用户手机未授权
  142. wx.redirectTo({
  143. url:
  144. "../../getphoneInfo/index?couponChannelId=" +
  145. that.data.couponChannelId +
  146. "&couponId=" +
  147. that.data.couponId
  148. });
  149. }
  150. if (err.code == 11006) {
  151. // 用户手机已加密
  152. wx.redirectTo({
  153. url:
  154. "../../phoneinput/phoneinput?couponChannelId=" +
  155. that.data.couponChannelId +
  156. "& couponId=" +
  157. that.data.couponId
  158. });
  159. }
  160. })
  161. .then(res => {
  162. console.log(res);
  163. console.log("点击领取卡券一直在loading");
  164. if (res != "undefined") {
  165. const orderId = "" + res.data.id;
  166. that.setData({
  167. orderId: orderId
  168. });
  169. if (res.data.payment > 0) {
  170. // 支付金额不为0
  171. /**
  172. * 支付订单创建
  173. */
  174. Http.post({
  175. url: config.api.payOrderCreate,
  176. data: {
  177. orderId: orderId
  178. }
  179. }).then(res => {
  180. console.log(res);
  181. /// Begin payment ----
  182. var payOrderId = "" + res.data.payOrderId;
  183. wx.hideLoading();
  184. wx.requestPayment({
  185. timeStamp: res.data.timeStamp,
  186. nonceStr: res.data.nonceStr,
  187. package: res.data.package,
  188. signType: "MD5",
  189. paySign: res.data.paySign,
  190. success: res => {
  191. console.log(res);
  192. console.log("姐在检查付款异常");
  193. that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  194. console.log(res);
  195. if (res.errMsg == "requestPayment:ok") {
  196. wx.showToast({
  197. title: "购买成功",
  198. duration: 2500
  199. });
  200. }
  201. wx.navigateBack({
  202. delta: 2
  203. });
  204. },
  205. fail: res => {
  206. that.payOrderUpdate(that.data.orderId, payOrderId, 2); // 支付失败
  207. console.log(res);
  208. return;
  209. },
  210. complete: res => {
  211. console.log(res);
  212. console.log("支付完成");
  213. // var url = res.data.url;
  214. // console.log("get url", url);
  215. if (res.errMsg == "requestPayment:ok") {
  216. wx.showModal({
  217. title: "提示",
  218. content: "支付成功"
  219. });
  220. // if (url) {
  221. // setTimeout(function () {
  222. // wx.redirectTo({
  223. // url: "/pages" + url
  224. // });
  225. // }, 2000);
  226. // } else {
  227. // setTimeout(() => {
  228. // wx.navigateBack();
  229. // }, 2000);
  230. // }
  231. } else {
  232. wx.showToast({
  233. title: "支付失败",
  234. image: "./../../../assets/img/fail.png",
  235. duration: 1500,
  236. mask: false
  237. });
  238. }
  239. return;
  240. }
  241. });
  242. /// End payment --------
  243. ///// virtual pay
  244. //var payOrderId = "" + res.data.out_trade_no;
  245. //that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  246. ///// end virtual pay
  247. });
  248. } else {
  249. // 免费券
  250. that.payOrderUpdate(orderId, "0", 1);
  251. wx.showToast({
  252. title: "领取成功",
  253. duration: 3000
  254. });
  255. }
  256. }
  257. })
  258. .catch(err => {
  259. console.log(err);
  260. });
  261. }
  262. }
  263. });