goods-list.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // pages/goods/goods-list.js
  2. import * as API_Goods from '../../api/goods.js'
  3. import * as API_Passport from '../../api/passport.js'
  4. import {
  5. $wuxToptips,
  6. $wuxToast,
  7. $wuxLoading,
  8. $wuxDialog
  9. } from '../../wux/index.js'
  10. import {unpGoods} from "../../api/goods.js";
  11. Page({
  12. /**
  13. * 页面的初始数据
  14. */
  15. data: {
  16. goods: [],
  17. pageParam: {
  18. page_size: 10,
  19. page_no: 1
  20. },
  21. finished: false,
  22. stockEditShow: false,
  23. editingGoodsId: null,
  24. stock: null
  25. },
  26. /**
  27. * 生命周期函数--监听页面加载
  28. */
  29. onLoad: function(options) {
  30. //this.getGoodsList(this.data.pageParam)
  31. },
  32. /**
  33. * 生命周期函数--监听页面初次渲染完成
  34. */
  35. onReady: function() {
  36. },
  37. /**
  38. * 生命周期函数--监听页面显示
  39. */
  40. onShow: function() {
  41. this.getTabBar().init();
  42. this.setData({
  43. ['pageParam.page_no']: 1,
  44. finished: false,
  45. goods: []
  46. })
  47. this.getGoodsList()
  48. },
  49. /**
  50. * 生命周期函数--监听页面隐藏
  51. */
  52. onHide: function() {
  53. },
  54. /**
  55. * 生命周期函数--监听页面卸载
  56. */
  57. onUnload: function() {
  58. },
  59. /**
  60. * 页面相关事件处理函数--监听用户下拉动作
  61. */
  62. onPullDownRefresh: function() {
  63. wx.showNavigationBarLoading()
  64. this.setData({
  65. ['pageParam.page_no']: 1,
  66. finished: false,
  67. goods:[]
  68. })
  69. this.getGoodsList().then(res=>{
  70. wx.hideNavigationBarLoading()
  71. wx.stopPullDownRefresh()
  72. })
  73. },
  74. onCollapseChange(event) {
  75. this.setData({
  76. activeNames: event.detail
  77. });
  78. },
  79. /**
  80. * 页面上拉触底事件的处理函数
  81. */
  82. onReachBottom: function() {
  83. if (!this.data.finished) {
  84. this.setData({
  85. ['pageParam.page_no']: this.data.pageParam.page_no + 1
  86. })
  87. this.getGoodsList()
  88. }
  89. },
  90. /**
  91. * 用户点击右上角分享
  92. */
  93. onShareAppMessage: function() {
  94. },
  95. /**触发搜索 */
  96. onSearch(e) {
  97. const kw = e.detail
  98. this.setData({
  99. ['pageParam.keyword']: kw,
  100. ['pageParam.page_no']: 1,
  101. goods: []
  102. })
  103. this.getGoodsList()
  104. },
  105. /**取消搜索 */
  106. onCancel(e) {
  107. console.log('cancel')
  108. this.setData({
  109. ['pageParam.page_no']: 1,
  110. goods: [],
  111. finished: false
  112. })
  113. delete this.data.pageParam.keyword
  114. this.getGoodsList()
  115. },
  116. /** 编辑产品 */
  117. editGoods(e) {
  118. this.setData({
  119. ['pageParam.page_no']: 1,
  120. finished: false,
  121. goods: []
  122. })
  123. const {
  124. id
  125. } = e.currentTarget.dataset
  126. wx.navigateTo({
  127. url: '/pages/goods/edit?id=' + id,
  128. })
  129. },
  130. publishGoods(e) {
  131. wx.navigateTo({
  132. url: '/pages/goods/edit',
  133. })
  134. },
  135. getGoodsList() {
  136. return new Promise((resolve,reject)=>{
  137. const that = this
  138. API_Goods.getGoodsList(this.data.pageParam).then(res => {
  139. const {
  140. data
  141. } = res
  142. let goods = []
  143. if (this.data.goods.length === 0) {
  144. goods = [...data]
  145. } else {
  146. goods = that.data.goods
  147. goods.push(...data)
  148. }
  149. if (data.length < 10) {
  150. that.setData({
  151. finished: true
  152. })
  153. }
  154. that.setData({
  155. goods: goods
  156. })
  157. resolve(res)
  158. }).catch(err=>{
  159. reject(err)
  160. })
  161. })
  162. },
  163. /** 下架产品 */
  164. offProduct(e) {
  165. const that = this
  166. const goods_id = e.currentTarget.dataset.id
  167. $wuxDialog().confirm({
  168. resetOnClose: true,
  169. closable: true,
  170. title: '操作提示',
  171. content: '你确定要下架商品?',
  172. onConfirm(e) {
  173. API_Goods.underGoods(goods_id, {}).then(res => {
  174. if (res === '') {
  175. that.data.goods.forEach((item, index) => {
  176. if (item.goods_id === parseInt(goods_id)) {
  177. that.setData({
  178. ['goods[' + index + '].market_enable']: 0
  179. })
  180. }
  181. })
  182. $wuxToptips().success({
  183. hidden: false,
  184. text: '产品已下架',
  185. duration: 2000,
  186. success: () => {}
  187. })
  188. } else {
  189. $wuxToptips().error({
  190. icon: 'cancel',
  191. hidden: false,
  192. text: res.message,
  193. duration: 2000
  194. })
  195. }
  196. }).catch(error => {
  197. $wuxToptips().error({
  198. icon: 'cancel',
  199. hidden: false,
  200. text: error.message,
  201. duration: 2000
  202. })
  203. })
  204. },
  205. onCancel(e) {
  206. },
  207. })
  208. },
  209. /** 下架产品 */
  210. onProduct(e) {
  211. const that = this
  212. const goods_id = e.currentTarget.dataset.id
  213. API_Goods.unpGoods(goods_id).then(res => {
  214. if (res === '') {
  215. that.data.goods.forEach((item, index) => {
  216. if (item.goods_id === parseInt(goods_id)) {
  217. that.setData({
  218. ['goods[' + index + '].market_enable']: 1
  219. })
  220. }
  221. })
  222. $wuxToptips().success({
  223. hidden: false,
  224. text: '产品已上架',
  225. duration: 2000,
  226. success: () => {}
  227. })
  228. } else {
  229. $wuxToptips().error({
  230. icon: 'cancel',
  231. hidden: false,
  232. text: res.message,
  233. duration: 2000
  234. })
  235. }
  236. }).catch(error => {
  237. $wuxToptips().error({
  238. icon: 'cancel',
  239. hidden: false,
  240. text: error.message,
  241. duration: 2000
  242. })
  243. })
  244. },
  245. /** 此次修改库存 只考虑默认SKU,没有考虑多种规格的情况 */
  246. stockPopShow(e) {
  247. const that = this
  248. const goods_id = e.currentTarget.dataset.id
  249. API_Goods.getGoodsStockList(goods_id).then(res => {
  250. that.setData({
  251. stockEditShow: true,
  252. editingGoodsId: goods_id,
  253. stock: res[0].quantity,
  254. currentSkuId: res[0].sku_id
  255. })
  256. })
  257. },
  258. stockEditClose(e) {
  259. this.setData({
  260. stockEditShow: false
  261. })
  262. },
  263. /** 表单输入 */
  264. inputChange(e) {
  265. let dataKey = e.currentTarget.dataset.name
  266. this.setData({
  267. [dataKey]: e.detail.value
  268. })
  269. },
  270. onError(e) {
  271. const that = this
  272. let dataKey = e.currentTarget.dataset.name
  273. let {
  274. label,
  275. rules
  276. } = e.currentTarget.dataset
  277. const {
  278. value
  279. } = e.detail
  280. if (!value && rules.indexOf('required') > -1) {
  281. label = label + '必填'
  282. } else {
  283. label = label + '格式错误'
  284. }
  285. $wuxToptips().show({
  286. icon: 'cancel',
  287. hidden: false,
  288. text: label,
  289. duration: 3000,
  290. success() {
  291. that.setData({
  292. [dataKey + '_focus']: true
  293. })
  294. },
  295. })
  296. },
  297. onBlur(e) {
  298. const that = this
  299. let dataKey = e.currentTarget.dataset.name
  300. const {
  301. rules
  302. } = e.currentTarget.dataset
  303. const {
  304. value
  305. } = e.detail
  306. if (rules) {
  307. that.setData({
  308. [dataKey + '_error']: false
  309. })
  310. rules.split(',').forEach(n => {
  311. if (n === 'required' && !value) {
  312. that.setData({
  313. [dataKey + '_error']: true
  314. })
  315. }
  316. if (n !== 'required' && !Reg[n].test(value)) {
  317. that.setData({
  318. [dataKey + '_error']: true
  319. })
  320. }
  321. })
  322. }
  323. },
  324. updateQuantity(e) {
  325. if (this.data.stock && this.data.currentSkuId) {
  326. const param = [{
  327. quantity_count: parseInt(this.data.stock),
  328. sku_id: this.data.currentSkuId
  329. }]
  330. let that = this
  331. API_Goods.reserveStockGoods(this.data.editingGoodsId, param).then(res => {
  332. that.data.goods.forEach((item, index) => {
  333. if (item.goods_id === that.data.editingGoodsId) {
  334. that.setData({
  335. ['goods[' + index + '].quantity']: that.data.stock
  336. })
  337. }
  338. })
  339. that.setData({
  340. stockEditShow: false
  341. })
  342. })
  343. }
  344. },
  345. formSubmit(e) {
  346. console.log(e)
  347. API_Passport.saveFormId({ form_id: e.detail.formId }).then(res => {
  348. })
  349. }
  350. })