detail.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. const _Http = getApp().globalData.http;
  2. Page({
  3. data: {
  4. sc_orderid: '',
  5. detail: {},
  6. tabsList: [{
  7. label: "订单明细",
  8. idname: "sc_orderid",
  9. model: "#Product"
  10. }, {
  11. label: "附件",
  12. idname: "sc_orderid",
  13. model: "#Files"
  14. }, {
  15. label: "操作记录",
  16. idname: "sc_orderid",
  17. model: "#Record"
  18. }],
  19. tabsActive: 0,
  20. tabbarList: []
  21. },
  22. onLoad(options) {
  23. getApp().globalData.Language.getLanguagePackage(this, '积分订单详情');
  24. if (options.id) {
  25. this.setData({ sc_orderid: options.id });
  26. this.getDetail();
  27. }
  28. },
  29. getDetail() {
  30. _Http.basic({
  31. id: 2026052510105106,
  32. content: { sc_orderid: this.data.sc_orderid }
  33. }).then(res => {
  34. if (res.code != '1') return wx.showToast({ title: res.msg, icon: "none" });
  35. const item = res.data;
  36. // 处理商品图片
  37. if (item.items && item.items.length) {
  38. item.items.forEach(i => {
  39. try {
  40. if (i.attinfos && i.attinfos.length) {
  41. let att = i.attinfos[0];
  42. i.imgUrl = att.subfiles ? _Http.getSpecifiedImage(att) : (att.url || '');
  43. } else {
  44. i.imgUrl = '';
  45. }
  46. } catch (e) {
  47. i.imgUrl = '';
  48. }
  49. });
  50. }
  51. this.setData({ detail: item });
  52. this.setTabbar();
  53. // 直接设置商品列表到 Product 组件
  54. try {
  55. let Product = this.selectComponent('#Product');
  56. if (Product) {
  57. Product.setList(item.sc_orderid, item.items || []);
  58. }
  59. } catch (e) {}
  60. // 新建订单且无收货地址时,自动选中默认地址
  61. if (item.status === '新建' && !item.rec_contactsid) {
  62. this.setDefaultAddress(item);
  63. }
  64. });
  65. },
  66. setTabbar() {
  67. const { status } = this.data.detail;
  68. const tabbarList = [];
  69. if (status === '新建') {
  70. tabbarList.push({ icon: "icon-tijiao1", label: "提交" });
  71. }
  72. this.setData({ tabbarList });
  73. },
  74. onTabbarClick(e) {
  75. let label = e.currentTarget.dataset.label;
  76. switch (label) {
  77. case '提交':
  78. this.submitOrder();
  79. break;
  80. }
  81. },
  82. /* 提交订单 */
  83. submitOrder() {
  84. // 校验收货地址是否已填写
  85. if (!this.data.detail.rec_contactsid) {
  86. return wx.showToast({ title: '请填写收货信息!', icon: 'none' });
  87. }
  88. wx.showModal({
  89. title: '提示',
  90. content: '确认提交该订单吗?',
  91. confirmText: '确定',
  92. cancelText: '取消',
  93. complete: ({ confirm }) => {
  94. if (confirm) {
  95. _Http.basic({
  96. id: 2026052510111906,
  97. content: { sc_orderid: this.data.sc_orderid }
  98. }).then(res => {
  99. wx.showToast({
  100. title: res.code == '1' ? '提交成功' : res.msg,
  101. icon: res.code == '1' ? 'success' : 'none'
  102. });
  103. if (res.code == '1') this.getDetail();
  104. });
  105. }
  106. }
  107. });
  108. },
  109. /* 选择收货地址 */
  110. selectAddress() {
  111. if (this.data.detail.status != '新建') return;
  112. wx.navigateTo({
  113. url: `/pages/tabbar/mine/address/index?userid=${this.data.detail.userid}&enterpriseid=${this.data.detail.sys_enterpriseid}&select=1`
  114. });
  115. getApp().globalData.handleSelect = this.setAddress.bind(this);
  116. },
  117. /* 设置收货地址(参照 web 版:乐观更新 + 仅传 contactsid) */
  118. setAddress({ item }) {
  119. // 乐观更新:先更新 UI,再调接口
  120. this.setData({
  121. 'detail.rec_name': item.name || '',
  122. 'detail.rec_phonenumber': item.phonenumber || '',
  123. 'detail.rec_province': item.province || '',
  124. 'detail.rec_city': item.city || '',
  125. 'detail.rec_county': item.county || '',
  126. 'detail.rec_address': item.address || ''
  127. });
  128. _Http.basic({
  129. id: 2026052510105206,
  130. content: {
  131. sc_orderid: this.data.sc_orderid,
  132. rec_contactsid: item.contactsid
  133. }
  134. }).then(s => {
  135. if (s.code == '1') {
  136. wx.showToast({ title: '设置成功', icon: 'success' });
  137. wx.navigateBack();
  138. this.getDetail();
  139. } else {
  140. wx.showToast({ title: s.msg, icon: 'none' });
  141. }
  142. });
  143. },
  144. /* 自动选中默认地址 */
  145. setDefaultAddress(item) {
  146. const userid = item.userid || wx.getStorageSync('userMsg').userid;
  147. _Http.basic({
  148. id: "20221022165503",
  149. content: {
  150. nocache: true,
  151. pageNumber: 1,
  152. pageSize: 1,
  153. where: {
  154. createuserid: userid,
  155. isdefault: 1
  156. }
  157. }
  158. }).then(res => {
  159. if (res.code == '1' && res.data && res.data.length > 0) {
  160. const defaultAddr = res.data[0];
  161. // 乐观更新 UI
  162. this.setData({
  163. 'detail.rec_name': defaultAddr.name || '',
  164. 'detail.rec_phonenumber': defaultAddr.phonenumber || '',
  165. 'detail.rec_province': defaultAddr.province || '',
  166. 'detail.rec_city': defaultAddr.city || '',
  167. 'detail.rec_county': defaultAddr.county || '',
  168. 'detail.rec_address': defaultAddr.address || ''
  169. });
  170. // 调用接口设置收货地址
  171. _Http.basic({
  172. id: 2026052510105206,
  173. content: {
  174. sc_orderid: this.data.sc_orderid,
  175. rec_contactsid: defaultAddr.contactsid
  176. }
  177. }).then(s => {
  178. if (s.code == '1') {
  179. this.getDetail();
  180. }
  181. });
  182. }
  183. });
  184. },
  185. /* 修改备注 */
  186. changeRemarks(e) {
  187. let value = e.detail.value;
  188. if (value == this.data.detail.remarks) return;
  189. wx.showModal({
  190. title: '提示',
  191. content: '确认修改备注吗?',
  192. confirmText: '确定',
  193. cancelText: '取消',
  194. complete: (res) => {
  195. if (res.cancel) {
  196. this.setData({ 'detail.remarks': this.data.detail.remarks });
  197. }
  198. if (res.confirm) {
  199. let items = (this.data.detail.items || []).map(v => ({
  200. sc_orderitemsid: v.sc_orderitemsid,
  201. qty: v.qty
  202. }));
  203. _Http.basic({
  204. id: 2026060210000006,
  205. content: {
  206. sc_orderid: this.data.sc_orderid,
  207. remarks: value,
  208. items
  209. }
  210. }).then(s => {
  211. wx.showToast({
  212. title: s.code == '1' ? '修改成功' : s.msg,
  213. icon: s.code == '1' ? 'success' : 'none'
  214. });
  215. if (s.code == '1') this.getDetail();
  216. });
  217. }
  218. }
  219. });
  220. },
  221. /* 修改商品数量(防抖) */
  222. _qtyTimer: null,
  223. _qtyItemData: null,
  224. onChangeProduct(e) {
  225. let { sc_orderitemsid, qty } = e.detail;
  226. // 更新本地数据中对应商品的数量
  227. let items = this.data.detail.items || [];
  228. let idx = items.findIndex(v => v.sc_orderitemsid === sc_orderitemsid);
  229. if (idx > -1) {
  230. let item = items[idx];
  231. let totalpoints = item.points * qty;
  232. this.setData({
  233. [`detail.items[${idx}].qty`]: qty,
  234. [`detail.items[${idx}].totalpoints`]: totalpoints,
  235. 'detail.totalpoints': items.reduce((sum, v, i) => sum + (i === idx ? totalpoints : (v.totalpoints || 0)), 0)
  236. });
  237. }
  238. // 保存变更的商品信息
  239. this._qtyItemData = { sc_orderitemsid, qty };
  240. // 清除上次定时器
  241. if (this._qtyTimer) clearTimeout(this._qtyTimer);
  242. // 设置防抖,500ms后执行
  243. this._qtyTimer = setTimeout(() => {
  244. this._qtyTimer = null;
  245. let changeData = this._qtyItemData;
  246. if (!changeData) return;
  247. // 构造所有商品的items数组
  248. let allItems = (this.data.detail.items || []).map(v => ({
  249. sc_orderitemsid: v.sc_orderitemsid,
  250. qty: v.qty
  251. }));
  252. _Http.basic({
  253. id: 2026060210000006,
  254. content: {
  255. sc_orderid: this.data.sc_orderid,
  256. remarks: this.data.detail.remarks || '',
  257. items: allItems
  258. }
  259. }).then(res => {
  260. if (res.code != '1') {
  261. wx.showToast({ title: res.msg, icon: 'none' });
  262. }
  263. });
  264. }, 500);
  265. },
  266. /* 更新总价 */
  267. onUpdateTotal(e) {
  268. this.setData({ 'detail.totalpoints': e.detail.totalpoints });
  269. },
  270. /* 刷新商品列表 */
  271. onRefreshProducts() {
  272. this.getDetail();
  273. },
  274. tabsChange({ detail }) {
  275. this.setData({ tabsActive: detail });
  276. this.partialRenewal();
  277. },
  278. partialRenewal(init = false) {
  279. try {
  280. let ac = this.data.tabsList[this.data.tabsActive];
  281. let model = ac.model;
  282. if (model && model !== '#Product') {
  283. let Component = this.selectComponent(model);
  284. if (Component && Component.getList) {
  285. let id = this.data.detail[ac.idname || 'sc_orderid'];
  286. Component.getList(id, init);
  287. }
  288. }
  289. } catch (error) {}
  290. },
  291. onUnload() {
  292. getCurrentPages().forEach(page => {
  293. if (page.__route__ == 'bgj/pointsBasedOrder/index') {
  294. page.setData({ isback: true });
  295. }
  296. });
  297. }
  298. });