C端小程序
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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