UtilsWX.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. window.UtilsWX = {
  2. soundManage : null,
  3. wxgameLogin : function(callbacks){
  4. this.userLogin(function (callback) {
  5. this.getUserInfo(function (res) {
  6. // userData.httpData.data = res.userInfo;
  7. // console.log('登陆过的信息',res.userInfo)
  8. userData.httpData.data = res.userInfo;
  9. // this.wxlogin(res);
  10. if (callbacks!=null) {
  11. callbacks(res);
  12. }
  13. }.bind(this), function () {
  14. this.createUserInfoButton(callbacks);
  15. }.bind(this));
  16. }.bind(this), function () {
  17. // console.log('没登陆过')
  18. //创建获取授权的按钮
  19. this.createUserInfoButton(callbacks);
  20. }.bind(this));
  21. },
  22. wxlogin: function (userdata,callback,callbackfail) {
  23. // console.log("userdata", userdata.userInfo);
  24. var avatarUrl = userdata.userInfo.avatarUrl;
  25. var gender = userdata.userInfo.gender;
  26. var nickName = userdata.userInfo.nickName;
  27. var city = userdata.userInfo.city;
  28. var province = userdata.userInfo.province;
  29. wx.login({
  30. success: function (res) {
  31. wx.request({
  32. url: "https://www.youaigclm.top/Iamrichman_app/Wxlogingame/?wx=" + res.code
  33. +
  34. "&avatarUrl=" + avatarUrl +
  35. "&nickName=" + nickName +
  36. "&gender=" + gender +
  37. "&city=" + city +
  38. "&province=" + province
  39. ,
  40. method: "GET",
  41. success: function (data) {
  42. if (data.statusCode == 200) {
  43. // console.log("request咱们后台给的", data);
  44. userData.httpData = data.data;
  45. userData.openId = data.data.data.openid;
  46. if (callback != null) {
  47. callback();
  48. }
  49. // console.log("获取到临时代码保存的数据是", userData);
  50. }
  51. }.bind(this),
  52. fail : function () {
  53. if (callbackfail != null) {
  54. callbackfail();
  55. }
  56. }
  57. });
  58. }
  59. });
  60. },
  61. userLogin : function (successCallback,failCallback) {
  62. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  63. wx.checkSession({
  64. success: function () {
  65. //存在登陆态
  66. if (successCallback!=null) {
  67. successCallback();
  68. // console.log('微信登陆状态 --- 登陆');
  69. }
  70. },
  71. fail: function () {
  72. //不存在登陆态
  73. if (failCallback!=null) {
  74. failCallback();
  75. // console.log('微信登陆状态 --- 未登陆');
  76. }
  77. }
  78. })
  79. }
  80. },
  81. getUserInfo : function(successCallback,failCallback){
  82. if (cc.sys.platform === cc.sys.WECHAT_GAME){
  83. wx.getUserInfo({
  84. withCredentials: true,//此处设为true,才会返回encryptedData等敏感信息
  85. success: function (res) {
  86. // 可以将 res 发送给后台解码出 unionId
  87. // app.globalData.userInfo = res.userInfo;
  88. // app.globalData.encryptedData = res.encryptedData;
  89. // app.globalData.iv = res.iv;
  90. if (successCallback!=null) {
  91. successCallback(res);
  92. // console.log('微信登陆状态 --- 获取个人信息');
  93. }
  94. }.bind(this),
  95. fail: function (res) {
  96. if (failCallback!=null) {
  97. failCallback(res);
  98. // console.log('微信登陆状态 --- 没授权过');
  99. }
  100. }.bind(this),
  101. })
  102. }
  103. },
  104. createUserInfoButton : function (onclickCallback) {
  105. if (cc.sys.platform === cc.sys.WECHAT_GAME){
  106. this.width = 160;
  107. this.height = 60;
  108. this.getSystemInfo(function (obj) {
  109. this.userInfoButton = wx.createUserInfoButton({
  110. type: 'image',
  111. text: '',
  112. image: httpUtils.PlayButton,
  113. style: {
  114. left: obj.windowWidth/2-this.width/2,
  115. top: obj.windowHeight/1.3-this.height/2,
  116. width: this.width,
  117. height: this.height,
  118. lineHeight: 40,
  119. backgroundColor: '#ff0000',
  120. color: '#ffffff',
  121. textAlign: 'center',
  122. fontSize: 16,
  123. borderRadius: 4
  124. }
  125. })
  126. this.userInfoButton.onTap(function (res) {
  127. // console.log("createUserInfoButton", res);
  128. if (res.errMsg == "getUserInfo:ok") {
  129. // this.userInfoButton.destroy();
  130. if (onclickCallback!=null) {
  131. onclickCallback(res);
  132. // console.log('点击登陆授权按钮获取授权之后',res);
  133. }
  134. //
  135. }
  136. }.bind(this))
  137. }.bind(this));
  138. return this.userInfoButton;
  139. }
  140. },
  141. removeUserInfoButton : function(){
  142. if (this.userInfoButton!=null) {
  143. this.userInfoButton.destroy();
  144. }
  145. },
  146. shareMenu : function () {
  147. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  148. //开启右上角的分享
  149. wx.showShareMenu();
  150. //监听右上角的分享调用 
  151. cc.loader.loadRes("Share",function(err,data){
  152. // console.log("图片地址",data);
  153. wx.onShareAppMessage(function(res){
  154. return {
  155. title: "不怕,就来PK!转发",
  156. // imageUrl: data.url,
  157. imageUrl: httpUtils.Share,
  158. success(res){
  159. // console.log("转发成功!!!")
  160. // common.diamond += 20;
  161. },
  162. fail(res){
  163. // console.log("转发失败!!!")
  164. }
  165. }
  166. })
  167. });
  168. }
  169. },
  170. sharebtn : function (callback) {
  171. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  172. //监听右上角的分享调用 
  173. cc.loader.loadRes("Share",function(err,data){
  174. // console.log("图片地址",data);
  175. // console.log("转发进来!!!")
  176. if (callback!=null) {
  177. callback();
  178. }
  179. wx.shareAppMessage({
  180. title: "不怕,就来PK!按钮",
  181. imageUrl: httpUtils.Share,
  182. success(res){
  183. console.log("转发成功!!!")
  184. if (callback!=null) {
  185. callback();
  186. }
  187. },
  188. fail(res){
  189. console.log("转发失败!!!")
  190. if (callback!=null) {
  191. callback();
  192. }
  193. }
  194. })
  195. });
  196. }else{
  197. // console.log("啥都没做!!!")
  198. if (callback!=null) {
  199. callback();
  200. }
  201. }
  202. },
  203. getSystemInfo : function (success) {
  204. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  205. wx.getSystemInfo({
  206. success: function (obj) {
  207. // console.log('微信 --- 获取设备尺寸',obj);
  208. if (success != null) {
  209. success(obj);
  210. }
  211. },
  212. fail: function () {
  213. }
  214. });
  215. }
  216. },
  217. //给子域 发消息
  218. postMessage : function (data) {
  219. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  220. wx.postMessage({
  221. message: data
  222. });
  223. }
  224. },
  225. createVideo : function (data) {
  226. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  227. this.getSystemInfo(function (obj) {
  228. this.width = obj.windowWidth;
  229. this.height = obj.windowHeight/1.5;
  230. // console.log("那个",this.height);
  231. this.video = wx.createVideo({
  232. x: obj.windowWidth/2-this.width/2,
  233. y: obj.windowHeight/2-this.height/2,
  234. width: this.width,
  235. height: this.height,
  236. // poster: 'http://192.168.1.25:9999/NoviceBG.png',
  237. // src: "https://www.yuyekeji.cn/static/Cocos/fuhao/Course.mp4",
  238. src: "http://192.168.1.25:9999/Course.mp4",
  239. objectFit : "contain"
  240. })
  241. this.soundManage.startVideo();
  242. // this.video.requestFullScreen({ direction: 0 })
  243. this.video.play();
  244. // this.video.exitFullScreen()
  245. }.bind(this));
  246. }
  247. },
  248. removeVideo : function () {
  249. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  250. if (this.video!=null) {
  251. this.video.destroy();
  252. this.soundManage.stopVideo();
  253. }
  254. }
  255. },
  256. //设置排行榜上面的数据
  257. setUserData : function (dataKey,dataValue) {
  258. if (cc.sys.platform === cc.sys.WECHAT_GAME)
  259. {
  260. wx.setUserCloudStorage({
  261. KVDataList: [
  262. { key: String(dataKey), value: String(dataValue) },
  263. ],
  264. success: res => {
  265. // console.log('成功setUserCloudStorage',res);
  266. },
  267. fail: res => {
  268. // console.log('失败setUserCloudStorage',res);
  269. }
  270. });
  271. }
  272. },
  273. }