cart.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. const app = getApp()
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. loading: false,
  8. show: false,
  9. page: 0,
  10. cart_total: 0,
  11. cart_number: 0,
  12. item: [],
  13. likeitem:[],
  14. amount: [],
  15. address:[],
  16. address_isnull: 0,
  17. actions: [],
  18. orderParams: {}, // 支付参数
  19. chickPayBtn: false, //点击了支付按钮(订单信息交由古德云组件)
  20. chickOnPay: false, // 用户是否已经点击了「支付」并成功跳转到 古德云收银台 小程序
  21. },
  22. /**
  23. * 生命周期函数--监听页面显示
  24. */
  25. onShow: function () {
  26. this.getCart();
  27. app.setTabBarCartNumber();
  28. },
  29. /**
  30. * 获取购物车中的订单
  31. */
  32. getCart: function () {
  33. let that = this;
  34. var cart = wx.getStorageSync('shopping_cart') || [];
  35. app.api().Post('api/v3/fastshop/cart/cartItem', { cart: cart }, function (result) {
  36. if (result.code == 200) {
  37. that.setData({
  38. item: result.data.item,
  39. amount: result.data.amount,
  40. cart_total: parseFloat(result.data.amount.real_amount) + parseFloat(result.data.amount.real_freight),
  41. cart_number: app.util().count(result.data.item),
  42. })
  43. that.getAddress();
  44. } else {
  45. wx.removeStorageSync('shopping_cart')
  46. wx.removeStorageSync('shopping_num')
  47. that.setData({
  48. item: [],
  49. cart_total: 0,
  50. cart_number: 0
  51. })
  52. that.getItem();
  53. }
  54. })
  55. },
  56. /**
  57. * 获取商品
  58. * 如果没有了商品将不再重复请求加载
  59. */
  60. getItem: function () {
  61. let that = this;
  62. var parms = {
  63. cate_id: 0,
  64. types: 3,
  65. page: 1
  66. }
  67. app.api().Get('api/v3/fastshop/shop/cateitem', parms, function (result) {
  68. that.setData({
  69. likeitem: result.data
  70. });
  71. })
  72. },
  73. /**
  74. * 删除购物车数据
  75. */
  76. delItem: function (e) {
  77. let that = this,
  78. item = that.data.item,
  79. id = parseInt(e.currentTarget.dataset.id);
  80. delete item[id];
  81. //计算总价格
  82. var cart_total = 0;
  83. var shopping_num = 0;
  84. for (let i in item) {
  85. cart_total += parseFloat(item[i].amount);
  86. shopping_num += parseInt(item[i].num);
  87. }
  88. //重新设置变量
  89. that.setData({
  90. item: item,
  91. cart_total: cart_total,
  92. cart_number: app.util().count(item),
  93. })
  94. //重新设置缓存
  95. var cart = wx.getStorageSync('shopping_cart');
  96. delete cart[id];
  97. wx.setStorageSync('shopping_cart',cart)
  98. wx.setStorageSync('shopping_num', shopping_num) //购物车数量
  99. app.setTabBarCartNumber();
  100. },
  101. /**
  102. * 修改购物车数量
  103. */
  104. onChange: function (e) {
  105. let that = this;
  106. var item = that.data.item;
  107. var num = parseInt(e.detail);
  108. var id = parseInt(e.currentTarget.dataset.id);
  109. //重写购物车数据
  110. item[id].num = num;
  111. item[id].amount = app.util().accMul(num, item[id].sell_price).toFixed(2);
  112. var cart = wx.getStorageSync('shopping_cart') || [],
  113. cart_total = 0,
  114. shopping_num = 0;
  115. //计算总价格
  116. for (let i in item) {
  117. cart_total += parseFloat(item[i].amount);
  118. shopping_num += parseInt(item[i].num);
  119. }
  120. //重新设置缓存
  121. cart[id] = num;
  122. wx.setStorageSync('shopping_cart', cart)
  123. wx.setStorageSync('shopping_num', shopping_num)
  124. app.setTabBarCartNumber();
  125. //请求
  126. app.api().Post('api/v3/fastshop-cart-cartItem',{cart:cart}, function (result) {
  127. if (result.code == 200) {
  128. that.setData({
  129. amount: result.data.amount,
  130. item: item,
  131. cart_total: cart_total + parseFloat(result.data.amount.real_freight)
  132. })
  133. }
  134. })
  135. },
  136. /**
  137. * 唤起微信支付
  138. */
  139. wchatPayment: function (buytype) {
  140. let that = this;
  141. var ids = app.util().clearArray(wx.getStorageSync('shopping_cart'));
  142. var param = {
  143. ids:JSON.stringify(ids),
  144. address: that.data.address.id,
  145. buytype: buytype,
  146. ucode:app.globalData.loginuser.ucode
  147. }
  148. app.api().Post("api/v3/fastshop/cart/doPay",param,function (rel) {
  149. if (200 == rel.code) {
  150. wx.removeStorageSync('shopping_cart')
  151. wx.removeStorageSync('shopping_num')
  152. if (rel.data.type == 1) {
  153. that.setData({
  154. chickPayBtn: true,
  155. orderParams: rel.data.order
  156. })
  157. } else {
  158. app.doWechatPay(rel.data.order,function (res) {
  159. wx.navigateTo({
  160. url: '../order/index'
  161. })
  162. }, function (res) {
  163. wx.showModal({
  164. content: '支付失败或取消', showCancel: false, complete: function () {
  165. wx.navigateTo({
  166. url: '../order/index'
  167. });
  168. }
  169. })
  170. })
  171. }
  172. }
  173. })
  174. },
  175. /**
  176. * 读取微信地址
  177. */
  178. getAddress: function () {
  179. let that = this;
  180. app.api().Get("openapi/v1/user/getaddress", {'signkey': 'dopay'},function (rel) {
  181. if (rel.code == 200) {
  182. that.setData({
  183. address: rel.data,
  184. address_isnull: Object.keys(rel.data).length,
  185. })
  186. }
  187. })
  188. },
  189. /**
  190. * 读取微信地址
  191. */
  192. address: function () {
  193. let that = this;
  194. wx.getSetting({
  195. success(res) {
  196. if (res.authSetting['scope.address'] == false) {
  197. wx.openSetting({
  198. success: (res) => {
  199. res.authSetting = {
  200. "scope.address": true,
  201. }
  202. }
  203. })
  204. } else {
  205. wx.chooseAddress({
  206. success: function (res) {
  207. var name = res.userName;
  208. var telNumber = res.telNumber;
  209. var city = res.provinceName + res.cityName + res.countyName;
  210. var address = res.detailInfo;
  211. app.api().Post("openapi/v1/user/createaddress", {
  212. name: name,
  213. telphone: telNumber,
  214. city: city,
  215. address: address
  216. }, function (rel) {
  217. that.setData({
  218. address: rel.data,
  219. address_isnull: Object.keys(rel.data).length,
  220. })
  221. });
  222. }
  223. })
  224. }
  225. }
  226. })
  227. },
  228. /**
  229. * 支付方式
  230. */
  231. payTypes: function () {
  232. let that = this;
  233. var actions = that.data.actions;
  234. var address_isnull = that.data.address_isnull
  235. if (address_isnull == 0) {
  236. wx.showModal({
  237. content: '必须选择收货地址',
  238. })
  239. } else {
  240. this.setData({
  241. show: true
  242. });
  243. }
  244. if (that.data.show) {
  245. app.api().Get('api/v3/fastshop/index/shopBuyTypes',{'signkey':'shopBuyTypes'},function (result) {
  246. that.setData({
  247. actions: result.data
  248. });
  249. })
  250. }
  251. },
  252. /**
  253. * 是否弹出支付菜单
  254. */
  255. onClose() {
  256. this.setData({
  257. show:false
  258. });
  259. },
  260. onSelect(event) {
  261. let that = this;
  262. var address_isnull = that.data.address_isnull
  263. var addressId = that.data.address.id;
  264. var payTypes = event.detail.types;
  265. wx.showLoading({ title: '支付中' })
  266. switch (payTypes) {
  267. case 1:
  268. that.wchatPayment("wepay");
  269. break;
  270. case 2:
  271. that.wchatPayment("point");
  272. break;
  273. default:
  274. that.onClose();
  275. }
  276. },
  277. /**
  278. * 支付成功的事件处理函数
  279. * res.detail 为 payjs 小程序返回的订单信息
  280. * 可通过 res.detail.payjsOrderId 拿到 payjs 订单号
  281. * 可通过 res.detail.responseData 拿到详细支付信息
  282. */
  283. goodPaySuccess: function (res) {
  284. if (res.detail.return_code = "SUCCESS") {
  285. wx.navigateTo({
  286. url: '/pages/order/index'
  287. })
  288. }
  289. },
  290. /**
  291. * 支付失败的事件处理函数
  292. * res.detail.error 为 true 代表传入小组件的参数存在问题
  293. * res.detail.navigateSuccess 代表了是否成功跳转到 PAYJS 小程序
  294. * res.detail.event 可能存有失败的原因
  295. * 如果下单成功但是用户取消支付则 res.detail.event.return_code == FAIL
  296. */
  297. goodPayFail: function (res) {
  298. this.setData({
  299. chickPayBtn: false,
  300. cart_number: 0
  301. })
  302. },
  303. /**
  304. * 支付完毕的事件处理函数
  305. * 无论支付成功或失败均会执行
  306. */
  307. goodPayComplete: function () {
  308. this.setData({
  309. chickPayBtn: false,
  310. cart_number:0
  311. })
  312. },
  313. /**
  314. * 组件内部数据被修改时的事件
  315. * 当用户跳转到 云收银台 小程序并等待返回的过程中 chickOnPay 值为 true
  316. */
  317. goodPayChange(res) {
  318. if (res.detail.chickOnPay) {
  319. this.setData({
  320. chickOnPay: res.detail.chickOnPay
  321. })
  322. }
  323. }
  324. })