post.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. var app = getApp();
  2. var util = require('../../utils/util.js');
  3. var api = require('../../config/api.js');
  4. var user = require('../../services/user.js');
  5. Page({
  6. data: {
  7. desc: '',
  8. title: '',
  9. regionId: 0,
  10. region: '',
  11. price: '',
  12. marketPrice: '',
  13. isPostageFree: false,
  14. postage: null,
  15. ableSelfTake: false,
  16. ableMeet: false,
  17. ableExpress: false,
  18. cateId: 0,
  19. cateName: '',
  20. imgList: [],
  21. tmpImgList:[],
  22. },
  23. onLoad: function(options) {
  24. var that = this;
  25. user.checkLoginAndNav()
  26. },
  27. onClose() {
  28. wx.navigateBack({
  29. delta: 1
  30. });
  31. },
  32. addImage() {
  33. let that = this;
  34. let remain = 10 - this.data.imgList.length;
  35. console.log('上传图片')
  36. wx.chooseImage({
  37. count: remain,
  38. success(res) {
  39. let length = res.tempFiles.length
  40. // tempFilePath可以作为img标签的src属性显示图片
  41. let tempFilePaths = res.tempFilePaths
  42. let tempFiles = res.tempFiles
  43. that.setData({
  44. tmpImgList: that.data.tmpImgList.concat(tempFilePaths)
  45. })
  46. for (var i = 0; i < length; i++) {
  47. that.data.imgList.push('false');
  48. var index = that.data.imgList.length-1
  49. that.setData({
  50. imgList: that.data.imgList
  51. })
  52. if (tempFiles[i].size > 4500000) {
  53. console.log("图片太大")
  54. that.compressImg(tempFilePaths[i], index)
  55. } else {
  56. console.log("上传到图床")
  57. that.uploadFile(tempFilePaths[i], index)
  58. }
  59. // console.log(tempFilePaths[i]);
  60. }
  61. console.log(that.data.imgList);
  62. },
  63. fail(res) {
  64. console.log(res);
  65. },
  66. })
  67. },
  68. uploadFile(url, i) {
  69. let that = this;
  70. // wx.uploadFile({
  71. // url: ' https://sm.ms/api/upload',
  72. // filePath: url,
  73. // name: 'smfile',
  74. // success(res) {
  75. // const data = JSON.parse(res.data);
  76. // console.log(data)
  77. // if (data.code == 'success') {
  78. // console.log("图片上传成功, " + data.data.url)
  79. // that.data.imgList[i] = data.data.url
  80. // that.setData({
  81. // imgList: that.data.imgList
  82. // })
  83. // // that.onLoad();
  84. // } else if (data.code == 'error' && data.msg == 'File is too large.') {
  85. // console.log("上传失败,图片太大")
  86. // that.compressImg(url, i)
  87. // }
  88. // }
  89. // })
  90. //模拟上传
  91. setTimeout(function goback() {
  92. console.log("图片上传成功, " + url)
  93. that.data.imgList[i] = url
  94. that.setData({
  95. imgList: that.data.imgList
  96. })
  97. }, 2000)
  98. },
  99. compressImg(url, i) {
  100. let that = this
  101. wx.compressImage({
  102. src: url, // 图片路径
  103. quality: 50, // 压缩质量
  104. success() {
  105. console.log("压缩后重新上传")
  106. that.uploadFile(url, i)
  107. },
  108. fail(res) {
  109. console.log(res)
  110. console.log("压缩失败 ")
  111. }
  112. })
  113. },
  114. removeImg(event){
  115. console.log("删除元素")
  116. let index = event.currentTarget.dataset.index
  117. let that = this
  118. that.data.imgList.splice(index, 1)
  119. that.data.tmpImgList.splice(index, 1)
  120. this.setData({
  121. imgList: that.data.imgList ,
  122. tmpImgList: that.data.tmpImgList,
  123. })
  124. },
  125. preview(event){
  126. let url = event.currentTarget.dataset.url
  127. let urls = [];
  128. let imgList = this.data.imgList
  129. for (var index in imgList){
  130. if (imgList[index]!='false'){
  131. urls.push(imgList[index])
  132. }
  133. }
  134. wx.previewImage({
  135. current: url,
  136. urls: urls // 需要预览的图片http链接列表
  137. })
  138. },
  139. onPost() {
  140. if (this.data.title.trim() == '') {
  141. util.showErrorToast('必须填写商品名')
  142. return;
  143. }
  144. if (this.data.desc.trim() == '') {
  145. util.showErrorToast('必须填写介绍')
  146. return;
  147. }
  148. if (this.data.region.trim() == '') {
  149. util.showErrorToast('请选择发货地点')
  150. return;
  151. }
  152. if (this.data.imgList.length < 1) {
  153. util.showErrorToast('请上传图片')
  154. return;
  155. }
  156. if (this.data.cateName.trim() == '') {
  157. util.showErrorToast('请选择分类')
  158. return;
  159. }
  160. let reg1 = /^[0-9]+(.[0-9]{1,})?$/;
  161. let reg2 = /^[0-9]+(.[0-9]{1,2})?$/;
  162. if (this.data.price == '') {
  163. util.showErrorToast('必须填写价格')
  164. return;
  165. }
  166. let postage = this.data.postage == null ? '0.00' : this.data.postage
  167. this.setData({
  168. marketPrice: this.data.marketPrice == '' ? '0' : this.data.marketPrice
  169. })
  170. if (!reg1.test(this.data.price) || !reg1.test(this.data.marketPrice) || !reg1.test(postage)) {
  171. util.showErrorToast('价格必须是数字')
  172. return;
  173. }
  174. if (!reg2.test(this.data.price) || !reg2.test(this.data.marketPrice) || !reg2.test(postage)) {
  175. util.showErrorToast('小数必须是最大2位')
  176. return;
  177. }
  178. if (parseFloat(this.data.price) >= 100000000 || parseFloat(this.data.marketPrice) >= 100000000) {
  179. util.showErrorToast("必须在0到1亿元之间")
  180. return;
  181. }
  182. if (parseFloat(postage) > 1000) {
  183. util.showErrorToast("邮费最大1千元")
  184. return;
  185. }
  186. if (!this.data.ableSelfTake && !this.data.ableMeet && !this.data.ableExpress) {
  187. util.showErrorToast("请选择交易方式")
  188. return;
  189. }
  190. let imgList = this.data.imgList
  191. for (var index in imgList) {
  192. if (imgList[index] == 'false') {
  193. util.showErrorToast('图片上传中')
  194. return;
  195. }
  196. }
  197. let that = this
  198. user.checkLoginAndNav().then(() => {
  199. util.request(api.GoodsPost, {
  200. name: this.data.title,
  201. desc: this.data.desc,
  202. regionId: this.data.regionId,
  203. region: this.data.region,
  204. categoryId: this.data.cateId,
  205. price: this.data.price,
  206. marketPrice: this.data.marketPrice,
  207. postage: postage,
  208. ableSelfTake: this.data.ableSelfTake,
  209. ableMeet: this.data.ableMeet,
  210. ableExpress: this.data.ableExpress,
  211. images: this.data.imgList,
  212. }, 'POST').then(function(res) {
  213. if (res.errno === 0) {
  214. setTimeout(function goback() {
  215. wx.reLaunch({
  216. url: '/pages/index/index'
  217. })
  218. }, 1000)
  219. wx.showToast({
  220. title: '发布成功'
  221. })
  222. }
  223. console.log(res)
  224. });
  225. })
  226. },
  227. bindInputDesc(event) {
  228. this.setData({
  229. desc: event.detail.value,
  230. })
  231. console.log(event.detail)
  232. },
  233. bindInputTitle(event) {
  234. this.setData({
  235. title: event.detail.value,
  236. })
  237. console.log(event.detail)
  238. },
  239. bindInputPrice(event) {
  240. this.setData({
  241. price: event.detail.value,
  242. })
  243. console.log(event.detail)
  244. },
  245. bindInputMarketPrice(event) {
  246. this.setData({
  247. marketPrice: event.detail.value,
  248. })
  249. },
  250. bindInputPostage(event) {
  251. this.setData({
  252. postage: event.detail.value,
  253. })
  254. },
  255. postageFree(event) {
  256. if (event.detail.value[0]) {
  257. this.setData({
  258. isPostageFree: true,
  259. postage: null
  260. })
  261. } else {
  262. this.setData({
  263. isPostageFree: false,
  264. })
  265. }
  266. console.log(event.detail.value[0])
  267. },
  268. trade(event) {
  269. this.setData({
  270. ableSelfTake: false,
  271. ableMeet: false,
  272. ableExpress: false,
  273. })
  274. for (let i in event.detail.value) {
  275. console.log(event.detail.value[i])
  276. if (event.detail.value[i] == 'ableSelfTake') {
  277. this.setData({
  278. ableSelfTake: true,
  279. })
  280. } else if (event.detail.value[i] == 'ableMeet') {
  281. this.setData({
  282. ableMeet: true,
  283. })
  284. } else if (event.detail.value[i] == 'ableExpress') {
  285. this.setData({
  286. ableExpress: true,
  287. })
  288. }
  289. };
  290. console.log(event.detail)
  291. },
  292. onReady: function() {
  293. },
  294. onShow: function() {
  295. // 页面显示
  296. if (app.post.region.id) {
  297. this.setData({
  298. regionId: app.post.region.id,
  299. region: app.post.region.name
  300. });
  301. app.post.region.id = 0;
  302. app.post.region.name = ''
  303. }
  304. if (app.post.cate.id) {
  305. this.setData({
  306. cateId: app.post.cate.id,
  307. cateName: app.post.cate.name
  308. });
  309. app.post.cate.id = 0;
  310. app.post.cate.name = ''
  311. }
  312. },
  313. onHide: function() {
  314. // 页面隐藏
  315. },
  316. onUnload: function() {
  317. // 页面关闭
  318. //重启
  319. wx.reLaunch({
  320. url: '/pages/index/index'
  321. })
  322. }
  323. })