detail.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. const _Http = getApp().globalData.http;
  2. let sa_brandid = null,
  3. sys_enterpriseid = null;
  4. import {
  5. fileList
  6. } from "../../utils/FormatTheAttachment";
  7. import currency from "../../utils/currency";
  8. Page({
  9. data: {
  10. itemCount: 0,
  11. selectCount: 0,
  12. hidePrice: wx.getStorageSync('hidePrice'),
  13. badge: getApp().globalData.collectCount,
  14. favoriteCount: getApp().globalData.favoriteCount,
  15. loading: true,
  16. content: {
  17. sa_itemgroupid: null,
  18. spec: "", //规格
  19. color: "", //颜色
  20. material: "", //材质
  21. cheek: "" //边框
  22. },
  23. itmes: [],
  24. detail: {
  25. spec: ""
  26. },
  27. privacyFieldC: [],
  28. isShowFavorites: false
  29. },
  30. onLoad(options) {
  31. let data = JSON.parse(options.params);
  32. sa_brandid = data.sa_brandid;
  33. if (wx.getStorageSync('userrole') == '业务员') sys_enterpriseid = data.sys_enterpriseid
  34. this.data.content.sa_itemgroupid = data.sa_itemgroupid;
  35. this.setData({
  36. userrole: wx.getStorageSync('userrole'),
  37. isShowFavorites: wx.getStorageSync('auth').wfavorites ? true : false,
  38. isdisplaysaleqty: wx.getStorageSync('siteP').isdisplaysaleqty
  39. })
  40. try {
  41. let privacyFieldC = wx.getStorageSync('auth').wmarket.forms.wmarket.formcols.map(v => v.title);
  42. this.setData({
  43. privacyFieldC
  44. })
  45. console.log("privacyFieldC", privacyFieldC)
  46. } catch (error) {
  47. }
  48. },
  49. /* 获取详情 */
  50. getDetail(reset = false) {
  51. let content = this.data.content;
  52. if (sys_enterpriseid) content.sys_enterpriseid = sys_enterpriseid;
  53. _Http.basic({
  54. "id": "20221223165503",
  55. content
  56. }).then(res => {
  57. wx.hideLoading();
  58. console.log("商品详情", res)
  59. if (res.msg != '成功') return wx.showToast({
  60. title: res.msg,
  61. icon: "none"
  62. })
  63. if (res.data.item.length == 0) return wx.showToast({
  64. title: '未查询到商品',
  65. icon: "none",
  66. mask: true
  67. })
  68. const item = res.data.item[0];
  69. this.handleFiles(item.attinfos)
  70. const CNY = sum => currency(sum, {
  71. symbol: "¥",
  72. precision: 2
  73. }).format();
  74. item.gradeprice = CNY(item.gradeprice);
  75. item.marketprice = CNY(item.marketprice);
  76. item.qty = item.orderaddqty;
  77. let row = {
  78. specRows: res.data.specRows.reverse(),
  79. cheekRows: res.data.cheekRows,
  80. materialRows: res.data.materialRows,
  81. colorRows: res.data.colorRows,
  82. };
  83. let obj = {},
  84. isRecursion = false;
  85. res.data.customschemeItems.map(v => v.value).forEach(key => obj[key] = row[key + 'Rows'].filter(v => v.flag))
  86. for (const key in obj) {
  87. if (obj[key].length == 1) {
  88. content[key] = obj[key][0].parm;
  89. isRecursion = true;
  90. }
  91. }
  92. let itemCount = res.data.customschemeItems.length,
  93. selectCount = ['cheek', 'color', 'material', 'spec'].filter(v => content[v].length).length;
  94. this.setData({
  95. content,
  96. detail: item,
  97. row,
  98. customschemeItems: res.data.customschemeItems,
  99. loading: false,
  100. itemCount,
  101. selectCount
  102. });
  103. if (!reset && isRecursion) {
  104. wx.showLoading({
  105. title: '加载中...',
  106. mask: true
  107. })
  108. this.getDetail(true)
  109. }
  110. //是否定制
  111. if (itemCount == selectCount && item.iscustomsize == 1) {
  112. this.selectComponent("#customMade").init(item);
  113. } else {
  114. this.selectComponent("#customMade").setData({
  115. list: []
  116. });
  117. }
  118. })
  119. },
  120. /* 预览媒体 */
  121. viewMedias(e) {
  122. const {
  123. index,
  124. type
  125. } = e.currentTarget.dataset;
  126. wx.previewMedia({
  127. current: index,
  128. sources: type == 'image' ? this.data.files.viewImages : this.data.files.viewVideos,
  129. })
  130. },
  131. /* 处理附件 */
  132. handleFiles(arr) {
  133. let files = {
  134. images: [],
  135. viewImages: [],
  136. videos: [],
  137. viewVideos: [],
  138. files: []
  139. },
  140. list = fileList(arr);
  141. list.forEach(v => {
  142. switch (v.fileType) {
  143. case "video":
  144. files.videos.push(v)
  145. files.viewVideos.push({
  146. url: v.url,
  147. type: "video",
  148. poster: v.subfiles[0].url
  149. })
  150. break;
  151. case "image":
  152. files.images.push(v)
  153. files.viewImages.push({
  154. url: v.url,
  155. type: "image"
  156. })
  157. break;
  158. default:
  159. files.files.push(v)
  160. break;
  161. }
  162. });
  163. this.setData({
  164. files
  165. })
  166. },
  167. /* 切换产品 */
  168. changeItemno(e) {
  169. const {
  170. value,
  171. valuename
  172. } = e.currentTarget.dataset,
  173. content = this.data.content;
  174. if (!value.flag) return;
  175. wx.showLoading({
  176. title: '加载中...',
  177. mask: true
  178. })
  179. if (content[valuename] == value.parm && this.data.row[valuename + 'Rows'].filter(v => v.flag).length == 1) {
  180. ['spec', 'cheek', 'material', 'color'].forEach(key => content[key] = '')
  181. } else {
  182. content[valuename] = (content[valuename] == value.parm) ? "" : value.parm;
  183. }
  184. this.setData({
  185. selectCount: ['cheek', 'color', 'material', 'spec'].filter(v => content[v].length).length,
  186. content
  187. })
  188. this.getDetail()
  189. },
  190. /* 打开文档 */
  191. openDocument(e) {
  192. const {
  193. item
  194. } = e.currentTarget.dataset;
  195. console.log(item)
  196. wx.showLoading({
  197. title: '加载中...',
  198. mask: true,
  199. })
  200. wx.downloadFile({
  201. url: item.url,
  202. success: function (res) {
  203. const filePath = res.tempFilePath
  204. wx.openDocument({
  205. filePath: filePath,
  206. showMenu: true,
  207. fileType: item.postfix,
  208. success: function (res) {
  209. wx.hideLoading();
  210. console.log('打开文档成功')
  211. },
  212. fail(e) {
  213. console.log(e)
  214. wx.showToast({
  215. title: '打开失败',
  216. icon: "error",
  217. mask: true
  218. })
  219. }
  220. })
  221. },
  222. fail(e) {
  223. console.log(e)
  224. wx.showToast({
  225. title: '打开失败',
  226. icon: "error",
  227. mask: true
  228. })
  229. }
  230. })
  231. },
  232. clickBut(e) {
  233. if (this.data.itemCount != this.data.selectCount) return wx.showToast({
  234. title: '请完成产品选项在进行操作',
  235. icon: "none"
  236. })
  237. this.data.detail.tradefield.length >= 2 ? wx.showToast({
  238. title: '请选择加入产品领域',
  239. icon: "none",
  240. duration: 3000
  241. }) : this[e.target.id](0);
  242. },
  243. /* 选择领域 */
  244. storage(e) {
  245. this.addToShoppingCart(e.detail.value)
  246. },
  247. /* 加入购物车 */
  248. addToShoppingCart(index) {
  249. let detail = this.data.detail,
  250. custom = {
  251. length: 0,
  252. width: 0
  253. };
  254. //是否为定制项
  255. if (detail.iscustomsize == 1) custom = this.selectComponent("#customMade").getResult(true);
  256. if (typeof custom == "boolean") return;
  257. let content = Object.assign(this.data.content, {
  258. sa_brandid,
  259. itemid: detail.itemid, //货品id
  260. qty: detail.qty, //数量
  261. itemno: detail.itemno, //货品编号
  262. tradefield: detail.tradefield[index].tradefield,
  263. }, custom);
  264. _Http.basic({
  265. "id": 20220924095102,
  266. content,
  267. }).then(res => {
  268. console.log("加入购物车", res)
  269. wx.showToast({
  270. title: res.msg == '成功' ? '加入成功' : res.msg,
  271. icon: "none"
  272. });
  273. if (res.msg == '成功') getApp().globalData.getCollectCount().then(badge => this.setData({
  274. badge
  275. }))
  276. })
  277. },
  278. /* 加入收藏夹 */
  279. addToFavorites() {
  280. let detail = this.data.detail,
  281. iscollection = detail.iscollection == 1 ? false : true,
  282. custom = {
  283. width: 0,
  284. length: 0,
  285. },
  286. favorites = {};
  287. //是否为定制项
  288. if (detail.iscustomsize == 1) custom = this.selectComponent("#customMade").getResult(true);
  289. for (const key in custom) {
  290. favorites['favorites' + key] = custom[key]
  291. }
  292. if (typeof custom == "boolean") return;
  293. _Http.basic({
  294. "id": 20231121143403,
  295. "content": {
  296. "itemid": this.data.detail.itemid, //货品id
  297. "qty": detail.qty, //数量
  298. ...custom,
  299. iscollection,
  300. ...favorites
  301. },
  302. }).then(res => {
  303. console.log(iscollection + "收藏夹", res)
  304. wx.showToast({
  305. title: res.msg == '成功' ? iscollection ? '收藏成功' : "已取消收藏" : res.msg,
  306. icon: "none"
  307. });
  308. if (res.msg == '成功') {
  309. getApp().globalData.getFavoriteCount().then(num => {
  310. this.setData({
  311. favoriteCount: num
  312. })
  313. })
  314. this.setData({
  315. "detail.iscollection": iscollection ? 1 : 0
  316. })
  317. }
  318. })
  319. },
  320. /* 立即下单 */
  321. placeAnOrder(index) {
  322. let detail = this.data.detail,
  323. content = {
  324. "type": "标准订单",
  325. tradefield: detail.tradefield[index].tradefield
  326. },
  327. custom = {
  328. length: 0,
  329. width: 0
  330. }
  331. //是否为定制项
  332. if (detail.iscustomsize == 1) custom = this.selectComponent("#customMade").getResult(true);
  333. console.log("custom", custom)
  334. if (typeof custom == "boolean") return;
  335. content.items = [Object.assign({
  336. sa_orderitemsid: 0,
  337. sa_brandid,
  338. itemid: detail.itemid, //货品id
  339. qty: detail.qty, //数量
  340. itemno: detail.itemno, //货品编号
  341. }, custom)];
  342. _Http.basic({
  343. "id": 20221128183202,
  344. content,
  345. }).then(res => {
  346. console.log("创建订单", res)
  347. wx.showModal({
  348. title: '提示',
  349. content: res.msg == '成功' ? '订单创建成功!是否立即查看' : res.msg,
  350. showCancel: res.msg == '成功',
  351. complete: ({
  352. confirm
  353. }) => {
  354. if (res.msg == '成功' && confirm) wx.navigateTo({
  355. url: '/packageA/orderForm/detail?id=' + res.data.sa_orderid,
  356. })
  357. }
  358. })
  359. })
  360. },
  361. /* 前往购物车 */
  362. toCollect(e) {
  363. getApp().globalData.changeBar({
  364. detail: "Collect"
  365. })
  366. wx.navigateBack();
  367. },
  368. onShow() {
  369. this.setData({
  370. favoriteCount: getApp().globalData.favoriteCount,
  371. badge: getApp().globalData.collectCount,
  372. })
  373. this.getDetail(true);
  374. },
  375. /* 步进器输入框失去焦点 */
  376. inputBlur(e) {
  377. let item = this.data.detail;
  378. let qty = 0;
  379. if (item.orderminqty > e.detail.value) {
  380. wx.showToast({
  381. title: '输入数量低于最低起订量!',
  382. icon: "none"
  383. })
  384. qty = item.orderminqty;
  385. } else if (item.orderminqty < e.detail.value) {
  386. var currencyRounding = value => currency(value, {
  387. increment: item.orderaddqty
  388. });
  389. qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
  390. } else {
  391. qty = e.detail.value;
  392. }
  393. this.setData({
  394. "detail.qty": 0
  395. });
  396. this.setData({
  397. "detail.qty": qty
  398. });
  399. },
  400. stepperChange(e) {
  401. let item = this.data.detail;
  402. if (e.type == 'plus') {
  403. item.qty += (item.orderaddqty) - 0 || 1
  404. } else {
  405. item.qty -= item.orderaddqty || 1
  406. }
  407. this.setData({
  408. detail: item
  409. })
  410. },
  411. })