C端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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