post.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. },
  21. onLoad: function(options) {
  22. var that = this;
  23. user.checkLoginAndNav()
  24. },
  25. onClose() {
  26. wx.navigateBack({
  27. delta: 1
  28. });
  29. },
  30. onPost() {
  31. if (this.data.title.trim() == '') {
  32. util.showErrorToast('必须填写商品名')
  33. return;
  34. }
  35. if (this.data.desc.trim() == '') {
  36. util.showErrorToast('必须填写介绍')
  37. return;
  38. }
  39. if (this.data.region.trim() == '') {
  40. util.showErrorToast('请选择发货地点')
  41. return;
  42. }
  43. if (this.data.cateName.trim() == '') {
  44. util.showErrorToast('请选择分类')
  45. return;
  46. }
  47. let reg1 = /^[0-9]+(.[0-9]{1,})?$/;
  48. let reg2 = /^[0-9]+(.[0-9]{1,2})?$/;
  49. if (this.data.price == '') {
  50. util.showErrorToast('必须填写价格')
  51. return;
  52. }
  53. let postage = this.data.postage == null ? '0.00' : this.data.postage
  54. this.setData({
  55. marketPrice: this.data.marketPrice == '' ? '0' : this.data.marketPrice
  56. })
  57. if (!reg1.test(this.data.price) || !reg1.test(this.data.marketPrice) || !reg1.test(postage)) {
  58. util.showErrorToast('价格必须是数字')
  59. return;
  60. }
  61. if (!reg2.test(this.data.price) || !reg2.test(this.data.marketPrice) || !reg2.test(postage)) {
  62. util.showErrorToast('小数必须是最大2位')
  63. return;
  64. }
  65. if (parseFloat(this.data.price) >= 100000000 || parseFloat(this.data.marketPrice) >= 100000000) {
  66. util.showErrorToast("必须在0到1亿元之间")
  67. return;
  68. }
  69. if (parseFloat(postage) > 1000) {
  70. util.showErrorToast("邮费最大1千元")
  71. return;
  72. }
  73. if (!this.data.ableSelfTake && !this.data.ableMeet && !this.data.ableExpress ){
  74. util.showErrorToast("请选择交易方式")
  75. return;
  76. }
  77. let that = this
  78. user.checkLoginAndNav().then(() => {
  79. util.request(api.GoodsPost, {
  80. name: this.data.title,
  81. desc: this.data.desc,
  82. regionId: this.data.regionId,
  83. region: this.data.region,
  84. categoryId: this.data.cateId,
  85. price: this.data.price,
  86. marketPrice: this.data.marketPrice,
  87. postage: postage,
  88. ableSelfTake: this.data.ableSelfTake,
  89. ableMeet: this.data.ableMeet,
  90. ableExpress: this.data.ableExpress
  91. }, 'POST').then(function(res) {
  92. if (res.errno === 0) {
  93. setTimeout(function goback(){
  94. wx.reLaunch({
  95. url: '/pages/index/index'
  96. })
  97. }, 1000)
  98. wx.showToast({
  99. title: '发布成功'
  100. })
  101. }
  102. console.log(res)
  103. });
  104. })
  105. },
  106. bindInputDesc(event) {
  107. this.setData({
  108. desc: event.detail.value,
  109. })
  110. console.log(event.detail)
  111. },
  112. bindInputTitle(event) {
  113. this.setData({
  114. title: event.detail.value,
  115. })
  116. console.log(event.detail)
  117. },
  118. bindInputPrice(event) {
  119. this.setData({
  120. price: event.detail.value,
  121. })
  122. console.log(event.detail)
  123. },
  124. bindInputMarketPrice(event) {
  125. this.setData({
  126. marketPrice: event.detail.value,
  127. })
  128. },
  129. bindInputPostage(event) {
  130. this.setData({
  131. postage: event.detail.value,
  132. })
  133. },
  134. postageFree(event) {
  135. if (event.detail.value[0]) {
  136. this.setData({
  137. isPostageFree: true,
  138. postage: null
  139. })
  140. } else {
  141. this.setData({
  142. isPostageFree: false,
  143. })
  144. }
  145. console.log(event.detail.value[0])
  146. },
  147. trade(event) {
  148. this.setData({
  149. ableSelfTake: false,
  150. ableMeet: false,
  151. ableExpress: false,
  152. })
  153. for (let i in event.detail.value) {
  154. console.log(event.detail.value[i])
  155. if (event.detail.value[i] == 'ableSelfTake') {
  156. this.setData({
  157. ableSelfTake: true,
  158. })
  159. } else if (event.detail.value[i] == 'ableMeet') {
  160. this.setData({
  161. ableMeet: true,
  162. })
  163. } else if (event.detail.value[i] == 'ableExpress') {
  164. this.setData({
  165. ableExpress: true,
  166. })
  167. }
  168. };
  169. console.log(event.detail)
  170. },
  171. onReady: function() {
  172. },
  173. onShow: function() {
  174. // 页面显示
  175. if (app.post.region.id) {
  176. this.setData({
  177. regionId: app.post.region.id,
  178. region: app.post.region.name
  179. });
  180. app.post.region.id = 0;
  181. app.post.region.name = ''
  182. }
  183. if(app.post.cate.id){
  184. this.setData({
  185. cateId: app.post.cate.id,
  186. cateName: app.post.cate.name
  187. });
  188. app.post.cate.id = 0;
  189. app.post.cate.name = ''
  190. }
  191. },
  192. onHide: function() {
  193. // 页面隐藏
  194. },
  195. onUnload: function() {
  196. // 页面关闭
  197. //重启
  198. wx.reLaunch({
  199. url: '/pages/index/index'
  200. })
  201. }
  202. })