detail.js 9.5 KB

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