hwq hai 1 ano
pai
achega
7cdb18e717
Modificáronse 5 ficheiros con 137 adicións e 73 borrados
  1. 13 0
      api/login.js
  2. 6 2
      pages/collection/wx.vue
  3. 2 1
      pages/collection/zfb.vue
  4. 36 3
      pages/set/phone.vue
  5. 80 67
      utils/newRequest.js

+ 13 - 0
api/login.js

@@ -62,3 +62,16 @@ export function bangding(data) {
 		data
 	});
 }
+
+
+/**
+ * 获取图片上传加密key
+ */
+export function getUpimgKey(data) {
+  return request({
+  	url: '/api/upload/sign',
+  	method: 'POST',
+  	data
+  });
+}
+

+ 6 - 2
pages/collection/wx.vue

@@ -78,6 +78,7 @@
 					this.phone = data.wx.phone
 				}
 			})
+			
 		},
 		methods: {
 			//发送验证码
@@ -107,10 +108,13 @@
 				}
 			},
 			uploads() {
+				const that = this;
 				upload({
-					filename: ''
+					filename: '',
+					file_name:'collection/wx/'+that.userInfo.uid
 				}).then(data => {
-					this.image = data[0].url;
+					console.log(data[0].url,'data');
+					that.image = data[0].url;
 				})
 			},
 			confirm() {

+ 2 - 1
pages/collection/zfb.vue

@@ -90,7 +90,8 @@
 			...mapMutations('user', ['setUserInfo', 'setOrderInfo']),
 			uploads() {
 				upload({
-					filename: ''
+					filename: '',
+					file_name:'collection/zfb/'+that.userInfo.uid
 				}).then(data => {
 					this.image = data[0].url;
 				})

+ 36 - 3
pages/set/phone.vue

@@ -97,7 +97,8 @@
 				obj.loding = true;
 				binding({
 						phone: obj.account,
-						captcha: obj.captcha
+						captcha: obj.captcha,
+						step: 1
 					})
 					.then(({
 						data
@@ -110,8 +111,40 @@
 							});
 						}, 1000);
 					})
