GlobalWeChat.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //定义一个全局变量
  2. window.WeChat = {};
  3. //微信登录注册调用
  4. WeChat.onRegisterUser = function (_userinfo, _callback, _callbackFail) {
  5. if (typeof wx === 'undefined') {
  6. return;
  7. }
  8. wx.showToast({
  9. title: '',
  10. icon: 'loading',
  11. duration: 2000,
  12. mask: true
  13. })
  14. wx.cloud.callFunction({
  15. // 云函数名称
  16. name: 'onLogin',
  17. // 传给云函数的参数
  18. data: {
  19. userinfo: _userinfo,
  20. },
  21. success: res => {
  22. console.log('登录回调:', res)
  23. wx.hideToast();
  24. if (_callback)
  25. _callback(res);
  26. },
  27. fail: err => {
  28. if (_callbackFail)
  29. _callbackFail(err);
  30. console.error(err);
  31. wx.hideToast();
  32. }
  33. })
  34. }
  35. //右上角微信自带按钮吊起分享
  36. WeChat.onRightUpShare = function () {
  37. if (typeof wx === 'undefined') {
  38. return;
  39. }
  40. //设置显示转发按钮
  41. wx.showShareMenu({
  42. withShareTicket: true
  43. })
  44. let _title, _imageUrl;
  45. wx.cloud.callFunction({
  46. name: 'onGetSharedInfo',
  47. success(res) {
  48. //获取数据成功后
  49. console.log('右上角调用,获取分享对应的信息:', res.result);
  50. _title = res.result.title.title;
  51. _imageUrl = res.result.picture.url;
  52. },
  53. fail: console.error
  54. })
  55. wx.onShareAppMessage(() => ({
  56. title: _title,
  57. imageUrl: _imageUrl
  58. }))
  59. }
  60. //主动吊起调起分享页面
  61. WeChat.onShareFunction = function () {
  62. if (typeof wx === 'undefined') {
  63. return;
  64. }
  65. wx.cloud.callFunction({
  66. name: 'onGetSharedInfo',
  67. success(res) {
  68. //获取数据成功后
  69. console.log('获取分享对应的信息:', res.result)
  70. let _title = res.result.title.title;
  71. wx.shareAppMessage({
  72. title: _title,
  73. imageUrl: res.result.picture.url,
  74. });
  75. },
  76. fail: console.error
  77. })
  78. }
  79. //上传文件
  80. WeChat.onUploadFile = function () {
  81. if (typeof wx === 'undefined') {
  82. return;
  83. }
  84. wx.cloud.uploadFile({
  85. cloudPath: 'example.png', // 上传至云端的路径
  86. filePath: '', // 小程序临时文件路径
  87. success: res => {
  88. // 返回文件 ID
  89. console.log(res.fileID)
  90. },
  91. fail: console.error
  92. })
  93. }
  94. //下载文件
  95. WeChat.onGetPlayButtonPicture = function (_callback, _callbackFail) {
  96. if (typeof wx === 'undefined') {
  97. return;
  98. }
  99. //云开发id
  100. const fileList = ['cloud://yichael-c6557e.7969-yichael-c6557e/pictures/PlayButtonnew.png']
  101. wx.cloud.getTempFileURL({
  102. fileList,
  103. success: res => {
  104. if (_callback)
  105. _callback(res.fileList[0]);
  106. },
  107. fail: err => {
  108. // handle error
  109. if (_callbackFail)
  110. _callbackFail(err);
  111. }
  112. })
  113. }
  114. //拿到搜索的人物信息
  115. WeChat.onSearchRoles = function (callback) {
  116. if (typeof wx === 'undefined') {
  117. return;
  118. }
  119. wx.cloud.callFunction({
  120. name: 'search',
  121. success(res) {
  122. //获取数据成功后
  123. console.log('搜索到的人物信息:', res.result);
  124. callback(res.result);
  125. },
  126. fail: console.error
  127. })
  128. }
  129. //保存游戏数据上服务器
  130. WeChat.onAddGameData = function (_gamedata) {
  131. if (typeof wx === 'undefined') {
  132. return;
  133. }
  134. wx.cloud.callFunction({
  135. // 云函数名称
  136. name: 'onAddGameData',
  137. // 传给云函数的参数
  138. data: {
  139. gamedata: _gamedata,
  140. },
  141. success(res) {
  142. console.log('保存游戏数据后回调:', res)
  143. },
  144. fail: console.error
  145. })
  146. }
  147. //获取游戏数据
  148. WeChat.onGetGameData = function (_callback) {
  149. if (typeof wx === 'undefined') {
  150. return;
  151. }
  152. wx.cloud.callFunction({
  153. // 云函数名称
  154. name: 'onGetGameData',
  155. // 传给云函数的参数
  156. data: {},
  157. success(res) {
  158. console.log('获取游戏数据后回调:', res)
  159. if (_callback)
  160. _callback(res);
  161. },
  162. fail: console.error
  163. })
  164. }
  165. //创建一个游戏视频
  166. WeChat.onCreateWXVideo = function (_url) {
  167. if (typeof wx === 'undefined') {
  168. return;
  169. }
  170. const w = wx.getSystemInfoSync().screenWidth
  171. const h = wx.getSystemInfoSync().screenHeight
  172. let video = wx.createVideo({
  173. x: 0,
  174. y: 0,
  175. width: w,
  176. height: h,
  177. src: _url,
  178. autoplay: true,
  179. // objectFit: 'cover'
  180. });
  181. return video;
  182. }