PlatformModel.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import Gamecfg from "../../common/gameCfg";
  2. import { gameMethod } from "../../common/gameMethod";
  3. import { TaobaoAddAction, TaobaoAddActionPram, UserWechatSq, UserWechatSqPram } from "../../common/Xyc";
  4. import Config from "../../Config";
  5. import UIHelp from "../../logic/ui/UIHelp";
  6. import PlatformBase from "../../plat/PlatformBase";
  7. import PlatformDianHunWeChat from "../../plat/PlatformDianHunWeChat";
  8. import PlatformFenYouH5 from "../../plat/PlatformFenYouH5";
  9. import PlatformHaiTuoH5 from "../../plat/PlatformHaiTuoH5";
  10. import PlatformHaiTuoMini from "../../plat/PlatformHaiTuoMini";
  11. import PlatformLocal from "../../plat/PlatformLocal";
  12. import PlatformQuickH5 from "../../plat/PlatformQuickH5";
  13. import PlatformTaoBao from "../../plat/PlatformTaoBao";
  14. import PlatformWan17 from "../../plat/PlatformWan17";
  15. import PlatformWan17H5 from "../../plat/PlatformWan17H5";
  16. import PlatformWan17MiniGame from "../../plat/PlatformWan17MiniGame";
  17. import PlatformXXGame from "../../plat/PlatformXXGame";
  18. import PlatformZhilingH5 from "../../plat/PlatformZhilingH5";
  19. import { I18n } from "../../utils/I18nUtil";
  20. import WechatUserBtn from "../../utils/WechatUserBtn";
  21. import { GameErrCode, InGame, PlatformType, PlatUserInfo } from "../const/TypeConst";
  22. import GameDataCenter from "../GameDataCenter";
  23. import IDataModel from "../../frameWork/model/IDataModel";
  24. export default class PlatformModel extends IDataModel {
  25. instance: PlatformBase
  26. type: PlatformType
  27. queryparams: {
  28. inviter: string // 邀请者uuid
  29. sharer: string // 分享者uuid
  30. scene: number // 微信场景值
  31. } = { inviter: "", sharer: "", scene: 1000 }
  32. hasGetWechatUserInfo: boolean = false;
  33. constructor() {
  34. super('platform');
  35. console.log("=====pid=====", Config.pid)
  36. let packageCfg = Gamecfg.packageInfo.getItem(Config.pid)
  37. this.type = <PlatformType>packageCfg.plat
  38. switch (packageCfg.plat) {
  39. case PlatformType.zhiling:
  40. this.instance = new PlatformZhilingH5()
  41. break;
  42. case PlatformType.fenyou:
  43. this.instance = new PlatformFenYouH5()
  44. break;
  45. case PlatformType.haituoH5:
  46. this.instance = new PlatformHaiTuoH5()
  47. break;
  48. case PlatformType.haituoMini:
  49. this.instance = new PlatformHaiTuoMini()
  50. break;
  51. case PlatformType.DHWeChat:
  52. this.instance = new PlatformDianHunWeChat()
  53. break;
  54. case PlatformType.bth5:
  55. this.instance = new PlatformQuickH5()
  56. break;
  57. case PlatformType.wan17H5:
  58. this.instance = new PlatformWan17H5()
  59. break;
  60. case PlatformType.taobao:
  61. this.instance = new PlatformTaoBao()
  62. break;
  63. case PlatformType.wan17:
  64. this.instance = new PlatformWan17()
  65. break;
  66. case PlatformType.wan17MiniGame:
  67. this.instance = new PlatformWan17MiniGame()
  68. break;
  69. case PlatformType.XXGame:
  70. this.instance = new PlatformXXGame()
  71. break;
  72. default:
  73. this.instance = new PlatformLocal()
  74. break;
  75. }
  76. }
  77. //微信获取个人信息
  78. checkUserInfo(type: string, node: fgui.GObject, cb: Function) {
  79. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  80. return
  81. }
  82. if (this.instance.hasAuthUserInfo) {
  83. console.log("已授权")
  84. // 已授权,如果是初次,获取一次用户信息
  85. // 如果不是首次,则不做任何事情
  86. if (!this.hasGetWechatUserInfo) {
  87. console.log("获取用户信息111")
  88. this.hasGetWechatUserInfo = true
  89. // setTimeout(()=>{
  90. this.getWechatUserInfo()
  91. // }, 100)
  92. }
  93. } else {
  94. // 未授权,弹出按钮
  95. console.log("未授权,弹出按钮")
  96. // setTimeout(()=>{
  97. this.createUserInfoBtn(type, node, cb)
  98. // }, 100)
  99. }
  100. }
  101. userInfoBtnList: any = {}
  102. hasClickUserInfoBtn: boolean = false // 是否点击过了
  103. // 创建授权个人信息按钮
  104. createUserInfoBtn(type: string, node: fgui.GObject, cb: Function) {
  105. if (this.hasClickUserInfoBtn) {
  106. return
  107. }
  108. // if (this.userInfoBtn) {
  109. // this.userInfoBtn.show()
  110. // return
  111. // }
  112. // let param = uiCommon.getWxBtnPos(this.ui.btnUser) // this.getWxBtnParam(this.ui.btnUser)
  113. let userInfoBtn = WechatUserBtn.create(node, () => {
  114. this.hasClickUserInfoBtn = true
  115. this.removeAllUserBtn()
  116. cb && cb()
  117. })
  118. this.userInfoBtnList[type] = userInfoBtn
  119. }
  120. removeAllUserBtn() {
  121. for (const key in this.userInfoBtnList) {
  122. if (Object.prototype.hasOwnProperty.call(this.userInfoBtnList, key)) {
  123. this.userInfoBtnList[key].hide();
  124. }
  125. }
  126. }
  127. removeUserBtn(type: string) {
  128. this.userInfoBtnList[type]?.hide()
  129. }
  130. // 从微信平台获取个人信息
  131. getWechatUserInfo() {
  132. if (cc.sys.platform != cc.sys.WECHAT_GAME) { return }
  133. // 用户已授权
  134. GameDataCenter.plat.instance.hasAuthUserInfo = true
  135. wx.getUserInfo({
  136. lang: "zh_CN",
  137. success: function (res) {
  138. GameDataCenter.plat.instance.reportUserInfo(res)
  139. if (Config.inGame >= InGame.home) {
  140. // 在游戏内部时才触发储存
  141. GameDataCenter.plat.sendWxSq({
  142. nickName: res.userInfo.nickName,
  143. avatarUrl: res.userInfo.avatarUrl
  144. })
  145. // huosdk 需要上传用户信息
  146. console.log("接收到了信息:", res.userInfo.nickName, res.userInfo.avatarUrl)
  147. }
  148. },
  149. fail: function () { }
  150. })
  151. }
  152. // 微信授权后保存名字和头像到后端
  153. sendWxSq(platUserInfo: PlatUserInfo) { // @TODO 抖音平台也用这个?
  154. if (cc.sys.platform != cc.sys.WECHAT_GAME) { return }
  155. let param: UserWechatSqPram = { nickname: platUserInfo.nickName, headimgurl: platUserInfo.avatarUrl }
  156. console.log("通知储存用户数据")
  157. this.send(UserWechatSq.url, param)
  158. //保存微信头像地址
  159. // let paramWxHead: UserSetWxheadPram = { str: platUserInfo.avatarUrl }
  160. // this.send(UserSetWxhead.url, paramWxHead)
  161. }
  162. showErrDialog(errCode: GameErrCode) {
  163. UIHelp.ShowDialog({
  164. content: I18n.getI18nText("PlatformModel_showtips_1", errCode),
  165. onlyConfirm: true,
  166. cbConfirm: () => {
  167. GameDataCenter.plat.instance.restart()
  168. }
  169. })
  170. }
  171. sendTBActionId() {
  172. let param: TaobaoAddActionPram = {
  173. }
  174. this.send(TaobaoAddAction.url, param, () => {
  175. })
  176. }
  177. shareAppMessage(params: { [key: string]: string }): void {
  178. if (cc.sys.platform == cc.sys.WECHAT_GAME) {
  179. // let param: UserGetFxPram = {}
  180. // GameController.network.send(UserGetFx.url, param, (result: SevBack) => {
  181. // if (result.type == 1 && result.fenxiang) {
  182. // let qurey = ""
  183. // for (const key in params) {
  184. // if (qurey.length > 0) {
  185. // qurey += "&"
  186. // }
  187. // qurey += key + "=" + params[key]
  188. // }
  189. // let userSDKVersion = wx.getAppBaseInfo().SDKVersion
  190. // console.log("---分享参数:", result.fenxiang.newtupian)
  191. // if (GameDataCenter.plat.compareWxVersion(userSDKVersion, "2.4.3") < 0 || gameMethod.isEmpty(result.fenxiang.newtupian)) {
  192. // console.log("版本低了", userSDKVersion, "2.4.3")
  193. // wx.shareAppMessage({
  194. // title: result.fenxiang.wenan, // 标题会导致场景值变为1044
  195. // imageUrl: result.fenxiang.tupian, // 比例为5:4
  196. // query: qurey
  197. // })
  198. // } else {
  199. // console.log("版本高,可高级分享", userSDKVersion, "2.4.3")
  200. // wx.shareAppMessage({
  201. // title: result.fenxiang.wenan, // 标题会导致场景值变为1044
  202. // imageUrlId: result.fenxiang.newtupian[0],
  203. // imageUrl: result.fenxiang.newtupian[1],
  204. // query: qurey
  205. // })
  206. // }
  207. // console.log("分享上报")
  208. // if (!gameMethod.isEmpty(window["huoSdk"])) {
  209. // window["huoSdk"].addShareInfo({
  210. // data: {
  211. // to_target: 'wx'
  212. // }
  213. // }).then(res => {
  214. // console.log("分享结果:", res)
  215. // })
  216. // }
  217. // GameDataCenter.plat.instance.resportShareEvent()
  218. // }
  219. // })
  220. }
  221. }
  222. // wx版本对比专用 [玩家系统版本,指定版本] 1:玩家版本高 -1玩家版本低 0一样
  223. compareWxVersion(v1, v2): number {
  224. v1 = v1.split('.')
  225. v2 = v2.split('.')
  226. const len = Math.max(v1.length, v2.length)
  227. while (v1.length < len) {
  228. v1.push('0')
  229. }
  230. while (v2.length < len) {
  231. v2.push('0')
  232. }
  233. for (let i = 0; i < len; i++) {
  234. const num1 = parseInt(v1[i])
  235. const num2 = parseInt(v2[i])
  236. if (num1 > num2) {
  237. return 1
  238. } else if (num1 < num2) {
  239. return -1
  240. }
  241. }
  242. return 0
  243. }
  244. //是否能调用微信的重启
  245. canRestartApp(): boolean {
  246. let sysInfo = wx.getSystemInfoSync();
  247. let SDKVersion = sysInfo.SDKVersion;
  248. return this.compareWxVersion(SDKVersion, "2.22.1") >= 0;
  249. }
  250. }