goods_classify_add_edit.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <template>
  2. <div class="container add_edit">
  3. <el-form :model="form" ref="ruleForm" :rules="rules" label-width="100px">
  4. <el-form-item label="上级分类">
  5. <el-select class="sele" v-model="form.pid" placeholder="请选择" :disabled="dataId != ''">
  6. <el-option v-for="(item, idx) in classData" :key="idx" :label="item.name" :value="item.id">
  7. </el-option>
  8. </el-select>
  9. </el-form-item>
  10. <el-form-item label="分类名称" prop="name" ref="name">
  11. <el-input v-model="form.name" placeholder="分类名称"></el-input>
  12. </el-form-item>
  13. <el-form-item label="图标">
  14. <uni-file-picker v-model="imageValue" fileMediatype="image" returnType="object"
  15. :image-styles="imageStyle" @select="imgUpload" @delete="imgDelete" />
  16. </el-form-item>
  17. <el-form-item label="排序">
  18. <el-input type="number" v-model.number="form.sort" min="0" placeholder="请输入排序"></el-input>
  19. <div class="remark_txt">此处排序将决定前端模块的位置,数字越小,位置越前</div>
  20. </el-form-item>
  21. <el-form-item label="状态">
  22. <el-switch v-model="form.state" active-color="#ff6a6c" inactive-color="#bbb" active-value="1"
  23. inactive-value="0"></el-switch>
  24. </el-form-item>
  25. <el-form-item>
  26. <el-button class="confirm_btn" @click="submitForm('ruleForm')">确 定</el-button>
  27. <el-button @click="returnPage">返回列表</el-button>
  28. </el-form-item>
  29. </el-form>
  30. </div>
  31. </template>
  32. <script>
  33. import bus from '@/common/bus';
  34. const __name = 'usemall-goods-category';
  35. import { cos } from '@/util/cos.js'
  36. export default {
  37. props: {},
  38. data() {
  39. return {
  40. dataId: '',
  41. form: {
  42. sort: 0,
  43. pid: '',
  44. name: '',
  45. icon: '',
  46. state: '1',
  47. level:'',
  48. id:'',
  49. },
  50. imageValue: null,
  51. imageStyle: {
  52. height: '150px',
  53. width: '150px'
  54. },
  55. classData: [],
  56. rules: {
  57. name: [{
  58. required: true,
  59. message: '请输入分类名称',
  60. trigger: 'change'
  61. }]
  62. },
  63. now_date:'',//当前时间
  64. };
  65. },
  66. methods: {
  67. loadData() {
  68. this.getNowDate()
  69. //获取依据数据
  70. this.$axios.get("/goodsCategory/open/list",
  71. {
  72. params:{
  73. },
  74. headers:{
  75. }
  76. }).then(response => {
  77. let res = response
  78. if (res.success) {
  79. this.classData = res.data
  80. console.log(this.classData)
  81. } else {
  82. }
  83. }).catch(res =>{
  84. });
  85. // this.$db[__name]
  86. // .where({
  87. // pid: ''
  88. // })
  89. // .tolist({
  90. // page: 1,
  91. // rows: 500
  92. // })
  93. // .then(res => {
  94. // if (res.code == 200) {
  95. // this.classData = res.datas.filter(x => !x.pid);
  96. // this.classData.unshift({
  97. // _id: '',
  98. // name: '请选择'
  99. // });
  100. // }
  101. // });
  102. if (!this.dataId) {
  103. return;
  104. }
  105. // this.$db[__name].tofirst(this.dataId).then(res => {
  106. // if (res.code == 200) {
  107. // for (let item in this.form) {
  108. // this.form[item] = res.datas[item];
  109. // }
  110. // if (this.form.img) {
  111. // this.imageValue = {};
  112. // this.imageValue.url = this.form.img;
  113. // }
  114. // }
  115. // });
  116. },
  117. submitForm(formName) {
  118. this.$refs[formName].validate((valid, obj) => {
  119. // 默认获取第一个未验证 form 属性名
  120. this.$api.set_unvalidated_form_focus(this, obj);
  121. if (valid) {
  122. console.log(!this.form.pid)
  123. console.log(this.form.name)
  124. if(!this.form.pid){
  125. var level=1
  126. }else{
  127. level=2
  128. }
  129. if (!this.dataId) {//新增
  130. this.$axios.post("/goodsCategory/admin/save",
  131. {
  132. 'createBy':uni.getStorageSync("nickName"),//创建人
  133. 'createTime':this.now_date,//创建时间
  134. "name": this.form.name,
  135. "icon": this.form.icon,
  136. "level": level,
  137. 'sort':this.form.sort,//排序
  138. 'state':this.form.state,
  139. 'pid':this.form.pid
  140. },
  141. {
  142. headers:{
  143. 'Mall-Token': uni.getStorageSync("token")
  144. }
  145. }).then(response => {
  146. let res = response
  147. if (res.success) {
  148. this.getOpenerEventChannel().emit('loadData');
  149. this.returnPage();
  150. } else {
  151. }
  152. }).catch(res =>{
  153. });
  154. // this.$db[__name].add(this.form).then(res => {
  155. // if (res.code == 200) {
  156. // this.getOpenerEventChannel().emit('loadData');
  157. // this.returnPage();
  158. // }
  159. // });
  160. } else {
  161. this.$axios.put("/goodsCategory/admin/update",
  162. {
  163. 'createBy':this.form.createBy,//创建人
  164. 'createTime':this.form.createTime,//创建时间
  165. 'updateBy':uni.getStorageSync("nickName"),//更新人
  166. 'updateTime':this.now_date,//更新时间
  167. "name": this.form.name,
  168. "icon": this.form.icon,
  169. "level": this.form.level,
  170. "pid": this.form.pid,
  171. 'sort':this.form.sort,//排序
  172. 'state':this.form.state,
  173. 'id':this.dataId
  174. },
  175. {
  176. headers:{
  177. 'Mall-Token': uni.getStorageSync("token")
  178. }
  179. }).then(response => {
  180. let res = response
  181. if (res.success) {
  182. this.getOpenerEventChannel().emit('loadData');
  183. this.returnPage();
  184. } else {
  185. alert(res.message)
  186. }
  187. })
  188. // this.$db[__name].update(this.dataId, this.form).then(res => {
  189. // if (res.code == 200) {
  190. // this.getOpenerEventChannel().emit('loadData');
  191. // this.returnPage();
  192. // }
  193. // });
  194. }
  195. }
  196. });
  197. },
  198. imgUpload(e) {
  199. this.form.icon = e.tempFilePaths[0];
  200. const file = this.form.icon;
  201. let Key = e.tempFiles[0].name
  202. cos.postObject(
  203. {
  204. Bucket: 'jinganminsu-1320402385',
  205. Region: 'ap-nanjing',
  206. Key: Key,
  207. FilePath: file,
  208. onProgress: (info) => {
  209. // console.log(info)
  210. }
  211. },
  212. (err, data) => {
  213. if (err) {
  214. console.log('上传失败', err)
  215. } else {
  216. console.log('上传成功', data)
  217. uni.hideLoading()
  218. let imgUrl = 'https://' + data.Location
  219. this.form.icon =imgUrl
  220. }
  221. }
  222. )
  223. },
  224. imgDelete() {
  225. this.imageValue = null;
  226. this.form.icon = '';
  227. },
  228. returnPage() {
  229. bus.$emit('tab-close');
  230. this.$router.push('/pages/goods/classify/goods_classify');
  231. },
  232. //获取当前时间
  233. getNowDate() {
  234. var _this = this;
  235. // this.timer = setInterval(function() {
  236. var aData = new Date();
  237. var month = aData.getMonth() < 9 ? "0" + (aData.getMonth() + 1) : aData.getMonth() + 1;
  238. var date = aData.getDate() <= 9 ? "0" + aData.getDate() : aData.getDate();
  239. var date2 = aData.getDate() <= 9 ? "0" + (aData.getDate()-1) : (aData.getDate()-1);
  240. var Hour = aData.getHours() <= 9 ? "0" + (aData.getHours()) : aData.getHours();
  241. var Miunte = aData.getMinutes() <= 9 ? "0" + (aData.getMinutes()) : aData.getMinutes();
  242. var Seconds = aData.getSeconds() <= 9 ? "0" + (aData.getSeconds()) : aData.getSeconds();
  243. // console.log(aData.getTime())
  244. _this.now_date = aData.getFullYear() + "-" + month + "-" + date + ' '+ Hour +":"+ Miunte +":"+ Seconds;
  245. // console.log(aData.getFullYear() + "-" + month + "-" + date2)昨天
  246. // }, 86400000);
  247. },
  248. },
  249. created() {
  250. this.dataId = this.$route.query.id || '';
  251. this.loadData();
  252. this.form.sort=this.$route.query.sort
  253. if(!this.$route.query.pid){
  254. return
  255. }else{
  256. this.form.pid=this.$route.query.pid.slice(0,19)
  257. }
  258. this.form.name=this.$route.query.name
  259. this.form.icon=this.$route.query.icon
  260. this.form.state=this.$route.query.state
  261. this.form.level=this.$route.query.level
  262. if (this.$route.query.icon) {
  263. this.imageValue = {};
  264. this.imageValue.url = this.$route.query.icon;
  265. }
  266. }
  267. };
  268. </script>
  269. <style></style>