|
|
@@ -1,162 +1,74 @@
|
|
|
<template>
|
|
|
- <view class="index">
|
|
|
- <view>
|
|
|
- <img src="" alt="">
|
|
|
+ <view class="wrap">
|
|
|
+ <nut-cell>
|
|
|
+ <view v-if="state.editMode">
|
|
|
+ <img :src="state.imgSrc" alt="" />
|
|
|
+ <watermark v-if="state.editMode" :fullPage="false" font-color="#fa2c19" :content="state.watermarkContent">
|
|
|
+ </watermark>
|
|
|
+ </view>
|
|
|
+ <nut-empty v-if="!state.editMode" image="empty" description="无内容"></nut-empty>
|
|
|
+ </nut-cell>
|
|
|
+ <nut-cell v-if="!state.editMode">
|
|
|
+ <nut-button type='warning' size="large" shape="square" @click="chooseImg">选择图片</nut-button>
|
|
|
+ </nut-cell>
|
|
|
+ <view v-if="state.editMode">
|
|
|
+ <nut-input v-model="state.watermarkContent" placeholder="请输入要显示的水印内容" clearable clearSize="14" show-word-limit
|
|
|
+ :max-length="20" :showClearIcon="true" @update:model-value="refresh"></nut-input>
|
|
|
+ <nut-cell>
|
|
|
+ <nut-button type='success' size="large" shape="square">下载</nut-button>
|
|
|
+ </nut-cell>
|
|
|
</view>
|
|
|
- {{ msg }}
|
|
|
- <Dongdong />
|
|
|
- <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>
|
|
|
- <nut-button type="primary" @click="http()">http封装</nut-button>
|
|
|
- </view>
|
|
|
- <nut-toast :msg="msg2" v-model:visible="show" :type="type" :cover="cover" />
|
|
|
</view>
|
|
|
</template>
|
|
|
|
|
|
-<script lang="ts">
|
|
|
-// import JsonResponse from './vo/JsonResponse';
|
|
|
-import Taro from '@tarojs/taro';
|
|
|
+<script>
|
|
|
import { reactive, toRefs } from 'vue';
|
|
|
-import { Dongdong } from '@nutui/icons-vue-taro';
|
|
|
-import httpClient from '../../core/http';
|
|
|
-
|
|
|
+import { Plus } from '@nutui/icons-vue-taro';
|
|
|
+import watermark from '../../core/components/watermark.vue';
|
|
|
export default {
|
|
|
name: 'Index',
|
|
|
components: {
|
|
|
- Dongdong
|
|
|
+ Plus, watermark
|
|
|
},
|
|
|
setup() {
|
|
|
const state = reactive({
|
|
|
- msg: '欢迎使用 NutUI4.0 开发小程序',
|
|
|
- msg2: '你成功了~',
|
|
|
- type: 'text',
|
|
|
- show: false,
|
|
|
- cover: false
|
|
|
+ editMode: true,
|
|
|
+ imgSrc: "https://img10.360buyimg.com/ling/jfs/t1/181258/24/10385/53029/60d04978Ef21f2d42/92baeb21f907cd24.jpg",
|
|
|
+ watermarkContent: '仅供入职使用',
|
|
|
});
|
|
|
|
|
|
- const handleClick = (type, msg, cover = false) => {
|
|
|
- state.show = true;
|
|
|
- state.msg2 = msg;
|
|
|
- state.type = type;
|
|
|
- state.cover = cover;
|
|
|
- };
|
|
|
-
|
|
|
- const login = () => {
|
|
|
- var token = Taro.getStorageSync("token");
|
|
|
- if (token) {
|
|
|
- console.log("缓存中有token");
|
|
|
- Taro.request({
|
|
|
- url: BASE_URL + '/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 chooseImg = function () {
|
|
|
+ state.editMode = !state.editMode;
|
|
|
+ console.log(state.editMode);
|
|
|
};
|
|
|
|
|
|
- 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", ret.data)
|
|
|
- //do something
|
|
|
- var data = JSON.parse(ret.data);
|
|
|
- Taro.setStorageSync("attachmentId", data.data[0]);
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
- })
|
|
|
- };
|
|
|
-
|
|
|
- const download = () => {
|
|
|
- console.log("附件ID:", Taro.getStorageSync("attachmentId"))
|
|
|
- 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("下载成功了", res);
|
|
|
- Taro.previewMedia({
|
|
|
- sources: [{
|
|
|
- "url": res.tempFilePath,
|
|
|
- "type": "image"
|
|
|
- }]
|
|
|
- })
|
|
|
- }
|
|
|
- }
|
|
|
- })
|
|
|
- };
|
|
|
-
|
|
|
- const http = () => {
|
|
|
- httpClient({
|
|
|
- url: "/oss/test",
|
|
|
- method: "GET",
|
|
|
- data: {},
|
|
|
- success(result: string) {
|
|
|
- console.log("成功啦", result);
|
|
|
- }
|
|
|
- });
|
|
|
+ const refresh = function () {
|
|
|
+ var tmpImgSrc = state.imgSrc;
|
|
|
+ state.imgSrc = '';
|
|
|
+ state.imgSrc = tmpImgSrc;
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
- ...toRefs(state), handleClick, login, upload, download, http
|
|
|
- }
|
|
|
+ state,
|
|
|
+ chooseImg,
|
|
|
+ refresh
|
|
|
+ };
|
|
|
}
|
|
|
}
|
|
|
</script>
|
|
|
|
|
|
<style lang="scss">
|
|
|
-.index {
|
|
|
- font-family: "Avenir", Helvetica, Arial, sans-serif;
|
|
|
- -webkit-font-smoothing: antialiased;
|
|
|
- -moz-osx-font-smoothing: grayscale;
|
|
|
- text-align: center;
|
|
|
+.wrap {
|
|
|
+ width: 100vw;
|
|
|
+ height: 100vh;
|
|
|
+ background-color: #eff2f5;
|
|
|
+
|
|
|
+ .watermark {
|
|
|
+ width: 100%;
|
|
|
+ height: 50vh;
|
|
|
+ margin-bottom: 20px;
|
|
|
+ display: block;
|
|
|
+ background-color: #fff;
|
|
|
+ }
|
|
|
}
|
|
|
</style>
|