C端小程序
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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