| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- //定义一个全局变量
- window.WeChat = {};
- //微信登录注册调用
- WeChat.onRegisterUser = function (_userinfo, _callback, _callbackFail) {
- if (typeof wx === 'undefined') {
- return;
- }
- wx.showToast({
- title: '',
- icon: 'loading',
- duration: 2000,
- mask: true
- })
- wx.cloud.callFunction({
- // 云函数名称
- name: 'onLogin',
- // 传给云函数的参数
- data: {
- userinfo: _userinfo,
- },
- success: res => {
- console.log('登录回调:', res)
- wx.hideToast();
- if (_callback)
- _callback(res);
- },
- fail: err => {
- if (_callbackFail)
- _callbackFail(err);
- console.error(err);
- wx.hideToast();
- }
- })
- }
- //右上角微信自带按钮吊起分享
- WeChat.onRightUpShare = function () {
- if (typeof wx === 'undefined') {
- return;
- }
- //设置显示转发按钮
- wx.showShareMenu({
- withShareTicket: true
- })
- let _title, _imageUrl;
- wx.cloud.callFunction({
- name: 'onGetSharedInfo',
- success(res) {
- //获取数据成功后
- console.log('右上角调用,获取分享对应的信息:', res.result);
- _title = res.result.title.title;
- _imageUrl = res.result.picture.url;
- },
- fail: console.error
- })
- wx.onShareAppMessage(() => ({
- title: _title,
- imageUrl: _imageUrl
- }))
- }
- //主动吊起调起分享页面
- WeChat.onShareFunction = function () {
- if (typeof wx === 'undefined') {
- return;
- }
- wx.cloud.callFunction({
- name: 'onGetSharedInfo',
- success(res) {
- //获取数据成功后
- console.log('获取分享对应的信息:', res.result)
- let _title = res.result.title.title;
- wx.shareAppMessage({
- title: _title,
- imageUrl: res.result.picture.url,
- });
- },
- fail: console.error
- })
- }
- //上传文件
- WeChat.onUploadFile = function () {
- if (typeof wx === 'undefined') {
- return;
- }
- wx.cloud.uploadFile({
- cloudPath: 'example.png', // 上传至云端的路径
- filePath: '', // 小程序临时文件路径
- success: res => {
- // 返回文件 ID
- console.log(res.fileID)
- },
- fail: console.error
- })
- }
- //下载文件
- WeChat.onGetPlayButtonPicture = function (_callback, _callbackFail) {
- if (typeof wx === 'undefined') {
- return;
- }
- //云开发id
- const fileList = ['cloud://yichael-c6557e.7969-yichael-c6557e/pictures/PlayButtonnew.png']
- wx.cloud.getTempFileURL({
- fileList,
- success: res => {
- if (_callback)
- _callback(res.fileList[0]);
- },
- fail: err => {
- // handle error
- if (_callbackFail)
- _callbackFail(err);
- }
- })
- }
- //拿到搜索的人物信息
- WeChat.onSearchRoles = function (callback) {
- if (typeof wx === 'undefined') {
- return;
- }
- wx.cloud.callFunction({
- name: 'search',
- success(res) {
- //获取数据成功后
- console.log('搜索到的人物信息:', res.result);
- callback(res.result);
- },
- fail: console.error
- })
- }
- //保存游戏数据上服务器
- WeChat.onAddGameData = function (_gamedata) {
- if (typeof wx === 'undefined') {
- return;
- }
- wx.cloud.callFunction({
- // 云函数名称
- name: 'onAddGameData',
- // 传给云函数的参数
- data: {
- gamedata: _gamedata,
- },
- success(res) {
- console.log('保存游戏数据后回调:', res)
- },
- fail: console.error
- })
- }
- //获取游戏数据
- WeChat.onGetGameData = function (_callback) {
- if (typeof wx === 'undefined') {
- return;
- }
- wx.cloud.callFunction({
- // 云函数名称
- name: 'onGetGameData',
- // 传给云函数的参数
- data: {},
- success(res) {
- console.log('获取游戏数据后回调:', res)
- if (_callback)
- _callback(res);
- },
- fail: console.error
- })
- }
- //创建一个游戏视频
- WeChat.onCreateWXVideo = function (_url) {
- if (typeof wx === 'undefined') {
- return;
- }
- const w = wx.getSystemInfoSync().screenWidth
- const h = wx.getSystemInfoSync().screenHeight
- let video = wx.createVideo({
- x: 0,
- y: 0,
- width: w,
- height: h,
- src: _url,
- autoplay: true,
- // objectFit: 'cover'
- });
- return video;
- }
|