carousel_add_edit.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <div class="container add_edit">
  3. <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
  4. <el-form-item label="轮播图片">
  5. <uni-file-picker v-model="imageValue" fileMediatype="image" returnType="object"
  6. :image-styles="imageStyle" @select="imgUpload" @delete="imgDelete" />
  7. </el-form-item>
  8. <el-form-item label="跳转类型">
  9. <el-radio-group v-model="form.type">
  10. <el-radio label="2">网页</el-radio>
  11. <el-radio label="1">页面</el-radio>
  12. <el-radio label="0">不跳转</el-radio>
  13. </el-radio-group>
  14. </el-form-item>
  15. <!-- <el-form-item label="地址">
  16. <el-select v-model="mpPageUrl" placeholder="请选择" class="sele" @change="urlChange">
  17. <el-option v-for="(item, idx) in urlOptions" :key="idx" :label="item.name + ' ' + item.url"
  18. :value="item._id"></el-option>
  19. </el-select>
  20. </el-form-item> -->
  21. <el-form-item label="跳转地址">
  22. <el-input v-model="form.url" placeholder="自动获取上面选中的值"></el-input>
  23. <div class="remark_txt">{{ remark }}</div>
  24. </el-form-item>
  25. <el-form-item label="排序">
  26. <el-input type="number" v-model.number="form.sort" min="0" placeholder="请输入排序"></el-input>
  27. </el-form-item>
  28. <el-form-item label="备注">
  29. <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
  30. </el-form-item>
  31. <el-form-item label="状态">
  32. <el-switch v-model="form.state" active-color="#ff6a6c" inactive-color="#bbb" active-value="1"
  33. inactive-value="0"></el-switch>
  34. </el-form-item>
  35. <el-form-item>
  36. <el-button class="confirm_btn" @click="submitForm('ruleForm')">确 定</el-button>
  37. <el-button @click="returnPage">返回列表</el-button>
  38. </el-form-item>
  39. </el-form>
  40. </div>
  41. </template>
  42. <script>
  43. import bus from '@/common/bus';
  44. const __name = 'usemall-app-carousel';
  45. const __mpPage = 'usemall-app-wxmini-page';
  46. export default {
  47. data() {
  48. return {
  49. dataId: '',
  50. form: {
  51. img: '',
  52. type: '0',
  53. url: '',
  54. sort: 0,
  55. remark: '',
  56. state: '1',//0禁用1启用
  57. createTime:'',
  58. createBy:''
  59. },
  60. imageValue: null,
  61. imageStyle: {
  62. height: '200px',
  63. width: '500px'
  64. },
  65. urlOptions: [],
  66. remark: '',
  67. mpPageUrl: '',
  68. rules: {},
  69. now_date:'',//当前时间
  70. };
  71. },
  72. methods: {
  73. loadData() {
  74. if (!this.dataId) {
  75. return;
  76. }else{
  77. }
  78. //轮播信息
  79. this.$axios.post("/carousel/admin/info/"+this.dataId,
  80. {
  81. },
  82. {
  83. headers:{
  84. 'Mall-Token': uni.getStorageSync("token")
  85. }
  86. }).then(response => {
  87. let res = response
  88. console.log(res)
  89. if (res.success) {
  90. for (let item in this.form) {
  91. this.form[item] = res.data[item];
  92. }
  93. console.log(this.form)
  94. if (this.form.img) {
  95. this.imageValue = {};
  96. this.imageValue.url = this.form.img;
  97. }
  98. } else {
  99. }
  100. }).catch(res =>{
  101. });
  102. // await this.$db[__name].tofirst(this.dataId).then(res => {
  103. // if (res.code == 200) {
  104. // for (let item in this.form) {
  105. // this.form[item] = res.datas[item];
  106. // }
  107. // if (this.form.img) {
  108. // this.imageValue = {};
  109. // this.imageValue.url = this.form.img;
  110. // }
  111. // }
  112. // });
  113. },
  114. submitForm(formName) {
  115. console.log(this.form)
  116. console.log(this.imageValue)
  117. this.getNowDate()
  118. if(this.form.type!=0 &&this.form.url==''){
  119. alert('跳转地址不能为空!')
  120. }else if(this.form.img==''){
  121. alert('图片不能为空!')
  122. }else{
  123. console.log('触发')
  124. if (!this.dataId) {
  125. this.$axios.post("/carousel/admin/add",
  126. {
  127. 'createBy':uni.getStorageSync("nickName"),//创建人
  128. 'createTime':this.now_date,//创建时间
  129. 'img':this.form.img,//图片地址
  130. 'remark':this.form.remark,//备注
  131. 'sort':this.form.sort,//排序
  132. 'state':this.form.state,//状态0禁用1启用
  133. 'type':this.form.type,//跳转类型0不跳转 1跳转页面 2跳转网页
  134. 'url':this.form.url,//跳转地址type不为0时不能为空
  135. },
  136. {
  137. headers:{
  138. 'Mall-Token': uni.getStorageSync("token")
  139. }
  140. }).then(response => {
  141. let res = response
  142. console.log(res)
  143. if (res.success) {
  144. this.getOpenerEventChannel().emit('loadData');
  145. this.returnPage();
  146. } else {
  147. }
  148. }).catch(res =>{
  149. });
  150. }else{//编辑
  151. this.$axios.put("/carousel/admin/update",
  152. {
  153. 'createBy':this.form.createBy,//创建人
  154. 'createTime':this.form.createTime,//创建时间
  155. 'updateBy':uni.getStorageSync("nickName"),//更新人
  156. 'updateTime':this.now_date,//更新时间
  157. 'img':this.form.img,//图片地址
  158. 'remark':this.form.remark,//备注
  159. 'sort':this.form.sort,//排序
  160. 'state':this.form.state,//状态0禁用1启用
  161. 'type':this.form.type,//跳转类型0不跳转 1跳转页面 2跳转网页
  162. 'url':this.form.url,//跳转地址type不为0时不能为空
  163. 'id':this.dataId
  164. },
  165. {
  166. headers:{
  167. 'Mall-Token': uni.getStorageSync("token")
  168. }
  169. }).then(response => {
  170. let res = response
  171. if (res.success) {
  172. this.getOpenerEventChannel().emit('loadData');
  173. this.returnPage();
  174. } else {
  175. alert(res.message)
  176. }
  177. }).catch(res =>{
  178. });
  179. }
  180. }
  181. },
  182. //获取当前时间
  183. getNowDate() {
  184. var _this = this;
  185. // this.timer = setInterval(function() {
  186. var aData = new Date();
  187. var month = aData.getMonth() < 9 ? "0" + (aData.getMonth() + 1) : aData.getMonth() + 1;
  188. var date = aData.getDate() <= 9 ? "0" + aData.getDate() : aData.getDate();
  189. var date2 = aData.getDate() <= 9 ? "0" + (aData.getDate()-1) : (aData.getDate()-1);
  190. var Hour = aData.getHours() <= 9 ? "0" + (aData.getHours()) : aData.getHours();
  191. var Miunte = aData.getMinutes() <= 9 ? "0" + (aData.getMinutes()) : aData.getMinutes();
  192. var Seconds = aData.getSeconds() <= 9 ? "0" + (aData.getSeconds()) : aData.getSeconds();
  193. // console.log(aData.getTime())
  194. _this.now_date = aData.getFullYear() + "-" + month + "-" + date + ' '+ Hour +":"+ Miunte +":"+ Seconds;
  195. // console.log(aData.getFullYear() + "-" + month + "-" + date2)昨天
  196. // }, 86400000);
  197. },
  198. imgUpload(e) {
  199. this.form.img = e.tempFiles[0].file;
  200. const file = this.form.img;
  201. const formData = new FormData();
  202. formData.append('file', file);
  203. // 文件上传
  204. this.$axios.post("/file/open/",formData,
  205. {
  206. headers:{
  207. 'Content-type' : 'multipart/form-data'
  208. }
  209. }).then(response => {
  210. let res = response
  211. if (res.success) {
  212. this.form.img =res.data
  213. } else {
  214. uni.showToast({
  215. icon:'error',
  216. title: '请重新上传!'
  217. });
  218. this.form.img =''
  219. this.imageValue = null;
  220. }
  221. }).catch(res =>{
  222. uni.showToast({
  223. icon:'error',
  224. title: '请重新上传!'
  225. });
  226. this.form.img =''
  227. this.imageValue = null;
  228. });
  229. },
  230. imgDelete() {
  231. this.imageValue = null;
  232. this.form.img = '';
  233. },
  234. returnPage() {
  235. bus.$emit('tab-close');
  236. this.$router.push('/pages/shop/home/page');
  237. },
  238. // urlChange(data) {
  239. // let obj = this.urlOptions.find(x => x._id == data);
  240. // this.form.url = obj.url;
  241. // this.remark = obj.remark;
  242. // }
  243. },
  244. created() {
  245. this.dataId = this.$route.query.id || '';
  246. this.loadData();
  247. }
  248. };
  249. </script>
  250. <style></style>