index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.attinfos.length != 0) {
  65. value.attinfos = file.fileList(value.attinfos)
  66. let image = value.attinfos.find(v => v.fileType == "image");
  67. value.cover = image ? image.cover : "";
  68. }
  69. if (newPrice) value.newPrice = CNY(value[newPrice] || 0);
  70. value.brandName = value.brand.map(name => name.brandname)
  71. value.tradefields = value.tradefield.map(name => name.tradefield)
  72. value.qty = value.orderminqty;
  73. value.width = 0;
  74. value.length = 0;
  75. if (value.iscustomsize) value.customText = getCustomText(value);
  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. this.bindChangeFun()
  93. if (item) this.selectComponent("#Custom").onClick(item)
  94. },
  95. bindChangeFun() {
  96. getApp().globalData.customizedProduct = (item, custom) => {
  97. return new Promise((resolve) => {
  98. item = Object.assign(item, custom)
  99. item.customText = getCustomText(item);
  100. let index = this.data.list.findIndex(v => v[this.data.idname] == item[this.data.idname]);
  101. if (index != -1) this.data.list[index] = item;
  102. if (item.pitchOn) this.data.result.push(item[this.data.idname])
  103. this.setData({
  104. list: this.data.list,
  105. result: this.data.result
  106. })
  107. resolve(true)
  108. })
  109. }
  110. },
  111. /* 选中 */
  112. changeResult(e) {
  113. let {
  114. id,
  115. item
  116. } = e.currentTarget.dataset, result = this.data.result;
  117. if (this.data.radio) {
  118. result = [id];
  119. } else {
  120. let isAdd = result.some(v => v == id);
  121. if (!isAdd) {
  122. if (item.iscustomsize == 1) {
  123. if ((item.widthschemeid && item.width == 0) || (item.lengthschemeid && item.length == 0)) {
  124. item.pitchOn = true;
  125. this.bindChangeFun()
  126. if (item) this.selectComponent("#Custom").onClick(item)
  127. } else {
  128. result.push(id)
  129. }
  130. } else {
  131. result.push(id)
  132. }
  133. } else {
  134. result = result.filter(v => v != id)
  135. }
  136. }
  137. this.setData({
  138. result
  139. });
  140. if (this.data.radio) this.submit();
  141. },
  142. /* 提交 */
  143. submit() {
  144. let result = this.data.result,
  145. obj = this.data.radio ? {
  146. id: result,
  147. item: this.data.list.find(value => value[this.data.idname] == result),
  148. value: [this.data.list.find(value => value[this.data.idname] == result)[this.data.showName], result]
  149. } : {
  150. result,
  151. list: result.map(v => this.data.list.find(value => value[this.data.idname] == v)),
  152. value: [result.map(v => {
  153. let data = this.data.list.find(value => value[this.data.idname] == v);
  154. return data ? data[this.data.showName] : ""
  155. }), result]
  156. }
  157. getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  158. },
  159. /* 预览图片 */
  160. viewImage(e) {
  161. const {
  162. file
  163. } = e.currentTarget.dataset;
  164. if (file.length) wx.previewMedia({
  165. sources: file.filter(value => ['image', 'vadio'].includes(value.fileType)).map(v => {
  166. return {
  167. url: v.url,
  168. type: v.fileType
  169. }
  170. }),
  171. current: 0,
  172. showmenu: true
  173. })
  174. },
  175. /* 开始搜索 */
  176. startSearch({
  177. detail
  178. }) {
  179. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  180. if (detail == condition) return;
  181. this.setData({
  182. 'content.where.condition': detail,
  183. 'params.content.where.condition': detail
  184. });
  185. this.getList(true);
  186. },
  187. /* 取消搜索 */
  188. onClear() {
  189. this.setData({
  190. 'content.where.condition': "",
  191. 'params.content.where.condition': ""
  192. });
  193. this.getList(true);
  194. },
  195. onReady() {
  196. this.selectComponent("#ListBox").setHeight(".total", this);
  197. },
  198. /* 步进器输入框失去焦点 */
  199. inputBlur(e) {
  200. const {
  201. index
  202. } = e.currentTarget.dataset;
  203. let item = this.data.list[index];
  204. let qty = 0;
  205. if (item.orderminqty > e.detail.value) {
  206. wx.showToast({
  207. title: '输入数量低于最低起订量!',
  208. icon: "none"
  209. })
  210. qty = item.orderminqty;
  211. } else if (item.orderminqty < e.detail.value) {
  212. var currencyRounding = value => currency(value, {
  213. increment: item.orderaddqty
  214. });
  215. qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
  216. } else {
  217. qty = e.detail.value;
  218. }
  219. this.setData({
  220. [`list[${index}].qty`]: 0
  221. });
  222. this.setData({
  223. [`list[${index}].qty`]: qty
  224. });
  225. },
  226. stepperChange(e) {
  227. const {
  228. index
  229. } = e.currentTarget.dataset;
  230. let item = this.data.list[index];
  231. if (e.type == 'plus') {
  232. item.qty += (item.orderaddqty) - 0
  233. } else {
  234. item.qty -= item.orderaddqty
  235. }
  236. this.setData({
  237. [`list[${index}]`]: item
  238. })
  239. },
  240. onUnload() {
  241. getApp().globalData.handleSelect = null;
  242. }
  243. })