index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // index.js
  2. let myPro = require("../../../../utils/wxRequest.js");
  3. let util = require("../../../../utils/util.js");
  4. Page({
  5. data: {
  6. imgUrl: getApp().globalData.imgUrl,
  7. type: '-1', // 订单状态 空全部(-1) 0未支付 1待发货 2待自提 3已完成
  8. page: 1,
  9. size: 10,
  10. dataList: [],
  11. finished: false,
  12. loading: false,
  13. timer: null // 配送计时器
  14. },
  15. onLoad: function (options) {
  16. // Do some initialize when page load.
  17. let that = this;
  18. if(options.type){
  19. that.setData({
  20. type: options.type
  21. });
  22. console.log('type',that.data.type,typeof that.data.type)
  23. }
  24. },
  25. onShow: function () {
  26. // Do something when page show.
  27. let that = this;
  28. // 验证登录
  29. getApp().verifyLogin(function(loginStatus){
  30. // console.log('登录状态',loginStatus)
  31. if(loginStatus){
  32. that.setData({
  33. page: 1,
  34. dataList: [],
  35. finished: false
  36. });
  37. that.getDataList();
  38. };
  39. });
  40. },
  41. onReady: function () {
  42. // Do something when page ready.
  43. },
  44. onHide: function () {
  45. // Do something when page hide.
  46. },
  47. onUnload: function () {
  48. // Do something when page close.
  49. },
  50. onPullDownRefresh: function () {
  51. // Do something when pull down.
  52. let that = this;
  53. that.setData({
  54. page: 1,
  55. dataList: [],
  56. finished: false,
  57. });
  58. that.getDataList();
  59. //停止当前页面下拉刷新
  60. wx.stopPullDownRefresh();
  61. },
  62. onReachBottom: function () {
  63. // Do something when page reach bottom.
  64. let that = this;
  65. if(!that.data.finished){
  66. that.getDataList();
  67. };
  68. },
  69. onPageScroll: function () {
  70. // Do something when page scroll
  71. },
  72. onResize: function () {
  73. // Do something when page resize
  74. },
  75. // 订单列表
  76. getDataList(){
  77. let that = this;
  78. clearInterval(that.data.timer);
  79. let params = {
  80. page: that.data.page,
  81. size: that.data.size
  82. };
  83. if(that.data.type != -1){
  84. params.status = that.data.type
  85. };
  86. myPro.wxRequest("user/order/list","GET",params).then(res=>{
  87. let list = res.result;
  88. // 无数据
  89. if(list.length == 0){
  90. wx.showToast({
  91. title: "暂无更多数据",
  92. icon: "none",
  93. });
  94. that.setData({
  95. finished: true
  96. });
  97. }else{
  98. for(let i in list){
  99. list[i].goodsNums = 0; // 单笔订单的商品总数
  100. list[i].thumbArr = []; // 单笔订单的商品图片
  101. for(let j in list[i].order_good){
  102. let goods = list[i].order_good[j];
  103. list[i].goodsNums += parseInt(goods.nums);
  104. list[i].thumbArr.push(goods.good_thumb);
  105. };
  106. list[i].thumbArrLength = list[i].thumbArr.length; // 单笔订单的商品图片数量
  107. // 多于三张截取
  108. if(list[i].thumbArr.length > 3){
  109. list[i].thumbArrCustom = list[i].thumbArr.slice(0,3);
  110. }else{
  111. list[i].thumbArrCustom = list[i].thumbArr;
  112. };
  113. };
  114. if(that.data.page == 1){
  115. that.setData({
  116. dataList: list
  117. });
  118. }else{
  119. that.setData({
  120. dataList: that.data.dataList.concat(list)
  121. });
  122. }
  123. that.setData({
  124. page: that.data.page + 1
  125. });
  126. // 开启定时
  127. if(that.data.type == 1){
  128. that.newTimer()
  129. };
  130. };
  131. }).catch(err=>{
  132. console.log('报错信息',err);
  133. wx.showToast({
  134. title: err,
  135. icon: "none"
  136. });
  137. })
  138. },
  139. // 切换tab
  140. onChange(event){
  141. console.log(event.detail.name,typeof event.detail.name)
  142. let that = this;
  143. that.setData({
  144. type: event.detail.name,
  145. page: 1,
  146. dataList: [],
  147. finished: false
  148. });
  149. that.getDataList();
  150. },
  151. // 去详情
  152. goDetail(event){
  153. let that = this;
  154. let id = event.currentTarget.dataset.id;
  155. wx.navigateTo({
  156. url: "/pages/user/order/detail/index?order_id="+id
  157. });
  158. },
  159. // 取消订单
  160. cancleOrder(event){
  161. let that = this;
  162. let item = event.currentTarget.dataset.item;
  163. // 未支付的订单可以取消
  164. if(item.order_status == 1 && item.pay_status != 1){
  165. // 取消订单
  166. myPro.wxRequest("user/v2/order/cancelorder","POST",{order_id: item.id}).then(res=>{
  167. wx.showToast({
  168. title: res.msg,
  169. icon: "none"
  170. });
  171. that.setData({
  172. page: 1,
  173. dataList: [],
  174. finished: false
  175. });
  176. that.getDataList();
  177. }).catch(err=>{
  178. console.log('报错信息',err);
  179. wx.showToast({
  180. title: err,
  181. icon: "none"
  182. });
  183. });
  184. }else{
  185. // 已支付的订单要联系商家
  186. wx.showModal({
  187. title: "可联系商家取消",
  188. content: "商家电话"+item.store.phone,
  189. showCancel: false,
  190. confirmText: "我知道了",
  191. confirmColor: "#000000"
  192. });
  193. }
  194. },
  195. // 收货
  196. receiveOrder(event){
  197. let that = this;
  198. let order_id = event.currentTarget.dataset.id;
  199. let params = {
  200. order_id: order_id
  201. };
  202. myPro.wxRequest("user/order/commit","POST",params).then(res=>{
  203. wx.showToast({
  204. title: res.msg,
  205. icon: "none"
  206. });
  207. that.setData({
  208. page: 1,
  209. dataList: [],
  210. finished: false
  211. });
  212. that.getDataList();
  213. }).catch(err=>{
  214. console.log('报错信息',err);
  215. wx.showToast({
  216. title: err,
  217. icon: "none"
  218. });
  219. });
  220. },
  221. // 再来一单
  222. toGoods(){
  223. let that = this;
  224. wx.reLaunch({
  225. url: '/pages/goods/index'
  226. })
  227. },
  228. // 去支付
  229. goTopay(event){
  230. let that = this;
  231. let id = event.currentTarget.dataset.id;
  232. wx.navigateTo({
  233. url: '/pages/pay-order/index?order_id='+id
  234. })
  235. },
  236. // 配送计时
  237. newTimer(){
  238. let that = this;
  239. let list = that.data.dataList;
  240. clearInterval(that.data.timer);
  241. that.data.timer = setInterval(function() {
  242. for(let i in list){
  243. // 当前订单为外卖单且正在配送,开启倒计时
  244. if(list[i].fare_type == 0 && list[i].order_status == 1 && list[i].ship_status){
  245. let ship_at = list[i].ship_at;
  246. if(!list[i].isend){
  247. let shijian = util.daojishi(ship_at,30);
  248. list[i].count_time = shijian['timeStr']
  249. list[i].isend = shijian['isend']
  250. }
  251. }
  252. };
  253. that.setData({
  254. dataList: list
  255. })
  256. },1000)
  257. }
  258. });