list.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // pages/user/address/address.js
  2. var app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. current: 0,
  9. loading: true,
  10. loadding: false,
  11. pages: null,
  12. addressList: [],
  13. isSales: false,
  14. check: false,
  15. addr: {},
  16. addrIndex: null,
  17. startX: 0, //开始坐标
  18. startY: 0,
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. async onLoad(options) {
  24. let that = this;
  25. const eventChannel = this.getOpenerEventChannel();
  26. if (!eventChannel) {
  27. return;
  28. }
  29. eventChannel.on('acceptDataFromOpenerPage', function (data) {
  30. that.setData({
  31. check: data.check,
  32. addr: data.addr
  33. })
  34. });
  35. },
  36. /**
  37. * 生命周期函数--监听页面初次渲染完成
  38. */
  39. onReady() {
  40. },
  41. /**
  42. * 生命周期函数--监听页面显示
  43. */
  44. async onShow() {
  45. this.setData({
  46. current: 0,
  47. pages: null,
  48. addressList: []
  49. });
  50. if (!wx.getStorageSync('userInfo')) {
  51. return;
  52. }
  53. let {
  54. data
  55. } = await app.ajax.gets('/admin/address/check');
  56. if (data.code == 0) {
  57. if (data.data == 2004) {
  58. this.setData({
  59. isSales: false
  60. })
  61. } else if (data.data == 2005) {
  62. this.setData({
  63. isSales: true
  64. })
  65. }
  66. }
  67. this.initData();
  68. this.setData({
  69. loading: false,
  70. })
  71. },
  72. searchGoods(e) {
  73. this.setData({
  74. current: 0,
  75. pages: null,
  76. addressList: []
  77. });
  78. this.initData(e);
  79. },
  80. async initData(e) {
  81. let that = this;
  82. this.setData({
  83. loadding: true,
  84. });
  85. let addrName = e && e.detail.value ? e.detail.value : '';
  86. let {
  87. data
  88. } = await app.ajax.get({
  89. url: this.data.isSales ? "/admin/address/page" : "/admin/bxdClientAddress/page",
  90. data: {
  91. current: this.data.current + 1,
  92. size: 500,
  93. addrName
  94. }
  95. })
  96. this.setData({
  97. current: this.data.current + 1,
  98. addressList: this.data.addressList.concat(data.data.records.map(v => {
  99. if (this.data.isSales) {
  100. v.eccId = v.eccId;
  101. v.address = v.provice + v.city + v.area + v.address;
  102. v.addrId = `clientAddrId_${v.id}`;
  103. v.customerName = v.eccName;
  104. v.province = v.provice;
  105. v.addr = v.address;
  106. v.remainQuota = v.remainQuota;
  107. v.receiver = v.receiver;
  108. v.mobile = v.mobile;
  109. } else {
  110. v.addrId = v.addrId;
  111. v.customerName = v.customerName;
  112. v.receiver = v.receiver;
  113. v.mobile = v.mobile;
  114. v.tel = v.tel;
  115. v.province = v.province;
  116. v.addr = v.addr;
  117. v.email = v.email;
  118. v.remark = v.remark;
  119. }
  120. v.city = v.city;
  121. v.area = v.area;
  122. v.choseArr = [];
  123. v.chosePop = false;
  124. v['check'] = that.data.addr.addrId == v.addrId;
  125. return v;
  126. })),
  127. pages: data.data.pages,
  128. loadding: false,
  129. })
  130. },
  131. async setAddrd(e) {
  132. let that = this;
  133. let item = e.currentTarget.dataset.item;
  134. await app.ajax.post({
  135. url: "/admin/bxdClientAddress/default",
  136. method: 'put',
  137. data: {
  138. addrId: item.addrId
  139. }
  140. });
  141. setTimeout(() => {
  142. wx.showToast({
  143. title: '设置成功',
  144. mask: true
  145. });
  146. }, 200);
  147. setTimeout(() => {
  148. that.setData({
  149. current: 0,
  150. pages: null,
  151. addressList: []
  152. });
  153. that.initData();
  154. }, 1200);
  155. },
  156. async delAddr(e) {
  157. let that = this;
  158. let item = e.currentTarget.dataset.item;
  159. wx.showModal({
  160. title: '提示',
  161. content: `此操作将删除该 [${item.receiver}] 收货人数据, 是否继续?`,
  162. complete: async (res) => {
  163. if (res.confirm) {
  164. await app.ajax.post({
  165. url: '/admin/bxdClientAddress',
  166. method: 'put',
  167. data: {
  168. addrId: item.addrId,
  169. status: 0,
  170. }
  171. });
  172. wx.showToast({
  173. title: '删除成功!',
  174. });
  175. setTimeout(() => {
  176. that.setData({
  177. current: 0,
  178. pages: null,
  179. addressList: []
  180. });
  181. that.initData();
  182. }, 1000);
  183. }
  184. }
  185. })
  186. },
  187. renderAddr() {
  188. // /admin/bxdClientAddress/addressParse?address=
  189. },
  190. chooseAddr: function (e) {
  191. if (!this.data.check) {
  192. return;
  193. }
  194. let addressList = this.data.addressList.map(v => {
  195. v['check'] = false;
  196. return v;
  197. });
  198. addressList[Number(e.currentTarget.dataset.index)]['check'] = true;
  199. this.setData({
  200. addressList,
  201. addr: e.currentTarget.dataset.item,
  202. addrIndex: e.currentTarget.dataset.index,
  203. })
  204. },
  205. goBackPage: function (e) {
  206. if (!this.data.check) {
  207. return;
  208. }
  209. // let addr = this.data.addressList[this.data.addrIndex];
  210. // if (!addr || !addr.addrId) {
  211. // wx.showToast({
  212. // title: '请选择地址',
  213. // icon: 'error'
  214. // });
  215. // return;
  216. // }
  217. if (!this.data.addr || !this.data.addr.addrId) {
  218. wx.showToast({
  219. title: '请选择地址',
  220. icon: 'error'
  221. });
  222. return;
  223. }
  224. let addrs = this.data.addressList.filter(v => v.addrId == this.data.addr.addrId);
  225. if (!addrs || addrs.length == 0) {
  226. wx.showToast({
  227. title: '请选择地址',
  228. icon: 'error'
  229. });
  230. return;
  231. } else {
  232. this.setData({
  233. addr: addrs[0]
  234. })
  235. }
  236. const eventChannel = this.getOpenerEventChannel()
  237. //console.log(eventChannel)
  238. if (!eventChannel || !this.data.check) {
  239. return;
  240. }
  241. eventChannel.emit('acceptDataFromOpenerPage', {
  242. addr: this.data.addr
  243. });
  244. wx.navigateBack({
  245. data: 1
  246. })
  247. },
  248. /**
  249. * 生命周期函数--监听页面隐藏
  250. */
  251. onHide() {
  252. },
  253. /**
  254. * 生命周期函数--监听页面卸载
  255. */
  256. onUnload() {
  257. },
  258. /**
  259. * 页面相关事件处理函数--监听用户下拉动作
  260. */
  261. onPullDownRefresh() {
  262. },
  263. /**
  264. * 页面上拉触底事件的处理函数
  265. */
  266. onReachBottom() {
  267. },
  268. /**
  269. * 用户点击右上角分享
  270. */
  271. onShareAppMessage() {
  272. },
  273. touchE: function (e) {
  274. // //console.log(e);
  275. var that = this
  276. if (e.changedTouches.length == 1) {
  277. //手指移动结束后触摸点位置的X坐标
  278. var endX = e.changedTouches[0].clientX;
  279. //触摸开始与结束,手指移动的距离
  280. var disX = that.data.startX - endX;
  281. var delBtnWidth = 120;
  282. //如果距离小于删除按钮的1/2,不显示删除按钮
  283. var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "rpx" : "left:0rpx";
  284. //获取手指触摸的是哪一项
  285. var index = e.currentTarget.dataset.index;
  286. var list = that.data.addressList;
  287. list[index].txtStyle = txtStyle;
  288. //更新列表的状态
  289. that.setData({
  290. addressList: list
  291. });
  292. }
  293. },
  294. //手指触摸动作开始 记录起点X坐标
  295. touchstart: function (e) {
  296. //开始触摸时 重置所有删除
  297. this.data.addressList.forEach(function (v, i) {
  298. if (v.isTouchMove) //只操作为true的
  299. v.isTouchMove = false;
  300. })
  301. this.setData({
  302. startX: e.changedTouches[0].clientX,
  303. startY: e.changedTouches[0].clientY,
  304. addressList: this.data.addressList
  305. })
  306. },
  307. //滑动事件处理
  308. touchmove: function (e) {
  309. var that = this,
  310. index = e.currentTarget.dataset.index, //当前索引
  311. startX = that.data.startX, //开始X坐标
  312. startY = that.data.startY, //开始Y坐标
  313. touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
  314. touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
  315. //获取滑动角度
  316. angle = that.angle({
  317. X: startX,
  318. Y: startY
  319. }, {
  320. X: touchMoveX,
  321. Y: touchMoveY
  322. });
  323. that.data.addressList.forEach(function (v, i) {
  324. v.isTouchMove = false
  325. //滑动超过30度角 return
  326. if (Math.abs(angle) > 30) return;
  327. if (i == index) {
  328. if (touchMoveX > startX) //右滑
  329. v.isTouchMove = false
  330. else //左滑
  331. v.isTouchMove = true
  332. }
  333. })
  334. //更新数据
  335. that.setData({
  336. addressList: that.data.addressList
  337. })
  338. },
  339. /**
  340. * 计算滑动角度
  341. * @param {Object} start 起点坐标
  342. * @param {Object} end 终点坐标
  343. */
  344. angle: function (start, end) {
  345. var _X = end.X - start.X,
  346. _Y = end.Y - start.Y
  347. //返回角度 /Math.atan()返回数字的反正切值
  348. return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  349. },
  350. })