create.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <template>
  2. <div class="create-opensource-wrapper">
  3. <div class="create-opensource-title">新建开源项目</div>
  4. <div class="form-wrapper">
  5. <!-- 标题 -->
  6. <div class="title-field">
  7. <div class="field-name"><span class="required">*</span> 项目标题</div>
  8. <el-input
  9. class="title-input"
  10. v-model="title"
  11. maxlength="50"
  12. show-word-limit
  13. placeholder="请输入项目标题"></el-input>
  14. </div>
  15. <!-- 描述 -->
  16. <div class="content-field">
  17. <div class="content-field-name">
  18. <div><span class="required">*</span> 项目介绍</div>
  19. <!-- <a class="example" :href="`/c/${exampleUid}?from=example`" target="_blank">查看示例</a> -->
  20. </div>
  21. <el-input
  22. class="content-input"
  23. type="textarea"
  24. :rows="8"
  25. :autosize="{ minRows: 8 }"
  26. placeholder="请输入项目介绍"
  27. v-model="content"></el-input>
  28. </div>
  29. <!-- 分类 -->
  30. <div class="price-field">
  31. <div class="cate-wrapper">
  32. <div class="field-name"><span class="required">*</span> 项目分类</div>
  33. <div class="cate-content">
  34. <el-cascader
  35. class="cate-cascader"
  36. v-model="selectedOpensourceCate"
  37. :options="opensourceCate"
  38. :props="{ expandTrigger: 'hover' }"
  39. placeholder="请选择类别"></el-cascader>
  40. </div>
  41. </div>
  42. </div>
  43. <!-- 项目源码链接 -->
  44. <div class="sourcelink-field">
  45. <div class="field-name">项目源码链接</div>
  46. <el-input
  47. class="sourcelink-input"
  48. v-model="sourcelink"
  49. placeholder="请添加github或gitee项目链接"></el-input>
  50. </div>
  51. <!-- 图片 -->
  52. <div class="image-field">
  53. <div class="field-name">项目logo</div>
  54. <multi-uploader
  55. class="opensource-image-uploader"
  56. :showTips="false"
  57. :limit="1"
  58. v-model="imageList"></multi-uploader>
  59. <div class="upload-tips"><strong style="color: #19222e;">请优先上传项目logo</strong>
  60. 请优先注重手机浏览体验,分辨率至少800*400,最多5M
  61. 电脑浏览可在上述基础上增加宽度 1500*400
  62. </div>
  63. </div>
  64. <!-- 按钮 -->
  65. <div class="actions-wrapper">
  66. <el-button
  67. class="create-btn"
  68. :loading="publishIsLoading"
  69. @click="handleCreate">保存并提交审核</el-button>
  70. </div>
  71. </div>
  72. </div>
  73. </template>
  74. <script>
  75. import { mapState } from "vuex";
  76. import editor from "@/components/editor";
  77. import multiUploader from '@/components/multi-uploader';
  78. // 查看示例 uid
  79. const exampleUid = process.env.NODE_ENV == "development" ? '680581' : '583073'
  80. export default {
  81. head: {
  82. title: '新建开源项目'
  83. },
  84. components: {
  85. editor,
  86. multiUploader
  87. },
  88. data () {
  89. return {
  90. sale_id: '', // 编辑时的 id
  91. title: '', // 标题
  92. selectedOpensourceCate: [], // 选中的开源充电分类
  93. opensourceCate: [], // 开源充电分类数据源
  94. content: '', // 话题描述
  95. contentText: '', // 话题描述文字
  96. imageList: [], // 用户上传的图片
  97. sourcelink: '', // 源码链接
  98. publishIsLoading: false,
  99. exampleUid,
  100. }
  101. },
  102. computed: {
  103. },
  104. mounted () {
  105. this.needLogin()
  106. this._getOpensourceCate()
  107. this.sale_id = this.$route.query.sale_id || ''
  108. if (this.sale_id) {
  109. this._getOpensourceDetail()
  110. }
  111. },
  112. methods: {
  113. /** 获取开源充电分类 */
  114. _getOpensourceCate () {
  115. this.$axios.$post('/api/sale/cateListYes', { type: 5, point: 1 }).then(res => {
  116. if (res.status === 1) {
  117. let opensourceCate = res.data || []
  118. this.opensourceCate = opensourceCate.map(item => {
  119. let children = item.child_list.map(child => {
  120. return {
  121. value: child.category_id,
  122. label: child.name
  123. }
  124. })
  125. return {
  126. value: item.category_id,
  127. label: item.name,
  128. children: children
  129. }
  130. })
  131. }
  132. }).catch(err => {
  133. console.log('get opensource cate error: ', err)
  134. })
  135. },
  136. /** 获取开源充电服务详情 */
  137. _getOpensourceDetail () {
  138. const self = this
  139. this.$axios.$post('/api/sale/saleInfo', { sale_id: this.sale_id }).then(res => {
  140. if (res.status === 1) {
  141. console.log('获取开源充电详情成功', res.data)
  142. self.title = res.data.title || ""
  143. self.content = res.data.content || ""
  144. if (res.data.cate_id_one && res.data.cate_id_two) {
  145. self.selectedOpensourceCate = [res.data.cate_id_one, res.data.cate_id_two]
  146. }
  147. self.sourcelink = res.data.source_link || ""
  148. if (res.data.image) {
  149. self._setOpensourceImages(res.data)
  150. }
  151. }
  152. })
  153. },
  154. /** 编辑时初始化图片列表 */
  155. _setOpensourceImages (data) {
  156. let images = data.image.split(',')
  157. if (images && images.length > 0) {
  158. images.forEach((image, index) => {
  159. let imageName = this._getImageName(image);
  160. console.log("imageName", imageName);
  161. this.imageList.push({ name: imageName + index, url: image });
  162. })
  163. }
  164. },
  165. /** 通过 image url 获取名字 */
  166. _getImageName (url) {
  167. if (url) {
  168. try {
  169. const lastquotaIndex = url.lastIndexOf("/");
  170. const lastDotIndex = url.lastIndexOf('.');
  171. return url.substring(lastquotaIndex + 1, lastDotIndex);
  172. } catch (e) {
  173. console.log(e);
  174. }
  175. }
  176. return ""
  177. },
  178. /** 创建、编辑开源充电服务 */
  179. _publishOpensourceData (actType) {
  180. const self = this
  181. const data = {
  182. title: this.title,
  183. content: this.content,
  184. img: this.imageList.map((it) => it.url).join(","),
  185. type: 5,
  186. cate_id_one: this.selectedOpensourceCate[0],
  187. cate_id_two: this.selectedOpensourceCate[1],
  188. act: actType,
  189. source_link: this.sourcelink,
  190. sale_id: this.sale_id || '',
  191. }
  192. this.publishIsLoading = true
  193. this.$axios.$post('/api/sale/create', data).then(res => {
  194. if (res.status === 1) {
  195. self.$message.success('您已成功提交开源充电服务审核,平台将在1-3个工作日内完成审核,并通过短信、邮件通知您')
  196. setTimeout(() => {
  197. window.location.href = '/workbench/opensource/index'
  198. }, 1200)
  199. } else {
  200. console.error('保存失败')
  201. }
  202. }).then(() => {
  203. setTimeout(() => {
  204. self.publishIsLoading = false
  205. }, 800)
  206. })
  207. },
  208. /** 验证提交的数据 */
  209. _validateOpensourceData () {
  210. if (this.title === '') {
  211. this.$message.error('请输入项目标题')
  212. return false
  213. }
  214. if (this.title.length < 5) {
  215. this.$message.error('项目标题不可少于5字符')
  216. return false
  217. }
  218. if (this.title.length > 50) {
  219. this.$message.error('项目标题不可超过50字符,请删减')
  220. return false
  221. }
  222. if (this.content.length < 50) {
  223. this.$message.error('项目描述不可少于50字符')
  224. return false
  225. }
  226. if (this.content.length > 5000) {
  227. this.$message.error('项目描述不可超过5000字符,请删减')
  228. return false
  229. }
  230. if (this.selectedOpensourceCate.length !== 2 ||
  231. (!this.selectedOpensourceCate[0] || !this.selectedOpensourceCate[1])) {
  232. this.$message.error('请选择项目类别')
  233. return false
  234. }
  235. return true
  236. },
  237. /** 点击提交审核时 */
  238. handleCreate () {
  239. if (this._validateOpensourceData()) {
  240. this._publishOpensourceData(this.sale_id ? 'update' : 'create')
  241. }
  242. },
  243. }
  244. }
  245. </script>
  246. <style scope lang="scss">
  247. @import "@/assets/css/opensource/create.scss";
  248. </style>
  249. <style>
  250. input::-webkit-outer-spin-button,
  251. input::-webkit-inner-spin-button {
  252. -webkit-appearance: none !important;
  253. }
  254. input[type="number"]{
  255. -moz-appearance: textfield !important;
  256. }
  257. </style>
  258. <style lang="scss">
  259. // 图片上传器样式
  260. .opensource-image-uploader {
  261. .el-upload-list__item {
  262. width: 300px !important;
  263. height: 168.75px !important;
  264. line-height: 168.75px !important;
  265. }
  266. .el-upload {
  267. .el-upload-dragger {
  268. width: 300px !important;
  269. height: 168.75px !important;
  270. display: flex !important;
  271. align-items: center;
  272. justify-content: center;
  273. }
  274. }
  275. .el-upload--picture-card {
  276. width: 300px !important;
  277. height: 168.75px !important;
  278. line-height: 168.75px !important;
  279. .el-upload-list__item-thumbnail {
  280. object-fit: scale-down !important;
  281. }
  282. }
  283. .el-upload-list--picture-card {
  284. .el-upload-list__item-thumbnail {
  285. object-fit: scale-down !important;
  286. }
  287. }
  288. }
  289. </style>