index.js 6.6 KB

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