detail.js 9.7 KB

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