Authored by TaoHuang

feat(captcha): add captcha width send coupon

@@ -11,12 +11,15 @@ @@ -11,12 +11,15 @@
11 <div id="coupon1" class="yoho-conpon" data-token="wangdachui233" data-type="ufo"> 11 <div id="coupon1" class="yoho-conpon" data-token="wangdachui233" data-type="ufo">
12 <a href="javascript:;">点我领取UFO优惠券</a> 12 <a href="javascript:;">点我领取UFO优惠券</a>
13 </div> 13 </div>
  14 + <div class="yoho-tencentCaptcha" data-appid="2008945059" data-on="1">
  15 + <div>
14 <script src="https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js"></script> 16 <script src="https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js"></script>
15 <script src="//cdn.yoho.cn/js-sdk/1.3.15/jssdk.js"></script> 17 <script src="//cdn.yoho.cn/js-sdk/1.3.15/jssdk.js"></script>
  18 + <script src="../dist/js-sdk/1.3.26-beta1/jssdk.js"></script>
16 <script> 19 <script>
17 function onChange(e) { 20 function onChange(e) {
18 $('#coupon1').data('token', e.target.value) 21 $('#coupon1').data('token', e.target.value)
19 } 22 }
20 </script> 23 </script>
21 </body> 24 </body>
22 -</html>  
  25 +</html>
