index.js 6.2 KB

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