裂变星小程序-25.03.04
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

400 lines
10 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
  1. <template>
  2. <view class="page">
  3. <navbar title="文章分享" leftClick @leftClick="$utils.navigateBack" />
  4. <view class="content">
  5. <uv-form
  6. ref="form"
  7. :model="form"
  8. :rules="getRules()"
  9. labelPosition="left"
  10. labelWidth="300rpx"
  11. :labelStyle="{
  12. color: '#1B1B1B',
  13. fontSize: '32rpx',
  14. fontWeight: 'bold',
  15. }"
  16. errorType="toast"
  17. >
  18. <view class="form-item">
  19. <uv-form-item label="用户ID" prop="userId">
  20. <view class="form-item-content">
  21. <text>{{ userId }}</text>
  22. <view style="margin-left: 20rpx;">
  23. <button class="btn-simple" plain @click="$utils.copyText(userId)">
  24. <uv-icon name="file-text" color="#05D9A2" size="28rpx"></uv-icon>
  25. </button>
  26. </view>
  27. </view>
  28. </uv-form-item>
  29. </view>
  30. <view class="form-item">
  31. <uv-form-item label="选择封面图" prop="headImage">
  32. <view class="form-item-content">
  33. <formUpload v-model="form.headImage">
  34. <template v-slot="{ value }">
  35. <view class="flex" style="min-width: 116rpx; height: 45rpx;">
  36. <image
  37. :src="value"
  38. mode="aspectFill"
  39. style="width: 68rpx; height: 68rpx;"
  40. radius="14rpx"
  41. />
  42. <uv-icon style="margin-left: 20rpx" name="arrow-right" color="#000000" size="28rpx"></uv-icon>
  43. </view>
  44. </template>
  45. </formUpload>
  46. </view>
  47. </uv-form-item>
  48. </view>
  49. <view class="form-item">
  50. <uv-form-item label="标题" labelWidth="84rpx" prop="headTitle">
  51. <view class="form-item-content">
  52. <formInput v-model="form.headTitle" placeholder="请输入你的文章标题" width="584rpx"></formInput>
  53. </view>
  54. </uv-form-item>
  55. </view>
  56. <view class="form-item">
  57. <uv-form-item label="设置转发次数(次)" prop="num">
  58. <view class="form-item-content">
  59. <formNumberBox v-model="form.num" ></formNumberBox>
  60. </view>
  61. </uv-form-item>
  62. </view>
  63. <view class="form-item">
  64. <uv-form-item label="选择二维码" prop="wxCodeImage">
  65. <view class="form-item-content">
  66. <formUpload v-model="form.wxCodeImage">
  67. <template v-slot="{ value }">
  68. <view class="flex" style="min-width: 93rpx; height: 45rpx;">
  69. <image
  70. :src="value"
  71. mode="aspectFill"
  72. style="width: 45rpx; height: 45rpx;"
  73. radius="14rpx"
  74. />
  75. <uv-icon style="margin-left: 20rpx" name="arrow-right" color="#000000" size="28rpx"></uv-icon>
  76. </view>
  77. </template>
  78. </formUpload>
  79. </view>
  80. </uv-form-item>
  81. </view>
  82. <view class="form-item">
  83. <uv-form-item label="文章内容" prop="textDetails" labelPosition="top">
  84. <view class="editor__view" style="margin-top: 32rpx;">
  85. <editor id="editor" class="editor"
  86. placeholder="请输入你的文章内容"
  87. @ready="onEditorReady"
  88. @input="onEditroInput"
  89. ></editor>
  90. <view class="flex editor-tools">
  91. <view style="flex: 1;">
  92. <button @click="insertImage" plain class="btn-simple" style="float: left; font-size: 0;">
  93. <image src="../static/record/icon-add.png" style="width: 126rpx; height: 126rpx;" ></image>
  94. </button>
  95. </view>
  96. <view style="flex: 1; text-align: right;">
  97. <text class="editor-count">{{ `${descLen}/${descLengthLimit}` }}</text>
  98. </view>
  99. </view>
  100. </view>
  101. </uv-form-item>
  102. </view>
  103. </uv-form>
  104. </view>
  105. <!-- 审核通过 -->
  106. <button v-if="status === 1" class="button-submit" open-type="share">
  107. 发布
  108. </button>
  109. <!-- 不是 审核中 已发布 -> 创建分享 审核不通过 -->
  110. <button v-else-if="![0,1].includes(status)" class="button-submit" @click="preSubmit">
  111. 提交审核
  112. </button>
  113. <autoCrop ref="autoCropRef" @change="onSubmit"></autoCrop>
  114. </view>
  115. </template>
  116. <script>
  117. import { mapState } from 'vuex'
  118. import formInput from '../components/formInput.vue'
  119. import formNumberBox from '../components/formNumberBox.vue'
  120. import formUpload from '../components/formUpload.vue'
  121. import formTextarea from '../components/formTextarea.vue'
  122. import autoCrop from '../components/autoCrop.vue'
  123. export default {
  124. components: {
  125. formInput,
  126. formNumberBox,
  127. formUpload,
  128. formTextarea,
  129. autoCrop,
  130. },
  131. data() {
  132. return {
  133. id: null,
  134. status: null,
  135. form: {
  136. userId: null,
  137. headImage: null,
  138. headTitle: null,
  139. num: 0,
  140. wxCodeImage: null,
  141. textDetails: null,
  142. },
  143. editorCtx: null,
  144. descLen: 0,
  145. descLengthLimit: 1000,
  146. }
  147. },
  148. computed: {
  149. ...mapState(['userInfo']),
  150. userId() {
  151. return this.userInfo?.intentionCode
  152. },
  153. },
  154. onLoad(option) {
  155. const { id } = option
  156. if (!id) {
  157. return
  158. }
  159. this.id = id
  160. this.fetchDetails(id)
  161. },
  162. onReady() {
  163. this.$refs.form.setRules(this.getRules());
  164. },
  165. onShareAppMessage(res) {
  166. const {
  167. headTitle,
  168. headImage,
  169. } = this.form
  170. let o = {
  171. title : headTitle,
  172. imageUrl: headImage,
  173. path: `/pages_order/sharing/article?id=${this.id}&state=3&shareId=${this.userInfo.id}`
  174. }
  175. //调用增加分享次数的方法
  176. const params = {
  177. id:this.id,
  178. state:"3",
  179. }
  180. this.$fetch('addLogShareInfo', params)
  181. // todo: get times and check is unlocked
  182. this.isLocked = false
  183. return o
  184. },
  185. methods: {
  186. onEditorReady() {
  187. uni.createSelectorQuery().select('#editor').context((res) => {
  188. this.editorCtx = res.context
  189. }).exec()
  190. },
  191. onEditroInput(e) {
  192. const { text } = e.detail
  193. this.descLen = text?.length || 0
  194. },
  195. insertImage() {
  196. uni.chooseImage({
  197. count: 1,
  198. success: (res) => {
  199. // this.editorCtx.insertImage({
  200. // src: res.tempFilePaths[0],
  201. // alt: '图像',
  202. // })
  203. // todo: check
  204. this.$Oss.ossUpload(res.tempFilePaths[0]).then(url => {
  205. this.editorCtx.insertImage({
  206. src: url,
  207. alt: '图像',
  208. })
  209. })
  210. }
  211. })
  212. },
  213. setEditorContents(html) {
  214. if (!this.editorCtx) {
  215. setTimeout(() => {
  216. this.setEditorContents(html)
  217. }, 200)
  218. return
  219. }
  220. this.editorCtx.setContents({ html })
  221. },
  222. getEditorContents() {
  223. return new Promise((resolve, reject) => {
  224. this.editorCtx.getContents({
  225. success: (e) => {
  226. const { html, text } = e
  227. resolve({ html, text })
  228. },
  229. fail: () => {
  230. reject()
  231. }
  232. })
  233. })
  234. },
  235. getRules() {
  236. const textDetailsValidator = async (rule, value, callback) => {
  237. const textDetails = (await this.getEditorContents())?.html
  238. if (textDetails) {
  239. callback()
  240. return
  241. }
  242. callback(new Error('请输入文章内容'))
  243. }
  244. return {
  245. 'headImage': {
  246. type: 'string',
  247. required: true,
  248. message: '请选择封面图',
  249. },
  250. 'headTitle': {
  251. type: 'string',
  252. required: true,
  253. message: '请输入你的文章标题',
  254. },
  255. 'num': {
  256. type: 'number',
  257. required: true,
  258. message: '请设置转发次数(大于0)',
  259. validator: (rule, value, callback) => {
  260. if (value > 0) {
  261. return true
  262. }
  263. return false;
  264. },
  265. },
  266. 'wxCodeImage': {
  267. type: 'string',
  268. required: true,
  269. message: '请选择二维码',
  270. },
  271. 'textDetails': {
  272. asyncValidator: textDetailsValidator,
  273. },
  274. }
  275. },
  276. async fetchDetails(id) {
  277. try {
  278. const result = await this.$fetch('getArticleShareInfo', { id })
  279. const {
  280. userId,
  281. headImage,
  282. headTitle,
  283. num,
  284. wxCodeImage,
  285. textDetails,
  286. status,
  287. } = result || {}
  288. this.form = {
  289. userId,
  290. headImage,
  291. headTitle,
  292. num,
  293. wxCodeImage,
  294. textDetails,
  295. }
  296. this.status = status
  297. this.setEditorContents(textDetails)
  298. } catch (err) {
  299. }
  300. },
  301. async preSubmit() {
  302. try {
  303. await this.$refs.form.validate()
  304. this.$refs.autoCropRef.set(this.form.headImage)
  305. } catch (err) {
  306. }
  307. },
  308. async onSubmit(headImage) {
  309. try {
  310. await this.$refs.form.validate()
  311. const textDetails = (await this.getEditorContents())?.html
  312. const {
  313. headTitle,
  314. num,
  315. wxCodeImage,
  316. } = this.form
  317. const params = {
  318. userId: this.userId,
  319. headImage,
  320. headTitle,
  321. num,
  322. wxCodeImage,
  323. textDetails,
  324. }
  325. if (this.id) {
  326. params.id = this.id
  327. }
  328. await this.$fetch('saveOrUpdateArticleShare', params)
  329. uni.showToast({
  330. title: '提交成功',
  331. icon: 'none'
  332. })
  333. setTimeout(uni.navigateBack, 1000, -1)
  334. } catch (err) {
  335. }
  336. },
  337. }
  338. }
  339. </script>
  340. <style scoped lang="scss">
  341. @import '../styles/pageForm.scss';
  342. .editor__view {
  343. background-color: #F3F3F3;
  344. border-radius: 12rpx;
  345. }
  346. .editor {
  347. height: 253rpx;
  348. padding: 20rpx;
  349. &-tools {
  350. align-items: flex-end;
  351. padding: 0 20rpx 16rpx 14rpx;
  352. }
  353. &-count {
  354. color: #999999;
  355. font-size: 28rpx;
  356. }
  357. }
  358. </style>