productInfo.js 4.6 KB

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