carousel_add_edit.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. import { cos } from '@/util/cos.js'
  47. export default {
  48. data() {
  49. return {
  50. dataId: '',
  51. form: {
  52. img: '',
  53. type: '0',
  54. url: '',
  55. sort: 0,
  56. remark: '',
  57. state: '1',//0禁用1启用
  58. createTime:'',
  59. createBy:''
  60. },
  61. imageValue: null,
  62. imageStyle: {
  63. height: '200px',
  64. width: '500px'
  65. },
  66. urlOptions: [],
  67. remark: '',
  68. mpPageUrl: '',
  69. rules: {},
  70. now_date:'',//当前时间
  71. };
  72. },
  73. methods: {
  74. loadData() {
  75. if (!this.dataId) {
  76. return;
  77. }else{
  78. }
  79. //轮播信息
  80. this.$axios.post("/carousel/admin/info/"+this.dataId,
  81. {
  82. },
  83. {
  84. headers:{
  85. 'Mall-Token': uni.getStorageSync("token")
  86. }
  87. }).then(response => {
  88. let res = response
  89. console.log(res)
  90. if (res.success) {
  91. for (let item in this.form) {
  92. this.form[item] = res.data[item];
  93. }
  94. console.log(this.form)
  95. if (this.form.img) {
  96. this.imageValue = {};
  97. this.imageValue.url = this.form.img;
  98. }
  99. } else {
  100. }
  101. }).catch(res =>{
  102. });
  103. // await this.$db[__name].tofirst(this.dataId).then(res => {
  104. // if (res.code == 200) {
  105. // for (let item in this.form) {
  106. // this.form[item] = res.datas[item];
  107. // }
  108. // if (this.form.img) {
  109. // this.imageValue = {};
  110. // this.imageValue.url = this.form.img;
  111. // }
  112. // }
  113. // });
  114. },
  115. submitForm(formName) {
  116. console.log(this.form)
  117. console.log(this.imageValue)
  118. this.getNowDate()
  119. if(this.form.type!=0 &&this.form.url==''){
  120. alert('跳转地址不能为空!')
  121. }else if(this.form.img==''){
  122. alert('图片不能为空!')
  123. }else{
  124. console.log('触发')
  125. if (!this.dataId) {
  126. this.$axios.post("/carousel/admin/add",
  127. {
  128. 'createBy':uni.getStorageSync("nickName"),//创建人
  129. 'createTime':this.now_date,//创建时间
  130. 'img':this.form.img,//图片地址
  131. 'remark':this.form.remark,//备注
  132. 'sort':this.form.sort,//排序
  133. 'state':this.form.state,//状态0禁用1启用
  134. 'type':this.form.type,//跳转类型0不跳转 1跳转页面 2跳转网页
  135. 'url':this.form.url,//跳转地址type不为0时不能为空
  136. },
  137. {
  138. headers:{
  139. 'Mall-Token': uni.getStorageSync("token")
  140. }
  141. }).then(response => {
  142. let res = response
  143. console.log(res)
  144. if (res.success) {
  145. this.getOpenerEventChannel().emit('loadData');
  146. this.returnPage();
  147. } else {
  148. }
  149. }).catch(res =>{
  150. });
  151. }else{//编辑
  152. this.$axios.put("/carousel/admin/update",
  153. {
  154. 'createBy':this.form.createBy,//创建人
  155. 'createTime':this.form.createTime,//创建时间
  156. 'updateBy':uni.getStorageSync("nickName"),//更新人
  157. 'updateTime':this.now_date,//更新时间
  158. 'img':this.form.img,//图片地址
  159. 'remark':this.form.remark,//备注
  160. 'sort':this.form.sort,//排序
  161. 'state':this.form.state,//状态0禁用1启用
  162. 'type':this.form.type,//跳转类型0不跳转 1跳转页面 2跳转网页
  163. 'url':this.form.url,//跳转地址type不为0时不能为空
  164. 'id':this.dataId
  165. },
  166. {
  167. headers:{
  168. 'Mall-Token': uni.getStorageSync("token")
  169. }
  170. }).then(response => {
  171. let res = response
  172. if (res.success) {
  173. this.getOpenerEventChannel().emit('loadData');
  174. this.returnPage();
  175. } else {
  176. alert(res.message)
  177. }
  178. }).catch(res =>{
  179. });
  180. }
  181. }
  182. },
  183. //获取当前时间
  184. getNowDate() {
  185. var _this = this;
  186. // this.timer = setInterval(function() {
  187. var aData = new Date();
  188. var month = aData.getMonth() < 9 ? "0" + (aData.getMonth() + 1) : aData.getMonth() + 1;
  189. var date = aData.getDate() <= 9 ? "0" + aData.getDate() : aData.getDate();
  190. var date2 = aData.getDate() <= 9 ? "0" + (aData.getDate()-1) : (aData.getDate()-1);
  191. var Hour = aData.getHours() <= 9 ? "0" + (aData.getHours()) : aData.getHours();
  192. var Miunte = aData.getMinutes() <= 9 ? "0" + (aData.getMinutes()) : aData.getMinutes();
  193. var Seconds = aData.getSeconds() <= 9 ? "0" + (aData.getSeconds()) : aData.getSeconds();
  194. // console.log(aData.getTime())
  195. _this.now_date = aData.getFullYear() + "-" + month + "-" + date + ' '+ Hour +":"+ Miunte +":"+ Seconds;
  196. // console.log(aData.getFullYear() + "-" + month + "-" + date2)昨天
  197. // }, 86400000);
  198. },
  199. imgUpload(e) {
  200. this.form.img = e.tempFilePaths[0]
  201. const file = this.form.img;
  202. let Key = e.tempFiles[0].name
  203. cos.postObject(
  204. {
  205. Bucket: 'jinganminsu-1320402385',
  206. Region: 'ap-nanjing',
  207. Key: Key,
  208. FilePath: file,
  209. onProgress: (info) => {
  210. // console.log(info)
  211. }
  212. },
  213. (err, data) => {
  214. if (err) {
  215. console.log('上传失败', err)
  216. } else {
  217. console.log('上传成功', data)
  218. uni.hideLoading()
  219. let imgUrl = 'https://' + data.Location
  220. this.form.img =imgUrl
  221. }
  222. }
  223. )
  224. // //文件上传
  225. // this.$axios.post("/file/open/",formData,
  226. // {
  227. // headers:{
  228. // 'Content-type' : 'multipart/form-data'
  229. // }
  230. // }).then(response => {
  231. // let res = response
  232. // if (res.success) {
  233. // this.form.img =res.data
  234. // } else {
  235. // uni.showToast({
  236. // icon:'error',
  237. // title: '请重新上传!'
  238. // });
  239. // this.form.img =''
  240. // this.imageValue = null;
  241. // }
  242. // }).catch(res =>{
  243. // uni.showToast({
  244. // icon:'error',
  245. // title: '请重新上传!'
  246. // });
  247. // this.form.img =''
  248. // this.imageValue = null;
  249. // });
  250. },
  251. imgDelete() {
  252. this.imageValue = null;
  253. this.form.img = '';
  254. },
  255. returnPage() {
  256. bus.$emit('tab-close');
  257. this.$router.push('/pages/shop/home/page');
  258. },
  259. // urlChange(data) {
  260. // let obj = this.urlOptions.find(x => x._id == data);
  261. // this.form.url = obj.url;
  262. // this.remark = obj.remark;
  263. // }
  264. },
  265. created() {
  266. this.dataId = this.$route.query.id || '';
  267. this.loadData();
  268. }
  269. };
  270. </script>
  271. <style></style>