裂变星小程序-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.

392 lines
10 KiB

1 week 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="onSubmit">
  111. 提交审核
  112. </button>
  113. </view>
  114. </template>
  115. <script>
  116. import { mapState } from 'vuex'
  117. import formInput from '../components/formInput.vue'
  118. import formNumberBox from '../components/formNumberBox.vue'
  119. import formUpload from '../components/formUpload.vue'
  120. import formTextarea from '../components/formTextarea.vue'
  121. export default {
  122. components: {
  123. formInput,
  124. formNumberBox,
  125. formUpload,
  126. formTextarea,
  127. },
  128. data() {
  129. return {
  130. id: null,
  131. status: null,
  132. form: {
  133. userId: null,
  134. headImage: null,
  135. headTitle: null,
  136. num: 0,
  137. wxCodeImage: null,
  138. textDetails: null,
  139. },
  140. editorCtx: null,
  141. descLen: 0,
  142. descLengthLimit: 1000,
  143. }
  144. },
  145. computed: {
  146. ...mapState(['userInfo']),
  147. userId() {
  148. return this.form.userId || this.userInfo?.id
  149. },
  150. },
  151. onLoad(option) {
  152. const { id } = option
  153. if (!id) {
  154. return
  155. }
  156. this.id = id
  157. this.fetchDetails(id)
  158. },
  159. onReady() {
  160. this.$refs.form.setRules(this.getRules());
  161. },
  162. onShareAppMessage(res) {
  163. const {
  164. headTitle,
  165. headImage,
  166. } = this.form
  167. let o = {
  168. title : headTitle,
  169. imageUrl: headImage,
  170. path: `/pages_order/sharing/article?id=${this.id}&state=3&shareId=${this.userInfo.id}`
  171. }
  172. //调用增加分享次数的方法
  173. const params = {
  174. id:this.id,
  175. state:"4",
  176. }
  177. this.$fetch('addLogShareInfo', params)
  178. // todo: get times and check is unlocked
  179. this.isLocked = false
  180. return o
  181. },
  182. methods: {
  183. onEditorReady() {
  184. uni.createSelectorQuery().select('#editor').context((res) => {
  185. this.editorCtx = res.context
  186. }).exec()
  187. },
  188. onEditroInput(e) {
  189. const { text } = e.detail
  190. this.descLen = text?.length || 0
  191. },
  192. insertImage() {
  193. uni.chooseImage({
  194. count: 1,
  195. success: (res) => {
  196. // this.editorCtx.insertImage({
  197. // src: res.tempFilePaths[0],
  198. // alt: '图像',
  199. // })
  200. // todo: check
  201. this.$Oss.ossUpload(res.tempFilePaths[0]).then(url => {
  202. this.editorCtx.insertImage({
  203. src: url,
  204. alt: '图像',
  205. })
  206. })
  207. }
  208. })
  209. },
  210. setEditorContents(html) {
  211. if (!this.editorCtx) {
  212. setTimeout(() => {
  213. this.setEditorContents(html)
  214. }, 200)
  215. return
  216. }
  217. this.editorCtx.setContents({ html })
  218. },
  219. getEditorContents() {
  220. return new Promise((resolve, reject) => {
  221. this.editorCtx.getContents({
  222. success: (e) => {
  223. const { html, text } = e
  224. resolve({ html, text })
  225. },
  226. fail: () => {
  227. reject()
  228. }
  229. })
  230. })
  231. },
  232. getRules() {
  233. const textDetailsValidator = async (rule, value, callback) => {
  234. const textDetails = (await this.getEditorContents())?.html
  235. if (textDetails) {
  236. callback()
  237. return
  238. }
  239. callback(new Error('请输入你的文案'))
  240. }
  241. return {
  242. 'headImage': {
  243. type: 'string',
  244. required: true,
  245. message: '请选择头像',
  246. },
  247. 'headTitle': {
  248. type: 'string',
  249. required: true,
  250. message: '请输入你的昵称',
  251. },
  252. 'num': {
  253. type: 'number',
  254. required: true,
  255. message: '请设置转发次数(大于1)',
  256. validator: (rule, value, callback) => {
  257. if (value > 1) {
  258. return true
  259. }
  260. return false;
  261. },
  262. },
  263. 'wxCodeImage': {
  264. type: 'string',
  265. required: true,
  266. message: '请选择二维码',
  267. },
  268. 'textDetails': {
  269. asyncValidator: textDetailsValidator,
  270. },
  271. }
  272. },
  273. async fetchDetails(id) {
  274. try {
  275. const result = await this.$fetch('getArticleShareInfo', { id })
  276. const {
  277. userId,
  278. headImage,
  279. headTitle,
  280. num,
  281. wxCodeImage,
  282. textDetails,
  283. status,
  284. } = result || {}
  285. this.form = {
  286. userId,
  287. headImage,
  288. headTitle,
  289. num,
  290. wxCodeImage,
  291. textDetails,
  292. }
  293. this.status = status
  294. this.setEditorContents(textDetails)
  295. } catch (err) {
  296. }
  297. },
  298. async onSubmit() {
  299. try {
  300. await this.$refs.form.validate()
  301. const textDetails = (await this.getEditorContents())?.html
  302. const {
  303. headImage,
  304. headTitle,
  305. num,
  306. wxCodeImage,
  307. } = this.form
  308. const params = {
  309. id: this.id,
  310. userId: this.userId,
  311. headImage,
  312. headTitle,
  313. num,
  314. wxCodeImage,
  315. textDetails,
  316. }
  317. if (this.id) {
  318. params.id = this.id
  319. }
  320. await this.$fetch('saveOrUpdateArticleShare', params)
  321. uni.showToast({
  322. title: '提交成功',
  323. icon: 'none'
  324. })
  325. setTimeout(uni.navigateBack, 1000, -1)
  326. } catch (err) {
  327. }
  328. },
  329. onPublish() {
  330. // todo
  331. },
  332. }
  333. }
  334. </script>
  335. <style scoped lang="scss">
  336. @import '../styles/pageForm.scss';
  337. .editor__view {
  338. background-color: #F3F3F3;
  339. border-radius: 12rpx;
  340. }
  341. .editor {
  342. height: 253rpx;
  343. padding: 20rpx;
  344. &-tools {
  345. align-items: flex-end;
  346. padding: 0 20rpx 16rpx 14rpx;
  347. }
  348. &-count {
  349. color: #999999;
  350. font-size: 28rpx;
  351. }
  352. }
  353. </style>