Browse Source

'小学堂双十一活动'

Rorschach 3 years ago
parent
commit
6fcc976ca6
14 changed files with 354 additions and 159 deletions
  1. 1 1
      README.md
  2. 2 2
      src/about.html
  3. 18 7
      src/about.js
  4. 57 0
      src/about.less
  5. 1 1
      src/helper/const.js
  6. BIN
      src/img/bad.png
  7. BIN
      src/img/choose.png
  8. BIN
      src/img/close.png
  9. BIN
      src/img/grey.png
  10. BIN
      src/img/success.png
  11. 26 13
      src/index.html
  12. 177 82
      src/index.js
  13. 64 48
      src/index.less
  14. 8 5
      webpack.config.js

+ 1 - 1
README.md

@@ -10,7 +10,7 @@ npm install
 npm run build 
 ```
 
-### 本地调试
+### 本地调试 要修改webpack中host
 ```
 
 npm run server 

+ 2 - 2
src/about.html

@@ -3,8 +3,8 @@
 
 <head>
     <meta charset="UTF-8">
-    <meta name="viewport" content="width=device-width, initial-scale=1.0">
-    <title>Document</title>
+    <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
+    <title>义方小学堂</title>
 </head>
 
 <body>

+ 18 - 7
src/about.js

@@ -1,11 +1,20 @@
+require('./index.less');
 import Api from './helper/const';
 import Utils from './helper/utils';
+import './helper/response';
 
-const channel = Utils.GetQueryString('channel');
-const uid = Utils.GetQueryString('uid');
-
-// 
+// 时间
+let actEnd = false;
+let originPrice = 0;
+let price = 0;
+let name = ''
 
+$('#buy').on('click', () => {
+    if (actEnd) return;
+    window.location.href = `http://ott80-web.ai160.com/otttest/efunbox_xyyf/res/h5pay/index.html?op=${originPrice}&p=${price}&name=${name}`;
+    // console.log(321,name)
+    // window.location.href = `http://192.168.1.66:3000?op=${originPrice}&p=${price}&name=${name}`;
+});
 
 const timer = (endTime) => {
     setTimeout(() => {
@@ -23,17 +32,19 @@ const getProduct = (orderId) => {
         url: Api.composeApiPath(`product/101001`),
         dataType: "json",
         async: true,
-        headers: {
-            uid: uid
-        },
         type: "GET",
         success: function (res) {
             let endTime = res.data.endTime;
             let curTime = new Date().getTime();
             if (endTime - curTime < 0) {
                 document.getElementById('buy').className += ' none';
+                actEnd = true;
             } else {
                 timer(endTime);
+                originPrice = res.data.originalPrice;
+                price = res.data.price;
+                name = encodeURIComponent(res.data.title);
+                console.log(123, name)
             }
         }
     });

+ 57 - 0
src/about.less

@@ -0,0 +1,57 @@
+html,
+body {
+    margin: 0;
+    border: none;
+    padding: 0;
+    height: 100%;
+}
+
+.about_pic {
+    display: block;
+    width: 100%;
+}
+
+.buy_btn {
+    width: 6.7rem;
+    height: 1rem;
+    position: fixed;
+    left: 0;
+    right: 0;
+    margin: 0 auto;
+    bottom: .5rem;
+    border-radius: 1rem;
+    overflow: hidden;
+    display: flex;
+
+    .left {
+        width: 3.35rem;
+        height: 1rem;
+        background: #fcd758;
+        display: flex;
+        flex-direction: column;
+        justify-content: center;
+        align-items: center;
+        font-size: .3rem;
+        color: #995916;
+
+        .top {
+            margin-bottom: .1rem;
+        }
+    }
+
+
+
+    .right {
+        width: 3.35rem;
+        height: 1rem;
+        background: #ff6a67;
+        line-height: 1rem;
+        color: #fff;
+        font-size: .4rem;
+        text-align: center;
+    }
+
+    .none {
+        background: #929292;
+    }
+}

+ 1 - 1
src/helper/const.js

@@ -1,6 +1,6 @@
 export default class url {
     static composeApiPath(apiName) {
-      const requestBaseUrl = 'http://asxx.efunbox.cn/activity/'
+      const requestBaseUrl = 'http://yfxxt-api.ai160.com/activity/'
       return `${requestBaseUrl}${apiName}`
     }
   }

BIN
src/img/bad.png


BIN
src/img/choose.png


BIN
src/img/close.png


BIN
src/img/grey.png


BIN
src/img/success.png


File diff suppressed because it is too large
+ 26 - 13
src/index.html


+ 177 - 82
src/index.js

@@ -4,124 +4,219 @@ 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) => {
-    // 查询是否成功
+let wxPay = false;
+let aliPay = false;
+
+// 发送验证码
+const getSignAjax = (mobile) => {
     $.ajax({
-        url: Api.composeApiPath(`order/${orderId}`),
+        url: Api.composeApiPath(`msg?mobileNo=${mobile}`),
         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')
-            }
+            console.log(res);
+            alert('验证码已发送!')
         }
     });
 }
