https://github.com/gpake/qiniu-wxapp-sdk/blob/master/README.md
//qiniu.js文件
const config = require('../api/config')
const qiniuUploader = require('./lib/qiniuUploader')
// 初始化七牛相关参数
const initQiniu = () => {
var options = {
region: config.REGION,
// bucket 域名,下载资源时用到。如果设置,会在 success callback 的 res 参数加上可以直接使用的 ImageURL 字段。否则需要自己拼接
domain: config.DOMAIN,
// key: 'customFileName.jpg', // [非必须]自定义文件 key。如果不设置,默认为使用微信小程序 API 的临时文件名
// 从指定 url 通过 HTTP GET 获取 uptoken,返回的格式必须是 json 且包含 uptoken 字段,例如: {"uptoken": "[yourTokenString]"}
uptokenURL: config.UPTOKEN,
shouldUseQiniuFileName: true, // 如果是 true,则文件 key 由 qiniu 服务器分配 (全局去重)。默认是 false: 即使用微信产生的 filename
};
return qiniuUploader.init(options)
}
module.exports = {
initQiniu,
qiniuUploader
}
// config.js文件
// export const WEBSOCKET = 'wss://bnbapp.kexin.link/websocket/applet/bnb/' // 正式服
export const WEBSOCKET = 'ws://47.113.91.40:9090/websocket/applet/bnb/' // 测试
// export const WEBSOCKET = 'ws://47.106.66.5:9090/websocket/applet/bnb/' // 开发
export const MINI_PROGRAM_VERSION = '2.5.4'
// -- 七牛云配置
export const REGION = 'SCN' // SCN:华南区,ECN:华东区,NCN:华北区,NA:北美区
export const DOMAIN = 'https://image.kexin.link/'
export const UPTOKEN = 'https://bnbapp.kexin.link/qiniu/uptoken'
export const QQMapKey = '5TCBZ-7LUKW-H7YRF-OKZK2-YB2IF-IKFOZ'
export const BMapKey = 'b1pcAUywRBXTyNmWHGfFTI3nWd3j6yN5'
export const isCanConsole = true
// export const isCanConsole = false
export const KX_ERROR_CODE_40001 = '40001'
export const KX_ERROR_CODE_40002 = '40002'
export const KX_ERROR_CODE_40004 = '40004'
export const KX_ERROR_CODE_10313 = '10313'
export const SCENE_FORM_MSG_CARD = [1007, 1008]
export const SCENE_FORM_QR_CODE = [1011, 1012, 1013]
export const SCENE_FROM_MINI_PROGRAM_QR_CODE = [1047, 1048, 1049]
export const FACE_SUCCESS = '21100'
export const FACE_FAIL = '21101'
export const FACE_FAIL_BY_PHOTO = '21102'
export const FACE_FAIL_BY_SERVICE = '21103'
export const SUCCESS = 'SUCCESS'
export const FAIL = 'FAIL'
export const TIMEOUT = 10000
export const TIP_EMPTY_TEL = '请输入手机号'
export const TIP_EMPTY_NAME = '请输入姓名'
export const TIP_EMPTY_IDCARD = '请输入身份证号'
export const TIP_EMPTY_CAPTCHA = '请输入短信验证码'
export const TIP_ERROR_TEL = '不支持此号码'
export const TIP_ERROR_NAME = '姓名格式错误'
export const TIP_ERROR_IDCARD = '身份证号格式错误'
export const TIP_ERROR_CAPTCHA = '短信验证码格式错误'
export const CHECKIN_SOURCE_CHECKIN_APPLY = 'isCheckInApply'
export const CHECKIN_SOURCE_CHECKIN_SPECAIL = 'isCheckInSpecial'
export const CHECKIN_SOURCE_MSG_SHARE = 'isCheckInShare'
export const CHECKIN_SOURCE_ORDER = 'isCheckInOrder'
export const CHECKIN_SOURCE_RESERVE = 'isCheckInReserve'
export const CHECKIN_SOURCE_RESERVE_AUTOMATIC = 'isReserveAutomatic'
export const CHECKIN_SOURCE_RESERVE_MANUAL = 'isReserveManual'
export const FAULT_FORMAL_DEVICE_ID = [
'13z18c21101001', '13z18c21101002', '13z18c21101003', '13z18c21101004', '13z18c21101005',
'13z18c21101006', '13z18c21101007', '13z18c21101008', '13z18c21101009', '13z18c21101010',
'13z18c21101011', '13z18c21101012', '13z18c21101013', '13z18c21101014', '13z18c21101015',
'13z18c21101016', '13z18c21101017', '13z18c21101018', '13z18c21101019', '13z18c21101020',
'13z18c21101021', '13z18c21101022', '13z18c21101023', '13z18c21101024', '13z18c21101025',
'13z18c21101026', '13z18c21101027', '13z18c21101028', '13z18c21101029', '13z18c21101030',
'13z18c21101031', '13z18c21101032', '13z18c21101033', '13z18c21101034', '13z18c21101035',
'13z18c21101036', '13z18c21101037', '13z18c21101038', '13z18c21101039', '13z18c21101040',
'13z18c21101041', '13z18c21101042', '13z18c21101043', '13z18c21101044', '13z18c21101045',
'13z18c21101046', '13z18c21101047', '13z18c21101048', '13z18c21101049', '13z18c21101050',
'13z18c21101051', '13z18c21101052', '13z18c21101053', '13z18c21101054', '13z18c21101055',
'13z18c21101056', '13z18c21101057', '13z18c21101058', '13z18c21101059', '13z18c21101060',
'13z18c21101061', '13z18c21101062', '13z18c21101063', '13z18c21101064', '13z18c21101065',
'13z18c21101066', '13z18c21101067', '13z18c21101068', '13z18c21101069', '13z18c21101070',
'13z18c21101071', '13z18c21101072', '13z18c21101073', '13z18c21101074', '13z18c21101075',
'13z18c21101076', '13z18c21101077', '13z18c21101078', '13z18c21101079', '13z18c21101080',
'13z18c21101081', '13z18c21101082', '13z18c21101083', '13z18c21101084', '13z18c21101085',
'13z18c21101086', '13z18c21101087', '13z18c21101088', '13z18c21101089', '13z18c21101090',
'13z18c21101091', '13z18c21101092', '13z18c21101093', '13z18c21101094', '13z18c21101095',
'13z18c21101096', '13z18c21101097', '13z18c21101098', '13z18c21101099', '13z18c21101100'
]
// https://github.com/gpake/qiniu-wxapp-sdk/blob/master/README.md
// created by gpake
(function() {
var config = {
qiniuRegion: '',
qiniuImageURLPrefix: '',
qiniuUploadToken: '',
qiniuUploadTokenURL: '',
qiniuUploadTokenFunction: null,
qiniuShouldUseQiniuFileName: false
}
module.exports = {
init: init,
upload: upload,
}
// 在整个程序生命周期中,只需要 init 一次即可
// 如果需要变更参数,再调用 init 即可
function init(options) {
config = {
qiniuRegion: '',
qiniuImageURLPrefix: '',
qiniuUploadToken: '',
qiniuUploadTokenURL: '',
qiniuUploadTokenFunction: null,
qiniuShouldUseQiniuFileName: false
};
updateConfigWithOptions(options);
}
function updateConfigWithOptions(options) {
if (options.region) {
config.qiniuRegion = options.region;
} else {
console.error('qiniu uploader need your bucket region');
}
if (options.uptoken) {
config.qiniuUploadToken = options.uptoken;
} else if (options.uptokenURL) {
config.qiniuUploadTokenURL = options.uptokenURL;
} else if(options.uptokenFunc) {
config.qiniuUploadTokenFunction = options.uptokenFunc;
}
if (options.domain) {
config.qiniuImageURLPrefix = options.domain;
}
config.qiniuShouldUseQiniuFileName = options.shouldUseQiniuFileName
}
function upload(filePath, success, fail, options) {
if (null == filePath) {
console.error('qiniu uploader need filePath to upload');
return;
}
if (options) {
updateConfigWithOptions(options);
}
if (config.qiniuUploadToken) {
doUpload(filePath, success, fail, options);
} else if (config.qiniuUploadTokenURL) {
getQiniuToken(function() {
doUpload(filePath, success, fail, options);
});
} else if (config.qiniuUploadTokenFunction) {
config.qiniuUploadToken = config.qiniuUploadTokenFunction();
if (null == config.qiniuUploadToken && config.qiniuUploadToken.length > 0) {
console.error('qiniu UploadTokenFunction result is null, please check the return value');
return
}
} else {
console.error('qiniu uploader need one of [uptoken, uptokenURL, uptokenFunc]');
return;
}
}
function doUpload(filePath, success, fail, options) {
if (null == config.qiniuUploadToken && config.qiniuUploadToken.length > 0) {
console.error('qiniu UploadToken is null, please check the init config or networking');
wx.hideLoading()
return
}
var url = uploadURLFromRegionCode(config.qiniuRegion);
var fileName = filePath.split('//')[1];
if (options && options.key) {
fileName = options.key;
}
var formData = {
'token': config.qiniuUploadToken
};
if (!config.qiniuShouldUseQiniuFileName) {
formData['key'] = fileName
}
wx.uploadFile({
url: url,
filePath: filePath,
name: 'file',
formData: formData,
success: function (res) {
var dataString = res.data
try {
var dataObject = JSON.parse(dataString);
//do something
var imageUrl = config.qiniuImageURLPrefix + '/' + dataObject.key;
dataObject.imageURL = imageUrl;
console.log(dataObject);
if (success) {
success(dataObject);
}
} catch(e) {
console.log('parse JSON failed, origin String is: ' + dataString)
if (fail) {
fail(e);
}
}
},
fail: function (error) {
console.error(error);
if (fail) {
fail(error);
}
}
})
}
function getQiniuToken(callback) {
wx.request({
url: config.qiniuUploadTokenURL,
success: function (res) {
var token = res.data.uptoken;
if (token && token.length > 0) {
config.qiniuUploadToken = token;
if (callback) {
callback();
}
} else {
console.error('qiniuUploader cannot get your token, please check the uptokenURL or server')
}
},
fail: function (error) {
console.error('qiniu UploadToken is null, please check the init config or networking: ' + error);
}
})
}
function uploadURLFromRegionCode(code) {
var uploadURL = null;
switch(code) {
case 'ECN': uploadURL = 'https://up.qbox.me'; break;
case 'NCN': uploadURL = 'https://up-z1.qbox.me'; break;
case 'SCN': uploadURL = 'https://up-z2.qbox.me'; break;
case 'NA': uploadURL = 'https://up-na0.qbox.me'; break;
default: console.error('please make the region is with one of [ECN, SCN, NCN, NA]');
}
return uploadURL;
}
})();