123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- require('./index.less');
- import 'jquery.cookie'
- import './helper/response';
- import Api from './helper/const';
- import Utils from './helper/utils';
- const channel = Utils.GetQueryString('channel');
- const uid = Utils.GetQueryString('uid');
- let payWay = 'wx';
- let productId = '';
- const getStatus = (orderId) => {
- // 查询是否成功
- $.ajax({
- url: Api.composeApiPath(`order/${orderId}`),
- dataType: "json",
- async: true,
- headers: {
- uid: uid
- },
- type: "GET",
- success: function (res) {
- $.removeCookie('orderId');
- $.removeCookie('isPayBack');
- if ($.cookie('isWx')) {
- $.removeCookie('isWx');
- $('.buy-dialog').css('display', 'none')
- }
- if (res.data.status === 2) {
- console.log('支付成功')
- system.postMessage('success')
- } else {
- console.log('支付失败')
- system.postMessage('close')
- }
- }
- });
- }
- // 默认微信支付
- $('.wx-choose').addClass('choose');
- // 渲染页面
- // 修改支付方式
- const changePayWay = (flag) => {
- // if (typeof system !== 'undefined') {
- // system.postMessage(window.location.search)
- // }
- if (flag === 'wx' && payWay !== 'wx') {
- payWay = 'wx';
- $('.zfb-choose').removeClass('choose');
- $('.wx-choose').addClass('choose');
- }
- if (flag === 'zfb' && payWay !== 'zfb') {
- payWay = 'zfb';
- $('.wx-choose').removeClass('choose');
- $('.zfb-choose').addClass('choose');
- }
- }
- // 生成订单
- const createOrder = (method, id) => {
- let str = method === 'zfb' ? '/aliPay/prePay' : '/wxPay/prePay';
- $.ajax({
- url: Api.composeApiPath(str),
- dataType: "json",
- async: true,
- data: JSON.stringify({
- "channel": channel,
- "productId": id
- }),
- type: "POST",
- headers: {
- uid: uid
- },
- contentType: "application/json",
- success: res => {
- $.cookie('orderId', res.data.orderId);
- $.cookie('isPayBack', 1);
- if (method === 'zfb') {
- $('body').append(
- $(`<div>${res.data.payInfo}</div>`)
- )
- } else if (method === 'wx') {
- $.cookie('isWx', 1);
- window.location.href = res.data.payInfo
- }
- }
- });
- }
- const chooseProduct = (e) => {
- e.stopPropagation();
- $('.item').removeClass('focus');
- $(e.currentTarget).addClass('focus');
- productId = e.currentTarget.dataset.id.toString();
- $('.pay-price').html(`¥${e.currentTarget.dataset.price}元`)
- }
- const getSign = () => {
- console.log(123)
- }
- // 绑定事件
- $(document).ready(() => {
- if (Utils.GetQueryString('payBack') === '1') {
- getStatus($.cookie('orderId'))
- }
- // 获取验证码
- $('#sign_btn').on('click', () => {
- getSign();
- });
- $('.zfb-pay').on('click', () => {
- changePayWay('zfb');
- });
- $('.pay-btn').on('click', () => {
- if (payWay && productId) {
- createOrder(payWay, productId)
- }
- });
- $('.yes').on('click', () => {
- getStatus($.cookie('orderId'))
- });
- $('.no').on('click', () => {
- getStatus($.cookie('orderId'))
- });
- });
|