| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- window.UtilsWX = {
- soundManage : null,
- wxgameLogin : function(callbacks){
- this.userLogin(function (callback) {
- this.getUserInfo(function (res) {
- // userData.httpData.data = res.userInfo;
- // console.log('登陆过的信息',res.userInfo)
- userData.httpData.data = res.userInfo;
- // this.wxlogin(res);
- if (callbacks!=null) {
- callbacks(res);
- }
- }.bind(this), function () {
- this.createUserInfoButton(callbacks);
- }.bind(this));
- }.bind(this), function () {
- // console.log('没登陆过')
- //创建获取授权的按钮
- this.createUserInfoButton(callbacks);
- }.bind(this));
- },
- wxlogin: function (userdata,callback,callbackfail) {
- // console.log("userdata", userdata.userInfo);
- var avatarUrl = userdata.userInfo.avatarUrl;
- var gender = userdata.userInfo.gender;
- var nickName = userdata.userInfo.nickName;
- var city = userdata.userInfo.city;
- var province = userdata.userInfo.province;
- wx.login({
- success: function (res) {
- wx.request({
- url: "https://www.youaigclm.top/Iamrichman_app/Wxlogingame/?wx=" + res.code
- +
- "&avatarUrl=" + avatarUrl +
- "&nickName=" + nickName +
- "&gender=" + gender +
- "&city=" + city +
- "&province=" + province
- ,
- method: "GET",
- success: function (data) {
- if (data.statusCode == 200) {
- // console.log("request咱们后台给的", data);
- userData.httpData = data.data;
- userData.openId = data.data.data.openid;
- if (callback != null) {
- callback();
- }
- // console.log("获取到临时代码保存的数据是", userData);
- }
- }.bind(this),
- fail : function () {
- if (callbackfail != null) {
- callbackfail();
- }
- }
- });
- }
- });
- },
- userLogin : function (successCallback,failCallback) {
- if (cc.sys.platform === cc.sys.WECHAT_GAME) {
- wx.checkSession({
- success: function () {
- //存在登陆态
- if (successCallback!=null) {
- successCallback();
- // console.log('微信登陆状态 --- 登陆');
- }
- },
- fail: function () {
- //不存在登陆态
- if (failCallback!=null) {
- failCallback();
- // console.log('微信登陆状态 --- 未登陆');
- }
- }
- })
- }
- },
- getUserInfo : function(successCallback,failCallback){
- if (cc.sys.platform === cc.sys.WECHAT_GAME){
- wx.getUserInfo({
- withCredentials: true,//此处设为true,才会返回encryptedData等敏感信息
- success: function (res) {
- // 可以将 res 发送给后台解码出 unionId
- // app.globalData.userInfo = res.userInfo;
- // app.globalData.encryptedData = res.encryptedData;
- // app.globalData.iv = res.iv;
- if (successCallback!=null) {
- successCallback(res);
- // console.log('微信登陆状态 --- 获取个人信息');
- }
- }.bind(this),
- fail: function (res) {
- if (failCallback!=null) {
- failCallback(res);
- // console.log('微信登陆状态 --- 没授权过');
- }
- }.bind(this),
- })
- }
- },
- createUserInfoButton : function (onclickCallback) {
- if (cc.sys.platform === cc.sys.WECHAT_GAME){
- this.width = 160;
- this.height = 60;
- this.getSystemInfo(function (obj) {
- this.userInfoButton = wx.createUserInfoButton({
- type: 'image',
- text: '',
- image: httpUtils.PlayButton,
- style: {
- left: obj.windowWidth/2-this.width/2,
- top: obj.windowHeight/1.3-this.height/2,
- width: this.width,
- height: this.height,
- lineHeight: 40,
- backgroundColor: '#ff0000',
- color: '#ffffff',
- textAlign: 'center',
- fontSize: 16,
- borderRadius: 4
- }
- })
- this.userInfoButton.onTap(function (res) {
- // console.log("createUserInfoButton", res);
- if (res.errMsg == "getUserInfo:ok") {
- // this.userInfoButton.destroy();
- if (onclickCallback!=null) {
- onclickCallback(res);
- // console.log('点击登陆授权按钮获取授权之后',res);
- }
- //
- }
- }.bind(this))
- }.bind(this));
- return this.userInfoButton;
- }
- },
- removeUserInfoButton : function(){
- if (this.userInfoButton!=null) {
- this.userInfoButton.destroy();
- }
- },
- shareMenu : function () {
- if (cc.sys.platform === cc.sys.WECHAT_GAME) {
- //开启右上角的分享
- wx.showShareMenu();
- //监听右上角的分享调用
- cc.loader.loadRes("Share",function(err,data){
- // console.log("图片地址",data);
- wx.onShareAppMessage(function(res){
- return {
- title: "不怕,就来PK!转发",
- // imageUrl: data.url,
- imageUrl: httpUtils.Share,
- success(res){
- // console.log("转发成功!!!")
- // common.diamond += 20;
- },
- fail(res){
- // console.log("转发失败!!!")
- }
- }
- })
- });
- }
- },
- sharebtn : function (callback) {
- if (cc.sys.platform === cc.sys.WECHAT_GAME) {
- //监听右上角的分享调用
- cc.loader.loadRes("Share",function(err,data){
- // console.log("图片地址",data);
- // console.log("转发进来!!!")
- if (callback!=null) {
- callback();
- }
- wx.shareAppMessage({
- title: "不怕,就来PK!按钮",
- imageUrl: httpUtils.Share,
- success(res){
- console.log("转发成功!!!")
- if (callback!=null) {
- callback();
- }
- },
- fail(res){
- console.log("转发失败!!!")
- if (callback!=null) {
- callback();
- }
- }
- })
- });
- }else{
- // console.log("啥都没做!!!")
- if (callback!=null) {
- callback();
- }
- }
- },
- getSystemInfo : function (success) {
- if (cc.sys.platform === cc.sys.WECHAT_GAME) {
- wx.getSystemInfo({
- success: function (obj) {
- // console.log('微信 --- 获取设备尺寸',obj);
- if (success != null) {
- success(obj);
- }
- },
- fail: function () {
- }
- });
- }
- },
- //给子域 发消息
- postMessage : function (data) {
- if (cc.sys.platform === cc.sys.WECHAT_GAME) {
- wx.postMessage({
- message: data
- });
- }
- },
- createVideo : function (data) {
- if (cc.sys.platform === cc.sys.WECHAT_GAME) {
- this.getSystemInfo(function (obj) {
- this.width = obj.windowWidth;
- this.height = obj.windowHeight/1.5;
- // console.log("那个",this.height);
- this.video = wx.createVideo({
- x: obj.windowWidth/2-this.width/2,
- y: obj.windowHeight/2-this.height/2,
- width: this.width,
- height: this.height,
- // poster: 'http://192.168.1.25:9999/NoviceBG.png',
- // src: "https://www.yuyekeji.cn/static/Cocos/fuhao/Course.mp4",
- src: "http://192.168.1.25:9999/Course.mp4",
- objectFit : "contain"
- })
- this.soundManage.startVideo();
- // this.video.requestFullScreen({ direction: 0 })
- this.video.play();
- // this.video.exitFullScreen()
- }.bind(this));
- }
- },
- removeVideo : function () {
- if (cc.sys.platform === cc.sys.WECHAT_GAME) {
- if (this.video!=null) {
- this.video.destroy();
- this.soundManage.stopVideo();
- }
- }
- },
- //设置排行榜上面的数据
- setUserData : function (dataKey,dataValue) {
- if (cc.sys.platform === cc.sys.WECHAT_GAME)
- {
- wx.setUserCloudStorage({
- KVDataList: [
- { key: String(dataKey), value: String(dataValue) },
- ],
- success: res => {
- // console.log('成功setUserCloudStorage',res);
- },
- fail: res => {
- // console.log('失败setUserCloudStorage',res);
- }
- });
- }
- },
- }
|