家政小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. const $ = global;
  2. new $.Page({
  3. data: {
  4. goodsList: [],
  5. selectedAmount: 0,
  6. totalPrice: 0,
  7. isEditing: false, // 是否正在编辑
  8. hasAmountModify: false
  9. },
  10. onShow() {
  11. $.wx.showToast({
  12. title: '加载中'
  13. }, 'loading');
  14. this.setData({
  15. hasAmountModify: false,
  16. isEditing: false
  17. });
  18. this.getCarts();
  19. },
  20. getCarts() {
  21. $.request('shopcars', 'GET', {}).then((res) => {
  22. $.wx.hideToast('loading');
  23. if (res.data && res.data.length > 0) {
  24. this.setData({
  25. goodsList: res.data
  26. });
  27. this.calAmount();
  28. } else {
  29. this.setData({
  30. goodsList: [],
  31. isEditing: false,
  32. selectedAmount: 0,
  33. totalPrice: 0
  34. });
  35. }
  36. });
  37. },
  38. changeEditStatus() {
  39. this.setData({
  40. isEditing: !this.data.isEditing
  41. });
  42. if (!this.data.isEditing && this.data.hasAmountModify) {
  43. this.confirmModify();
  44. }
  45. },
  46. // 删除指定商品
  47. deleteGoods(e) {
  48. const idx = e.currentTarget.dataset.index;
  49. const id = e.currentTarget.dataset.id;
  50. const goodsList = this.data.goodsList;
  51. $.wx.showToast({
  52. title: '删除中'
  53. }, 'loading');
  54. $.request(`shopcar/${id}`, 'DELETE', {}).then(() => {
  55. $.wx.hideToast('loading');
  56. if (goodsList.length > 0) {
  57. goodsList.splice(idx, 1);
  58. this.setData({
  59. goodsList
  60. });
  61. if (goodsList.length === 0) {
  62. this.setData({
  63. isEditing: false
  64. });
  65. }
  66. this.calAmount();
  67. }
  68. });
  69. },
  70. // 删除选中的商品
  71. deleteSelected() {
  72. const ids = [];
  73. const goodsList = this.data.goodsList;
  74. goodsList.forEach((g) => {
  75. if (g.checked) {
  76. ids.push(g.id);
  77. }
  78. });
  79. if (ids.length > 0) {
  80. $.wx.showToast({
  81. title: '删除中'
  82. }, 'loading');
  83. $.request('shopcar/pdelete', 'DELETE', {
  84. ids
  85. }).then(() => {
  86. this.getCarts();
  87. });
  88. } else {
  89. $.wx.showToast({
  90. title: '请选择需要删除的商品'
  91. });
  92. }
  93. },
  94. changeSelect(e) {
  95. const idx = e.currentTarget.dataset.index;
  96. const goodsList = this.data.goodsList;
  97. goodsList[idx].checked = !goodsList[idx].checked;
  98. this.setData({
  99. goodsList
  100. });
  101. this.calAmount();
  102. },
  103. changeSelectAll() {
  104. let goodsList = this.data.goodsList;
  105. let selectCount = 0;
  106. for (let goods of goodsList) {
  107. if (goods.checked) {
  108. selectCount++;
  109. }
  110. }
  111. if (selectCount < goodsList.length) {
  112. // 切到全选
  113. goodsList = goodsList.map((g) => {
  114. g.checked = true;
  115. return g;
  116. });
  117. } else {
  118. // 全不选
  119. goodsList = goodsList.map((g) => {
  120. g.checked = false;
  121. return g;
  122. });
  123. }
  124. this.setData({
  125. goodsList
  126. });
  127. this.calAmount();
  128. },
  129. minusAmount(e) {
  130. const idx = e.currentTarget.dataset.index;
  131. const goodsList = this.data.goodsList;
  132. if (goodsList[idx].product_amount > 1) {
  133. goodsList[idx].product_amount--;
  134. this.setData({
  135. goodsList,
  136. hasAmountModify: true
  137. });
  138. this.calAmount();
  139. }
  140. },
  141. addAmount(e) {
  142. const idx = e.currentTarget.dataset.index;
  143. const goodsList = this.data.goodsList;
  144. goodsList[idx].product_amount++;
  145. this.setData({
  146. goodsList,
  147. hasAmountModify: true
  148. });
  149. this.calAmount();
  150. },
  151. calAmount() {
  152. const goodsList = this.data.goodsList;
  153. let totalPrice = 0;
  154. let selectedAmount = 0;
  155. goodsList.forEach((g) => {
  156. if (g.checked) {
  157. selectedAmount++;
  158. totalPrice += g.price * g.product_amount;
  159. }
  160. });
  161. this.setData({
  162. selectedAmount,
  163. totalPrice
  164. });
  165. },
  166. confirmModify() {
  167. let carts = [];
  168. const goodsList = this.data.goodsList;
  169. if (goodsList.length === 0) return;
  170. goodsList.forEach((g) => {
  171. carts.push({
  172. id: g.id,
  173. product_id: g.product_id,
  174. product_amount: g.product_amount
  175. });
  176. });
  177. $.request('shopcar/pstore', 'POST', {
  178. data: carts
  179. }).then(() => {
  180. this.setData({
  181. hasAmountModify: false
  182. });
  183. });
  184. },
  185. toSettle() {
  186. const products = [];
  187. const { name, phone, address, goodsList } = this.data;
  188. if (goodsList.length === 0) return;
  189. if (!name || !phone || !address) {
  190. return $.wx.showToast({
  191. title: '请把地址信息填写完整'
  192. });
  193. }
  194. if (!this._isPhone(phone)) {
  195. return $.wx.showToast({
  196. title: '请输入正确的手机号码'
  197. });
  198. }
  199. goodsList.forEach((g) => {
  200. if (g.checked) {
  201. g.amount = g.product_amount;
  202. g.id = g.product_id;
  203. products.push(g);
  204. }
  205. });
  206. if (products.length === 0) {
  207. return $.wx.showToast({
  208. title: '请先选择购买商品'
  209. });
  210. }
  211. $.wx.showToast({
  212. title: '购买中'
  213. }, 'loading');
  214. $.request('product_order/store', 'POST', {
  215. address,
  216. contact: name,
  217. phone,
  218. products
  219. }).then((res) => {
  220. if (res.data) {
  221. this.callWXPay(res.data.id, res.data.order_type || 'product_order');
  222. }
  223. });
  224. },
  225. callWXPay(order_id, order_type) {
  226. $.request('payments/pay', 'POST', {
  227. order_id,
  228. order_type
  229. }).then((res) => {
  230. if (res.msg) {
  231. const {
  232. timestamp,
  233. paySign,
  234. nonceStr
  235. } = res.msg;
  236. const pck = res.msg.package;
  237. wx.requestPayment({
  238. timeStamp: timestamp + '',
  239. nonceStr,
  240. package: pck,
  241. signType: 'MD5',
  242. paySign,
  243. success(res) {
  244. $.wx.showToast({
  245. title: '支付成功'
  246. });
  247. },
  248. fail() {
  249. $.wx.showToast({
  250. title: '支付失败'
  251. });
  252. },
  253. complete() {
  254. setTimeout(function () {
  255. $.router.goto({
  256. path: '/pages/order/index',
  257. type: $.ROUTER_TYPE.SWITCH_TAB
  258. });
  259. }, 2000);
  260. }
  261. });
  262. }
  263. });
  264. }
  265. });