main.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import common from '../utils/util';
  2. var app = getApp();
  3. function getData(url, callback) {
  4. //console.log("加密前的结果为===", url);
  5. var url = common.Encrypt(url);
  6. //console.log("加密后的结果为===",url);
  7. wx.request({
  8. url: app.globalData.serverUrl + url,
  9. success: function (res) {
  10. if (res.statusCode)
  11. common.checkError(res.statusCode);
  12. var data = res.data.result;
  13. callback(data);
  14. },
  15. fail: function () {
  16. wx.redirectTo({
  17. url: './error',
  18. });
  19. },
  20. });
  21. }
  22. function postData(url, postData, callback) {
  23. var url = common.Encrypt(url);
  24. //console.log("加密后的结果为===",url);
  25. wx.request({
  26. url: app.globalData.serverUrl + url,
  27. method: "POST",
  28. data: postData,
  29. success: function (res) {
  30. if (res.statusCode)
  31. common.checkError(res.statusCode);
  32. var data = res.data.result;
  33. callback(data);
  34. },
  35. fail: function () {
  36. wx.showToast({
  37. title: '服务器忙,请稍候再试!',
  38. duration: 3000
  39. });
  40. wx.redirectTo({
  41. url: './error',
  42. });
  43. },
  44. });
  45. }
  46. function getLocalHost(callback) {
  47. if (!app.globalData.IsProduction) {
  48. var url = common.Encrypt("Ping");
  49. wx.request({
  50. url: app.globalData.serverUrlLocalhost + url,
  51. success: function (res) {
  52. app.globalData.serverUrl = app.globalData.serverUrlLocalhost;
  53. callback();
  54. },
  55. fail: function () {
  56. app.globalData.serverUrl = app.globalData.serverUrlServer;
  57. callback();
  58. },
  59. });
  60. }
  61. else {
  62. app.globalData.serverUrl = app.globalData.serverUrlServer;
  63. callback();
  64. }
  65. }
  66. function shareGroup(shareTickets, isShared) {
  67. // 转发成功
  68. console.log("shareTickets:" + shareTickets);
  69. if (shareTickets && shareTickets.length > 0) {
  70. var that = this;
  71. for (var i = 0; i < shareTickets.length; i++) {
  72. wx.getShareInfo({
  73. shareTicket: shareTickets[i],
  74. success: function (groupInfo) {
  75. wx.login({
  76. success: function (res0) {
  77. console.log("groupInfo:" + groupInfo);
  78. that.postData('AddSpatialIntelligenceGroup', {
  79. Code: res0.code,
  80. UserID: app.globalData.userInfo.UserID,
  81. GroupInfo: groupInfo,
  82. IsShared: isShared,
  83. }, function (data) {
  84. });
  85. }
  86. });
  87. },
  88. fail: function (err) {
  89. console.log(err);
  90. },
  91. });
  92. }
  93. }
  94. }
  95. function payMoney(payType, money, callback) {
  96. console.log(money);
  97. //登录认证
  98. wx.login({
  99. success: function (res) {
  100. if (res.code) {
  101. console.log('获取用户登录态成功!' + res.code);
  102. //预支付
  103. getData('PayLogin?code=' + res.code + '&payType=' + payType + '&money=' + money, function (data) {
  104. if (data && data.timeStamp) {
  105. //微信支付
  106. wx.requestPayment({
  107. 'timeStamp': data.timeStamp.toString(),
  108. 'nonceStr': data.nonceStr,
  109. 'package': data.package,
  110. 'signType': 'MD5',
  111. 'paySign': data.paySign,
  112. 'success': function (res3) {
  113. console.log("success:" + res3);
  114. callback();
  115. },
  116. 'fail': function (err) {
  117. if (err && err.errMsg && err.errMsg.indexOf("fail cancel")) {
  118. }
  119. else {
  120. wx.showToast({
  121. title: '服务器忙,请稍候再试!',
  122. duration: 3000
  123. });
  124. wx.redirectTo({
  125. url: './error',
  126. });
  127. }
  128. }
  129. });
  130. }
  131. });
  132. } else {
  133. console.log('获取用户登录态失败!' + res.errMsg);
  134. }
  135. }
  136. });
  137. }
  138. function getTimeFormat(duration) {
  139. //console.log("duration:" + duration);
  140. var arr = ['', '', '']
  141. if (duration.indexOf("'") > 0)
  142. arr[0] = duration.substring(0, duration.indexOf("'"));
  143. if (duration.indexOf(".") > 0) {
  144. arr[1] = duration.substring(duration.indexOf("'") + 1, duration.indexOf(".") + 1);
  145. arr[2] = duration.substring(duration.indexOf(".") + 1, duration.indexOf('"'));
  146. }
  147. else {
  148. arr[1] = duration.substring(duration.indexOf("'") + 1, duration.indexOf('"'));
  149. }
  150. return arr;
  151. }
  152. function getWindowHeight() {
  153. var height = app.globalData.systemInfo.windowHeight;
  154. //console.log("app.globalData.systemInfo.windowHeight:" + app.globalData.systemInfo.windowHeight * 2);
  155. if (app.globalData.systemInfo.model) {
  156. if (height == 504 && (
  157. app.globalData.systemInfo.model.indexOf("iPhone 6<") >= 0
  158. || app.globalData.systemInfo.model.indexOf("iPhone 7<") >= 0
  159. || app.globalData.systemInfo.model.indexOf("iPhone 6s<") >= 0
  160. || app.globalData.systemInfo.model.indexOf("iPhone 5") >= 0
  161. || app.globalData.systemInfo.model.indexOf("iPhone SE") >= 0
  162. )) {
  163. height = 596;
  164. }
  165. }
  166. height = height * 2;
  167. if (app.globalData.systemInfo.system && app.globalData.systemInfo.system.indexOf("Android") >= 0) {
  168. height = height + 168;
  169. }
  170. else {
  171. height = height + 50;
  172. }
  173. //console.log("height:" + height);
  174. return height;
  175. }
  176. //获取存储数据,若不存在,则获得缺省值。
  177. function getStorageValue(obj, name, defaultStatus, callback) {
  178. wx.getStorage({
  179. key: name,
  180. success: function (res) {
  181. obj.data[name] = res.data;
  182. obj.setData(obj.data);
  183. callback();
  184. },
  185. fail: function (res) {
  186. obj.data[name] = defaultStatus;
  187. obj.setData(obj.data);
  188. callback();
  189. },
  190. });
  191. }
  192. function getDifficulty(num) {
  193. switch (Number(num)) {
  194. case 3:
  195. case 4:
  196. case 5:
  197. return "EASY";
  198. case 6:
  199. case 7:
  200. return "HARD";
  201. case 8:
  202. case 9:
  203. return "SUPER";
  204. }
  205. }
  206. function getProgramList() {
  207. return [
  208. {
  209. id: 89,
  210. Name:"数学口算",
  211. appId: 'wx46a7b4c420e6d38f',
  212. path: 'pages/index/start?SourceID=' + app.globalData.ProgramID,
  213. imageName:"universalpic_product_kousuan_icon_size01",
  214. },
  215. {
  216. id: 98,
  217. Name: "语文汉语拼音",
  218. appId: 'wx331e8dd070f01d0e',
  219. path: 'pages/index/index?SourceID=' + app.globalData.ProgramID,
  220. imageName: "universalpic_product_pinyin_icon_size01",
  221. },
  222. {
  223. id: 106,
  224. Name: "语文识字",
  225. appId: 'wx313a8f2c0741efe1',
  226. path: 'pages/index/index?SourceID=' + app.globalData.ProgramID,
  227. imageName: "universalpic_product_shizi_icon_size01",
  228. },
  229. {
  230. id: 99,
  231. Name: "英语自然拼读",
  232. appId: 'wxb54a6d5aff836ee3',
  233. path: 'pages/index/index?SourceID=' + app.globalData.ProgramID,
  234. imageName: "universalpic_product_phonics_icon_size01",
  235. },
  236. {
  237. id: 90,
  238. Name: "注意力游戏",
  239. appId: 'wxa5e33c61fe37dd01',
  240. path: 'pages/index/index?SourceID=' + app.globalData.ProgramID,
  241. imageName: "universalpic_product_zhuyili_icon_size01",
  242. },
  243. {
  244. id: 127,
  245. Name: "空间智力游戏",
  246. appId: 'wxaa1a8b48d9853872',
  247. path: 'pages/index/game?SourceID=' + app.globalData.ProgramID,
  248. imageName: "universalpic_product_kongjian_icon_size01",
  249. },
  250. ];
  251. }
  252. function gotoFeedback() {
  253. wx.navigateToMiniProgram({
  254. appId: "wx80059777521b897c",
  255. path: "pages/index/feedback",
  256. extraData: {
  257. },
  258. success(res) {
  259. // 打开成功
  260. }
  261. });
  262. }
  263. function playAudio(ctx, sentence, player) {
  264. var url = app.globalData.audioUrlBaidu;
  265. url = url.replace("[token]", app.globalData.BaiduToken);
  266. url = url.replace("[word]", sentence);
  267. if (player)
  268. url = url.replace("per=4", "per="+player);
  269. //console.log(url);
  270. ctx.setSrc(url);
  271. ctx.play();
  272. }
  273. function playSound(file){
  274. const innerAudioContext = wx.createInnerAudioContext()
  275. innerAudioContext.autoplay = true
  276. innerAudioContext.src = file
  277. innerAudioContext.onPlay(() => {
  278. //console.log('开始播放')
  279. })
  280. }
  281. module.exports = {
  282. getData: getData,
  283. postData: postData,
  284. shareGroup: shareGroup,
  285. payMoney: payMoney,
  286. getLocalHost: getLocalHost,
  287. getTimeFormat: getTimeFormat,
  288. getWindowHeight: getWindowHeight,
  289. getStorageValue: getStorageValue,
  290. getDifficulty: getDifficulty,
  291. getProgramList: getProgramList,
  292. gotoFeedback: gotoFeedback,
  293. playAudio: playAudio,
  294. playSound: playSound,
  295. }