@mohun/uni-weixin
v1.0.3
Published
Method library generated by Uniapp combined with WeChat mini program.
Maintainers
Readme
uni-weixin
文档请参考:微信官方文档 uni-app官方文档
API
Date: 2017-11-23
Author: liumouliang
new API(getRequeData, getSuccessData)
| Param | Type | Description | | --- | --- | --- | | getRequeData | function | 请求处理函数 | | getSuccessData | function | 成功处理函数 |
Example
//封装
export const getApiUrl = function(route){
return APIURL[route] || route;
};
const apis = new API({
getApiUrl,
isLoading, //加载动画
getRequeData:function(requedata,hasSessionId){
if(hasSessionId){
requedata['appId'] = APP_ID;
requedata['token'] = getCookie('mpToken');
};
return requedata;
},
getSuccessData:function(res,resolve, reject){
if(res.data){
let data = res.data;
if(data.code===200 && data.success) resolve(data.data);
else reject(data);
}else reject(res);
}
});
export const $request = apis.$request
export const $get = apis.$get;
export const $post = apis.$post;
//使用
this.$post('family/save',params).then(data => {
wxToast('保存成功');
if(data.familyId) this.familyId = data.familyId;
this.wxNavito(data.id);
}).catch((err)=>{
this.saveLoading = false;
wxToast(err.info || '保存失败');
});_request(url, requedata, method, apiObj) ⇒ Promise
Date: 2017-11-23
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | url | String | 请求地址 | | requedata | Object | 请求数据 | | method | String | 默认get | | apiObj | Object | 微信API |
Example
// 文档参考: https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html
_request(url, requedata = {}, method = 'GET', apiObj = {})_downloadFile(url, apis) ⇒ Promise
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | url | String | | apis | Object |
Example
_downloadFile(url)setRemoteImgInfo(data, callback)
Date: 2021-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | data | Object | Array | String | 处理数据 | | callback | function | 成功回调 |
Example
export const setRemoteImgInfo = function (...arg) {
// 添加缓存记录
// if(!window.$$global.RemoteImgInfo) window.$$global.RemoteImgInfo = {}
// let RemoteImgInfo = window.$$global.RemoteImgInfo;
return _for(arg, (_) => _setRemoteImgInfo(_));
};
setRemoteImgInfo({'err':'https://'})
setRemoteImgInfo(["https://"])
setRemoteImgInfo("https://")uploadImage(tempFilePaths, _uploadFile) ⇒ Promise
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | tempFilePaths | Array | 本地路径 | | _uploadFile | function | 上传方法 |
Example
// 只适用于服务器一张张上传的情况,
// 多文件参数的可以直接chooseImage后赋值就行
export const _uploadFile = function uploadFile(filePath) {
return new Promise((resolve, reject) => {
return uni.uploadFile({
url: `${API_PATH}/storage/upload`,
filePath: filePath,
name: "file",
header: {
// "Content-Type": "multipart/form-data",
},
success: (res) => {
const _res = _toJson(res.data);
if (_res.errno == 0) {
const url = _res.data.url;
if (url) {
resolve(url);
} else {
warning('接口上传错误-解析图片url失败,',_res.data);
reject(_res);
}
}
},
fail: (err) => {
warning('上传文件接口错误');
reject(err);
},
});
});
};Locals : Object
get(key) ⇒ *
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | key | String |
Example
Locals.get()set(key, value)
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | key | String | | value | * |
Example
Locals.setremove(key) ⇒ *
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | key | String |
Example
Locals.removeclear(key) ⇒ *
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | key | String |
Example
Locals.clearkeys() ⇒ Object
Date: 2022-12-22
Author: liumouliang
Example
Locals.info()local(key, value) ⇒ *
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | key | String | | | value | * | 值 |
Example
local(key, null)stopLocationInfo() ⇒ any
Date: 2017-11-23
Author: liumouliang
Example
stopLocationInfo()startLocationInfo(callBack, type) ⇒ any
Date: 2017-11-23
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | callBack | function | 回调 | | type | String | 定位类型 |
Example
startLocationInfo(()=>{})getLocationInfo(callBack, type) ⇒ any
Date: 2017-11-23
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | callBack | function | 回调 | | type | String | 定位类型 |
Example
startLocationInfo() {
const authInfo = setGlobalAuthInfo(authKey);
if(authInfo && authInfo.longitude && authInfo.latitude) return this._startLocationInfo(authInfo)
return getLocationInfo((res, flag) => {
if (flag) {
setGlobalAuthInfo((authInfo)=>{
authInfo[authKey] = res;
return authInfo;
})
return this._startLocationInfo(res);
} else {
this._reset();
return tipMassage("请设置微信获取定位权限");
}
});
},logManager : Object
Example
this.info()
this.warn()
this.error()
this.setMsg() // 从基础库2.7.3开始支持
this.addMsg() // 从基础库2.8.1开始支持setLog(tag, arg)
Date: 2017-11-23
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | tag | String | 自定义使用日志的类型 | | arg | Array | 多个自定义参数 |
Example
setLog('warn', '发布发生异常=== ' + code, statusCode[code]);trace() ⇒ null
Date: 2017-11-23
Author: liumouliang
Example
//调试+正常日志
trace(arguments)warning() ⇒ null
Date: 2017-11-23
Author: liumouliang
Example
//调试+错误日志
warning(arguments)SceneType : Object
getSceneKey(scene) ⇒ String
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | scene | String | Number |
Example
getSceneKey(1007)getSceneDesc(scene) ⇒ String
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | scene | String | Number |
Example
getSceneDesc(1007)isFromShare(scene) ⇒ Boolean
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | scene | String | Number |
Example
isFromShare(1007)isFromCode(scene) ⇒ Boolean
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | scene | String | Number |
Example
isFromCode(1007)isFromOfficial(scene) ⇒ Boolean
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | scene | String | Number |
Example
isFromOfficial(1007)isFromChannel(scene) ⇒ Boolean
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | scene | String | Number |
Example
isFromChannel(1007)isFromMiniProgram(scene) ⇒ Boolean
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | scene | String | Number |
Example
isFromMiniProgram(1007)wxClipData(data) ⇒ Promise
Date: 2017-11-23
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | data | String | 复制内容 |
Example
// 隐私 = 读取你的剪切板
// https://developers.weixin.qq.com/miniprogram/dev/api/device/clipboard/wx.setClipboardData.html
wxClipData(data)_saveImageToPhotosAlbum(files) ⇒ Promise
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | files | File | 临时文件对象 |
Example
_saveImageToPhotosAlbum(data).then()wxSaveImage(files) ⇒ Promise
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | files | Object | String | 远程链接 || 本地文件{tempFilePath} |
Example
wxSaveImage()_showShareImageMenu(files) ⇒ Promise
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | files | Object |
Example
_showShareImageMenu(files)wxShareImag(files) ⇒ Promise
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | files | Object | String |
Example
wxShareImag(url)_uploadFile(filePath) ⇒ Promise
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | filePath | File |
Example
_uploadFile(filePath)_uploadPhoto(tempFilePaths) ⇒ Promise
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | tempFilePaths | * | 临时文件路径 |
Example
_uploadPhoto(tempFilePaths)uploadPhoto(count, apis, chooseFN) ⇒ Promise
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | count | Number | | | apis | Object | | | chooseFN | function | 选择后处理函数 |
Example
uploadPhoto(3)setInitEnvExternal(apis, [externalKey])
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | apis | Object | | [externalKey] | String |
Example
setInitEnvExternal({}, '')setStorage(key, [value])
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | key | String | | [value] | * |
Example
setStorage()getTempStorage(tempKey, idKey, targetValue) ⇒ *
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | tempKey | String | | idKey | String | | targetValue | * |
Example
[1,2,3,{"id":"77"}]
console.log('getTempStorage',getTempStorage('lml','3.id', 77));getOneObjData(obj, resInfo) ⇒ Object
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | obj | Object | | resInfo | Object |
Example
getOneObjData()numToChinese(num) ⇒ String
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | num | Number |
Example
numToChinese(123)getSettledValue(data, key) ⇒ *
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | data | * | | key | String |
Example
this.setData_userInfo(this.$comApi.getSettledValue(data, 'userInfo'));setConfigSystemInfo(systemInfo)
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | systemInfo | Object |
Example
setConfigSystemInfo(systemInfo)setConfigPlatform(platform)
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | platform | String |
Example
setConfigPlatform('web')setConfigLogId(logId)
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | logId | String |
Example
setConfigLogId();setConfigConfirmColor(confirmColor)
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | confirmColor | String |
Example
setConfigConfirmColor(COLORS.red);getRequestData(res, key) ⇒ Object
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | res | Object | | key | String |
Example
getRequestData(res)parseSceneParams(sceneStr, keyName) ⇒ *
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | sceneStr | String | 二维码参数 | | keyName | String | 读取的参数 |
Example
shareUserId = parseSceneParams(scene, 'shareUserId')getPageUrl(urlKey) ⇒ String
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | urlKey | String |
Example
getPageUrl('home')imgSrc(url, defaultKey) ⇒ String
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | url | String | | | defaultKey | String | 默认图片 |
Example
imgSrc('', 'avatarUrl')setObjImgSrc(objData)
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | objData | Object |
Example
const icons = {
gender: [,'88u6b4ulrecz1w6y7dw8.png','hn8a6b7t0b5fidgmfhft.png'],
id: [,'bl2le16tftus1mxwgk29.png','i5r4ugsjd8hka9eqcaxx.png'],
refresh: '1wle1rkmcuj78l2xxifg.png',
};
setObjImgSrc(icons);getErrMsg(err, defaultInfo) ⇒ String
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | err | Object | | | defaultInfo | String | 默认错误信息 |
Example
let errMsg = this.$comApi.getErrMsg(err, '验证码发送失败');setInitEnv(newConfig, external)
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | newConfig | Object | 新config信息 | | external | Object | 额外配置信息 |
Example
setInitEnv()judge(outModel) ⇒ Promise
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | outModel | String | 额外参数 |
Example
wxapp-微信小程序
aliapp-支付宝小程序
web-纯浏览器
webview
增加参数outModel,默认不传为最细化,传入情况下isInapp为需要检测是否为app内的webview,isWeb为最宽松判断只判断是否打包时就是H5
参数payEnv,调用支付渠道时需要时传入,判断是否为浏览器的环境,如在ios和android的webview环境中返回相应的字段,在手机或者pc端的浏览器真机模拟中返回为h5,不传返回为webcheckToPath(url, isExit) ⇒ String
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | url | String | | | isExit | Boolean | 是否退出当前页面 |
Example
checkToPath(url);getToPath(url, isPage, params) ⇒ String
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | url | String | | | isPage | Boolean | 是否优先查询配置页面 | | params | Object | 页面参数 |
Example
url = getToPath(url, isPage, params);to(url, isPage, params) ⇒ *
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | url | String | | isPage | Boolean | | params | Object |
Example
to()rdt(url, isPage, params) ⇒ *
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | url | String | | isPage | * | | params | Object |
Example
rdt()closeNavPages(url, params, delta) ⇒ *
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | url | String | | | params | Object | | | delta | Number | [description] |
Example
closeNavPages('inviteDetail',{
id: data,
});_setUserInfo(data)
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | data | Object |
Example
_setUserInfo();setUserInfo() ⇒ Promise
Date: 2022-12-22
Author: liumouliang
Example
setUserInfo();getUserInfo() ⇒ Promise
Date: 2022-12-22
Author: liumouliang
Example
getUserInfo();getSceneParamKey(options, nameKey) ⇒ String
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | options | Object | 页面参数 | | nameKey | String | |
Example
# 缓存页面传参,如果没有就置空
# 销毁场景:
经测试,从不同的分享进入不会触发onLaunch事件
getSceneParamKey(options, 'shareUserId')longpress_saveImage(imgSrc, title) ⇒ Promise
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | imgSrc | String | 远程图片地址 | | title | String | 文件名 |
Example
longpress_saveImage()_default
toLogin(params, isExit, loginPage) ⇒ *
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | params | Object | | | isExit | Boolean | | | loginPage | String | 登录地址 |
Example
toLogin();exitLogin(backData, isExit) ⇒ Promise
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | backData | * | 退出地址 | | isExit | Boolean | |
Example
exitLogin(1);$request(args) ⇒ *
Date: 2022-12-22
Author: liumouliang
| Param | Type | Description | | --- | --- | --- | | args | * | 请求参数 |
Example
$request();isPageInstance(vm) ⇒ Boolean
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | vm | * |
Example
const testCases = [
{ $options: { mpType: 'page', __file: 'src/pages/home.vue' }, $route: {} }, // true
{ $options: { mpType: 'component', __file: 'src/components/header.vue' }, $route: {} }, // false
{ $options: { __file: 'src/pages/user/index.vue' }, $route: {} }, // true
{ $options: { __file: 'src\\pages\\login.vue' }, $route: {} }, // true
{ $options: { __file: 'pages/settings.vue' }, $route: {} }, // true
{ $options: { __file: 'components/button.vue' }, $route: {} }, // false
{ $options: { mpType: 'page' } }, // true
{ $options: { mpType: 'component' } }, // false
{ $options: { __file: 'src/views/home.vue' }, $route: {} }, // false
{ $route: {} }, // false
{}, // false
];
testCases.forEach((testCase, index) => {
console.log(`Test case ${index + 1}:`, isPageInstance(testCase));
});navigateBack(data, errKey) ⇒ Promise
Date: 2022-12-22
Author: liumouliang
| Param | Type | | --- | --- | | data | Number | | errKey | String |
Example
navigateBack()