C端小程序
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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