-					.catch(err => {
-						obj.loding = false;
+					.catch(res => {
+						let that = this;
+						if (res.data !== undefined && res.data.is_bind) {
+							uni.showModal({
+								title: '是否绑定账号',
+								content: res.msg,
+								confirmText: '绑定',
+								success(res) {
+									if (res.confirm) {
+										binding({
+											phone: that.account,
+											captcha: that.captcha,
+											step: 1
+										}).then(res => {
+											setTimeout(function() {
+												that.loding = false;
+												uni.switchTab({
+													url: '/pages/user/user'
+												});
+											}, 1000);
+											that.$api.msg('绑定成功!');
+										}).catch(err => {
+											that.loding = false;
+										})
+									} else if (res.cancel) {
+										that.loding = false;
+										that.$api.msg('您已取消绑定!');
+									}
+								}
+							});
+						} else {
+							that.loding = false;
+							that.$api.msg('绑定成功!');
+						}
 						console.log(err);
 					});
 			}

+ 80 - 67
utils/newRequest.js

@@ -1,4 +1,7 @@
-import store from '../store'
+import store from '../store';
+import {
+	getUpimgKey
+} from '@/api/login.js';
 let service = {
 	baseURL: store.state.baseURL, // 请求头
 	header: {
@@ -111,86 +114,96 @@ let service = {
 			// #endif
 			let config = {
 				// 上传图片的API
-				upload_img_url: url
+				upload_img_url: url,
+				data: requestData.data,
+				file_name: requestData.data.file_name||'user/'+store.state.user.userInfo.uid
 			}
-
-			let Uploader = {
-				// 选择图片
-				choose(num) {
-					return new Promise((resolve, reject) => {
-						uni.chooseImage({
-							count: num,
-							sourceType: ['camera', 'album'],
-							success(res) {
-								// console.log(res);
-								// 缓存文件路径
-								resolve(res.tempFilePaths)
-							},
-							fail(err) {
-								console.log(err)
-								reject(err)
-							}
-						})
-					})
-
-				},
-				// 上传图片
-				upload_one(path) {
-					return new Promise((resolve, reject) => {
+			uni.showLoading({
+				title: '请求key中'
+			})
+			getUpimgKey({
+				file_name: config.file_name
+			}).then(({
+				data
+			}) => {
+				uni.hideLoading()
+				uni.chooseImage({
+					count: 1,
+					sourceType: ['camera', 'album'],
+					success(res) {
 						uni.showLoading({
 							title: '上传中'
 						})
+						// console.log(res, '选中图片');
+						const time = (new Date()).getTime();
+						// #ifdef H5
+						const formData = {
+								"policy": data.sign.policy,
+								"OSSAccessKeyId": data.sign.accessid,
+								'success_action_status': 200,
+								"signature": data.sign.signature,
+								"key": config.file_name+'/'+time+res.tempFiles[0].name
+							}
+						// #endif
+						// #ifndef H5
+						const formData = {
+								"policy": data.sign.policy,
+								"OSSAccessKeyId": data.sign.accessid,
+								'success_action_status': 200,
+								"signature": data.sign.signature,
+								"key": config.file_name+'/'+time+'.png'
+							}
+						// #endif
+						// console.log(formData,'formData');
 						uni.uploadFile({
-							url: config.upload_img_url, //仅为示例,非真实的接口地址
-							filePath: path,
+							url: data.sign.host, //仅为示例,非真实的接口地址
+							filePath: res.tempFilePaths[0],
+							formData,
 							name: 'file',
 							header: {
-								"Authori-zation": 'Bearer ' + uni.getStorageSync(
-									'token')
+								"Authori-zation": 'Bearer ' + uni
+									.getStorageSync('token')
 							},
-							success: (uploadFileRes) => {
-								if ("string" === typeof uploadFileRes.data) {
-									resolve(JSON.parse(uploadFileRes.data).data)
-								} else {
-									resolve(uploadFileRes.data.data)
+							success: (suc) => {
+								if(suc.statusCode==200){
+									// #ifdef H5
+									const imgurl = [{url:data.sign.host+'/'+config.file_name+'/'+time+res.tempFiles[0].name}];
+									// #endif
+									// #ifndef H5
+									const imgurl = [{url:data.sign.host+'/'+config.file_name+'/'+time+'.png'}];
+									// #endif
+									resolve(imgurl)
 								}
+								uni.hideLoading()
 							},
-							complete() {
+							file(res) {
 								uni.hideLoading()
+								// console.log(res, '图片上传错误');
+								reject(res)
 							}
+
 						});
-					})
-				},
-				// 上传多个图片
-				upload(path_arr) {
-					let num = path_arr.length;
-					return new Promise(async (resolve, reject) => {
-						let img_urls = []
-						for (let i = 0; i < num; i++) {
-							let img_url = await this.upload_one(path_arr[i]);
-							img_urls.push(img_url)
-						};
-						resolve(img_urls)
-					})
-				},
-				// 触发上传图片事件
-				choose_and_upload(num) {
-					return new Promise(async (resolve, reject) => {
-						// 选择图片
-						let path_arr = await this.choose(num);
-						// 上传图片
-						await this.upload(path_arr).then((e) => {
-							resolve(e);
-						}).catch((e) => {
-							reject(e)
+					},
+					complete() {
+						uni.hideLoading()
+					},
+					fail(err) {
+						// console.log(res, '图片上传错误');
+						uni.showToast({
+							title: '图片上传失败',
+							icon:"error"
 						});
-					})
-				}
-			};
-			Uploader.choose_and_upload(1).then((e) => {
-				resolve(e);
-			}).catch((e) => {
-				console.log(e);
+						uni.hideLoading()
+						reject(err)
+					}
+				})
+			}).catch((res)=>{
+				uni.hideLoading()
+				uni.showToast({
+					title: 'key请求失败',
+					icon:"error"
+				});
+				reject(res)
 			})
 		})
 	},