delis.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <template>
  2. <view class="gg-container">
  3. <view class="gg-tit">
  4. <view class="gg-title">
  5. 我的吉农宝<text>上个页面传过来的参数{{id}}</text>
  6. </view>
  7. <view>
  8. <uni-section title="基本用法" type="line">
  9. <view class="example">
  10. <!-- 基础用法,不包含校验规则 -->
  11. <uni-forms ref="baseForm" :modelValue="baseFormData">
  12. <uni-forms-item label="姓名" required>
  13. <uni-easyinput v-model="baseFormData.name" placeholder="请输入姓名" />
  14. </uni-forms-item>
  15. <uni-forms-item label="年龄" required>
  16. <uni-easyinput v-model="baseFormData.age" placeholder="请输入年龄" />
  17. </uni-forms-item>
  18. <uni-forms-item label="性别" required>
  19. <uni-data-checkbox v-model="baseFormData.sex" :localdata="sexs" />
  20. </uni-forms-item>
  21. <uni-forms-item label="兴趣爱好" required>
  22. <uni-data-checkbox v-model="baseFormData.hobby" multiple :localdata="hobbys" />
  23. </uni-forms-item>
  24. <uni-forms-item label="自我介绍">
  25. <uni-easyinput type="textarea" v-model="baseFormData.introduction" placeholder="请输入自我介绍" />
  26. </uni-forms-item>
  27. <uni-forms-item label="日期时间">
  28. <uni-datetime-picker type="datetime" return-type="timestamp" v-model="baseFormData.datetimesingle"/>
  29. </uni-forms-item>
  30. </uni-forms>
  31. </view>
  32. </uni-section>
  33. <uni-section title="多行文本" subTitle="指定属性 type=textarea 使用多行文本框" type="line" padding>
  34. <uni-easyinput type="textarea" v-model="value" placeholder="请输入内容"></uni-easyinput>
  35. </uni-section>
  36. <view class="content">
  37. <editor
  38. class="myEdit"
  39. placeholder="写点什么吧~~"
  40. show-img-resize
  41. show-img-toolbar
  42. show-img-size
  43. @ready="onEditReady"
  44. @focus="OnFocus"
  45. @statuschange="statuschange"
  46. ></editor>
  47. </view>
  48. <view class="btnGroup">
  49. <u-button type="primary" text="确认发表" @click="onSubmit" :disabled="!artData.title"></u-button>
  50. </view>
  51. <view class="tools" v-show="toolsShow">
  52. <!-- 标题 -->
  53. <view class="item" @click="headChange">
  54. <text class="iconfont icon-zitibiaoti" :class="headShow ? 'active' : ''"></text>
  55. </view>
  56. <!-- 加粗 -->
  57. <view class="item" @click="boldChange">
  58. <text class="iconfont icon-zitijiacu" :class="boldShow ? 'active' : ''"></text>
  59. </view>
  60. <!-- 斜体 -->
  61. <view class="item" @click="italicChange">
  62. <text class="iconfont icon-zitixieti" :class="italicShow ? 'active' : ''"></text>
  63. </view>
  64. <!-- 分割线 -->
  65. <view class="item" @click="insertDivider"><text class="iconfont icon-fengexian"></text></view>
  66. <view class="item" @click="insertImage"><text class="iconfont icon-charutupian"></text></view>
  67. <!-- 完成 -->
  68. <view class="item" @click="editOk"><text class="iconfont icon-duigou_kuai"></text></view>
  69. </view>
  70. <!-- 这张图片就是自定义的图片,地址填写自己本地的就行 -->
  71. <image src="https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/unicloudlogo.png"
  72. mode="widthFix" style="width: 112rpx;height: 110rpx;" @click="uploadImagePhoto()"></image>
  73. </view>
  74. </view>
  75. </view>
  76. </template>
  77. <script>
  78. export default {
  79. data() {
  80. return {
  81. toolsShow: false,
  82. editorContent: null,
  83. headShow: false,
  84. boldShow: false,
  85. italicShow: false,
  86. artData: {
  87. title: '',
  88. content: ''
  89. },
  90. baseFormData: {
  91. name: '',
  92. age: '',
  93. introduction: '',
  94. sex: 2,
  95. hobby: [5],
  96. datetimesingle: 1627529992399
  97. },
  98. // 单选数据源
  99. sexs: [{
  100. text: '男',
  101. value: 0
  102. }, {
  103. text: '女',
  104. value: 1
  105. }, {
  106. text: '保密',
  107. value: 2
  108. }],
  109. // 多选数据源
  110. hobbys: [{
  111. text: '跑步',
  112. value: 0
  113. }, {
  114. text: '游泳',
  115. value: 1
  116. }, {
  117. text: '绘画',
  118. value: 2
  119. }, {
  120. text: '足球',
  121. value: 3
  122. }, {
  123. text: '篮球',
  124. value: 4
  125. }, {
  126. text: '其他',
  127. value: 5
  128. }],
  129. value: '',
  130. password: '',
  131. placeholderStyle: "color:#2979FF;font-size:14px",
  132. styles: {
  133. color: '#2979FF',
  134. borderColor: '#2979FF'
  135. },
  136. id:'',
  137. fileList1: []
  138. }
  139. },
  140. onLoad(options) {
  141. this.id = options.id;
  142. },
  143. onReady() {},
  144. methods: {
  145. input(e) {
  146. console.log('输入内容:', e);
  147. },
  148. iconClick(type) {
  149. uni.showToast({
  150. title: `点击了${type==='prefix'?'左侧':'右侧'}的图标`,
  151. icon: 'none'
  152. })
  153. },
  154. //*****************************************富文本编辑器方法**********************************************//
  155. // 初始化
  156. onEditReady() {
  157. // 查询节点信息的对象
  158. // 将选择器的选取范围更改为自定义组件 component 内,返回一个 SelectorQuery 对象实例。(初始时,选择器仅选取页面范围的节点,不会选取任何自定义组件中的节点)。
  159. // 在当前页面下选择第一个匹配选择器 selector 的节点,返回一个 NodesRef 对象实例,可以用于获取节点信息。
  160. // 获取节点的相关信息。第一个参数是节点相关信息配置(必选);第二参数是方法的回调函数,参数是指定的相关节点信息。
  161. // 执行所有的请求。请求结果按请求次序构成数组,在 callback 的第一个参数中返回。
  162. uni.createSelectorQuery()
  163. .in(this)
  164. .select('.myEdit')
  165. .fields(
  166. {
  167. size: true, //是否返回节点尺寸(width height)
  168. context: true //是否返回节点尺寸(width height)
  169. },
  170. (res) => {
  171. console.log(res);
  172. this.editorContent = res.context;
  173. }
  174. )
  175. .exec();
  176. },
  177. // 离开焦点
  178. OnFocus() {
  179. this.toolsShow = true;
  180. },
  181. // 添加分割线
  182. insertDivider() {
  183. this.editorContent.insertDivider();
  184. },
  185. // 添加大标题
  186. headChange() {
  187. this.headShow = !this.headShow;
  188. this.editorContent.format('header', this.headShow ? 'H2' : false);
  189. },
  190. // 加粗
  191. boldChange() {
  192. this.boldShow = !this.boldShow;
  193. this.editorContent.format('bold');
  194. },
  195. // 斜体
  196. italicChange() {
  197. this.italicShow = !this.italicShow;
  198. this.editorContent.format('italic');
  199. },
  200. // 用于检查对象detail是否包含名为name的属性
  201. checkStatus(name, detail, key) {
  202. if (detail.hasOwnProperty(name)) {
  203. this[key] = true;
  204. } else {
  205. this[key] = false;
  206. }
  207. },
  208. // 通过 Context 方法改变编辑器内样式时触发,返回选区已设置的样式(解决标题标签回弹问题)
  209. statuschange(e) {
  210. let detail = e.detail;
  211. console.log(detail);
  212. this.checkStatus('header', detail, 'headShow');
  213. this.checkStatus('bold', detail, 'boldShow');
  214. this.checkStatus('italic', detail, 'italicShow');
  215. },
  216. // 添加图像
  217. insertImage() {
  218. // 打开相机
  219. uni.chooseImage({
  220. count: 3, //默认9
  221. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  222. success: async (res) => {
  223. let tempFiles = res.tempFiles;
  224. for (let item of tempFiles) {
  225. uni.showLoading({
  226. mask: true,
  227. title: '上传中...'
  228. });
  229. try {
  230. let res = await uniCloud.uploadFile({
  231. filePath: item.path, //要上传的文件对象
  232. cloudPath: item.name //cloudPath为云端文件名,
  233. });
  234. // 设置图片地址 图片地址,仅支持 http(s)、base64、本地图片
  235. this.editorContent.insertImage({
  236. src: res.fileID
  237. });
  238. uni.hideLoading();
  239. } catch (e) {
  240. uni.hideLoading();
  241. uni.showModal({
  242. content: '上传失败',
  243. title: '提示'
  244. });
  245. }
  246. }
  247. }
  248. });
  249. },
  250. // 确认
  251. editOk() {
  252. this.toolsShow = false;
  253. },
  254. onSubmit() {
  255. console.log(11);
  256. this.editorContent.getContents({
  257. success: (res) => {
  258. console.log(res);
  259. this.artData.content = res.html;
  260. }
  261. });
  262. },
  263. //*****************************************富文本编辑器方法**********************************************//
  264. uploadImagePhoto() {
  265. uni.chooseImage({
  266. count: 9, //默认9
  267. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  268. sourceType: ['album'], //从相册选择
  269. success: function (res) {
  270. }
  271. });
  272. },
  273. // 新增图片
  274. async afterRead(event) {
  275. console.log(event)
  276. // 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
  277. let lists = [].concat(event.file)
  278. let fileListLen = this[`fileList${event.name}`].length
  279. lists.map((item) => {
  280. this[`fileList${event.name}`].push({
  281. ...item,
  282. status: 'uploading',
  283. message: '上传中'
  284. })
  285. })
  286. for (let i = 0; i < lists.length; i++) {
  287. const result = await this.uploadFilePromise(lists[i].url)
  288. let item = this[`fileList${event.name}`][fileListLen]
  289. this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
  290. status: 'success',
  291. message: '',
  292. url: result
  293. }))
  294. fileListLen++
  295. }
  296. },
  297. //上传图片
  298. uploadFilePromise(url) {
  299. return new Promise((resolve, reject) => {
  300. let a = uni.uploadFile({
  301. //url: this.$common.domain+'/api/common/upload', // 仅为示例,非真实的接口地址
  302. // url:'http://192.168.2.21:7001/upload', // 仅为示例,非真实的接口地址
  303. filePath: url,
  304. name: 'file',
  305. formData: {
  306. user: 'test'
  307. },
  308. success: (res) => {
  309. let data=JSON.parse(res.data) //最终传给的是字符串,这里需要转换格式
  310. resolve(data.data.url)
  311. }
  312. });
  313. })
  314. }
  315. }
  316. }
  317. </script>
  318. <style scoped lang="scss">
  319. .uni-mt-5 {
  320. margin-top: 5px;
  321. }
  322. /deep/ .ql-blank::before {
  323. font-style: normal;
  324. color: #e0e0e0;
  325. }
  326. .edit {
  327. padding: 30rpx;
  328. .title {
  329. input {
  330. height: 120rpx;
  331. font-size: 46rpx;
  332. border-bottom: 1px solid #e4e4e4;
  333. margin-bottom: 30rpx;
  334. color: #000;
  335. }
  336. .placeholderClass {
  337. color: #e0e0e0;
  338. }
  339. }
  340. .content {
  341. .myEdit {
  342. height: calc(100vh - 500rpx);
  343. margin-bottom: 30rpx;
  344. }
  345. }
  346. .tools {
  347. width: 100%;
  348. height: 80rpx;
  349. background: #fff;
  350. border-top: 1rpx solid #f4f4f4;
  351. position: fixed;
  352. left: 0;
  353. bottom: 0;
  354. display: flex;
  355. justify-content: space-around;
  356. align-items: center;
  357. .iconfont {
  358. font-size: 36rpx;
  359. color: #333;
  360. // 选择按钮的高亮
  361. &.active {
  362. color: #0199fe;
  363. }
  364. }
  365. }
  366. }
  367. </style>