C端小程序
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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