|
|
@@ -7,6 +7,8 @@
|
|
|
<view class="btn">
|
|
|
<nut-button type="primary" @click="handleClick('text', msg2, true)">点我</nut-button>
|
|
|
<nut-button type="primary" @click="login()">登录</nut-button>
|
|
|
+ <nut-button type="primary" @click="upload()">上传文件</nut-button>
|
|
|
+ <nut-button type="primary" @click="download()">下载文件</nut-button>
|
|
|
</view>
|
|
|
<nut-toast :msg="msg2" v-model:visible="show" :type="type" :cover="cover"/>
|
|
|
</view>
|
|
|
@@ -39,12 +41,83 @@ export default {
|
|
|
};
|
|
|
|
|
|
const login = () => {
|
|
|
- console.log("测试啦啦啦");
|
|
|
- Taro.request({
|
|
|
- url: "http://localhost:8080/wechat/miniprogram/test",
|
|
|
- success: (res:Taro.request.SuccessCallbackResult<JsonResponse<string>>) => {
|
|
|
-
|
|
|
- console.log(res)
|
|
|
+ var token = Taro.getStorageSync("token");
|
|
|
+ if (token) {
|
|
|
+ console.log("缓存中有token");
|
|
|
+ Taro.request({
|
|
|
+ url: 'http://localhost:8080/watermark/make',
|
|
|
+ header: {
|
|
|
+ "Authorization": token
|
|
|
+ },
|
|
|
+ method: "POST",
|
|
|
+ data:{},
|
|
|
+ success: function(ret) {
|
|
|
+ console.log("返回结果", ret);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ console.log("缓存中没有token");
|
|
|
+ Taro.login({
|
|
|
+ success: function (res) {
|
|
|
+ console.log("登录拉拉了", res);
|
|
|
+ if (res.code) {
|
|
|
+ //发起网络请求
|
|
|
+ Taro.request({
|
|
|
+ url: 'http://localhost:8080/wechat/miniprogram/login',
|
|
|
+ method: "POST",
|
|
|
+ data: {
|
|
|
+ code: res.code
|
|
|
+ },
|
|
|
+ success: function(ret) {
|
|
|
+ console.log("返回结果", ret);
|
|
|
+ // TODO 把token放入缓存
|
|
|
+ Taro.setStorageSync("token", ret.data.data);
|
|
|
+
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ console.log('登录失败!' + res.errMsg)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ const upload = () => {
|
|
|
+ Taro.chooseImage({
|
|
|
+ success (res) {
|
|
|
+ const tempFilePaths = res.tempFilePaths;
|
|
|
+ Taro.uploadFile({
|
|
|
+ url: 'http://localhost:8080/oss/upload', //仅为示例,非真实的接口地址
|
|
|
+ header: {
|
|
|
+ "Authorization": Taro.getStorageSync("token")
|
|
|
+ },
|
|
|
+ filePath: tempFilePaths[0],
|
|
|
+ name: 'files',
|
|
|
+ formData: {
|
|
|
+ 'user': 'test'
|
|
|
+ },
|
|
|
+ success (ret){
|
|
|
+ console.log("上传结果233", res)
|
|
|
+ //do something
|
|
|
+ Taro.setStorageSync("attachmentId", ret.data.data);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ };
|
|
|
+
|
|
|
+ const download = () => {
|
|
|
+ Taro.downloadFile({
|
|
|
+ url: 'http://localhost:8080/oss/download?attachmentId='+Taro.getStorageSync("attachmentId"), //仅为示例,并非真实的资源
|
|
|
+ header: {
|
|
|
+ "Authorization": Taro.getStorageSync("token")
|
|
|
+ },
|
|
|
+ success: function (res) {
|
|
|
+ // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
|
|
|
+ if (res.statusCode === 200) {
|
|
|
+ console.log("下载成功了")
|
|
|
+ }
|
|
|
}
|
|
|
})
|
|
|
};
|
|
|
@@ -52,7 +125,9 @@ export default {
|
|
|
return {
|
|
|
...toRefs(state),
|
|
|
handleClick,
|
|
|
- login
|
|
|
+ login,
|
|
|
+ upload,
|
|
|
+ download
|
|
|
}
|
|
|
}
|
|
|
}
|