detail.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. UniqueLine: false
  30. },
  31. onLoad(options) {
  32. let data = JSON.parse(options.params);
  33. sa_brandid = data.sa_brandid;
  34. if (wx.getStorageSync('userrole') == '业务员') sys_enterpriseid = data.sys_enterpriseid
  35. this.data.content.sa_itemgroupid = data.sa_itemgroupid;
  36. this.setData({
  37. userrole: wx.getStorageSync('userrole'),
  38. isShowFavorites: wx.getStorageSync('auth').wfavorites ? true : false
  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(init = false, getCustom = true) {
  51. let content = this.data.content;
  52. if (sys_enterpriseid) content.sys_enterpriseid = sys_enterpriseid;
  53. if (this.data.UniqueLine)['spec', 'cheek', 'material', 'color'].forEach(key => content[key] = '')
  54. _Http.basic({
  55. "id": "20221223165503",
  56. content
  57. }).then(res => {
  58. wx.hideLoading();
  59. console.log("商品详情", res)
  60. if (res.msg != '成功') return wx.showToast({
  61. title: res.msg,
  62. icon: "none"
  63. })
  64. if (res.data.item.length == 0) return wx.showToast({
  65. title: '未查询到商品',
  66. icon: "none",
  67. mask: true
  68. })
  69. const item = res.data.item[0];
  70. this.handleFiles(item.attinfos)
  71. const CNY = sum => currency(sum, {
  72. symbol: "¥",
  73. precision: 2
  74. }).format();
  75. item.gradeprice = CNY(item.gradeprice);
  76. item.marketprice = CNY(item.marketprice);
  77. item.qty = item.orderaddqty;
  78. let row = {
  79. specRows: res.data.specRows.reverse(),
  80. cheekRows: res.data.cheekRows,
  81. materialRows: res.data.materialRows,
  82. colorRows: res.data.colorRows,
  83. };
  84. let obj = {},
  85. auto = true,
  86. UniqueLine = true;
  87. res.data.customschemeItems.map(v => v.value).forEach(key => obj[key] = row[key + 'Rows'].filter(v => v.flag))
  88. for (const key in obj) {
  89. if (obj[key].length > 1) {
  90. auto = false
  91. UniqueLine = false;
  92. }
  93. }
  94. if (auto) {
  95. for (const key in obj) {
  96. if (obj[key].length) content[key] = obj[key][0].parm;
  97. }
  98. }
  99. this.setData({
  100. content,
  101. detail: item,
  102. row,
  103. customschemeItems: res.data.customschemeItems,
  104. loading: false,
  105. itemCount: ['specRows', 'cheekRows', 'materialRows', 'colorRows'].filter(v => res.data[v].length).length,
  106. selectCount: ['cheek', 'color', 'material', 'spec'].filter(v => content[v].length).length,
  107. UniqueLine
  108. });
  109. //是否定制
  110. if (item.iscustomsize == 1 && content.spec == "custom") {
  111. if (getCustom) 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. // UniqueLine
  179. content[valuename] = (content[valuename] == value.parm) ? "" : value.parm;
  180. this.setData({
  181. selectCount: ['cheek', 'color', 'material', 'spec'].filter(v => content[v].length).length
  182. })
  183. this.getDetail(valuename == "spec")
  184. },
  185. /* 打开文档 */
  186. openDocument(e) {
  187. const {
  188. item
  189. } = e.currentTarget.dataset;
  190. console.log(item)
  191. wx.showLoading({
  192. title: '加载中...',
  193. mask: true,
  194. })
  195. wx.downloadFile({
  196. url: item.url,
  197. success: function (res) {
  198. const filePath = res.tempFilePath
  199. wx.openDocument({
  200. filePath: filePath,
  201. showMenu: true,
  202. fileType: item.postfix,
  203. success: function (res) {
  204. wx.hideLoading();
  205. console.log('打开文档成功')
  206. },
  207. fail(e) {
  208. console.log(e)
  209. wx.showToast({
  210. title: '打开失败',
  211. icon: "error",
  212. mask: true
  213. })
  214. }
  215. })
  216. },
  217. fail(e) {
  218. console.log(e)
  219. wx.showToast({
  220. title: '打开失败',
  221. icon: "error",
  222. mask: true
  223. })
  224. }
  225. })
  226. },
  227. clickBut(e) {
  228. if (this.data.itemCount != this.data.selectCount) return wx.showToast({
  229. title: '请完成产品选项在进行操作',
  230. icon: "none"
  231. })
  232. this.data.detail.tradefield.length >= 2 ? wx.showToast({
  233. title: '请选择加入产品领域',
  234. icon: "none",
  235. duration: 3000
  236. }) : this[e.target.id](0);
  237. },
  238. /* 选择领域 */
  239. storage(e) {
  240. this.addToShoppingCart(e.detail.value)
  241. },
  242. /* 加入购物车 */
  243. addToShoppingCart(index) {
  244. let detail = this.data.detail,
  245. custom = {
  246. length: 0,
  247. width: 0
  248. };
  249. //是否为定制项
  250. if (detail.iscustomsize == 1) custom = this.selectComponent("#customMade").getResult(true);
  251. if (typeof custom == "boolean") return;
  252. let content = Object.assign(this.data.content, {
  253. sa_brandid,
  254. itemid: detail.itemid, //货品id
  255. qty: detail.qty, //数量
  256. itemno: detail.itemno, //货品编号
  257. tradefield: detail.tradefield[index].tradefield,
  258. }, custom);
  259. _Http.basic({
  260. "id": 20220924095102,
  261. content,
  262. }).then(res => {
  263. console.log("加入购物车", res)
  264. wx.showToast({
  265. title: res.msg == '成功' ? '加入成功' : res.msg,
  266. icon: "none"
  267. });
  268. if (res.msg == '成功') getApp().globalData.getCollectCount().then(badge => this.setData({
  269. badge
  270. }))
  271. })
  272. },
  273. /* 加入收藏夹 */
  274. addToFavorites() {
  275. let detail = this.data.detail,
  276. iscollection = detail.iscollection == 1 ? false : true,
  277. custom = {
  278. width: 0,
  279. length: 0,
  280. },
  281. favorites = {};
  282. //是否为定制项
  283. if (detail.iscustomsize == 1) custom = this.selectComponent("#customMade").getResult(true);
  284. for (const key in custom) {
  285. favorites['favorites' + key] = custom[key]
  286. }
  287. if (typeof custom == "boolean") return;
  288. _Http.basic({
  289. "id": 20231121143403,
  290. "content": {
  291. "itemid": this.data.detail.itemid, //货品id
  292. "qty": detail.qty, //数量
  293. ...custom,
  294. iscollection,
  295. ...favorites
  296. },
  297. }).then(res => {
  298. console.log(iscollection + "收藏夹", res)
  299. wx.showToast({
  300. title: res.msg == '成功' ? iscollection ? '收藏成功' : "已取消收藏" : res.msg,
  301. icon: "none"
  302. });
  303. if (res.msg == '成功') {
  304. getApp().globalData.getFavoriteCount().then(num => {
  305. this.setData({
  306. favoriteCount: num
  307. })
  308. })
  309. this.setData({
  310. "detail.iscollection": iscollection ? 1 : 0
  311. })
  312. }
  313. })
  314. },
  315. /* 立即下单 */
  316. placeAnOrder(index) {
  317. let detail = this.data.detail,
  318. content = {
  319. "type": "标准订单",
  320. tradefield: detail.tradefield[index].tradefield
  321. },
  322. custom = {
  323. length: 0,
  324. width: 0
  325. }
  326. //是否为定制项
  327. if (detail.iscustomsize == 1) custom = this.selectComponent("#customMade").getResult(true);
  328. console.log("custom", custom)
  329. if (typeof custom == "boolean") return;
  330. content.items = [Object.assign({
  331. sa_orderitemsid: 0,
  332. sa_brandid,
  333. itemid: detail.itemid, //货品id
  334. qty: detail.qty, //数量
  335. itemno: detail.itemno, //货品编号
  336. }, custom)];
  337. _Http.basic({
  338. "id": 20221128183202,
  339. content,
  340. }).then(res => {
  341. console.log("创建订单", res)
  342. wx.showModal({
  343. title: '提示',
  344. content: res.msg == '成功' ? '订单创建成功!是否立即查看' : res.msg,
  345. showCancel: res.msg == '成功',
  346. complete: ({
  347. confirm
  348. }) => {
  349. if (res.msg == '成功' && confirm) wx.navigateTo({
  350. url: '/packageA/orderForm/detail?id=' + res.data.sa_orderid,
  351. })
  352. }
  353. })
  354. })
  355. },
  356. /* 前往购物车 */
  357. toCollect(e) {
  358. getApp().globalData.changeBar({
  359. detail: "Collect"
  360. })
  361. wx.navigateBack();
  362. },
  363. onShow() {
  364. this.setData({
  365. favoriteCount: getApp().globalData.favoriteCount,
  366. badge: getApp().globalData.collectCount,
  367. })
  368. this.getDetail(true, this.data.loading);
  369. },
  370. /* 步进器输入框失去焦点 */
  371. inputBlur(e) {
  372. let item = this.data.detail;
  373. let qty = 0;
  374. if (item.orderminqty > e.detail.value) {
  375. wx.showToast({
  376. title: '输入数量低于最低起订量!',
  377. icon: "none"
  378. })
  379. qty = item.orderminqty;
  380. } else if (item.orderminqty < e.detail.value) {
  381. var currencyRounding = value => currency(value, {
  382. increment: item.orderaddqty
  383. });
  384. qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
  385. } else {
  386. qty = e.detail.value;
  387. }
  388. this.setData({
  389. "detail.qty": 0
  390. });
  391. this.setData({
  392. "detail.qty": qty
  393. });
  394. },
  395. stepperChange(e) {
  396. let item = this.data.detail;
  397. if (e.type == 'plus') {
  398. item.qty += (item.orderaddqty) - 0 || 1
  399. } else {
  400. item.qty -= item.orderaddqty || 1
  401. }
  402. this.setData({
  403. detail: item
  404. })
  405. },
  406. })