index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. fare_type: 1, // 0外卖 1自提 默认自提
  8. transprotFare: 0, // 配送费
  9. userInfo: null,
  10. forbidden: null, // 1不允许下单
  11. store: null,
  12. is_member: 0 // 是否为会员
  13. },
  14. onLoad: function (options) {
  15. // Do some initialize when page load.
  16. let that = this;
  17. that.setData({
  18. userInfo: getApp().globalData.userInfo
  19. });
  20. },
  21. onShow: function () {
  22. // Do something when page show.
  23. let that = this;
  24. that.setData({
  25. is_member: getApp().globalData.is_member
  26. });
  27. // 清掉备注
  28. wx.removeStorageSync('user_mark');
  29. // 店铺
  30. if(wx.getStorageSync('store')){
  31. that.setData({
  32. store: JSON.parse(wx.getStorageSync('store'))
  33. });
  34. };
  35. that.getCreatelist();
  36. },
  37. onReady: function () {
  38. // Do something when page ready.
  39. },
  40. onHide: function () {
  41. // Do something when page hide.
  42. },
  43. onUnload: function () {
  44. // Do something when page close.
  45. },
  46. onPullDownRefresh: function () {
  47. // Do something when pull down.
  48. },
  49. onReachBottom: function () {
  50. // Do something when page reach bottom.
  51. },
  52. onPageScroll: function () {
  53. // Do something when page scroll
  54. },
  55. onResize: function () {
  56. // Do something when page resize
  57. },
  58. // 获取购物车商品及配送费
  59. getCreatelist(){
  60. let that = this;
  61. let params = {};
  62. // 获取配送方式
  63. if(wx.getStorageSync('fare_type')){
  64. let fare_type = wx.getStorageSync('fare_type');
  65. params.fare_type = fare_type;
  66. that.setData({
  67. fare_type: fare_type
  68. });
  69. };
  70. // 获取收货地址
  71. if(that.data.fare_type != 1){
  72. if(wx.getStorageSync('checkedAddress')){
  73. let checkedAddress = JSON.parse(wx.getStorageSync('checkedAddress'));
  74. that.setData({
  75. checkedAddress: checkedAddress
  76. });
  77. params.address_id = checkedAddress.id;
  78. };
  79. };
  80. // console.log('params',params);
  81. myPro.wxRequest("user/v3/order/createlist","GET",params).then(res=>{
  82. that.setData({
  83. cartGoods: res.result.cartsList,
  84. transprotFare: res.result.orderInfo.delivery_fee,
  85. totalPrice: res.result.orderInfo.pay_price,
  86. });
  87. }).catch(err=>{
  88. console.log('报错信息',err);
  89. wx.showToast({
  90. title: err,
  91. icon: "none"
  92. })
  93. })
  94. },
  95. // 获取购物车商品
  96. getCartsList(){
  97. let that = this;
  98. let params = {};
  99. myPro.wxRequest("user/v3/carts/list","GET",params).then(res=>{
  100. // 已加购商品
  101. let list = res.result.list;
  102. that.setData({
  103. cartGoods: list,
  104. totalPrice: res.result.money,
  105. totalNums: res.result.total_num
  106. });
  107. // 计算价格和数量
  108. that.computedGoods();
  109. }).catch(err=>{
  110. console.log('报错信息',err);
  111. wx.showToast({
  112. title: err,
  113. icon: "none"
  114. });
  115. })
  116. },
  117. // 计算运费(店铺与收获地址的距离)
  118. getTransportFare(){
  119. let that = this;
  120. let params = {
  121. lat: that.data.checkedAddress.lat,
  122. lng: that.data.checkedAddress.lnt, // lnt没写错
  123. store_id: that.data.store.id
  124. };
  125. wx.request({
  126. method: "GET",
  127. url: getApp().globalData.api + "api/common/store/distance",
  128. data: params,
  129. success: function(res){
  130. let data = res.data;
  131. if(data.code == 200){
  132. that.setData({
  133. forbidden: null,
  134. transprotFare: data.result.delivery_fee
  135. });
  136. // 计算总费用
  137. that.computedGoods();
  138. }else if(data.code == 400){
  139. // 约定400 为超出配送范围
  140. wx.showToast({
  141. title: data.msg,
  142. icon: "none"
  143. });
  144. that.setData({
  145. transprotFare: data.result.delivery_fee
  146. });
  147. // 计算总费用 但不允许下单
  148. that.computedGoods();
  149. that.setData({
  150. forbidden: 1
  151. });
  152. }else{
  153. wx.showToast({
  154. title: data.msg,
  155. icon: "none"
  156. });
  157. }
  158. },
  159. fail: function(res){
  160. console.log("报错",res)
  161. }
  162. })
  163. },
  164. // 计算费用
  165. computedGoods(){
  166. let that = this;
  167. // 商品的总计数量和总价后台直接返回,但配送费得算上
  168. let totalPrice = that.data.totalPrice;
  169. // 加上配送费
  170. if(that.data.transprotFare){
  171. // console.log('配送费',that.data.transprotFare)
  172. totalPrice += parseFloat(that.data.transprotFare); // 返回的可能是字符串
  173. };
  174. that.setData({
  175. totalPrice: totalPrice
  176. });
  177. },
  178. // 输入的备注信息
  179. getUserMark(event){
  180. // console.log(event.detail);
  181. let that = this;
  182. wx.setStorage({
  183. key: 'user_mark',
  184. data: event.detail
  185. });
  186. },
  187. // 进创建订单页
  188. goCreateOrder(event){
  189. let that = this;
  190. if(that.data.forbidden){
  191. wx.showToast({
  192. title: "超出配送范围,请更换地址",
  193. icon: "none"
  194. })
  195. }else{
  196. let url = "";
  197. if(that.data.fare_type == 0){
  198. url = '/pages/pay-order/index?fare_type='+that.data.fare_type+'&address_id='+that.data.checkedAddress.id
  199. }else{
  200. url = '/pages/pay-order/index?fare_type='+that.data.fare_type
  201. }
  202. wx.redirectTo({
  203. url: url
  204. });
  205. }
  206. },
  207. // 更改配送地址
  208. goSelectAddress(){
  209. let that = this;
  210. wx.navigateTo({
  211. url: "/pages/user/address/list/index?is_back=1"
  212. });
  213. }
  214. });