From 20841e6254c171cbf828a8e0dcc9bb3e11988884 Mon Sep 17 00:00:00 2001 From: luoye <397379429@qq.com> Date: Wed, 30 Aug 2017 15:08:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- littleApp_child/app.json | 7 +- littleApp_child/pages/PersonalInfo/PersonalInfo.js | 25 ++++ .../pages/PersonalInfo/PersonalInfo.json | 7 + .../pages/PersonalInfo/PersonalInfo.wxml | 77 ++++++++++ .../pages/PersonalInfo/PersonalInfo.wxss | 39 +++++ littleApp_child/pages/Setting/Setting.js | 158 +++++++++++++++++++++ littleApp_child/pages/Setting/Setting.json | 6 + littleApp_child/pages/Setting/Setting.wxml | 42 ++++++ littleApp_child/pages/Setting/Setting.wxss | 136 ++++++++++++++++++ littleApp_child/pages/home/home.wxml | 8 +- littleApp_child/pages/home/home.wxss | 2 +- .../pages/hospitalDetail/hospitalDetail.js | 59 -------- .../pages/hospitalDetail/hospitalDetail.json | 6 - .../pages/hospitalDetail/hospitalDetail.wxml | 2 - .../pages/hospitalDetail/hospitalDetail.wxss | 1 - littleApp_child/pages/hospitalList/hospitalList.js | 24 ---- .../pages/hospitalList/hospitalList.json | 7 - .../pages/hospitalList/hospitalList.wxml | 29 ---- .../pages/hospitalList/hospitalList.wxss | 93 ------------ littleApp_child/utils/apiFile.js | 4 + littleApp_child/utils/qiniuUploader.js | 157 ++++++++++++++++++++ 21 files changed, 661 insertions(+), 228 deletions(-) create mode 100644 littleApp_child/pages/PersonalInfo/PersonalInfo.js create mode 100644 littleApp_child/pages/PersonalInfo/PersonalInfo.json create mode 100644 littleApp_child/pages/PersonalInfo/PersonalInfo.wxml create mode 100644 littleApp_child/pages/PersonalInfo/PersonalInfo.wxss create mode 100644 littleApp_child/pages/Setting/Setting.js create mode 100644 littleApp_child/pages/Setting/Setting.json create mode 100644 littleApp_child/pages/Setting/Setting.wxml create mode 100644 littleApp_child/pages/Setting/Setting.wxss delete mode 100644 littleApp_child/pages/hospitalDetail/hospitalDetail.js delete mode 100644 littleApp_child/pages/hospitalDetail/hospitalDetail.json delete mode 100644 littleApp_child/pages/hospitalDetail/hospitalDetail.wxml delete mode 100644 littleApp_child/pages/hospitalDetail/hospitalDetail.wxss delete mode 100644 littleApp_child/pages/hospitalList/hospitalList.js delete mode 100644 littleApp_child/pages/hospitalList/hospitalList.json delete mode 100644 littleApp_child/pages/hospitalList/hospitalList.wxml delete mode 100644 littleApp_child/pages/hospitalList/hospitalList.wxss create mode 100755 littleApp_child/utils/qiniuUploader.js diff --git a/littleApp_child/app.json b/littleApp_child/app.json index f988178..d447062 100644 --- a/littleApp_child/app.json +++ b/littleApp_child/app.json @@ -1,13 +1,12 @@ { "pages": [ - "pages/home/home", - + "pages/Setting/Setting", + "pages/PersonalInfo/PersonalInfo", "pages/checkTabList/checkTabList", "pages/CheckReport/CheckReport", "pages/articleLists/articleLists", - "pages/hospitalList/hospitalList", "pages/SearchPage/SearchPage", @@ -16,7 +15,7 @@ "pages/doctorList/doctorList", "pages/doctor/doctor", - "pages/hospitalDetail/hospitalDetail", + "pages/chatroom/chatroom", "pages/chat/chat", diff --git a/littleApp_child/pages/PersonalInfo/PersonalInfo.js b/littleApp_child/pages/PersonalInfo/PersonalInfo.js new file mode 100644 index 0000000..0fe82f0 --- /dev/null +++ b/littleApp_child/pages/PersonalInfo/PersonalInfo.js @@ -0,0 +1,25 @@ +//hospitalList.js 医院列表 联系医院 + +// 网络请求工具类 +var networkUtil = require('../../utils/network_util.js') + +var api = require('../../utils/apiFile.js') + +// 页码 +var page = 0 +// 地理位置信息 +var latitude = 0 +var longitude = 0 +// 是否有更多 +var hasMore = true + +Page({ + data: { + list:[] + }, + /*---------生命周期--------*/ + onLoad: function () { + + + } +}) diff --git a/littleApp_child/pages/PersonalInfo/PersonalInfo.json b/littleApp_child/pages/PersonalInfo/PersonalInfo.json new file mode 100644 index 0000000..0768c91 --- /dev/null +++ b/littleApp_child/pages/PersonalInfo/PersonalInfo.json @@ -0,0 +1,7 @@ +{ + "navigationBarBackgroundColor": "#48C17B", + "navigationBarTitleText": "儿童档案", + "navigationBarTextStyle": "white", + "backgroundColor": "#d8dbd4", + "enablePullDownRefresh": true +} \ No newline at end of file diff --git a/littleApp_child/pages/PersonalInfo/PersonalInfo.wxml b/littleApp_child/pages/PersonalInfo/PersonalInfo.wxml new file mode 100644 index 0000000..1b7362d --- /dev/null +++ b/littleApp_child/pages/PersonalInfo/PersonalInfo.wxml @@ -0,0 +1,77 @@ + + + + + + {{userData.username}}么么么 + + +分娩信息 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +高危及诊断 + + + + + + + + + + + + + + +院内系统 + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/littleApp_child/pages/PersonalInfo/PersonalInfo.wxss b/littleApp_child/pages/PersonalInfo/PersonalInfo.wxss new file mode 100644 index 0000000..5105a16 --- /dev/null +++ b/littleApp_child/pages/PersonalInfo/PersonalInfo.wxss @@ -0,0 +1,39 @@ +@import '../home/home.wxss'; + +.infoBar_title { + font-size: 12px; + color: #999999; + margin-left: 15px; + height: 24px; + line-height: 24px; + background: rgb(244, 244, 244); +} + +.infoBar_content { + background: white; + display: flex; + flex-wrap: wrap; +} + +.infoBar_content_item { + background: white; + height: 35px; + min-width: 50%; +} + +.infoBar_content_item_one { + width: 100%; +} + +.infoBar_content_item_title { + font-size: 13px; + color: #222222; + line-height: 35px; + margin-left: 15px; +} + +.infoBar_content_item_text { + font-size: 13px; + color: #666; + line-height: 35px; +} \ No newline at end of file diff --git a/littleApp_child/pages/Setting/Setting.js b/littleApp_child/pages/Setting/Setting.js new file mode 100644 index 0000000..1e95d89 --- /dev/null +++ b/littleApp_child/pages/Setting/Setting.js @@ -0,0 +1,158 @@ +//hospitalDetail.js 医院详情介绍 + +// 网络请求工具类 +var networkUtil = require('../../utils/network_util.js') +var api = require('../../utils/apiFile.js') +var qiniuUploader = require('../../utils/qiniuUploader.js') + +Page({ + data: { + setList: [{ icon: '../../source/jianchajilu.png', text: '我的预约', right_text:'',margin:0}, + { icon: '../../source/jianchajilu.png', text: '儿童档案', right_text: '', margin: 10 }, + { icon: '../../source/jianchajilu.png', text: '消息提醒', right_text: '', margin: 0 }, + { icon: '../../source/jianchajilu.png', text: '交易记录', right_text: '', margin: 0 }, + { icon: '../../source/jianchajilu.png', text: '我的收藏', right_text: '', margin: 10 }, + { icon: '../../source/jianchajilu.png', text: '用户反馈', right_text: '', margin: 0 }, + { icon: '../../source/jianchajilu.png', text: '清楚缓存', right_text: '0.2 MB', margin:10} ], + isShowOne: false, + isShowTwo: false, + isShowThree: false, + }, + onLoad: function (option) { + + }, + selectPhoto(){ + var that = this + wx.showActionSheet({ + itemList: ['手机拍照','本地相册'], + success: function (res) { + if (!res.tapIndex) { + return + } + var imgType = 'camera' + if (res.tapIndex == 1) { + imgType = 'album' + } + wx.chooseImage({ + count: 1, // 默认9 + sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 + sourceType: [imgType], // 可以指定来源是相册还是相机,默认二者都有 + success: function (res) { + // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片 + console.log('图片',res) + that.qiniuToken(res.tempFilePaths[0]) + } + }) + }, + fail: function (res) { + console.log(res.errMsg) + } + }) + }, + clickPage(e){ + console.log(e) + var index = e.currentTarget.dataset.index + var url = '' + switch(index){ + case 0: + url = '../PersonalInfo/PersonalInfo' + break + case 1: + url = '../PersonalInfo/PersonalInfo' + break + case 2: + url = '../system_notifacations/system_notifacations' + break + case 3: + url = '../PersonalInfo/PersonalInfo' + break + case 4: + url = '../PersonalInfo/PersonalInfo' + break + case 5: + url = '../PersonalInfo/PersonalInfo' + break + case 6: + + break + } + wx.navigateTo({ + url: url + }) + }, + logout(){ + var that = this + wx.showActionSheet({ + itemList: ['退出当前用户'], + success: function (res) { + if (res.tapIndex == 0) { + wx.setStorageSync('userToken', '') + wx.setStorageSync('userId', '') + getApp().globalData.token = '' + getApp().globalData.userId = '' + that.toLogin() + } + }, + fail: function (res) { + console.log(res.errMsg) + } + }) + }, + uploadImg(upToken,imageUrl){ + var that = this; + console.log(upToken, imageUrl) + // qiniuUploader.upload(imageUrl, (res) => { + // console.log(res) + // that.setData({ + // 'imageURL': res.imageURL, + // }); + // }, (error) => { + // console.log('error: ' + error); + // }, { + // uploadURL: 'https://up.qbox.me', + // domain: 'upload.qiniu.com', + // uptokenURL: 'UpTokenURL.com/uptoken', + // }) + wx.uploadFile({ + url: 'https://up.qbox.me', + filePath: imageUrl, + name: 'file', + formData: { + 'key': imageUrl.split('//')[1], + 'token': upToken + }, + success: function (res) { + var data = JSON.parse(res.data); + }, + fail(error) { + console.log(error) + }, + complete(res) { + console.log(res) + } + }) + }, + qiniuToken(imageUrl){ + var that = this; + networkUtil._get(api.kApiQiNiuImageToken, {}, function (res) { + if (res.data && res.data.upToken){ + that.uploadImg(res.data.upToken, imageUrl) + } + }, function (res) { + networkUtil.showErrorToast(res.errormsg) + }) + }, + changeUserInfo(image){ + var that = this; + networkUtil._put(api.kApiModifyMyInfo, {}, function (res) { + if (res.data && res.data.upToken) { + that.uploadImg(res.data.upToken, imageUrl) + } + }, function (res) { + networkUtil.showErrorToast(res.errormsg) + }) + } + +}) + + diff --git a/littleApp_child/pages/Setting/Setting.json b/littleApp_child/pages/Setting/Setting.json new file mode 100644 index 0000000..2467a9a --- /dev/null +++ b/littleApp_child/pages/Setting/Setting.json @@ -0,0 +1,6 @@ +{ + "navigationBarBackgroundColor": "#48C17B", + "navigationBarTitleText": "设置管理", + "navigationBarTextStyle": "white", + "backgroundColor": "#d8dbd4" +} \ No newline at end of file diff --git a/littleApp_child/pages/Setting/Setting.wxml b/littleApp_child/pages/Setting/Setting.wxml new file mode 100644 index 0000000..af3a2cb --- /dev/null +++ b/littleApp_child/pages/Setting/Setting.wxml @@ -0,0 +1,42 @@ + + + + + + + {{userData.username}}胡子涵 + {{userData.username}}18787231231 + + + + + + + 月龄 + {{userData.birth}}122 + + + 出生日期 + {{userData.birth}}122 + + + 出生时间 + {{userData.birth}}122 + + + 分娩方式 + {{userData.birth}}122 + + + + + + + + {{item.text}} + + + + + +退出登录 \ No newline at end of file diff --git a/littleApp_child/pages/Setting/Setting.wxss b/littleApp_child/pages/Setting/Setting.wxss new file mode 100644 index 0000000..7b94c2f --- /dev/null +++ b/littleApp_child/pages/Setting/Setting.wxss @@ -0,0 +1,136 @@ + +.top_baseInfo { + width: 100%; + height: 105px; + background: #48C17B; + display: flex; + flex-direction: row; + align-items: center; + justify-content: flex-start; +} + +.top_baseInfo_avatar { + width:64px; + height:64px; + border-radius: 50%; + /* position: absolute; + top: 15px; + left: 15px; */ + border: 3px solid white; + box-shadow: 0 2px 4px 0 rgba(186,70,70,0.5); + margin-left: 15px; +} + +.top_baseInfo_userName { + margin-left: 12px; + width: 200px; +} + +.top_baseInfo_userName .name { + font-family: PingFang-SC-Medium; + font-size: 20px; + color: #FFFFFF; +} + +.top_baseInfo_userName .phone { + font-family: PingFang-SC-Medium; + font-size: 14px; + color: #FFFFFF; +} + +.top_baseInfo .indicator { + height: 22px; + width: 22px; + margin-left: calc(100% - 330px); +} + +.infoBar { + width: 100%; + height: 60px; + background: #48C17B; + display: flex; + margin-bottom: 10px; +} + +.infoView { + width: 25%; + background: rgba(255,255,255,0.08); +} + +.titleText { + font-size: 11px; + color: #CAF7DE; + width: 100%; + height: 16px; + text-align: center; + margin-top: 13px; +} + +.timeText { + font-size: 12px; + color: #FFFFFF; + width: 100%; + text-align: center; + height: 17px; + margin-top: 2px; +} + +.setting_cell { + height: 50px; + width: 100%; + font-size: 14px; + background: white; +} + +.setting_cell image { + height: 26px; + width: 26px; + display: inline-block; + margin-top: 12px; + margin-left: 15px; +} + +.setting_cell text { + display: inline-block; + transform: translateY(-7px); + margin-left: 14px; + height: 20px; + color: #333333; +} + +.right_text { + float: right; + margin-top: 15px; + color: #888888; +} + +.setting_cell .indicator { + width: 12px; + height: 12px; + float: right; + margin-right: 8px; + margin-top: 20px; +} + +.line { + border-top: 1px solid #E6E6E6; + margin-left: 15px; + width: calc(100% - 15px); +} + +.btn { + margin-left: 15px; + margin-right: 15px; + height: 38px; + background: #EAEAEA; + border: 1px solid #E6E6E6; + border-radius: 51px; + text-align: center; + font-size: 16px; + color: #888888; + line-height: 38px; +} + +.hover { + background: lightcyan +} \ No newline at end of file diff --git a/littleApp_child/pages/home/home.wxml b/littleApp_child/pages/home/home.wxml index b472ca5..4e71f67 100644 --- a/littleApp_child/pages/home/home.wxml +++ b/littleApp_child/pages/home/home.wxml @@ -50,10 +50,14 @@ 消息通知 - + - 敬请期待 + 设置 + diff --git a/littleApp_child/pages/home/home.wxss b/littleApp_child/pages/home/home.wxss index 0483b92..a4a414b 100644 --- a/littleApp_child/pages/home/home.wxss +++ b/littleApp_child/pages/home/home.wxss @@ -113,7 +113,7 @@ color: #FFFFFF; display:block; margin-top: 2px; - overflow: hidden; + overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-box-orient: vertical; diff --git a/littleApp_child/pages/hospitalDetail/hospitalDetail.js b/littleApp_child/pages/hospitalDetail/hospitalDetail.js deleted file mode 100644 index 824135a..0000000 --- a/littleApp_child/pages/hospitalDetail/hospitalDetail.js +++ /dev/null @@ -1,59 +0,0 @@ -//hospitalDetail.js 医院详情介绍 - -// 网络请求工具类 -var networkUtil = require('../../utils/network_util.js') -var api = require('../../utils/apiFile.js') - -Page({ - data: { - hosiptalInfo: {}, - isShowOne: false, - isShowTwo: false, - isShowThree: false, - }, - onLoad: function (option) { - - }, - isShowMore:function(e){ - var self=this - console.log(e) - var index =e.currentTarget.dataset.index - console.log(index) - if(index==1){ - self.setData({ - isShowOne: !self.data.isShowOne, - isShowTwo: false, - isShowThree: false, - }) - } if (index == 2) { - self.setData({ - isShowOne: false , - isShowTwo: !self.data.isShowTwo, - isShowThree: false, - }) - } if (index == 3) { - self.setData({ - isShowOne: false, - isShowTwo: false, - isShowThree: !self.data.isShowThree, - }) - } - }, - getArticleInfo(days) { - var self = this - // 文章列表 - networkUtil._get(api.homeArtList, { page: 0, limit: 100, days: days }, function (res) { - console.log('获取文章信息', res.data.list) - app.globalData.artList = res.data.list - self.setData({ - list: res.data.list - }) - }, function (res) { - wx.stopPullDownRefresh() - }) - }, - - -}) - - diff --git a/littleApp_child/pages/hospitalDetail/hospitalDetail.json b/littleApp_child/pages/hospitalDetail/hospitalDetail.json deleted file mode 100644 index 23d7a20..0000000 --- a/littleApp_child/pages/hospitalDetail/hospitalDetail.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "navigationBarBackgroundColor": "#f4879b", - "navigationBarTitleText": "医院介绍", - "navigationBarTextStyle": "white", - "backgroundColor": "#d8dbd4" -} \ No newline at end of file diff --git a/littleApp_child/pages/hospitalDetail/hospitalDetail.wxml b/littleApp_child/pages/hospitalDetail/hospitalDetail.wxml deleted file mode 100644 index 9431fb9..0000000 --- a/littleApp_child/pages/hospitalDetail/hospitalDetail.wxml +++ /dev/null @@ -1,2 +0,0 @@ - - diff --git a/littleApp_child/pages/hospitalDetail/hospitalDetail.wxss b/littleApp_child/pages/hospitalDetail/hospitalDetail.wxss deleted file mode 100644 index 8b13789..0000000 --- a/littleApp_child/pages/hospitalDetail/hospitalDetail.wxss +++ /dev/null @@ -1 +0,0 @@ - diff --git a/littleApp_child/pages/hospitalList/hospitalList.js b/littleApp_child/pages/hospitalList/hospitalList.js deleted file mode 100644 index f68dbbe..0000000 --- a/littleApp_child/pages/hospitalList/hospitalList.js +++ /dev/null @@ -1,24 +0,0 @@ -//hospitalList.js 医院列表 联系医院 - -// 网络请求工具类 -var networkUtil = require('../../utils/network_util.js') -var api = require('../../utils/apiFile.js') - -// 页码 -var page = 0 -// 地理位置信息 -var latitude = 0 -var longitude = 0 -// 是否有更多 -var hasMore = true - -Page({ - data: { - list:[] - }, - /*---------生命周期--------*/ - onLoad: function () { - networkUtil.showLoading() - this.getUserLocation() - } -}) diff --git a/littleApp_child/pages/hospitalList/hospitalList.json b/littleApp_child/pages/hospitalList/hospitalList.json deleted file mode 100644 index a3e58c6..0000000 --- a/littleApp_child/pages/hospitalList/hospitalList.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "navigationBarBackgroundColor": "#f4879b", - "navigationBarTitleText": "联系医院", - "navigationBarTextStyle": "white", - "backgroundColor": "#d8dbd4", - "enablePullDownRefresh": true -} \ No newline at end of file diff --git a/littleApp_child/pages/hospitalList/hospitalList.wxml b/littleApp_child/pages/hospitalList/hospitalList.wxml deleted file mode 100644 index 5a5c46a..0000000 --- a/littleApp_child/pages/hospitalList/hospitalList.wxml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - {{item.title}} - - {{item.gradeLevelName == null ? '' : item.gradeLevelName}} - 距离{{item.distance}}km - - - - - - - - - {{item.introduction == null ? '-' : item.introduction}} - - - - - - 加载更多... - diff --git a/littleApp_child/pages/hospitalList/hospitalList.wxss b/littleApp_child/pages/hospitalList/hospitalList.wxss deleted file mode 100644 index 9328a09..0000000 --- a/littleApp_child/pages/hospitalList/hospitalList.wxss +++ /dev/null @@ -1,93 +0,0 @@ -/**hospitalList.wxss 医院列表 联系医院**/ - -.item{ - position: relative; - height: 273rpx; - display: flex; - flex-direction: row; - align-items: center; - background: white; - margin-bottom:10rpx; -} - -/*item左边布局-----------------------*/ -.item-left{ - width: 18.67%; - height: 273rpx; -} - -.item__logo{ - width: 90rpx; - height: 90rpx; - border-radius: 500rpx; - position: absolute; - top: 30rpx; - left: 20rpx; -} - -/*item中间布局----------------------*/ -.item-middle{ - width: 69.33%; - height: 273rpx; -} - -.item-middle__title{ - color: #5e5e5e; - font: medium; - font-size: 34rpx; - margin-top: 40rpx; - line-height: 34rpx; -} - -.item-middle__specialty{ - color: #ee7289; - font: normal; - font-size: 22rpx; - line-height: 22rpx; - margin-top: 16rpx; - - display: flex; - flex-direction: row; -} - -.item-middle__description{ - color: #858585; - font: lighter; - font-size: 24rpx; - line-height: 130%; - margin-top: 30rpx; - overflow: hidden; - text-overflow: ellipsis; - display: -webkit-box; - -webkit-box-orient: vertical; - -webkit-line-clamp: 3; -} - -.item-middle__specialty__middle{ - margin-left: 18rpx; -} - -.item-middle__specialty__right{ - margin-left: 30rpx -} - -.stars-star{ - width: 20rpx; - height: 20rpx; -} - -/*item右边布局--------------------------*/ -.item-right{ - width: 12%; -} -.item-right__right{ - width: 35rpx; - height: 35rpx; - margin-left: 50%; - transform: translateX(-50%); - - margin-right: 4rpx; -} - - - diff --git a/littleApp_child/utils/apiFile.js b/littleApp_child/utils/apiFile.js index 1d47963..79639ba 100644 --- a/littleApp_child/utils/apiFile.js +++ b/littleApp_child/utils/apiFile.js @@ -34,4 +34,8 @@ module.exports = { articleLike: 'v1/articleLike', // 文章列表 articleList: 'wx/articles/precise', + // 7niu + kApiQiNiuImageToken:'v1/tokens/qiniu', + // 改用户信息 + kApiModifyMyInfo:'v1/users' } \ No newline at end of file diff --git a/littleApp_child/utils/qiniuUploader.js b/littleApp_child/utils/qiniuUploader.js new file mode 100755 index 0000000..1c3c9bc --- /dev/null +++ b/littleApp_child/utils/qiniuUploader.js @@ -0,0 +1,157 @@ +// 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'); + 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; +} + +})(); \ No newline at end of file -- 1.8.3.1