C端小程序
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

293 Zeilen
8.5 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. if (options.flag) {
  38. this.orderFunc();
  39. } else {
  40. if (options.couponChannelId == "iambannercoupon") {
  41. that.setData({
  42. couponId: options.couponId
  43. });
  44. /**
  45. * 待解决
  46. */
  47. var parmer = {
  48. url: config.api.bannercoupondetai,
  49. data: {
  50. couponId: options.couponId
  51. }
  52. };
  53. } else {
  54. that.setData({
  55. couponChannelId: options.couponChannelId,
  56. couponId: options.couponId
  57. });
  58. var parmer = {
  59. url: config.api.couponDetail,
  60. data: {
  61. couponChannelId: options.couponChannelId
  62. }
  63. };
  64. }
  65. Http.get(parmer).then(res => {
  66. console.log(res);
  67. console.log("details");
  68. //当前时间与优惠券下架时间做计算
  69. var startTime = util.fmtDate(res.data.endTime);
  70. console.log(startTime);
  71. var s1 = new Date(startTime.replace(/-/g, "/"));
  72. var s2 = new Date();
  73. var runTime = parseInt((s1.getTime() - s2.getTime()) / 1000);
  74. var year = Math.floor(runTime / 86400 / 365);
  75. var runTime = runTime % (86400 * 365);
  76. var month = Math.floor(runTime / 86400 / 30);
  77. var runTime = runTime % (86400 * 30);
  78. var day = Math.floor(runTime / 86400);
  79. var runTime = runTime % 86400;
  80. var hour = Math.floor(runTime / 3600);
  81. var runTime = runTime % 3600;
  82. var minute = Math.floor(runTime / 60);
  83. var runTime = runTime % 60;
  84. var second = runTime;
  85. console.log(year, month, day, hour, minute, second);
  86. that.setData({
  87. year: year,
  88. month: month,
  89. day: day,
  90. hour: hour,
  91. minute: minute
  92. });
  93. console.log(that.data.day);
  94. wx.hideLoading();
  95. that.setData({
  96. data: res.data
  97. });
  98. });
  99. }
  100. },
  101. /**
  102. * 支付订单更新
  103. */
  104. payOrderUpdate: (orderId, payOrderId, status, reason) => {
  105. // 支付成功
  106. Http.post({
  107. url: config.api.payOrderUpdate,
  108. data: {
  109. payOrderId: payOrderId,
  110. orderId: orderId,
  111. status: status,
  112. reason: reason
  113. }
  114. })
  115. .then(res => {
  116. console.log("payOrderUpdate then", res);
  117. // wx.showToast({
  118. // title: "购买成功",
  119. // duration: 2500
  120. // });
  121. })
  122. .catch(err => {
  123. console.log("payOrderUpdate catch", err);
  124. });
  125. },
  126. /**
  127. * 发起支付
  128. */
  129. orderFunc() {
  130. var that = this;
  131. wx.showLoading({
  132. title: "加载中..."
  133. });
  134. if (that.data.type == 5) {
  135. // 领取 5.停车券
  136. // TODO 选取用户下的车牌
  137. } else {
  138. Http.post({
  139. url: config.api.checkPhoneStatus,
  140. data: {}
  141. })
  142. .then(res => {
  143. console.log(res);
  144. var data = {
  145. couponChannelId: "" + that.data.couponChannelId,
  146. couponId: "" + that.data.couponId
  147. }
  148. if (that.data.couponChannelId == null) {
  149. data = {
  150. couponId: "" + that.data.couponId
  151. }
  152. }
  153. /**
  154. * orderSave 下单
  155. */
  156. return Http.post({
  157. url: config.api.orderSave,
  158. data: data
  159. });
  160. })
  161. .catch(err => {
  162. console.log(err);
  163. if (err.code == 11005) {
  164. // 用户手机未授权
  165. wx.redirectTo({
  166. url:
  167. "../../getphoneInfo/index?couponChannelId=" +
  168. that.data.couponChannelId +
  169. "&couponId=" +
  170. that.data.couponId
  171. });
  172. }
  173. if (err.code == 11006) {
  174. // 用户手机已加密
  175. wx.redirectTo({
  176. url:
  177. "../../phoneinput/phoneinput?couponChannelId=" +
  178. that.data.couponChannelId +
  179. "& couponId=" +
  180. that.data.couponId
  181. });
  182. }
  183. })
  184. .then(res => {
  185. console.log(res);
  186. console.log("点击领取卡券一直在loading");
  187. if (res != "undefined") {
  188. const 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. wx.navigateBack({
  225. delta: 2
  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. wx.showModal({
  240. title: "提示",
  241. content: "支付成功"
  242. });
  243. // if (url) {
  244. // setTimeout(function () {
  245. // wx.redirectTo({
  246. // url: "/pages" + url
  247. // });
  248. // }, 2000);
  249. // } else {
  250. // setTimeout(() => {
  251. // wx.navigateBack();
  252. // }, 2000);
  253. // }
  254. } else {
  255. wx.showToast({
  256. title: "支付失败",
  257. image: "./../../../assets/img/fail.png",
  258. duration: 1500,
  259. mask: false
  260. });
  261. }
  262. return;
  263. }
  264. });
  265. /// End payment --------
  266. ///// virtual pay
  267. //var payOrderId = "" + res.data.out_trade_no;
  268. //that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  269. ///// end virtual pay
  270. });
  271. } else {
  272. // 免费券
  273. that.payOrderUpdate(orderId, "0", 1);
  274. wx.showToast({
  275. title: "领取成功",
  276. duration: 3000
  277. });
  278. }
  279. }
  280. })
  281. .catch(err => {
  282. console.log(err);
  283. });
  284. }
  285. }
  286. });