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.

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