家政小程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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({
  204. id: g.product_id,
  205. amount: g.product_amount
  206. });
  207. }
  208. });
  209. if (products.length === 0) {
  210. return $.wx.showToast({
  211. title: '请先选择购买商品'
  212. });
  213. }
  214. $.wx.showToast({
  215. title: '购买中'
  216. }, 'loading');
  217. $.request('product_order/store', 'POST', {
  218. address,
  219. contact: name,
  220. phone,
  221. products
  222. }).then((res) => {
  223. if (res.data) {
  224. this.callWXPay(res.data.id, res.data.order_type || 'product_order');
  225. }
  226. });
  227. },
  228. callWXPay(order_id, order_type) {
  229. $.request('payments/pay', 'POST', {
  230. order_id,
  231. order_type
  232. }).then((res) => {
  233. if (res.msg) {
  234. const {
  235. timestamp,
  236. paySign,
  237. nonceStr
  238. } = res.msg;
  239. const pck = res.msg.package;
  240. wx.requestPayment({
  241. timeStamp: timestamp + '',
  242. nonceStr,
  243. package: pck,
  244. signType: 'MD5',
  245. paySign,
  246. success(res) {
  247. $.wx.showToast({
  248. title: '支付成功'
  249. });
  250. },
  251. fail() {
  252. $.wx.showToast({
  253. title: '支付失败'
  254. });
  255. },
  256. complete() {
  257. setTimeout(function () {
  258. $.router.goto({
  259. path: '/pages/order/index',
  260. type: $.ROUTER_TYPE.SWITCH_TAB
  261. });
  262. }, 2000);
  263. }
  264. });
  265. }
  266. });
  267. }
  268. });