index.js 6.7 KB

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