detail.js 9.8 KB

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