index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. const _Http = getApp().globalData.http,
  2. file = require("../../utils/FormatTheAttachment");
  3. import currency from "../../utils/currency";
  4. Page({
  5. data: {
  6. hidePrice: wx.getStorageSync('hidePrice'),
  7. loading: true,
  8. params: {}, //请求体
  9. result: [], //返回结果
  10. radio: false, //是否为单选
  11. idname: "itemid", //idkey
  12. showName: "itemname"
  13. },
  14. onLoad(options) {
  15. if (options.params) {
  16. let params = JSON.parse(options.params);
  17. if (!params.content.pageNumber || !params.content.pageTotal) {
  18. params.content.pageNumber = 1;
  19. params.content.pageTotal = 1;
  20. }
  21. this.setData({
  22. params
  23. });
  24. };
  25. this.setData({
  26. radio: options.radio ? true : false,
  27. idname: options.idname || this.data.idname,
  28. showName: options.showName || this.data.showName,
  29. newPrice: options.newprice || "price", //红色价格
  30. });
  31. this.getList();
  32. getApp().globalData.customizedProduct = this.customizedProduct.bind(this);
  33. },
  34. customizedProduct(item) {
  35. return new Promise((resolve) => {
  36. let index = this.data.list.findIndex(v => v[this.data.idname] == item[this.data.idname]);
  37. if (index != -1) this.data.list[index] = item;
  38. if (item.pitchOn) this.data.result.push(item[this.data.idname])
  39. this.setData({
  40. list: this.data.list,
  41. result: this.data.result
  42. })
  43. resolve(true)
  44. })
  45. },
  46. getList(init = false) {
  47. //init 用于初始化分页
  48. if (init.detail != undefined) init = init.detail;
  49. let params = this.data.params;
  50. if (init) params.content.pageNumber = 1
  51. if (params.content.pageNumber > params.content.pageTotal) return;
  52. _Http.basic(params).then(res => {
  53. console.log("选择产品列表", res)
  54. this.selectComponent('#ListBox').RefreshToComplete();
  55. if (res.msg != '成功') return wx.showToast({
  56. title: res.msg,
  57. icon: "none"
  58. })
  59. const CNY = num => currency(num, {
  60. symbol: "¥",
  61. precision: 2
  62. }).format();
  63. let newPrice = this.data.newPrice;
  64. res.data = res.data.map(value => {
  65. if (value.attinfos.length != 0) {
  66. value.attinfos = file.fileList(value.attinfos)
  67. let image = value.attinfos.find(v => v.fileType == "image");
  68. value.cover = image ? image.cover : "";
  69. }
  70. if (newPrice) value.newPrice = CNY(value[newPrice] || 0);
  71. value.brandName = value.brand.map(name => name.brandname)
  72. value.tradefields = value.tradefield.map(name => name.tradefield)
  73. value.qty = value.orderminqty;
  74. value.width = 0;
  75. value.length = 0;
  76. return value;
  77. })
  78. this.setData({
  79. 'params.content.pageNumber': res.pageNumber + 1,
  80. 'params.content.pageTotal': res.pageTotal,
  81. 'params.content.total': res.total,
  82. list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
  83. loading: false
  84. })
  85. })
  86. },
  87. customization(e) {
  88. let {
  89. item
  90. } = e.target.dataset;
  91. item.pitchOn = false;
  92. if (item) this.selectComponent("#Custom").onClick(item)
  93. },
  94. /* 选中 */
  95. changeResult(e) {
  96. let {
  97. id,
  98. item
  99. } = e.currentTarget.dataset, result = this.data.result;
  100. if (this.data.radio) {
  101. result = [id];
  102. } else {
  103. let isAdd = result.some(v => v == id);
  104. if (!isAdd) {
  105. if (item.iscustomsize == 1) {
  106. if ((item.widthschemeid && item.width == 0) || (item.lengthschemeid && item.length == 0)) {
  107. item.pitchOn = true;
  108. if (item) this.selectComponent("#Custom").onClick(item)
  109. } else {
  110. result.push(id)
  111. }
  112. } else {
  113. result.push(id)
  114. }
  115. } else {
  116. result = result.filter(v => v != id)
  117. }
  118. }
  119. this.setData({
  120. result
  121. });
  122. if (this.data.radio) this.submit();
  123. },
  124. /* 提交 */
  125. submit() {
  126. let result = this.data.result,
  127. obj = this.data.radio ? {
  128. id: result,
  129. item: this.data.list.find(value => value[this.data.idname] == result),
  130. value: [this.data.list.find(value => value[this.data.idname] == result)[this.data.showName], result]
  131. } : {
  132. result,
  133. list: result.map(v => this.data.list.find(value => value[this.data.idname] == v)),
  134. value: [result.map(v => {
  135. let data = this.data.list.find(value => value[this.data.idname] == v);
  136. return data ? data[this.data.showName] : ""
  137. }), result]
  138. }
  139. getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  140. },
  141. /* 预览图片 */
  142. viewImage(e) {
  143. const {
  144. file
  145. } = e.currentTarget.dataset;
  146. if (file.length) wx.previewMedia({
  147. sources: file.filter(value => ['image', 'vadio'].includes(value.fileType)).map(v => {
  148. return {
  149. url: v.url,
  150. type: v.fileType
  151. }
  152. }),
  153. current: 0,
  154. showmenu: true
  155. })
  156. },
  157. /* 开始搜索 */
  158. startSearch({
  159. detail
  160. }) {
  161. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  162. if (detail == condition) return;
  163. this.setData({
  164. 'content.where.condition': detail,
  165. 'params.content.where.condition': detail
  166. });
  167. this.getList(true);
  168. },
  169. /* 取消搜索 */
  170. onClear() {
  171. this.setData({
  172. 'content.where.condition': "",
  173. 'params.content.where.condition': ""
  174. });
  175. this.getList(true);
  176. },
  177. onReady() {
  178. this.selectComponent("#ListBox").setHeight(".total", this);
  179. },
  180. /* 步进器输入框失去焦点 */
  181. inputBlur(e) {
  182. const {
  183. index
  184. } = e.currentTarget.dataset;
  185. let item = this.data.list[index];
  186. let qty = 0;
  187. if (item.orderminqty > e.detail.value) {
  188. wx.showToast({
  189. title: '输入数量低于最低起订量!',
  190. icon: "none"
  191. })
  192. qty = item.orderminqty;
  193. } else if (item.orderminqty < e.detail.value) {
  194. var currencyRounding = value => currency(value, {
  195. increment: item.orderaddqty
  196. });
  197. qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
  198. } else {
  199. qty = e.detail.value;
  200. }
  201. this.setData({
  202. [`list[${index}].qty`]: 0
  203. });
  204. this.setData({
  205. [`list[${index}].qty`]: qty
  206. });
  207. },
  208. stepperChange(e) {
  209. const {
  210. index
  211. } = e.currentTarget.dataset;
  212. let item = this.data.list[index];
  213. if (e.type == 'plus') {
  214. item.qty += (item.orderaddqty) - 0
  215. } else {
  216. item.qty -= item.orderaddqty
  217. }
  218. this.setData({
  219. [`list[${index}]`]: item
  220. })
  221. },
  222. onUnload() {
  223. getApp().globalData.handleSelect = null;
  224. }
  225. })