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.

410 line
12 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. carList: [],
  9. couponChannelId: null,
  10. couponId: null,
  11. orderId: "",
  12. day: "",
  13. hour: "",
  14. minute: "",
  15. tempFilePaths: '',
  16. userInfo: {},
  17. hasUserInfo: false,
  18. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  19. id: '',
  20. result: [],
  21. end_time: '',
  22. clock: ''
  23. },
  24. phone: function () {
  25. let that = this;
  26. console.log(that.data);
  27. if (that.data.data.merchantLinkPhone) {
  28. wx.makePhoneCall({
  29. phoneNumber: that.data.data.merchantLinkPhone
  30. });
  31. }
  32. },
  33. countdown(end_time) {
  34. let that = this;
  35. var EndTime = end_time;
  36. var NowTime = new Date().getTime();
  37. var total_micro_second = EndTime - NowTime || [];
  38. // 渲染倒计时时钟
  39. let obj = that.dateformat(total_micro_second);
  40. that.setData({
  41. clock: obj,
  42. day: obj.a1,
  43. hour: obj.b1,
  44. min: obj.c1,
  45. sec: obj.d1,
  46. });
  47. if (total_micro_second <= 0) {
  48. that.setData({
  49. clock: "已经截止"
  50. });
  51. //return;
  52. }
  53. setTimeout(function () {
  54. total_micro_second -= 1000;
  55. that.countdown(end_time);
  56. }, 1000)
  57. },
  58. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  59. dateformat(micro_second) {
  60. // 总秒数
  61. var second = Math.floor(micro_second / 1000);
  62. // 天数
  63. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  64. // 小时
  65. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  66. // 分钟
  67. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  68. // 秒
  69. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60): Math.floor(second % 60);
  70. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  71. return { a1: day, b1: hr, c1: min, d1: sec}
  72. },
  73. onLoad(options) {
  74. let that = this;
  75. wx.showLoading({
  76. title: "加载中..."
  77. });
  78. /**
  79. * 暂时注销
  80. * this.orderFunc()
  81. */
  82. that.setData({
  83. couponChannelId: options.couponChannelId,
  84. couponId: options.couponId
  85. });
  86. var parmer = {
  87. url: config.api.couponDetail,
  88. data: {
  89. couponChannelId: options.couponChannelId
  90. }
  91. };
  92. Http.get(parmer).then(res => {
  93. console.log(res.data);
  94. if (res.data.endTime){
  95. that.countdown(res.data.endTime);
  96. //当前时间与优惠券下架时间做计算
  97. var endTime = util.formatTime(res.data.endTime, "yyyy-MM-dddd hh:mm:ss");
  98. if (util.timechuo(endTime).indexOf('-') == 0) {
  99. that.setData({
  100. endtime: "活动已结束",
  101. });
  102. } else {
  103. that.setData({
  104. endtime: util.timechuo(endTime)
  105. });
  106. }
  107. }
  108. wx.hideLoading();
  109. that.setData({
  110. data: res.data
  111. });
  112. if (res.data.validType == 1) {
  113. that.setData({
  114. validStartDate: util.formatTime(res.data.validStartDate, "yyyy-MM-dddd"),
  115. validEndDate: util.formatTime(res.data.validEndDate, "yyyy-MM-dddd"),
  116. });
  117. } else {
  118. that.setData({
  119. validDays: res.data.validDays
  120. });
  121. }
  122. });
  123. },
  124. /**
  125. * 支付订单更新
  126. */
  127. payOrderUpdate: (orderId, payOrderId, status, reason) => {
  128. // 支付成功
  129. Http.post({
  130. url: config.api.payOrderUpdate,
  131. data: {
  132. payOrderId: payOrderId,
  133. orderId: orderId,
  134. status: status,
  135. reason: reason
  136. }
  137. })
  138. .then(res => {
  139. console.log("payOrderUpdate then", res);
  140. })
  141. .catch(err => {
  142. console.log("payOrderUpdate catch", err);
  143. });
  144. },
  145. /**
  146. * 发起支付
  147. */
  148. orderFunc() {
  149. var that = this;
  150. wx.showLoading({
  151. title: "加载中..."
  152. });
  153. if (that.data.data.type == 6) { } else {
  154. Http.post({
  155. url: config.api.checkPhoneStatus,
  156. data: {}
  157. })
  158. .then(res => {
  159. console.log(res);
  160. var data = {
  161. couponChannelId: "" + that.data.couponChannelId,
  162. couponId: "" + that.data.couponId
  163. };
  164. if (that.data.couponChannelId == null) {
  165. var data = {
  166. couponId: "" + that.data.couponId
  167. };
  168. }
  169. /**
  170. * orderSave 下单
  171. */
  172. return Http.post({
  173. url: config.api.orderSave,
  174. data: data
  175. });
  176. })
  177. .catch(err => {
  178. console.log(err);
  179. if (err.code == "2011") {
  180. wx.showToast({
  181. title: "商户信息没找到",
  182. image: "./../../../assets/img/fail.png",
  183. duration: 2000,
  184. mask: false
  185. });
  186. } else if (err.code == "2013") {
  187. wx.showToast({
  188. title: "商户信息禁用",
  189. image: "./../../../assets/img/fail.png",
  190. duration: 2000,
  191. mask: false
  192. });
  193. } else if (err.code == "3000") {
  194. wx.showToast({
  195. title: "库存不足",
  196. image: "./../../../assets/img/fail.png",
  197. duration: 2000,
  198. mask: false
  199. });
  200. } else if (err.code == "3001") {
  201. wx.showToast({
  202. title: "领取达到上限",
  203. image: "./../../../assets/img/fail.png",
  204. duration: 2000,
  205. mask: false
  206. });
  207. } else if (err.code == "3002") {
  208. wx.showToast({
  209. title: "订单失败",
  210. image: "./../../../assets/img/fail.png",
  211. duration: 2000,
  212. mask: false
  213. });
  214. } else if (err.code == "3003") {
  215. wx.showToast({
  216. title: "订单不存在",
  217. image: "./../../../assets/img/fail.png",
  218. duration: 2000,
  219. mask: false
  220. });
  221. } else if (err.code == "3004") {
  222. wx.showToast({
  223. title: "订单不存在",
  224. image: "./../../../assets/img/fail.png",
  225. duration: 2000,
  226. mask: false
  227. });
  228. } else if (err.code == "4003") {
  229. wx.showToast({
  230. title: "卡券已作废",
  231. image: "./../../../assets/img/fail.png",
  232. duration: 2000,
  233. mask: false
  234. });
  235. } else if (err.code == 11005) {
  236. // 用户手机未授权
  237. /**
  238. * 将值传到用户手机号授权的页面
  239. *
  240. */
  241. wx.redirectTo({
  242. url: "/pages/getphoneInfo/index?couponChannelId=" +
  243. that.data.couponChannelId +
  244. "&couponId=" +
  245. that.data.couponId
  246. });
  247. } else if (err.code == 11006) {
  248. // 用户手机已加密
  249. wx.redirectTo({
  250. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  251. that.data.couponChannelId +
  252. "&couponId=" +
  253. that.data.couponId
  254. });
  255. } else {
  256. wx.showToast({
  257. title: err.message,
  258. image: "./../../../assets/img/fail.png",
  259. duration: 2000,
  260. mask: false
  261. });
  262. }
  263. })
  264. .then(res => {
  265. console.log(res);
  266. console.log("点击领取卡券一直在loading");
  267. if (typeof (res) != "undefined") {
  268. console.log("", res);
  269. let orderId = "" + res.data.id;
  270. that.setData({
  271. orderId: orderId
  272. });
  273. if (res.data.payment > 0) {
  274. // 支付金额不为0
  275. /**
  276. * 支付订单创建
  277. */
  278. Http.post({
  279. url: config.api.payOrderCreate,
  280. data: {
  281. orderId: orderId
  282. }
  283. }).then(res => {
  284. console.log(res);
  285. /// Begin payment ----
  286. var payOrderId = "" + res.data.payOrderId;
  287. wx.hideLoading();
  288. wx.requestPayment({
  289. timeStamp: res.data.timeStamp,
  290. nonceStr: res.data.nonceStr,
  291. package: res.data.package,
  292. signType: (res.data.signType) ? res.data.signType : "MD5",
  293. paySign: res.data.paySign,
  294. success: res => {
  295. that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  296. console.log(res);
  297. if (res.errMsg == "requestPayment:ok") {
  298. setTimeout(function () {
  299. wx.hideLoading();
  300. }, 2000);
  301. setTimeout(() => {
  302. wx.navigateTo({
  303. url: `/pages/order/detail/index?orderId=${
  304. that.data.orderId
  305. }`
  306. });
  307. }, 1000)
  308. /**
  309. * 用户支付成功以后跳转到券包列表
  310. */
  311. wx.setStorage({
  312. key: 'couponNum',
  313. data: "couponNum",
  314. })
  315. if (that.data.data.type == 5) {
  316. setTimeout(() => {
  317. wx.switchTab({
  318. url: '/pages/passCar/passCar'
  319. });
  320. }, 1600);
  321. // this.initUsrCarList()
  322. // 领取 5.停车券
  323. // TODO 选取用户下的车牌
  324. }
  325. }
  326. },
  327. fail: res => {
  328. that.payOrderUpdate(that.data.orderId, payOrderId, 2); // 支付失败
  329. console.log(res);
  330. return;
  331. },
  332. complete: res => {
  333. }
  334. });
  335. /// End payment --------
  336. ///// virtual pay
  337. //var payOrderId = "" + res.data.out_trade_no;
  338. //that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  339. ///// end virtual pay
  340. });
  341. } else {
  342. // 免费券
  343. that.payOrderUpdate(orderId, "0", 1);
  344. wx.setStorage({
  345. key: 'couponNum',
  346. data: "couponNum"
  347. })
  348. setTimeout(function () {
  349. wx.navigateTo({
  350. url: `/pages/order/detail/index?orderId=${
  351. that.data.orderId
  352. }`
  353. });
  354. }, 1000)
  355. //////////////////////////停车券
  356. console.log(that.data.data.type)
  357. if (that.data.data.type == 5) {
  358. setTimeout(() => {
  359. wx.switchTab({
  360. url: '/pages/passCar/passCar'
  361. });
  362. }, 1600);
  363. }
  364. }
  365. }
  366. })
  367. .catch(err => {
  368. console.log("ERR", err);
  369. });
  370. }
  371. },
  372. onShareAppMessage: function (options) {
  373. var that = this;
  374. var shareObj = {
  375. title: options.target.dataset.title,
  376. path: '/pages/index/index',
  377. success: function (res) {
  378. if (res.errMsg == 'shareAppMessage:ok') {
  379. console.log(res)
  380. }
  381. },
  382. fail: function (error) {
  383. if (res.errMsg == 'shareAppMessage:fail cancel') {
  384. console.log(error)
  385. } else if (res.errMsg == 'shareAppMessage:fail') {
  386. console.log(error)
  387. }
  388. }
  389. };
  390. // 来自页面内的按钮的转发
  391. if (options.from == 'button') {
  392. console.log(options)
  393. var eData = options.target.dataset.id;
  394. var couponId = options.target.dataset.couponid;
  395. shareObj.path = `/pages/coupon/detail/index?couponChannelId=${eData}&couponId=${couponId}`;
  396. }
  397. // 返回shareObj
  398. return shareObj;
  399. }
  400. });