From 6706b92f9fd6f6dfc57d5bd915a5a50a80bad2ac Mon Sep 17 00:00:00 2001 From: educoder_weapp Date: Sun, 10 Nov 2019 22:46:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E4=BF=AE=E6=94=B9=E5=A4=B4?= =?UTF-8?q?=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + app.json | 7 +- data/client.js | 25 +++++++- data/eduapi.js | 31 ++++++++- data/requests.js | 9 ++- pages/findmore/findmore.js | 18 +++--- pages/findmore/findmore.json | 3 +- pages/image_crop/image_crop.js | 106 +++++++++++++++++++++++++++++++ pages/image_crop/image_crop.json | 4 ++ pages/image_crop/image_crop.wxml | 12 ++++ pages/image_crop/image_crop.wxss | 21 ++++++ pages/setting/setting.js | 33 ++++++++++ pages/setting/setting.wxml | 2 +- 13 files changed, 255 insertions(+), 18 deletions(-) create mode 100644 pages/image_crop/image_crop.js create mode 100644 pages/image_crop/image_crop.json create mode 100644 pages/image_crop/image_crop.wxml create mode 100644 pages/image_crop/image_crop.wxss diff --git a/.gitignore b/.gitignore index edccf46..18cb9a3 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,8 @@ project.config.json app.js towxml +we-cropper + .idea api_docs diff --git a/app.json b/app.json index 9e518da..9e39ef7 100644 --- a/app.json +++ b/app.json @@ -2,6 +2,7 @@ "pages": [ "pages/findmore/findmore", "pages/my_courses/my_courses", + "pages/login/login", "pages/course_setting/course_setting", "pages/exercises/exercises", "pages/courses/courses", @@ -9,13 +10,13 @@ "pages/setting/setting", "pages/classroom/classroom", "pages/about/about", - "pages/login/login", + "pages/test/test", "pages/course/course", "pages/files/files", "pages/mark_detail/mark_detail", - "pages/reset_password/reset_password" - + "pages/reset_password/reset_password", + "pages/image_crop/image_crop" ], "window": { "backgroundTextStyle": "dark", diff --git a/data/client.js b/data/client.js index 7124c71..48bf5bd 100644 --- a/data/client.js +++ b/data/client.js @@ -224,6 +224,29 @@ export class Client{ }); }) } + upload_avatar({avatar_path, success, fail, complete}){ + console.log("upload_avatar"); + return new Promise((resolve, reject) => { + Account.upload_avatar({ + session: this.session, + avatar_path: avatar_path, + login: this.current_user.login, + complete: complete, + success: res => { + if (typeof success == "function") { + success(res); + } + resolve(res); + }, + fail: error => { + if (typeof fail == "function") { + fail(error); + } + reject(error); + } + }) + }); + } search_courses({limit=20, page=1, order="all", search="", success, fail, complete}={}){ return new Promise((resolve, reject) => { @@ -271,7 +294,7 @@ export class Client{ session: this.session, page: page, per_page: per_page, - user: this.current_user.login, + login: this.current_user.login, complete: complete, success: res=>{ if(typeof success == "function"){ diff --git a/data/eduapi.js b/data/eduapi.js index a2bfa5a..14e908c 100644 --- a/data/eduapi.js +++ b/data/eduapi.js @@ -90,10 +90,10 @@ export class Account{ complete: complete }) } - static get_user_courses({session,user,category="",status="",sort_by="",sort_direction="",page=1, per_page=16, success, fail, complete}){ + static get_user_courses({session,login,category="",status="",sort_by="",sort_direction="",page=1, per_page=16, success, fail, complete}){ let data = {page: page, per_page: per_page, category:category, status: status, sort_by: sort_by, sort_direction: sort_direction}; return session.request({ - url: api_base_url + "/users/"+user +"/courses.json", + url: api_base_url + "/users/"+login +"/courses.json", method: "GET", data: data, success: success, @@ -125,6 +125,33 @@ export class Account{ } static set_password({}){ + } + static upload_avatar({session, login, avatar_path, success, fail, complete}){ + var avatar_data=null; + wx.getFileSystemManager().readFile({ + filePath: avatar_path, + encoding: "base64", + success:res=> { + console.log("in success"); + console.log(res); + avatar_data ="data:image/jpeg;base64,"+res.data; + console.log("upload_avatar"); + console.info(avatar_data); + session.request({ + url: "https://www.educoder.net/api/users/accounts/"+login+"/avatar.json", + method: "PUT", + data: {image: avatar_data}, + success: success, + fail: fail, + complete: complete + }) + }, + fail: error=>{ + console.error(error); + } + }); + + } } diff --git a/data/requests.js b/data/requests.js index 406c6bf..a15575b 100644 --- a/data/requests.js +++ b/data/requests.js @@ -49,11 +49,18 @@ export class Session { } uploadFile({url, filePath, name, header, formData, success, fail, complete}){ + console.log("uploadFile"); + console.log({ + url: url, + filePath: filePath, + name: name, + header: {"content-type": "multipart/form-data", "Cookie": this.cookie_str, ...header}, + formData: formData}); return wx.uploadFile({ url: url, filePath: filePath, name: name, - header: {...header, "content-type": "multipart/form-data", "Cookie": this.cookie_str}, + header: {"content-type": "multipart/form-data", "Cookie": this.cookie_str, ...header}, formData: formData, success: success, fail: fail, diff --git a/pages/findmore/findmore.js b/pages/findmore/findmore.js index 7b33557..38e7c6a 100644 --- a/pages/findmore/findmore.js +++ b/pages/findmore/findmore.js @@ -16,20 +16,20 @@ Page({ * 生命周期函数--监听页面加载 */ onLoad: function (options) { - + app.client.get_home_page() + .then(res => { + console.info(res); + this.setData({ page_info: res.data, page_status: 1 }); + }).catch(error => { + this.setData({ page_status: 2 }); + }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { - app.client.get_home_page() - .then(res=>{ - console.info(res); - this.setData({page_info: res.data, page_status: 1}); - }).catch(error=>{ - this.setData({page_status: 2}); - }) + }, /** @@ -57,7 +57,7 @@ Page({ * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { - + this.onLoad(); }, /** diff --git a/pages/findmore/findmore.json b/pages/findmore/findmore.json index 8835af0..7360326 100644 --- a/pages/findmore/findmore.json +++ b/pages/findmore/findmore.json @@ -1,3 +1,4 @@ { - "usingComponents": {} + "usingComponents": {}, + "enablePullDownRefresh": true } \ No newline at end of file diff --git a/pages/image_crop/image_crop.js b/pages/image_crop/image_crop.js new file mode 100644 index 0000000..9016fe5 --- /dev/null +++ b/pages/image_crop/image_crop.js @@ -0,0 +1,106 @@ +import WeCropper from '../../we-cropper/dist/we-cropper.min.js' + + +const device = wx.getSystemInfoSync() +const width = device.windowWidth +const height = device.windowHeight - 30 +const app = getApp(); + +Page({ + data: { + cropperOpt: { + id: 'cropper', + width, + height, + scale: 2.5, + zoom: 8, + cut: { + x: (width - 300) / 2, + y: (height - 300) / 2, + width: 300, + height: 300 + } + } + }, + + touchStart(e) { + console.log(e); + this.mycropper.touchStart(e) + }, + + touchMove(e) { + console.log(e); + this.mycropper.touchMove(e) + }, + + touchEnd(e) { + console.log(e); + this.mycropper.touchEnd(e) + }, + + getCropperImage() { + console.warn("ontap: getCropImage") + this.mycropper.getCropperImage((avatar) => { + console.log(avatar); + if (avatar) { + console.info("avatar"); + console.log(avatar); + app.client.upload_avatar({ + avatar_path: avatar, + success: res=>{ + console.log("upload avatar success"); + console.log(res); + }, + fail: error=>{ + console.error(error); + } + }); + } else { + console.error('获取图片失败') + } + }) + }, + + uploadTap() { + const self = this + + wx.chooseImage({ + count: 1, + success(res) { + const src = res.tempFilePaths[0] + self.mycropper.pushOrign(src) + } + }) + }, + + onLoad(option) { + const {cropperOpt} = this.data + const {src} = option + + if (src) { + Object.assign(cropperOpt, {src}) + this.mycropper = new WeCropper(cropperOpt); + this.mycropper + .on('ready', function (ctx) { + console.info("ready"); + console.info(ctx); + }) + .on('beforeImageLoad', (ctx) => { + console.info("beforeImageLoad"); + wx.showToast({ + title: '上传中', + icon: 'loading', + duration: 3000 + }) + }) + .on('imageLoad', (ctx) => { + console.info("imageLoad") + wx.hideToast() + }) + .on('beforeDraw', (ctx, instance) => { + console.log(`before canvas draw,i can do something`) + console.log(`current canvas context:`, ctx) + }) + } + } +}) \ No newline at end of file diff --git a/pages/image_crop/image_crop.json b/pages/image_crop/image_crop.json new file mode 100644 index 0000000..a2f054a --- /dev/null +++ b/pages/image_crop/image_crop.json @@ -0,0 +1,4 @@ +{ + "usingComponents": {}, + "navigationBarTitleText": "设置头像" +} \ No newline at end of file diff --git a/pages/image_crop/image_crop.wxml b/pages/image_crop/image_crop.wxml new file mode 100644 index 0000000..91d5c35 --- /dev/null +++ b/pages/image_crop/image_crop.wxml @@ -0,0 +1,12 @@ + + +