1 { 1 {
2 "name": "yoho-js-sdk", 2 "name": "yoho-js-sdk",
3 - "version": "1.3.24", 3 + "version": "1.3.26-beta1",
4 "description": "YOHO!前端js的功能封装包,主要用于在活动页面中,对主要功能的封装。", 4 "description": "YOHO!前端js的功能封装包,主要用于在活动页面中,对主要功能的封装。",
5 "keywords": [ 5 "keywords": [
6 "YOHO!", 6 "YOHO!",
@@ -23,7 +23,10 @@ @@ -23,7 +23,10 @@
23 </head> 23 </head>
24 <body> 24 <body>
25 <h1>测试 Token </h1> 25 <h1>测试 Token </h1>
26 - <a class="auth yoho-conpon" href="javascript:;" data-token="50f033ab37c3"> 26 + <div class="yoho-tencentCaptcha" data-appid="2008945059" data-open="1">
  27 + <div>
  28 +
  29 + <a class="yoho-conpon yoho-captcha" href="javascript:;" data-token="50f033ab37c3" data-type="1">
27 <h1>50f033ab37c3</h1> 30 <h1>50f033ab37c3</h1>
28 </a> 31 </a>
29 <hr> 32 <hr>
@@ -203,6 +206,6 @@ @@ -203,6 +206,6 @@
203 </div> 206 </div>
204 </div> 207 </div>
205 <script src="//cdn.bootcss.com/jquery/1.8.3/jquery.min.js"></script> 208 <script src="//cdn.bootcss.com/jquery/1.8.3/jquery.min.js"></script>
206 - <script src="../dist/js-sdk/1.3.13/jssdk.js"></script> 209 + <script src="../dist/js-sdk/1.3.26-beta1/jssdk.js"></script>
207 </body> 210 </body>
208 </html> 211 </html>
  1 +import $ from 'jquery';
  2 +
  3 +const url = 'https://ssl.captcha.qq.com/TCaptcha.js';
  4 +
  5 +function _initScript() {
  6 + return $.getScript(url);
  7 +}
  8 +
  9 +const store = {
  10 + appId: '',
  11 + open: 0
  12 +};
  13 +
  14 +export default {
  15 + init() {
  16 + let $tencentCaptcha = $('.yoho-tencentCaptcha');
  17 + store.appId = $tencentCaptcha.data('appid');
  18 + store.open = Number($tencentCaptcha.data('open'));
  19 +
  20 + if (store.appId) {
  21 + return _initScript();
  22 + }
  23 +
  24 + return $.Deferred().resolve();
  25 + },
  26 +
  27 + isOn() {
  28 + return store.open;
  29 + },
  30 +
  31 + getInstance(cb) {
  32 + console.log(store);
  33 + return new TencentCaptcha(store.appId+'', cb, {});
  34 + }
  35 +};
@@ -7,13 +7,16 @@ import share from './share'; @@ -7,13 +7,16 @@ import share from './share';
7 import openapp from './openapp'; 7 import openapp from './openapp';
8 import promotion from './promotion'; 8 import promotion from './promotion';
9 import individuation from './individuation'; 9 import individuation from './individuation';
  10 +import captcha from './captcha';
10 11
11 $(function() { 12 $(function() {
12 openapp.init(); 13 openapp.init();
13 14
14 user.init().then(uid => { 15 user.init().then(uid => {
15 individuation.init(uid); // 个性化推荐 16 individuation.init(uid); // 个性化推荐
16 - promotion.init(uid); // 促销 17 + captcha.init().then(() => {
  18 + promotion.init(uid); // 促销
  19 + });
17 user.auth(); // 权限验证 20 user.auth(); // 权限验证
18 share.init(); // 分享 21 share.init(); // 分享
19 utils.init(); 22 utils.init();
@@ -29,4 +32,4 @@ export default { @@ -29,4 +32,4 @@ export default {
29 getUid: function(){ 32 getUid: function(){
30 return user.uid; 33 return user.uid;
31 } 34 }
32 -};  
  35 +};
@@ -7,6 +7,7 @@ import utils from './utils'; @@ -7,6 +7,7 @@ import utils from './utils';
7 import user from './user'; 7 import user from './user';
8 import cookies from './cookies'; 8 import cookies from './cookies';
9 import jsonp from './jsonp'; 9 import jsonp from './jsonp';
  10 +import captcha from './captcha';
10 11
11 const miniAppEntrance = '/pages/common/webback'; 12 const miniAppEntrance = '/pages/common/webback';
12 13
@@ -269,30 +270,56 @@ let _initCoupon = function(uid) { @@ -269,30 +270,56 @@ let _initCoupon = function(uid) {
269 if (uid) { 270 if (uid) {
270 let conpontoken = cookies.cookie('yoho-conpon-token'); 271 let conpontoken = cookies.cookie('yoho-conpon-token');
271 let coupontype = cookies.cookie('yoho-conpon-type'); 272 let coupontype = cookies.cookie('yoho-conpon-type');
  273 + let couponCaptcha = Number(cookies.cookie('yoho-conpon-captcha'));
  274 +
272 if (conpontoken) { 275 if (conpontoken) {
273 - _getCoupon({  
274 - token: conpontoken,  
275 - uid: uid,  
276 - coupontype  
277 - }); 276 + if (couponCaptcha || captcha.isOn()) {
  277 + let captchaInstance = captcha.getInstance((result) => {
  278 + if (result.ret === 0) {
  279 + _getCoupon({
  280 + token: conpontoken,
  281 + uid: uid,
  282 + coupontype,
  283 + ticket: result.ticket,
  284 + randstr: result.randstr
  285 + });
  286 + }
  287 + });
  288 +
  289 + captchaInstance.show();
  290 + }
278 291
279 cookies.setCookie('yoho-conpon-token', ''); 292 cookies.setCookie('yoho-conpon-token', '');
280 cookies.setCookie('yoho-conpon-type', ''); 293 cookies.setCookie('yoho-conpon-type', '');
  294 + cookies.setCookie('yoho-conpon-captcha', '');
281 } 295 }
282 } 296 }
283 297
284 $('body').on('click', '.yoho-conpon', function() { 298 $('body').on('click', '.yoho-conpon', function() {
285 let token = $(this).data('token'); 299 let token = $(this).data('token');
286 let type = $(this).data('type'); 300 let type = $(this).data('type');
  301 + let isCaptcha = $(this).hasClass('yoho-captcha') ? 1 : 0;
  302 +
287 if (user.uid) { 303 if (user.uid) {
288 - _getCoupon({  
289 - token: token,  
290 - uid: user.uid,  
291 - coupontype: type  
292 - }); 304 + if (isCaptcha || captcha.isOn()) {
  305 + let captchaInstance = captcha.getInstance((result) => {
  306 + if (result.ret === 0) {
  307 + _getCoupon({
  308 + token: token,
  309 + uid: user.uid,
  310 + coupontype: type,
  311 + ticket: result.ticket,
  312 + randstr: result.randstr
  313 + });
  314 + }
  315 + });
  316 +
  317 + captchaInstance.show();
  318 + }
293 } else { 319 } else {
294 cookies.setCookie('yoho-conpon-token', token); 320 cookies.setCookie('yoho-conpon-token', token);
295 cookies.setCookie('yoho-conpon-type', type); 321 cookies.setCookie('yoho-conpon-type', type);
  322 + cookies.setCookie('yoho-conpon-captcha', isCaptcha);
296 323
297 gotoLogin(); 324 gotoLogin();
298 return; 325 return;