goodsCate.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // pages/goodsCate/goodsCate.js
  2. const utils = require('../../utils/util.js')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. showTopList: '', // 一级分类列表
  9. showTop: [], // 一级分类展示
  10. showTopIndex: '', // 一级分类下标
  11. showTwoList: [], // 二级分类列表
  12. showTwo: [], // 二级分类展示
  13. showTwoIndex: '', // 二级分类下标
  14. list: [], // 商品列表
  15. fadeIn: false, // 弹框是否显示
  16. item: '', // 选中的商品数据
  17. keywords: '' // 关键词
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad: function (options) {
  23. if (options.productName) {
  24. this.setData({
  25. keywords: options.productName
  26. })
  27. }
  28. this.getCategoryTop()
  29. },
  30. //一级类目
  31. getCategoryTop() {
  32. wx.showLoading({
  33. title: '加载中...',
  34. mask: true
  35. })
  36. wx.cloud.callFunction({
  37. name: 'getNxyData',
  38. data: {
  39. start: 0,
  40. count: 50,
  41. $url: 'getCategoryTop'
  42. }
  43. }).then(res => {
  44. wx.hideLoading()
  45. let list = res.result.data
  46. let showTop = []
  47. let showTopList = []
  48. this.setData({
  49. topList: list
  50. })
  51. for (let i = 0; i < list.length; i++) {
  52. if (list[i].parent_code === '') {
  53. showTopList.push(list[i])
  54. showTop.push(list[i].name)
  55. }
  56. }
  57. this.setData({
  58. showTop,
  59. showTopList
  60. })
  61. })
  62. },
  63. //二级类目
  64. getTwo() {
  65. let parent_code = this.data.showTopList[this.data.showTopIndex].code
  66. let showTwo = []
  67. let showTwoList = []
  68. for (let i = 0; i < this.data.topList.length; i++) {
  69. let item = this.data.topList[i]
  70. if (item.parent_code === parent_code) {
  71. showTwoList.push(item)
  72. showTwo.push(item.name)
  73. }
  74. }
  75. this.setData({
  76. showTwoList,
  77. showTwo
  78. })
  79. },
  80. //设置完一级类目之后才可以设置二级类目
  81. setTop(e) {
  82. let showTopIndex = e.detail.value
  83. this.setData({
  84. showTopIndex,
  85. showTwoIndex: '',
  86. showTwo: []
  87. })
  88. this.getTwo()
  89. this.toFresh()
  90. },
  91. //设置二级类目
  92. setTwo(e) {
  93. if (this.data.showTopIndex === '') {
  94. utils.toast('请先选择一级类目')
  95. return
  96. }
  97. this.setData({
  98. showTwoIndex: e.detail.value
  99. })
  100. this.toFresh()
  101. },
  102. //获取关键字
  103. getKeywords(e) {
  104. let keywords = e.detail.value
  105. this.setData({
  106. keywords
  107. })
  108. },
  109. //根据条件查询商品
  110. getCategoryAll() {
  111. const params = {
  112. entTaxId: wx.getStorageSync('entTaxId')
  113. }
  114. if (utils.trimAll(this.data.keywords)) {
  115. params.productName = utils.trimAll(this.data.keywords)
  116. }
  117. if (this.data.showTwoList && this.data.showTwoIndex && this.data.showTwoList[this.data.showTwoIndex].code) {
  118. params.searchCode = this.data.showTwoList[this.data.showTwoIndex].code
  119. }
  120. utils.axios({
  121. method: 'get',
  122. url: '/sys/product/commoditySearch',
  123. data: params,
  124. sendBefore() {
  125. wx.showLoading({
  126. title: '加载中...',
  127. mask: true
  128. })
  129. },
  130. success: res => {
  131. let list = this.data.list
  132. for (let i = 0; i < res.data.length; i++) {
  133. // 税率转化成%
  134. if (res.data[i].sortedRateSet && res.data[i].sortedRateSet.length > 0) {
  135. let rateList = []
  136. for (let j = 0; j < res.data[i].sortedRateSet.length; j++) {
  137. rateList.push(Number(res.data[i].sortedRateSet[j]) * 100 + '%')
  138. }
  139. res.data[i].rate = rateList.join(',')
  140. }
  141. }
  142. list = list.concat(res.data)
  143. this.setData({
  144. list
  145. })
  146. },
  147. complete() {
  148. wx.hideLoading()
  149. }
  150. })
  151. /* let params = {
  152. start: this.data.list.length,
  153. count: this.data.count,
  154. search_code: this.data.showTwoList[this.data.showTwoIndex].code,
  155. $url: 'getCategoryAll'
  156. }
  157. if (utils.trimAll(this.data.keywords) !== '') {
  158. params.keyword = utils.trimAll(this.data.keywords)
  159. }
  160. wx.showLoading({
  161. title: '加载中...',
  162. mask: true
  163. })
  164. wx.cloud.callFunction({
  165. name: 'getNxyData',
  166. data: params
  167. }).then(res => {
  168. wx.hideLoading()
  169. let list = this.data.list
  170. list = list.concat(res.result.data)
  171. this.setData({
  172. list
  173. })
  174. }) */
  175. },
  176. toFresh() {
  177. this.setData({
  178. list: []
  179. })
  180. },
  181. //搜索
  182. search() {
  183. this.toFresh()
  184. this.getCategoryAll()
  185. },
  186. //选择展示详情
  187. chooseItem(e) {
  188. this.setData({
  189. fadeIn: true,
  190. item: this.data.list[e.currentTarget.dataset.index]
  191. })
  192. },
  193. hideMoadal() {
  194. this.setData({
  195. fadeIn: false,
  196. item: ''
  197. })
  198. },
  199. selected() {
  200. //根据当前默认企业税号和选择的税收分类编码确定可用税率
  201. let params = {
  202. entTaxId: wx.getStorageSync('entTaxId'),
  203. taxCateCode: this.data.item.code
  204. }
  205. utils.axios({
  206. method: 'get',
  207. url: '/sys/entInfo/rates',
  208. data: params,
  209. sendBefore() {
  210. wx.showLoading({
  211. title: '加载中...',
  212. mask: true
  213. })
  214. },
  215. success: res => {
  216. let rateArray = [0]
  217. for (let i = 0; i < res.data.length; i++) {
  218. rateArray.push(res.data[i] * 100)
  219. }
  220. utils.funPrev(prev => {
  221. prev.setData({
  222. ['taxRate']: rateArray,
  223. ['params.taxationCateCode']: this.data.item.code,
  224. ['params.taxationCateName']: this.data.item.name
  225. })
  226. wx.navigateBack()
  227. })
  228. },
  229. complete() {
  230. wx.hideLoading()
  231. }
  232. })
  233. },
  234. noScroll() {},
  235. /**
  236. * 生命周期函数--监听页面初次渲染完成
  237. */
  238. onReady: function () {
  239. },
  240. /**
  241. * 生命周期函数--监听页面显示
  242. */
  243. onShow: function () {
  244. },
  245. /**
  246. * 生命周期函数--监听页面隐藏
  247. */
  248. onHide: function () {
  249. },
  250. /**
  251. * 生命周期函数--监听页面卸载
  252. */
  253. onUnload: function () {
  254. },
  255. /**
  256. * 页面相关事件处理函数--监听用户下拉动作
  257. */
  258. onPullDownRefresh: function () {
  259. },
  260. /**
  261. * 页面上拉触底事件的处理函数
  262. */
  263. onReachBottom: function () {
  264. },
  265. /**
  266. * 用户点击右上角分享
  267. */
  268. onShareAppMessage: function () {
  269. }
  270. })