index.js 6.4 KB

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