C端小程序
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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