C端小程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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