index.js 6.3 KB

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