product.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. const app = getApp();
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. keyword: '',
  8. pageNum: 1,
  9. pageSize: 999,
  10. productTypeId: '',
  11. productList: [],
  12. scrollHeight: 0, // scroll-view高度
  13. startX: 0, // 开始X坐标
  14. startY: 0, // 开始Y坐标
  15. },
  16. /**
  17. * 生命周期函数--监听页面加载
  18. */
  19. onLoad: function (options) {
  20. // this.getProductList();
  21. // 动态获取屏幕高度
  22. this.setData({
  23. scrollHeight: wx.getSystemInfoSync().screenHeight
  24. })
  25. },
  26. setKeyword(e) {
  27. this.setData({
  28. keyword: e.detail.value
  29. })
  30. },
  31. searchList(){
  32. this.getProductList(this.data.keyword);
  33. },
  34. getProductList(keyword) {
  35. var that = this;
  36. const {
  37. pageNum,
  38. pageSize,
  39. productTypeId
  40. } = that.data;
  41. app.requestP({
  42. url: '/productType/productTypeState',
  43. method: 'post',
  44. data: {
  45. keyword : keyword || '',
  46. pageNum,
  47. pageSize,
  48. productTypeId
  49. }
  50. }).then(res => {
  51. that.setData({
  52. productList: res.data.data
  53. })
  54. }).catch(err=>{
  55. app.showToptip(that,'error',err.data.msg)
  56. })
  57. },
  58. toProductInfo(e) {
  59. const id = e.currentTarget.dataset.id;
  60. const name = e.currentTarget.dataset.name;
  61. var that = this;
  62. wx.navigateTo({
  63. url: '/pages/workbench/product/productInfo/productInfo?id=' + id + '&name=' + name,
  64. })
  65. },
  66. addProductType() {
  67. var that = this;
  68. wx.navigateTo({
  69. url: '/pages/workbench/product/addProdutType/addProdutType',
  70. })
  71. },
  72. // 手指触摸动作开始
  73. touchStart: function (e) {
  74. let that = this;
  75. //开始触摸时 重置所有删除
  76. that.data.productList.forEach(function (v, i) {
  77. if (v.isTouchMove) v.isTouchMove = false; // 只操作为true的
  78. })
  79. // 记录手指触摸开始坐标
  80. that.setData({
  81. startX: e.changedTouches[0].clientX, // 开始X坐标
  82. startY: e.changedTouches[0].clientY, // 开始Y坐标
  83. productList: that.data.productList
  84. })
  85. },
  86. // 手指触摸后移动
  87. touchMove: function (e) {
  88. let that = this,
  89. index = e.currentTarget.dataset.index, // 当前下标
  90. startX = that.data.startX, // 开始X坐标
  91. startY = that.data.startY, // 开始Y坐标
  92. touchMoveX = e.changedTouches[0].clientX, // 滑动变化坐标
  93. touchMoveY = e.changedTouches[0].clientY, // 滑动变化坐标
  94. // 获取滑动角度
  95. angle = that.angle({
  96. X: startX,
  97. Y: startY
  98. }, {
  99. X: touchMoveX,
  100. Y: touchMoveY
  101. });
  102. // 判断滑动角度
  103. that.data.productList.forEach(function (v, i) {
  104. v.isTouchMove = false
  105. // 滑动超过30度角 return
  106. if (Math.abs(angle) > 30) return;
  107. if (i == index) {
  108. // 右滑
  109. if (touchMoveX > startX)
  110. v.isTouchMove = false
  111. // 左滑
  112. else
  113. v.isTouchMove = true
  114. }
  115. })
  116. // 更新数据
  117. that.setData({
  118. productList: that.data.productList
  119. })
  120. },
  121. // 计算滑动角度
  122. angle: function (start, end) {
  123. let that = this,
  124. _X = end.X - start.X,
  125. _Y = end.Y - start.Y;
  126. // 返回角度 /Math.atan()返回数字的反正切值
  127. return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  128. },
  129. // 删除
  130. delList: function (e) {
  131. let that = this,
  132. index = e.currentTarget.dataset.index,
  133. id = that.data.productList[index].id; // 当前下标
  134. app.requestP({
  135. url: '/productType/remove',
  136. method: 'post',
  137. data: {
  138. productTypeId:id
  139. }
  140. }).then(res => {
  141. //切割当前下标元素,更新数据
  142. that.data.productList.splice(index, 1);
  143. that.setData({
  144. productList: that.data.productList
  145. })
  146. app.showToptip(that, "success",res.data.msg);
  147. }).catch(error => {
  148. app.showToptip(that, "error",error.data.msg);
  149. })
  150. },
  151. editList(e){
  152. var id = e.currentTarget.dataset.id;
  153. var name = e.currentTarget.dataset.name;
  154. wx.navigateTo({
  155. url: '/pages/workbench/product/addProdutType/addProdutType?id=' + id + '&name=' + name,
  156. })
  157. },
  158. /**
  159. * 生命周期函数--监听页面初次渲染完成
  160. */
  161. onReady: function () {
  162. },
  163. /**
  164. * 生命周期函数--监听页面显示
  165. */
  166. onShow: function () {
  167. this.getProductList();
  168. // 动态获取屏幕高度
  169. this.setData({
  170. scrollHeight: wx.getSystemInfoSync().screenHeight
  171. })
  172. },
  173. /**
  174. * 生命周期函数--监听页面隐藏
  175. */
  176. onHide: function () {
  177. },
  178. /**
  179. * 生命周期函数--监听页面卸载
  180. */
  181. onUnload: function () {
  182. },
  183. /**
  184. * 页面相关事件处理函数--监听用户下拉动作
  185. */
  186. onPullDownRefresh: function () {
  187. },
  188. /**
  189. * 页面上拉触底事件的处理函数
  190. */
  191. onReachBottom: function () {
  192. },
  193. /**
  194. * 用户点击右上角分享
  195. */
  196. onShareAppMessage: function () {
  197. }
  198. })