index.js 6.9 KB

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