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

419 行
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. questionnaire: {
  23. title: "问题1" ,
  24. questions: [
  25. { ques: "小主,您有喜欢的小鲜肉吗?" },
  26. { ques: "小主,您有娃吗?" }]
  27. },
  28. },
  29. phone: function () {
  30. let that = this;
  31. console.log(that.data);
  32. if (that.data.data.merchantLinkPhone) {
  33. wx.makePhoneCall({
  34. phoneNumber: that.data.data.merchantLinkPhone
  35. });
  36. }
  37. },
  38. countdown(end_time) {
  39. let that = this;
  40. var EndTime = end_time;
  41. var NowTime = new Date().getTime();
  42. var total_micro_second = EndTime - NowTime || [];
  43. // 渲染倒计时时钟
  44. let obj = that.dateformat(total_micro_second);
  45. if (total_micro_second>0){
  46. that.setData({
  47. clock: obj,
  48. day: obj.a1,
  49. hour: obj.b1,
  50. min: obj.c1,
  51. sec: obj.d1,
  52. })
  53. }else{
  54. that.setData({
  55. clock: "00",
  56. day: "00",
  57. hour: "00",
  58. min: "00",
  59. sec: "00",
  60. })
  61. }
  62. setTimeout(function () {
  63. total_micro_second -= 1000;
  64. that.countdown(end_time);
  65. }, 1000)
  66. },
  67. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  68. dateformat(micro_second) {
  69. // 总秒数
  70. var second = Math.floor(micro_second / 1000);
  71. // 天数
  72. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  73. // 小时
  74. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  75. // 分钟
  76. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  77. // 秒
  78. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60): Math.floor(second % 60);
  79. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  80. return { a1: day, b1: hr, c1: min, d1: sec}
  81. },
  82. onLoad(options) {
  83. let that = this;
  84. wx.showLoading({
  85. title: "加载中..."
  86. });
  87. /**
  88. * 暂时注销
  89. * this.orderFunc()
  90. */
  91. that.setData({
  92. couponChannelId: options.couponChannelId,
  93. couponId: options.couponId
  94. });
  95. var parmer = {
  96. url: config.api.couponDetail,
  97. data: {
  98. couponChannelId: options.couponChannelId
  99. }
  100. };
  101. Http.get(parmer).then(res => {
  102. console.log(res.data);
  103. if (res.data.endTime){
  104. that.countdown(res.data.endTime);
  105. //当前时间与优惠券下架时间做计算
  106. var endTime = util.formatTime(res.data.endTime, "yyyy-MM-dd hh:mm:ss");
  107. if (util.timechuo(endTime).indexOf('-') == 0) {
  108. that.setData({
  109. endtime: "活动已结束",
  110. });
  111. } else {
  112. that.setData({
  113. endtime: util.timechuo(endTime)
  114. });
  115. }
  116. }
  117. wx.hideLoading();
  118. that.setData({
  119. data: res.data
  120. });
  121. if (res.data.validType == 1) {
  122. that.setData({
  123. validStartDate: util.formatTime(res.data.validStartDate, "yyyy-MM-dddd"),
  124. validEndDate: util.formatTime(res.data.validEndDate, "yyyy-MM-dddd"),
  125. });
  126. } else {
  127. that.setData({
  128. validDays: res.data.validDays
  129. });
  130. }
  131. });
  132. },
  133. /**
  134. * 支付订单更新
  135. */
  136. payOrderUpdate: (orderId, payOrderId, status, reason) => {
  137. // 支付成功
  138. Http.post({
  139. url: config.api.payOrderUpdate,
  140. data: {
  141. payOrderId: payOrderId,
  142. orderId: orderId,
  143. status: status,
  144. reason: reason
  145. }
  146. })
  147. .then(res => {
  148. console.log("payOrderUpdate then", res);
  149. })
  150. .catch(err => {
  151. console.log("payOrderUpdate catch", err);
  152. });
  153. },
  154. /**
  155. * 发起支付
  156. */
  157. orderFunc() {
  158. var that = this;
  159. wx.showLoading({
  160. title: "加载中..."
  161. });
  162. if (that.data.data.type == 6) { } else {
  163. Http.post({
  164. url: config.api.checkPhoneStatus,
  165. data: {}
  166. })
  167. .then(res => {
  168. console.log(res);
  169. var data = {
  170. couponChannelId: "" + that.data.couponChannelId,
  171. couponId: "" + that.data.couponId
  172. };
  173. if (that.data.couponChannelId == null) {
  174. var data = {
  175. couponId: "" + that.data.couponId
  176. };
  177. }
  178. /**
  179. * orderSave 下单
  180. */
  181. return Http.post({
  182. url: config.api.orderSave,
  183. data: data
  184. });
  185. })
  186. .catch(err => {
  187. console.log(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. console.log(res);
  275. console.log("点击领取卡券一直在loading");
  276. if (typeof (res) != "undefined") {
  277. console.log("", res);
  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. console.log(res);
  294. /// Begin payment ----
  295. var payOrderId = "" + res.data.payOrderId;
  296. wx.hideLoading();
  297. wx.requestPayment({
  298. timeStamp: res.data.timeStamp,
  299. nonceStr: res.data.nonceStr,
  300. package: res.data.package,
  301. signType: (res.data.signType) ? res.data.signType : "MD5",
  302. paySign: res.data.paySign,
  303. success: res => {
  304. that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  305. console.log(res);
  306. if (res.errMsg == "requestPayment:ok") {
  307. setTimeout(function () {
  308. wx.hideLoading();
  309. }, 2000);
  310. setTimeout(() => {
  311. wx.navigateTo({
  312. url: `/pages/order/detail/index?orderId=${
  313. that.data.orderId
  314. }`
  315. });
  316. }, 1000)
  317. /**
  318. * 用户支付成功以后跳转到券包列表
  319. */
  320. wx.setStorage({
  321. key: 'couponNum',
  322. data: "couponNum",
  323. })
  324. if (that.data.data.type == 5) {
  325. setTimeout(() => {
  326. wx.switchTab({
  327. url: '/pages/passCar/passCar'
  328. });
  329. }, 1600);
  330. // this.initUsrCarList()
  331. // 领取 5.停车券
  332. // TODO 选取用户下的车牌
  333. }
  334. }
  335. },
  336. fail: res => {
  337. that.payOrderUpdate(that.data.orderId, payOrderId, 2); // 支付失败
  338. console.log(res);
  339. return;
  340. },
  341. complete: res => {
  342. }
  343. });
  344. /// End payment --------
  345. ///// virtual pay
  346. //var payOrderId = "" + res.data.out_trade_no;
  347. //that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  348. ///// end virtual pay
  349. });
  350. } else {
  351. // 免费券
  352. that.payOrderUpdate(orderId, "0", 1);
  353. wx.setStorage({
  354. key: 'couponNum',
  355. data: "couponNum"
  356. })
  357. setTimeout(function () {
  358. wx.navigateTo({
  359. url: `/pages/order/detail/index?orderId=${
  360. that.data.orderId
  361. }`
  362. });
  363. }, 1000)
  364. //////////////////////////停车券
  365. console.log(that.data.data.type)
  366. if (that.data.data.type == 5) {
  367. setTimeout(() => {
  368. wx.switchTab({
  369. url: '/pages/passCar/passCar'
  370. });
  371. }, 1600);
  372. }
  373. }
  374. }
  375. })
  376. .catch(err => {
  377. console.log("ERR", err);
  378. });
  379. }
  380. },
  381. onShareAppMessage: function (options) {
  382. var that = this;
  383. var shareObj = {
  384. title: options.target.dataset.title,
  385. path: '/pages/index/index',
  386. success: function (res) {
  387. if (res.errMsg == 'shareAppMessage:ok') {
  388. console.log(res)
  389. }
  390. },
  391. fail: function (error) {
  392. if (res.errMsg == 'shareAppMessage:fail cancel') {
  393. console.log(error)
  394. } else if (res.errMsg == 'shareAppMessage:fail') {
  395. console.log(error)
  396. }
  397. }
  398. };
  399. // 来自页面内的按钮的转发
  400. if (options.from == 'button') {
  401. console.log(options)
  402. var eData = options.target.dataset.id;
  403. var couponId = options.target.dataset.couponid;
  404. // shareObj.path = `/pages/index/index?couponChannelId=${eData}&couponId=${couponId}`;
  405. shareObj.path = `/pages/index/index?couponChannelId=${eData}&couponId=${couponId}`;
  406. }
  407. // 返回shareObj
  408. return shareObj;
  409. }
  410. });