1 |
- "use strict";function wxGetSetting(e,t){wx.getSetting({success:function(o){Fyhd.console_log("getSettin respone",o),!0===o.authSetting["scope.userInfo"]?wx.getStorage({key:"btnRes",success:function(o){wx.getLaunchOptionsSync(),null==o.data||o.data=={}?_createUserInfoButton(e,function(e){t(e)}):Fyhd.getFyhdUser(o.data,e,function(e){Fyhd.console_log("getFyhdUser",e),t(e)})},fail:function(o){_createUserInfoButton(e,function(e){t(e)})}}):_createUserInfoButton(e,function(e){t(e)})}})}function wxGetLogin(e,t){wx.login({dataType:"json",success:function(e){wx.request({url:Fyhd.param._FyhdUrl+"/get_wx_userinfo?code="+e.code+"&game_appid="+Fyhd.param._gameAppid+"&system="+Fyhd.param._system,header:{"content-type":"application/x-www-form-urlencoded"},dataType:"json",success:function(e){Fyhd.console_log("get_wx_userinfo",e,1);var o={err_code:1,err_msg:"",result:{user:{openid:e.data.data.openid,token:e.data.data.token,timestamp:e.data.data.timestamp,sign:e.data.data.sign},shareInfo:wx.getLaunchOptionsSync().query}};t(o)},fail:function(e){Fyhd.console_log("get_wx_userinfo fail result",e,1)}})},fail:function(e){Fyhd.console_log("wx.login fail result",e,1)}})}function _createUserInfoButton(e,t){if(wx.createUserInfoButton){var o=wx.createUserInfoButton({type:"image",image:e.imageUrl,lang:"zh_CN",style:{left:e.left,top:e.top,width:e.width,height:e.height}});o.onTap(function(n){Fyhd.console_log("userinfoBtn.onTap.btnRes:",n),"getUserInfo:ok"===n.errMsg?(o.destroy(),o="",n.userInfo.isVisitor=0,wx.setStorage({key:"btnRes",data:n}),Fyhd.getFyhdUser(n,e,function(e){t(e)})):(o.destroy(),o="",_createUserInfoButton(e,t))})}else wx.showModal({title:"提示",content:"当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。"})}function callbackInfo(e,t,o){wx.login({dataType:"json",success:function(n){null==n.code||""==n.code?callbackInfo(e,t,function(e){o(e)}):wx.request({url:Fyhd.param._FyhdUrl+"/get_wx_userinfo?code="+n.code+"&game_appid="+Fyhd.param._gameAppid+"&system="+Fyhd.param._system,header:{"content-type":"application/x-www-form-urlencoded"},dataType:"json",success:function(t){Fyhd.console_log("get_wx_userinfo",t,1);var n={err_code:1,err_msg:"",result:{user:{openid:t.data.data.openid,nickname:e.userInfo.nickName,avatarUrl:e.userInfo.avatarUrl,country:e.userInfo.country,province:e.userInfo.province,city:e.userInfo.city,gender:e.userInfo.gender,token:t.data.data.token,isVisitor:e.userInfo.isVisitor,timestamp:t.data.data.timestamp,sign:t.data.data.sign},shareInfo:wx.getLaunchOptionsSync().query}};Fyhd.console_log("fyhdback",n),o(n)},fail:function(e){Fyhd.console_log("get_wx_userinfo fail result",e,1)}})},fail:function(e){Fyhd.console_log("wx.login fail result",e,1)}})}require("./crypto-js.js");var hd_config=require("./fyhd.js"),Fyhd={param:{_FyhdUrl:"",envVersion:"develop",_system:"ios",_gameAppid:"",_secret:"",_signType:"",_source_appid:"default",_sub_channel:""},url:{pro:"https://qd-api.szfyhd.com/api",dev:"https://xyxqd-api-test.szfyhd.com/api"},version:function(){return"v1.2.4"},now:function(){return Math.floor((new Date).getTime()/1e3)},console_log:function(e,t,o,n){n=1==o?"\n url:"+(n||this.param._FyhdUrl):"";console.log("=====sdk "+e+"====="+n+"\n",t||"")},startSdk:function(e){this.console_log("version",this.version()),this.getedition(e&&e.is_pro?e.is_pro:""),this.getLaunchOptionSync(),this.isIos(),e&&e.callback&&this.getStartInfo(e.callback),this.console_log("start param",this.param)},getedition:function(e){var t=__wxConfig.envVersion,o=this.param;switch(t){case"develop":o.envVersion="develop";break;case"trial":o.envVersion="trial";break;case"release":o.envVersion="release";break;default:o.envVersion="develop"}o._FyhdUrl=1===e?this.url.pro:0===e?this.url.dev:"release"==t?this.url.pro:this.url.dev,o._gameAppid=hd_config.config.appid,o._secret=hd_config.config.secret_key,o._signType=hd_config.config.sign_type,this.console_log("当前开发环境",o.envVersion),this.console_log("配置参数",hd_config.config),this.console_log("当前请求地址",o._FyhdUrl)},getLaunchOptionSync:function(){var e=this.param,t=wx.getLaunchOptionsSync(),o=t.query;o&&"share"==o.scene?e._source_appid="share":o&&"qrcode"==o.scene?e._source_appid="qrcode":o&&"ad"==o.scene&&o.chid?(e._source_appid=o.scene,e._sub_channel=o.chid):t.referrerInfo&&t.referrerInfo.appId&&(e._source_appid=t.referrerInfo.appId),this.console_log("getLaunchOptionSync",t)},isIos:function(){var e=wx.getSystemInfoSync(),t=this.param;"ios"==e.platform||"android"==e.platform?t._system=e.platform:t._system=e.system.toLowerCase().indexOf("ios")>-1?"ios":"android",this.console_log("当前系统",t._system)},getStartInfo:function(e){var t=this.param,o={game_appid:t._gameAppid,create_time:this.now(),source_appid:t._source_appid,system:t._system};o.sign=this.sdk_encrypt(o,t._secret),o.sign_type=t._signType,wx.request({url:t._FyhdUrl+"/start",data:o,method:"post",header:{"content-type":"application/x-www-form-urlencoded"},complete:function(t){Fyhd.console_log("getStartInfo result",t.data,1),e(t.data.data)}})},copyObj:function(e){return JSON.parse(JSON.stringify(e))},getFyhdUserInfo:function(e,t,o){t?wxGetSetting(e,function(e){o(e)}):wxGetLogin(e,function(e){o(e)})},getFyhdUser:function(e,t,o){Fyhd.console_log("getFyhdUser",e),callbackInfo(e,t,function(e){o(e)})},loginData:function(e,t){var o=e,n=this.param;o.game_appid=n._gameAppid,o.create_time=this.now(),o.source_appid=n._source_appid;var a={openid:o.openid,game_appid:o.game_appid,create_time:o.create_time,source_appid:o.source_appid};o.sign=this.sdk_encrypt(a,n._secret),o.sign_type=n._signType,o.extra_param=e.extra_param?e.extra_param:"",o.sub_channel=n._sub_channel,this.console_log("loginData 参数",o),wx.request({url:n._FyhdUrl+"/cps/authorizing",data:o,method:"post",header:{"content-type":"application/x-www-form-urlencoded"},complete:function(e){Fyhd.console_log("loginData result",e,1),t(e.data)},fail:function(e){Fyhd.console_log("loginData fail result",e,1)}})},valueuser:function(e,t){var o=this.param,n={openid:e.openid,game_appid:o._gameAppid,create_time:this.now(),source_appid:o._source_appid},a=this.copyObj(n);n.sign=this.sdk_encrypt(a,o._secret),n.sign_type=o._signType,n.sub_channel=o._sub_channel,this.console_log("sdk valueuser 参数",n),wx.request({url:o._FyhdUrl+"/cps/valueuser",data:n,method:"post",header:{"content-type":"application/x-www-form-urlencoded"},complete:function(e){Fyhd.console_log("valueuser result",e,1),t(e.data)},fail:function(e){Fyhd.console_log("valueuser fail result",e,1)}})},advertisement:function(e,t){if("push"!=e.ad_type||1==e.allow){var o=this.param,n={ad_type:e.ad_type,game_appid:o._gameAppid,source_appid:o._source_appid,create_time:this.now()};e.openid&&(n.openid=e.openid),n.sign=this.sdk_encrypt(this.copyObj(n),o._secret),n.sign_type=o._signType,n.sub_channel=o._sub_channel,this.console_log("advertisement 参数",n),wx.request({url:o._FyhdUrl+"/cps/advertisement",data:n,method:"post",header:{"content-type":"application/x-www-form-urlencoded"},complete:function(e){Fyhd.console_log("advertisement result",e,1),t(e.data)},fail:function(e){Fyhd.console_log("advertisement fail result",e,1)}})}},shareRecommend:function(e,t){var o=this.param,n={game_appid:o._gameAppid,create_time:this.now()};e.game_appid&&(n.game_appid=e.game_appid);var a=this.copyObj(n);n.sign=this.sdk_encrypt(a,o._secret),n.sign_type=o._signType,n.is_white_player=e.is_white_player?1:0,n.openid=e.openid?e.openid:"",wx.getLaunchOptionsSync().referrerInfo&&wx.getLaunchOptionsSync().referrerInfo.appId?n.source_appid=wx.getLaunchOptionsSync().referrerInfo.appId:n.source_appid="share_recommend_default",this.console_log("shareRecommend 参数",n),wx.request({url:o._FyhdUrl+"/shanwan/share_recommend",data:n,method:"post",header:{"content-type":"application/x-www-form-urlencoded"},complete:function(e){Fyhd.console_log("shareRecommend result",e,1),t(e.data)},fail:function(e){Fyhd.console_log("shareRecommend fail result",e,1)}})},sharePlay:function(e,t){var o=this.param,n={game_appid:o._gameAppid,to_appid:e.to_appid,openid:e.openid,create_time:this.now()},a=this.copyObj(n);n.sign=this.sdk_encrypt(a,o._secret),n.sign_type=o._signType,n.sort=e.sort,this.console_log("sharePlay 参数",n),wx.request({url:o._FyhdUrl+"/shanwan/share_play",data:n,method:"post",header:{"content-type":"application/x-www-form-urlencoded"},complete:function(o){Fyhd.console_log("sharePlay result",o,1),Fyhd.advertisement({ad_type:"push",openid:e.openid,allow:1},function(e){}),t(o.data)},fail:function(e){Fyhd.console_log("sharePlay fail result",e,1)}})},shareConfig:function(e,t){var o=this.param,n={game_appid:o._gameAppid,create_time:this.now()},a=this.copyObj(n);n.sign=this.sdk_encrypt(a,o._secret),n.sign_type=o._signType,this.console_log("shareConfig 参数",n),wx.request({url:o._FyhdUrl+"/cps/share_config",data:n,method:"post",header:{"content-type":"application/x-www-form-urlencoded"},complete:function(e){Fyhd.console_log("shareConfig result",e,1),t(e.data)},fail:function(e){Fyhd.console_log("shareConfig fail result",e,1),t({code:1,msg:"默认配置",time:Fyhd.now(),data:{is_share:!1}})}})},areaLimit:function(e,t){var o=this.param,n={game_appid:o._gameAppid,edition:o.envVersion,system:o._system,create_time:this.now()},a=this.copyObj(n);n.ip=e.ip?e.ip:"",n.sign=this.sdk_encrypt(a,o._secret),n.sign_type=o._signType,this.console_log("areaLimit 参数",n),wx.request({url:o._FyhdUrl+"/cps/area_limit",data:n,method:"post",header:{"content-type":"application/x-www-form-urlencoded"},complete:function(e){Fyhd.console_log("areaLimit result",e,1),t(e.data)},fail:function(e){Fyhd.console_log("areaLimit fail result",e,1)}})},whiteConfig:function(e){if(!wx.getLaunchOptionsSync().referrerInfo||!wx.getLaunchOptionsSync().referrerInfo.appId)return this.console_log("whiteConfig fail result","未找到source_appid"),void e({code:1,msg:"未找到source_appid",time:Fyhd.now(),data:{is_white:!1}});var t=this.param,o={game_appid:t._gameAppid,create_time:this.now(),source_appid:t._source_appid},n=this.copyObj(o);o.sign=this.sdk_encrypt(n,t._secret),o.sign_type=t._signType,this.console_log("whiteConfig 参数",o),wx.request({url:t._FyhdUrl+"/cps/white_config",data:o,method:"post",header:{"content-type":"application/x-www-form-urlencoded"},complete:function(t){Fyhd.console_log("whiteConfig result",t,1),e(t.data)},fail:function(t){Fyhd.console_log("whiteConfig fail result",t,1),e({code:1,msg:"默认配置",time:Fyhd.now(),data:{is_white:!1}})}})},specialEvent:function(e,t){var o=this.param,n=e,a=this.copyObj({game_appid:o._gameAppid});n.sign=this.sdk_encrypt(a,o._secret),n.sign_type=o._signType,n.game_appid=o._gameAppid,this.console_log("specialEvent 参数",n),wx.request({url:o._FyhdUrl+"/cps/special_event",data:n,method:"post",header:{"content-type":"application/x-www-form-urlencoded"},complete:function(e){Fyhd.console_log("specialEvent result",e),t(e.data)},fail:function(e){Fyhd.console_log("specialEvent fail result",e)}})},sdk_encrypt:function(e,t){var o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"sha1",n=Fyhd.sdk_sort(e),a="";for(var s in n)a+=s+"="+n[s]+"&";a=(a=a.substring(0,a.length-1)).replace(/\s+/g,"")+t;var i=("sha1"==o?CryptoJS.SHA1(a):CryptoJS.MD5(a)).toString();return Fyhd.console_log("加密","原串str:"+a+"\n 加密sign="+i),i},sdk_sort:function(e){var t=[];return Object.keys(e).sort().map(function(o){t[o]=e[o]}),t}};module.exports={getFyhd:function(){return Fyhd}};
|