C端小程序
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

393 satır
11 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,_this) => {
  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("成功");
  142. if(!type){
  143. setTimeout(() => {
  144. wx.navigateTo({
  145. url: `/pages/order/detail/index?orderId=${
  146. orderId
  147. }`
  148. });
  149. }, 1000)
  150. }
  151. })
  152. .catch(err => {
  153. console.log(_this);
  154. setTimeout(function(){
  155. _this.payOrderUpdate(orderId, payOrderId, status, reason, type, _this);
  156. },2000)
  157. });
  158. },
  159. /**
  160. * 发起支付
  161. */
  162. orderFunc() {
  163. var that = this;
  164. wx.showLoading({
  165. title: "加载中..."
  166. });
  167. if (that.data.data.type == 6) { } else {
  168. Http.post({
  169. url: config.api.checkPhoneStatus,
  170. data: {}
  171. })
  172. .then(res => {
  173. var data = {
  174. couponChannelId: "" + that.data.couponChannelId,
  175. couponId: "" + that.data.couponId
  176. };
  177. if (that.data.couponChannelId == null) {
  178. var data = {
  179. couponId: "" + that.data.couponId
  180. };
  181. }
  182. /**
  183. * orderSave 下单
  184. */
  185. return Http.post({
  186. url: config.api.orderSave,
  187. data: data
  188. });
  189. })
  190. .catch(err => {
  191. if (err.code == "2011") {
  192. wx.showToast({
  193. title: "商户信息没找到",
  194. image: "./../../../assets/img/fail.png",
  195. duration: 2000,
  196. mask: false
  197. });
  198. } else if (err.code == "2013") {
  199. wx.showToast({
  200. title: "商户信息禁用",
  201. image: "./../../../assets/img/fail.png",
  202. duration: 2000,
  203. mask: false
  204. });
  205. } else if (err.code == "3000") {
  206. wx.showToast({
  207. title: "库存不足",
  208. image: "./../../../assets/img/fail.png",
  209. duration: 2000,
  210. mask: false
  211. });
  212. } else if (err.code == "3001") {
  213. wx.showToast({
  214. title: "领取达到上限",
  215. image: "./../../../assets/img/fail.png",
  216. duration: 2000,
  217. mask: false
  218. });
  219. } else if (err.code == "3002") {
  220. wx.showToast({
  221. title: "订单失败",
  222. image: "./../../../assets/img/fail.png",
  223. duration: 2000,
  224. mask: false
  225. });
  226. } else if (err.code == "3003") {
  227. wx.showToast({
  228. title: "订单不存在",
  229. image: "./../../../assets/img/fail.png",
  230. duration: 2000,
  231. mask: false
  232. });
  233. } else if (err.code == "3004") {
  234. wx.showToast({
  235. title: "订单不存在",
  236. image: "./../../../assets/img/fail.png",
  237. duration: 2000,
  238. mask: false
  239. });
  240. } else if (err.code == "4003") {
  241. wx.showToast({
  242. title: "卡券已作废",
  243. image: "./../../../assets/img/fail.png",
  244. duration: 2000,
  245. mask: false
  246. });
  247. } else if (err.code == 11005) {
  248. // 用户手机未授权
  249. /**
  250. * 将值传到用户手机号授权的页面
  251. *
  252. */
  253. wx.redirectTo({
  254. url: "/pages/getphoneInfo/index?couponChannelId=" +
  255. that.data.couponChannelId +
  256. "&couponId=" +
  257. that.data.couponId
  258. });
  259. } else if (err.code == 11006) {
  260. // 用户手机已加密
  261. wx.redirectTo({
  262. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  263. that.data.couponChannelId +
  264. "&couponId=" +
  265. that.data.couponId
  266. });
  267. } else {
  268. wx.showToast({
  269. title: err.message,
  270. image: "./../../../assets/img/fail.png",
  271. duration: 2000,
  272. mask: false
  273. });
  274. }
  275. })
  276. .then(res => {
  277. if (typeof (res) != "undefined") {
  278. let orderId = "" + res.data.id;
  279. that.setData({
  280. orderId: orderId
  281. });
  282. if (res.data.payment > 0) {
  283. // 支付金额不为0
  284. /**
  285. * 支付订单创建
  286. */
  287. Http.post({
  288. url: config.api.payOrderCreate,
  289. data: {
  290. orderId: orderId
  291. }
  292. }).then(res => {
  293. /// Begin payment ----
  294. var payOrderId = "" + res.data.payOrderId;
  295. wx.hideLoading();
  296. wx.requestPayment({
  297. timeStamp: res.data.timeStamp,
  298. nonceStr: res.data.nonceStr,
  299. package: res.data.package,
  300. signType: (res.data.signType) ? res.data.signType : "MD5",
  301. paySign: res.data.paySign,
  302. success: res => {
  303. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  304. if (res.errMsg == "requestPayment:ok") {
  305. setTimeout(function () {
  306. wx.hideLoading();
  307. }, 2000);
  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. }
  322. }
  323. },
  324. fail: res => {
  325. that.payOrderUpdate(that.data.orderId, payOrderId, 2,'','fail'); // 支付失败
  326. return;
  327. },
  328. complete: res => {
  329. }
  330. });
  331. });
  332. } else {
  333. // 免费券
  334. that.payOrderUpdate(orderId, "0", 1, '', 'fail');
  335. wx.setStorage({
  336. key: 'couponNum',
  337. data: "couponNum"
  338. })
  339. setTimeout(function () {
  340. wx.navigateTo({
  341. url: `/pages/order/detail/index?orderId=${
  342. that.data.orderId
  343. }`
  344. });
  345. }, 1000)
  346. if (that.data.data.type == 5) {
  347. setTimeout(() => {
  348. wx.switchTab({
  349. url: '/pages/passCar/passCar'
  350. });
  351. }, 1600);
  352. }
  353. }
  354. }
  355. })
  356. .catch(err => {
  357. console.log("ERR", err);
  358. });
  359. }
  360. },
  361. onShareAppMessage: function (options) {
  362. var that = this;
  363. var shareObj = {
  364. title: options.target.dataset.title,
  365. path: '/pages/index/index',
  366. success: function (res) {
  367. if (res.errMsg == 'shareAppMessage:ok') {
  368. }
  369. },
  370. fail: function (error) {
  371. if (res.errMsg == 'shareAppMessage:fail cancel') {
  372. } else if (res.errMsg == 'shareAppMessage:fail') {
  373. }
  374. }
  375. };
  376. // 来自页面内的按钮的转发
  377. if (options.from == 'button') {
  378. var eData = options.target.dataset.id;
  379. var couponId = options.target.dataset.couponid;
  380. // shareObj.path = `/pages/index/index?couponChannelId=${eData}&couponId=${couponId}`;
  381. shareObj.path = `/pages/index/index?couponChannelId=${eData}&couponId=${couponId}`;
  382. }
  383. // 返回shareObj
  384. return shareObj;
  385. }
  386. });