酷炫小程序

app.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. //app.js
  2. var http = require('http/request.js');
  3. App({
  4. data: {
  5. loginInfo: null,
  6. hasLogin:false,
  7. userId:'',
  8. channel:"blackWhite3",
  9. code:'',
  10. audios: [[
  11. "/assets/audio/c1.mp3",
  12. "/assets/audio/c1.mp3",
  13. "/assets/audio/d1.mp3",
  14. "/assets/audio/c1.mp3",
  15. "/assets/audio/f1.mp3",
  16. "/assets/audio/e1.mp3",
  17. "/assets/audio/c1.mp3",
  18. "/assets/audio/c1.mp3",
  19. "/assets/audio/d1.mp3",
  20. "/assets/audio/c1.mp3",
  21. "/assets/audio/g1.mp3",
  22. "/assets/audio/f1.mp3",
  23. "/assets/audio/c1.mp3",
  24. "/assets/audio/c1.mp3",
  25. "/assets/audio/c2.mp3",
  26. "/assets/audio/a1.mp3",
  27. "/assets/audio/f1.mp3",
  28. "/assets/audio/e1.mp3",
  29. "/assets/audio/d1.mp3",
  30. "/assets/audio/_a1.mp3",
  31. "/assets/audio/_a1.mp3",
  32. "/assets/audio/a1.mp3",
  33. "/assets/audio/f1.mp3",
  34. "/assets/audio/g1.mp3",
  35. "/assets/audio/f1.mp3"
  36. ],[
  37. "/assets/audio/e2.mp3",
  38. "/assets/audio/_d2.mp3",
  39. "/assets/audio/e2.mp3",
  40. "/assets/audio/_d2.mp3",
  41. "/assets/audio/e2.mp3",
  42. "/assets/audio/b1.mp3",
  43. "/assets/audio/d2.mp3",
  44. "/assets/audio/c2.mp3",
  45. "/assets/audio/a1.mp3",
  46. "/assets/audio/c1.mp3",
  47. "/assets/audio/e1.mp3",
  48. "/assets/audio/a1.mp3",
  49. "/assets/audio/b1.mp3",
  50. "/assets/audio/e1.mp3",
  51. "/assets/audio/_g1.mp3",
  52. "/assets/audio/b1.mp3",
  53. "/assets/audio/c2.mp3",
  54. "/assets/audio/e1.mp3",
  55. "/assets/audio/e2.mp3",
  56. "/assets/audio/_d2.mp3",
  57. "/assets/audio/e2.mp3",
  58. "/assets/audio/_d2.mp3",
  59. "/assets/audio/e2.mp3",
  60. "/assets/audio/b1.mp3",
  61. "/assets/audio/_d2.mp3",
  62. "/assets/audio/c2.mp3",
  63. "/assets/audio/a1.mp3",
  64. "/assets/audio/c1.mp3",
  65. "/assets/audio/e1.mp3",
  66. "/assets/audio/a1.mp3",
  67. "/assets/audio/b1.mp3",
  68. "/assets/audio/e1.mp3",
  69. "/assets/audio/c2.mp3",
  70. "/assets/audio/b1.mp3",
  71. "/assets/audio/a1.mp3"
  72. ],[
  73. "/assets/audio/g1.mp3",
  74. "/assets/audio/g1.mp3",
  75. "/assets/audio/d2.mp3",
  76. "/assets/audio/d2.mp3",
  77. "/assets/audio/e2.mp3",
  78. "/assets/audio/e2.mp3",
  79. "/assets/audio/d2.mp3",
  80. "/assets/audio/c2.mp3",
  81. "/assets/audio/c2.mp3",
  82. "/assets/audio/b1.mp3",
  83. "/assets/audio/b1.mp3",
  84. "/assets/audio/a1.mp3",
  85. "/assets/audio/a1.mp3",
  86. "/assets/audio/g1.mp3",
  87. "/assets/audio/d2.mp3",
  88. "/assets/audio/d2.mp3",
  89. "/assets/audio/c2.mp3",
  90. "/assets/audio/c2.mp3",
  91. "/assets/audio/b1.mp3",
  92. "/assets/audio/b1.mp3",
  93. "/assets/audio/a1.mp3",
  94. "/assets/audio/d2.mp3",
  95. "/assets/audio/d2.mp3",
  96. "/assets/audio/c2.mp3",
  97. "/assets/audio/c2.mp3",
  98. "/assets/audio/b1.mp3",
  99. "/assets/audio/b1.mp3",
  100. "/assets/audio/a1.mp3",
  101. "/assets/audio/g1.mp3",
  102. "/assets/audio/g1.mp3",
  103. "/assets/audio/d2.mp3",
  104. "/assets/audio/d2.mp3",
  105. "/assets/audio/e2.mp3",
  106. "/assets/audio/e2.mp3",
  107. "/assets/audio/d2.mp3",
  108. "/assets/audio/c2.mp3",
  109. "/assets/audio/c2.mp3",
  110. "/assets/audio/b1.mp3",
  111. "/assets/audio/b1.mp3",
  112. "/assets/audio/a1.mp3",
  113. "/assets/audio/a1.mp3",
  114. "/assets/audio/g1.mp3"
  115. ],[
  116. "/assets/audio/c2.mp3",
  117. "/assets/audio/c2.mp3",
  118. "/assets/audio/c2.mp3",
  119. "/assets/audio/g1.mp3",
  120. "/assets/audio/e2.mp3",
  121. "/assets/audio/e2.mp3",
  122. "/assets/audio/e2.mp3",
  123. "/assets/audio/c2.mp3",
  124. "/assets/audio/c2.mp3",
  125. "/assets/audio/e2.mp3",
  126. "/assets/audio/g2.mp3",
  127. "/assets/audio/g2.mp3",
  128. "/assets/audio/f2.mp3",
  129. "/assets/audio/e2.mp3",
  130. "/assets/audio/d2.mp3",
  131. "/assets/audio/d2.mp3",
  132. "/assets/audio/e2.mp3",
  133. "/assets/audio/f2.mp3",
  134. "/assets/audio/f2.mp3",
  135. "/assets/audio/e2.mp3",
  136. "/assets/audio/d2.mp3",
  137. "/assets/audio/e2.mp3",
  138. "/assets/audio/c2.mp3",
  139. "/assets/audio/c2.mp3",
  140. "/assets/audio/e2.mp3",
  141. "/assets/audio/d2.mp3",
  142. "/assets/audio/g1.mp3",
  143. "/assets/audio/b1.mp3",
  144. "/assets/audio/d2.mp3",
  145. "/assets/audio/c2.mp3"
  146. ],[
  147. "/assets/audio/e2.mp3",
  148. "/assets/audio/e2.mp3",
  149. "/assets/audio/f2.mp3",
  150. "/assets/audio/g2.mp3",
  151. "/assets/audio/g2.mp3",
  152. "/assets/audio/f2.mp3",
  153. "/assets/audio/e2.mp3",
  154. "/assets/audio/d2.mp3",
  155. "/assets/audio/c2.mp3",
  156. "/assets/audio/c2.mp3",
  157. "/assets/audio/d2.mp3",
  158. "/assets/audio/e2.mp3",
  159. "/assets/audio/e2.mp3",
  160. "/assets/audio/d2.mp3",
  161. "/assets/audio/d2.mp3",
  162. "/assets/audio/e2.mp3",
  163. "/assets/audio/e2.mp3",
  164. "/assets/audio/f2.mp3",
  165. "/assets/audio/g2.mp3",
  166. "/assets/audio/g2.mp3",
  167. "/assets/audio/f2.mp3",
  168. "/assets/audio/e2.mp3",
  169. "/assets/audio/d2.mp3",
  170. "/assets/audio/c2.mp3",
  171. "/assets/audio/c2.mp3",
  172. "/assets/audio/d2.mp3",
  173. "/assets/audio/e2.mp3",
  174. "/assets/audio/d2.mp3",
  175. "/assets/audio/c2.mp3",
  176. "/assets/audio/c2.mp3",
  177. "/assets/audio/d2.mp3",
  178. "/assets/audio/d2.mp3",
  179. "/assets/audio/e2.mp3",
  180. "/assets/audio/c2.mp3",
  181. "/assets/audio/d2.mp3",
  182. "/assets/audio/e2.mp3",
  183. "/assets/audio/f2.mp3",
  184. "/assets/audio/e2.mp3",
  185. "/assets/audio/c2.mp3",
  186. "/assets/audio/d2.mp3",
  187. "/assets/audio/e2.mp3",
  188. "/assets/audio/f2.mp3",
  189. "/assets/audio/e2.mp3",
  190. "/assets/audio/d2.mp3",
  191. "/assets/audio/c2.mp3",
  192. "/assets/audio/d2.mp3",
  193. "/assets/audio/g1.mp3",
  194. "/assets/audio/e2.mp3",
  195. "/assets/audio/e2.mp3",
  196. "/assets/audio/f2.mp3",
  197. "/assets/audio/g2.mp3",
  198. "/assets/audio/g2.mp3",
  199. "/assets/audio/f2.mp3",
  200. "/assets/audio/e2.mp3",
  201. "/assets/audio/d2.mp3",
  202. "/assets/audio/c2.mp3",
  203. "/assets/audio/c2.mp3",
  204. "/assets/audio/d2.mp3",
  205. "/assets/audio/e2.mp3",
  206. "/assets/audio/d2.mp3",
  207. "/assets/audio/c2.mp3",
  208. "/assets/audio/c2.mp3"
  209. ],[
  210. "/assets/audio/c2.mp3",
  211. "/assets/audio/c2.mp3",
  212. "/assets/audio/c2.mp3",
  213. "/assets/audio/g1.mp3",
  214. "/assets/audio/e2.mp3",
  215. "/assets/audio/e2.mp3",
  216. "/assets/audio/e2.mp3",
  217. "/assets/audio/c2.mp3",
  218. "/assets/audio/c2.mp3",
  219. "/assets/audio/e2.mp3",
  220. "/assets/audio/g2.mp3",
  221. "/assets/audio/g2.mp3",
  222. "/assets/audio/f2.mp3",
  223. "/assets/audio/e2.mp3",
  224. "/assets/audio/d2.mp3",
  225. "/assets/audio/d2.mp3",
  226. "/assets/audio/e2.mp3",
  227. "/assets/audio/f2.mp3",
  228. "/assets/audio/f2.mp3",
  229. "/assets/audio/e2.mp3",
  230. "/assets/audio/d2.mp3",
  231. "/assets/audio/e2.mp3",
  232. "/assets/audio/c2.mp3",
  233. "/assets/audio/c2.mp3",
  234. "/assets/audio/e2.mp3",
  235. "/assets/audio/d2.mp3",
  236. "/assets/audio/g1.mp3",
  237. "/assets/audio/b1.mp3",
  238. "/assets/audio/d2.mp3",
  239. "/assets/audio/c2.mp3",
  240. "/assets/audio/e2.mp3",
  241. "/assets/audio/e2.mp3",
  242. "/assets/audio/f2.mp3",
  243. "/assets/audio/g2.mp3",
  244. "/assets/audio/g2.mp3",
  245. "/assets/audio/f2.mp3",
  246. "/assets/audio/e2.mp3",
  247. "/assets/audio/d2.mp3",
  248. "/assets/audio/c2.mp3",
  249. "/assets/audio/c2.mp3",
  250. "/assets/audio/d2.mp3",
  251. "/assets/audio/e2.mp3",
  252. "/assets/audio/e2.mp3",
  253. "/assets/audio/d2.mp3",
  254. "/assets/audio/d2.mp3",
  255. "/assets/audio/e2.mp3",
  256. "/assets/audio/e2.mp3",
  257. "/assets/audio/f2.mp3",
  258. "/assets/audio/g2.mp3",
  259. "/assets/audio/g2.mp3",
  260. "/assets/audio/f2.mp3",
  261. "/assets/audio/e2.mp3",
  262. "/assets/audio/d2.mp3",
  263. "/assets/audio/c2.mp3",
  264. "/assets/audio/c2.mp3",
  265. "/assets/audio/d2.mp3",
  266. "/assets/audio/e2.mp3",
  267. "/assets/audio/d2.mp3",
  268. "/assets/audio/c2.mp3",
  269. "/assets/audio/c2.mp3",
  270. "/assets/audio/d2.mp3",
  271. "/assets/audio/d2.mp3",
  272. "/assets/audio/e2.mp3",
  273. "/assets/audio/c2.mp3",
  274. "/assets/audio/d2.mp3",
  275. "/assets/audio/e2.mp3",
  276. "/assets/audio/f2.mp3",
  277. "/assets/audio/e2.mp3",
  278. "/assets/audio/c2.mp3",
  279. "/assets/audio/d2.mp3",
  280. "/assets/audio/e2.mp3",
  281. "/assets/audio/f2.mp3",
  282. "/assets/audio/e2.mp3",
  283. "/assets/audio/d2.mp3",
  284. "/assets/audio/c2.mp3",
  285. "/assets/audio/d2.mp3",
  286. "/assets/audio/g1.mp3",
  287. "/assets/audio/e2.mp3",
  288. "/assets/audio/e2.mp3",
  289. "/assets/audio/f2.mp3",
  290. "/assets/audio/g2.mp3",
  291. "/assets/audio/g2.mp3",
  292. "/assets/audio/f2.mp3",
  293. "/assets/audio/e2.mp3",
  294. "/assets/audio/d2.mp3",
  295. "/assets/audio/c2.mp3",
  296. "/assets/audio/c2.mp3",
  297. "/assets/audio/d2.mp3",
  298. "/assets/audio/e2.mp3",
  299. "/assets/audio/d2.mp3",
  300. "/assets/audio/c2.mp3",
  301. "/assets/audio/c2.mp3"
  302. ]],
  303. shareMsg: "发现一个很有趣的音乐游戏,一起来玩玩看~",
  304. failed: false,
  305. shareIcon: [
  306. "https://small-app.oss-cn-beijing.aliyuncs.com/black-white/shandian.png",
  307. "https://small-app.oss-cn-beijing.aliyuncs.com/black-white/xiaobang.png",
  308. "https://small-app.oss-cn-beijing.aliyuncs.com/black-white/zuoyoushou.png",
  309. "https://small-app.oss-cn-beijing.aliyuncs.com/black-white/huaxiele.png",
  310. "https://small-app.oss-cn-beijing.aliyuncs.com/black-white/shoucan.png",
  311. "https://small-app.oss-cn-beijing.aliyuncs.com/black-white/tiyulaoshi.png"
  312. ],
  313. currentShareIcon: "",
  314. shareTicket: "",
  315. },
  316. onLaunch: function () {
  317. // 展示本地存储能力
  318. },
  319. //页面现实加载,获取群信息
  320. onShow: function (options) {
  321. this.data.shareTicket = options.shareTicket;
  322. },
  323. // 登录
  324. login(callback) {
  325. var that = this;
  326. wx.login({
  327. success: res => {
  328. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  329. if (res.code) {
  330. //发起网络请求
  331. console.log(res.code)
  332. that.data.code = res.code;
  333. wx.getSetting({
  334. success: (res) => {
  335. if (res.authSetting["scope.userInfo"]) {
  336. //已经授权
  337. that.getUserInfo(callback);
  338. } else {
  339. //没有授权
  340. wx.authorize({
  341. scope: 'scope.userInfo',
  342. success: function () {
  343. that.getUserInfo(callback);
  344. },
  345. fail: function () {
  346. //用户拒绝授权
  347. that.authFailed(callback);
  348. }
  349. })
  350. }
  351. }
  352. })
  353. } else {
  354. console.log("获取用户登录态失败!" + res.errMsg)
  355. that.login(callback);
  356. }
  357. }
  358. })
  359. },
  360. //获取用户信息
  361. getUserInfo(callback) {
  362. var that = this;
  363. wx.getUserInfo({
  364. success: res => {
  365. console.log(res)
  366. that.data.loginInfo = {
  367. 'code': that.data.code,
  368. 'wechatName': res.userInfo.nickName,
  369. 'wechatHeadUrl': res.userInfo.avatarUrl,
  370. 'encryptedData': res.encryptedData,
  371. 'channel': that.data.channel,
  372. 'iv': res.iv
  373. }
  374. //登录服务器
  375. that.hostLogin(callback);
  376. wx.setStorage({
  377. key: 'userInfo',
  378. data: res.userInfo
  379. })
  380. callback();
  381. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  382. // 所以此处加入 callback 以防止这种情况
  383. if (this.userInfoReadyCallback) {
  384. this.userInfoReadyCallback(res)
  385. }
  386. },
  387. fail: function (e) {
  388. console.log("获取用户信息失败")
  389. }
  390. })
  391. },
  392. //用户拒绝授权
  393. authFailed(callback) {
  394. var that = this;
  395. wx.showModal({
  396. title: '提示',
  397. content: '您当前尚未登录,请点击“授权”完成授权方能进入游戏!',
  398. confirmText: "授权",
  399. showCancel: false,//隐藏取消按钮
  400. success: function (res) {
  401. //调取授权弹窗成功
  402. if (res.confirm) {
  403. //点击确认进入授权页面
  404. wx.openSetting({
  405. success: function (res) {
  406. console.log(res)
  407. if (!res.authSetting["scope.userInfo"]) {
  408. //这里是授权失败
  409. that.authFailed(callback)
  410. } else {
  411. //授权成功
  412. that.getUserInfo(callback)
  413. }
  414. }
  415. })
  416. } else if (res.cancel) {
  417. //用户拒绝授权
  418. that.authFailed(callback);
  419. }
  420. },
  421. fail() {
  422. //调取授权弹窗失败
  423. that.checkSession(callback);
  424. }
  425. })
  426. },
  427. //检测当前用户登录态是否有效
  428. checkSession(callback) {
  429. var that = this;
  430. if(this.data.hasLogin == true){
  431. callback();
  432. return;
  433. }
  434. wx.checkSession({
  435. success: function () {
  436. that.login(callback);
  437. },
  438. fail: function () {
  439. //登录态过期
  440. console.log("过期了")
  441. that.login(callback)//重新登录;
  442. }
  443. })
  444. },
  445. hostLogin(callback) {
  446. var that = this;
  447. this.func.req('/user/loginByWechatApplet', 'get', {}, this.data.loginInfo, function (res) {
  448. console.log(res)
  449. if (res.code == 0) {
  450. that.data.userId=res.data.userId;
  451. wx.setStorage({
  452. key: "userId",
  453. data: res.data.userId
  454. })
  455. that.data.userId = res.data.userId;
  456. that.data.hasLogin = true;
  457. wx.setStorageSync("hasLogin", "1")
  458. callback();
  459. }
  460. })
  461. },
  462. globalData: {
  463. userInfo: null
  464. },
  465. func: {
  466. req: http.req
  467. },
  468. //上报成绩事件
  469. reportGameScore: function (gameType,score) {
  470. var that = this;
  471. http.req('/blackwhite/reportGameScore', 'get', "userId=" + that.data.userId, {
  472. userId: that.data.userId,
  473. channel: that.data.channel,
  474. gameType: gameType,
  475. score: score
  476. }, (res) => {
  477. console.log(res)
  478. })
  479. }
  480. })