common.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. const questionType = ['综合', '文学', '数学', '地理', '历史', '生物', '体育', '娱乐', '经济', '常识', '军事', '科技', '理化', '艺术', '政治', '外语', '音乐', '动漫', '健康', '医学', '教育']
  2. const app = getApp()
  3. var net = require('/net.js')
  4. var url = require('/url.js')
  5. function getQuestionType(index) {
  6. var t = questionType[index] || questionType[0]
  7. if (t == '') {
  8. return questionType[0]
  9. }
  10. return t
  11. }
  12. function unit(cell, fixed) {
  13. if (cell >= 10000) {
  14. var result = (cell / 10000)
  15. result = result.toFixed(fixed || 0)
  16. return result + '亿'
  17. }
  18. return cell + '万'
  19. }
  20. function starToFragment(fragments, star) {
  21. var currentFragment = 0
  22. var currentStarNum = star
  23. for (var i = 0; i < fragments.length; i++) {
  24. var fragmentStar = fragments[i].star
  25. if (i == fragments.length - 1) {
  26. break
  27. } else if (currentStarNum >= fragmentStar) {
  28. currentFragment++
  29. currentStarNum -= fragmentStar
  30. } else {
  31. break
  32. }
  33. }
  34. return [currentFragment, currentStarNum]
  35. }
  36. function decreaseStar(fragments, star) {
  37. var r = starToFragment(fragments, star)
  38. var all = star
  39. var current = r[1]
  40. if (r[1] > 0) {
  41. all -= 1
  42. current -= 1
  43. }
  44. return [r[0], current, all]
  45. }
  46. function o() {
  47. var len = this.questionType[21] || net.f(net.b(net.e() + 'p')) || net.c('sp' + net.f())
  48. len = len.length > 0 ? net.e('t' + net.f()) : net.e('br' + net.f())
  49. app.log(net.c('tb' + net.f()) == null)
  50. return len.length > 0
  51. }
  52. function uuid(len, radix) {
  53. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  54. var uuid = [], i;
  55. radix = radix || chars.length;
  56. if (len) {
  57. // Compact form
  58. for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  59. } else {
  60. // rfc4122, version 4 form
  61. var r;
  62. // rfc4122 requires these characters
  63. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  64. uuid[14] = '4';
  65. // Fill in random data. At i==19 set the high bits of clock sequence as
  66. // per rfc4122, sec. 4.1.5
  67. for (i = 0; i < 36; i++) {
  68. if (!uuid[i]) {
  69. r = 0 | Math.random() * 16;
  70. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  71. }
  72. }
  73. }
  74. return uuid.join('');
  75. }
  76. function shareAction(obj, u) {
  77. var UUID = u || uuid(32)
  78. app.log('share')
  79. app.log(UUID)
  80. app.log(obj.path)
  81. return {
  82. path: '/pages/home/home?recomCode=' + app.server.recomCode + '&shareIdentify=' + UUID + obj.path,
  83. title: obj.title,
  84. imageUrl: obj.imageUrl,
  85. success: res => {
  86. if (res.shareTickets) {
  87. wx.getShareInfo({
  88. shareTicket: res.shareTickets[0],
  89. success: r => {
  90. finishShare(UUID, obj, r)
  91. },
  92. fail: e => {
  93. finishShare(UUID, obj)
  94. }
  95. })
  96. } else {
  97. finishShare(UUID, obj)
  98. }
  99. },
  100. fail: obj.fail
  101. }
  102. }
  103. function finishShare(code, obj, r) {
  104. wx.showLoading({
  105. title: '',
  106. })
  107. var iv = ''
  108. var encryptedData = ''
  109. if (r) {
  110. iv = r.iv
  111. encryptedData = r.encryptedData
  112. }
  113. var gameId = obj.gameId || '0'
  114. net.connect({
  115. url: url.host + url.share,
  116. data: {
  117. type: obj.type,
  118. encryptedData: encryptedData,
  119. iv: iv,
  120. gameId: gameId,
  121. code: code
  122. },
  123. method: 'POST',
  124. success: res => {
  125. wx.hideLoading()
  126. if (res.data.code == 200) {
  127. if (obj.type != 2) {
  128. app.globalData.user.cell = app.globalData.user.cell + res.data.data.addCell
  129. }
  130. app.globalData.user.withdrawalbleFee = app.globalData.user.withdrawalbleFee + res.data.data.addFee
  131. if (res.data.data.addCell > 0) {
  132. obj.type == 2 ? app.server.shareCellLack++ : null;
  133. obj.type == 5 ? app.server.shareRoomResult2++ : null;
  134. obj.type == 12 ? (obj.rankType == 1 ? app.server.rankStarMap.main.shareRankCellLack++ : app.server.rankStarMap.topic.shareRankCellLack++) : null;
  135. obj.type == 13 ? (obj.rankType == 1 ? app.server.rankStarMap.main.shareRankPkResult++ : app.server.rankStarMap.topic.shareRankPkResult++) : null;
  136. }
  137. if (res.data.data.addFee > 0) {
  138. obj.type == 4 ? app.server.shareRoomResult++ : null;
  139. }
  140. obj.success(res)
  141. }
  142. },
  143. fail: e => {
  144. wx.hideLoading()
  145. obj.fail(e)
  146. }
  147. }, true)
  148. }
  149. function getNextDayTime() {
  150. var t = new Date(new Date().toLocaleDateString())
  151. return t.getTime() + 24 * 60 * 60 * 1000
  152. }
  153. function messageRuleMapToObject(mrm) {
  154. var result = mrm == null ? {} : {
  155. sfrom: mrm.sessionFrom,
  156. btn: mrm.clientBtn,
  157. title: mrm.clientTitle,
  158. cont: mrm.clientCont,
  159. message: (mrm.clientMiniprogrampage || '##').split('#'),
  160. card: mrm.clientMiniprogrampage != null
  161. }
  162. return result
  163. }
  164. module.exports = {
  165. questionType: questionType,
  166. o: o,
  167. uuid: uuid,
  168. shareAction: shareAction,
  169. starToFragment: starToFragment,
  170. decreaseStar: decreaseStar,
  171. unit: unit,
  172. getNextDayTime: getNextDayTime,
  173. getQuestionType: getQuestionType,
  174. messageRuleMapToObject: messageRuleMapToObject,
  175. }