-// 默认微信支付
-$('.wx-choose').addClass('choose');
 
+const regTest = (n) => {
+    const zhengze = /^[1]([3-9])[0-9]{9}$/;
+    if (zhengze.test(n)) {
+        return true;
+    } else {
+        return false
+    }
+}
+
+let count = 60;
+const renderTimer = () => {
+    let timer = setTimeout(() => {
+        count--;
+        $('#sign_btn').html(`${count}秒后重新发送`)
+        if (count <= 0) {
+            $('#sign_btn').html(`发送验证码`)
+            count = 60;
+            clearTimeout(timer)
+        } else {
+            renderTimer()
+        }
+    }, 1000)
+}
 
-// 渲染页面
+const getSign = () => {
+    if (count < 60 && count > 0) {
+        alert(`请在${count}秒后重新获取`)
+        return;
+    }
+    renderTimer()
+    console.log('获取验证码')
+    console.log(document.getElementById('mobile_number').value)
+    let mobileNumber = document.getElementById('mobile_number').value;
 
-// 修改支付方式
-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 (regTest(mobileNumber)) {
+        getSignAjax(mobileNumber)
+    } else {
+        alert('手机号不规则')
     }
-    if (flag === 'zfb' && payWay !== 'zfb') {
-        payWay = 'zfb';
-        $('.wx-choose').removeClass('choose');
-        $('.zfb-choose').addClass('choose');
+}
+
+const showDialog = (type, text1, text2) => {
+    if (type === 'bad') {
+        $('.success').css('display', 'none');
     }
+    $('.pay_type').html(text1);
+    $('.pay_type_s').html(text2);
+    $('.buy-dialog').css('display', 'block')
+
 }
-// 生成订单
-const createOrder = (method, id) => {
-    let str = method === 'zfb' ? '/aliPay/prePay' : '/wxPay/prePay';
+const onBridgeReady = (data) => {
+    WeixinJSBridge.invoke(
+        'getBrandWCPayRequest',
+        data,
+        function (res) {
+            if (res.err_msg == "get_brand_wcpay_request:ok") {
+                // 使用以上方式判断前端返回,微信团队郑重提示:
+                //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
+                showDialog('success', '支付成功', '您已完成支付');
+                setTimeout(() => {
+                    window.location.href = 'http://ott80-web.ai160.com/otttest/efunbox_xyyf/res/h5pay/about.html'
+                }, 1000)
+                return;
+
+            }
+            if (res.err_msg == "get_brand_wcpay_request:cancel" || res.err_msg == "get_brand_wcpay_request: fail") {
+                showDialog('bad', '支付失败', '请重新支付');
+                setTimeout(() => {
+                    window.location.href = 'http://ott80-web.ai160.com/otttest/efunbox_xyyf/res/h5pay/about.html'
+                }, 2000)
+                return;
+            }
+
+        });
+}
+
+const postBuyInfo = (mobile, sign, payType) => {
     $.ajax({
-        url: Api.composeApiPath(str),
+        url: Api.composeApiPath('order'),
         dataType: "json",
         async: true,
         data: JSON.stringify({
-            "channel": channel,
-            "productId": id
+            "mobileNo": mobile,
+            "authorizeCode": Utils.GetQueryString('code'),
+            "identifyCode": sign,
+            "productId": 101001,
+            "payType": payType
         }),
         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
+        success: function (res) {
+            console.log('创建订单', res)
+            const resData = res.data;
+            if (payType === 'ALIPAY') {
+                document.body.innerHTML = resData;
+                console.log(resData);
+                // $("form[0]").submit();
+                document.forms[0].submit();
+            } else if (payType === 'WXPAY') {
+                // 拉起支付
+                if (typeof WeixinJSBridge == "undefined") {
+                    if (document.addEventListener) {
+                        document.addEventListener('WeixinJSBridgeReady', onBridgeReady.bind(this, {
+                            "appId": resData.appId, //公众号名称,由商户传入     
+                            "timeStamp": resData.timeStamp, //时间戳,自1970年以来的秒数     
+                            "nonceStr": resData.nonceStr, //随机串     
+                            "package": resData.package,
+                            "signType": "MD5", //微信签名方式:     
+                            "paySign": resData.sign //微信签名 
+                        }), false);
+                    } else if (document.attachEvent) {
+                        document.attachEvent('WeixinJSBridgeReady', onBridgeReady.bind(this, {
+                            "appId": resData.appId, //公众号名称,由商户传入     
+                            "timeStamp": resData.timeStamp, //时间戳,自1970年以来的秒数     
+                            "nonceStr": resData.nonceStr, //随机串     
+                            "package": resData.package,
+                            "signType": "MD5", //微信签名方式:     
+                            "paySign": resData.sign //微信签名 
+                        }));
+                        document.attachEvent('onWeixinJSBridgeReady', onBridgeRead.bind(this, {
+                            "appId": resData.appId, //公众号名称,由商户传入     
+                            "timeStamp": resData.timeStamp, //时间戳,自1970年以来的秒数     
+                            "nonceStr": resData.nonceStr, //随机串     
+                            "package": resData.package,
+                            "signType": "MD5", //微信签名方式:     
+                            "paySign": resData.sign //微信签名 
+                        }));
+                    }
+                } else {
+                    onBridgeReady({
+                        "appId": resData.appId, //公众号名称,由商户传入     
+                        "timeStamp": resData.timeStamp, //时间戳,自1970年以来的秒数     
+                        "nonceStr": resData.nonceStr, //随机串     
+                        "package": resData.package,
+                        "signType": "MD5", //微信签名方式:     
+                        "paySign": resData.sign //微信签名 
+                    });
+                }
             }
+        },
+        fail: function (res) {
+            showDialog('bad', '支付失败', '')
         }
     });
-}
+};
 
-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 buyTouchHandler = () => {
+    let mobileNumber = document.getElementById('mobile_number').value;
+    let signNumber = document.getElementById('sign_number').value;
+    if (!regTest(mobileNumber)) {
+        alert('手机号不规则');
+        return;
+    }
+    const signTest = /[0-9]{4}$/;
+    if (!signTest.test(signNumber)) {
+        alert('验证码格式不正确');
+        return;
+    }
+    let payType = aliPay ? 'ALIPAY' : 'WXPAY'
+    postBuyInfo(mobileNumber, signNumber, payType);
 }
 
-const getSign = () => {
-    console.log(123)
-}
 // 绑定事件
 $(document).ready(() => {
-    if (Utils.GetQueryString('payBack') === '1') {
-        getStatus($.cookie('orderId'))
+    const ua = navigator.userAgent.toLowerCase();
+    aliPay = ua.indexOf('alipayclient') !== -1;
+    wxPay = ua.indexOf('micromessenger') !== -1;
+    if (wxPay && !aliPay) {
+        if (!Utils.GetQueryString('code')) {
+            const url = encodeURIComponent(window.location.href)
+            window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf05800eea975a68b&redirect_uri=${url}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
+            return;
+        }
     }
+
     // 获取验证码
     $('#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'))
+    // 立即支付
+    $('#buy_btn').on('click', () => {
+        buyTouchHandler();
     });
-    $('.no').on('click', () => {
-        getStatus($.cookie('orderId'))
+    $('.close').on('click', () => {
+        $('.buy-dialog').css('display', 'none')
     });
+    let price = parseInt(Utils.GetQueryString('p')) / 100;
+    let originPrice = parseInt(Utils.GetQueryString('op')) / 100;
+    if (price) {
+        $('#op').html(`¥${originPrice.toFixed(2)}`);
+        $('#minus').html(`¥${(originPrice - price).toFixed(2)}`);
+        $('#price').html(`¥${price.toFixed(2)}`);
+        console.log(decodeURIComponent(Utils.GetQueryString('name')))
+        // $('.productName').html(decodeURIComponent(Utils.GetQueryString('name')));
+    }
+
 });

+ 64 - 48
src/index.less

@@ -5,7 +5,6 @@ body {
     padding: 0;
     height: 100%;
 }
-
 .about_pic {
     display: block;
     width: 100%;
@@ -60,6 +59,7 @@ body {
     width: 100%;
     height: 100%;
     background: #eee;
+    padding-top: 1rem;
 
     .input_box {
         width: 6.8rem;
@@ -72,6 +72,7 @@ body {
         .alert {
             font-size: .2rem;
             color: #f65d5d;
+            margin-bottom: .1rem;
         }
 
         .mobile_input {
@@ -144,7 +145,7 @@ body {
             border-radius: .1rem;
             position: absolute;
             right: 0;
-            top: 1.3rem;
+            top: 1.4rem;
             color: #fff;
         }
     }
@@ -157,6 +158,7 @@ body {
         background: #fff;
         padding: .3rem;
         box-sizing: border-box;
+
         .head {
             font-size: .3rem;
             color: #1a1a1a;
@@ -183,21 +185,24 @@ body {
             }
         }
     }
-    .info_bottom{
+
+    .info_bottom {
         width: 6.8rem;
         height: 0.8rem;
         margin: 0 auto;
-        background: lightblue;
+        background: #f8f8f8;
         display: flex;
         flex-direction: row-reverse;
         align-items: center;
-        span{
+
+        span {
             font-size: .3rem;
             color: #fd4141;
             margin-right: .3rem;
         }
     }
-    #buy_btn{
+
+    #buy_btn {
         font-size: .3rem;
         color: #fff;
         background: #f65d5d;
@@ -207,8 +212,8 @@ body {
         height: 0.82rem;
         border-radius: .1rem;
         position: absolute;
-        bottom: 1rem;
-        right: 0.8rem;
+        bottom: .5rem;
+        right: .6rem;
     }
 
 }
@@ -216,7 +221,7 @@ body {
 .buy-dialog {
     width: 100%;
     height: 100%;
-    background: rgba(0, 0, 0, .8);
+    background: rgba(0, 0, 0, .1);
     display: none;
     justify-content: center;
     align-items: center;
@@ -224,53 +229,64 @@ body {
     top: 0;
 
     .box {
-        width: 5.5rem;
-        height: 3.54rem;
+        width: 6.6rem;
+        height: 6rem;
         background: rgba(255, 255, 255, 1);
         border-radius: .2rem;
+        position: absolute;
+        top: 3rem;
+        left: 0;
+        right: 0;
+        margin: 0 auto;
+        display: flex;
+        flex-direction: column;
+        align-items: center;
 
-        .title {
-            width: 100%;
-            height: 0.56rem;
-            color: #434343;
-            font-size: .4rem;
-            text-align: center;
-            margin-top: .63rem;
-        }
+        .pay_icon {
+            display: block;
+            margin-top: 1.2rem;
+            width: 1.9rem;
+            height: 1.9rem;
 
-        .bottom {
-            width: 100%;
-            display: flex;
-            align-items: center;
-            justify-content: space-around;
-            margin-top: .98rem;
-
-            .yes {
-                width: 2.51rem;
-                height: .84rem;
-                background: rgba(245, 136, 13, 1);
-                border-radius: .47rem;
-                display: flex;
-                align-items: center;
-                justify-content: center;
-                color: #fff;
-                font-size: .36rem;
+            &.success {
+                display: block;
             }
 
-            .no {
-                width: 2.51rem;
-                height: .84rem;
-                background: rgba(61, 173, 254, 1);
-                border-radius: .47rem;
-                display: flex;
-                align-items: center;
-                text-align: center;
-                color: #fff;
-                font-size: .36rem;
-                justify-content: center;
-
+            &.bad {
+                display: none;
             }
+        }
+
+
+        .pay_type {
+            margin-top: .8rem;
+            font-size: .36rem;
+            color: #fc6168;
+        }
+
+        .pay_type_s {
+            margin-top: .5rem;
+            color: #999;
+            font-size: .26rem;
+        }
 
+    }
+
+
+    .close {
+        width: 1.2rem;
+        height: 1.2rem;
+        position: absolute;
+        left: 0;
+        right: 0;
+        margin: 0 auto;
+        top: 10rem;
+        overflow: hidden;
+        z-index: 10;
+
+        img {
+            width: 100%;
+            height: 100%;
         }
     }
 }

+ 8 - 5
webpack.config.js

@@ -12,7 +12,8 @@ const htmlPlugin = new HtmlWebpackPlugin({
     minify: {
         removeAttributeQuotes: true,
         collapseWhitespace: true
-    }
+    },
+    chunks: ['index']
 });
 
 const aboutPlugin = new HtmlWebpackPlugin({
@@ -22,7 +23,8 @@ const aboutPlugin = new HtmlWebpackPlugin({
     minify: {
         removeAttributeQuotes: true,
         collapseWhitespace: true
-    }
+    },
+    chunks: ['about']
 });
 /* const htmlPlugin = ['index', 'about'].map(chunkName => {
    return new HtmlWebpackPlugin({
@@ -68,9 +70,10 @@ module.exports = {
         // progress: true,
         contentBase: path.resolve(__dirname, './build'),
         open: true,
-        host: '127.0.0.1'
+        host: process.env.HOST || '192.168.1.66',
+        port: 3000
     },
-    mode: 'production', // development production
+    mode: 'development', // development production
     entry: {
         index: './src/index.js',
         about: './src/about.js'
@@ -93,7 +96,7 @@ module.exports = {
                     loader: 'html-loader',
                     options: {
                         attrs: ['img:src', 'img:data-src', 'audio:src'],
-                        minimize: true
+                        // minimize: true
                     }
                 }
             },