myInput.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // myComponents/myInput/myInput.js
  2. const api = require('../../api/index')
  3. Component({
  4. options: {
  5. multipleSlots: true // 在组件定义时的选项中启用多slot支持
  6. },
  7. /**
  8. * 组件的属性列表
  9. */
  10. properties: {
  11. value:{
  12. type:String,
  13. value:'',
  14. observer:function(val){
  15. this.changeContent(val)
  16. }
  17. },
  18. // placeholder:{
  19. // type:String,
  20. // value:'请输入'
  21. // },
  22. showEdit:{
  23. type:Boolean,
  24. value:true
  25. },
  26. ownner:{
  27. type:Boolean,
  28. value:true
  29. },
  30. filePath:{
  31. type:String,
  32. value:'',
  33. observer:function(val){
  34. this.changePath(val)
  35. }
  36. }
  37. },
  38. // 监听传入的变量,当传入的值发生变化时,触发方法
  39. observers: {
  40. 'value': function (val) {
  41. // console.log(val);
  42. }
  43. },
  44. /**
  45. * 页面的初始数据
  46. */
  47. data: {
  48. searchType:[
  49. {
  50. label:'按产品查',
  51. value:'key',
  52. placeholder:'请输入产品相关关键词,多个可以用空格或者逗号隔开'
  53. },
  54. {
  55. label:'按公司查',
  56. value:'companyName',
  57. placeholder:'请输入公司名称'
  58. }
  59. ],
  60. field:'key',
  61. lastField:'',
  62. placeholder:'请输入产品相关关键词',
  63. content:'',
  64. show:false,
  65. fileList:[]
  66. },
  67. methods:{
  68. changeSearchType(e){
  69. // var field = e.detail
  70. var lastField = this.data.field
  71. var {field} = e.currentTarget.dataset
  72. var obj = this.data.searchType.find(item=>{
  73. return item.value == field
  74. })
  75. this.setData(
  76. {
  77. field:field,
  78. lastField:lastField,
  79. placeholder:obj.placeholder
  80. }
  81. )
  82. this.triggerEvent('changeSearchType',field)
  83. },
  84. changeInput(e){
  85. const value = e.detail.value;
  86. this.changeContent(value)
  87. this.triggerEvent("input", value,this.data.field);
  88. },
  89. inputBlur(e){
  90. const value = e.detail.value;
  91. if(value == this.data.content){
  92. return false
  93. }
  94. this.changeContent(value)
  95. this.triggerEvent("change", value,this.data.field);
  96. },
  97. changeContent(value){
  98. this.setData(
  99. {
  100. content:value
  101. }
  102. )
  103. },
  104. changePath(value){
  105. if(!value){
  106. return
  107. }
  108. this.setData(
  109. {
  110. fileList:[
  111. {
  112. url:value
  113. }
  114. ]
  115. }
  116. )
  117. },
  118. clickInnerIcon(e){
  119. if(this.properties.showEdit){
  120. // this.data.show = !this.data.show
  121. this.setData(
  122. {
  123. show:!this.data.show
  124. }
  125. )
  126. }
  127. console.log(this.properties.showEdit,this.data.show)
  128. this.triggerEvent('clickInnerIcon',1)
  129. },
  130. buttonClick(e){
  131. if(this.data.content.trim() == ''){
  132. wx.showToast({
  133. title: '检索内容不能为空',
  134. icon:'none'
  135. })
  136. return false
  137. }
  138. if(this.properties.ownner){
  139. var isLogin = api.isLogin()
  140. if(!isLogin){
  141. return false
  142. }
  143. var params = {
  144. // key:this.data.content
  145. }
  146. params[this.data.field] = this.data.content
  147. if(this.data.fileList && this.data.fileList.length>0){
  148. params.filePath = this.data.fileList[0].url
  149. }
  150. var product = JSON.stringify(params)
  151. console.log(product)
  152. wx.navigateTo({
  153. url: '/pages/searchResults/searchResults?type=0&product='+encodeURIComponent(product),
  154. })
  155. }
  156. // console.log(e,this.properties.value)
  157. var data = {
  158. value:this.data.content,
  159. field:this.data.field,
  160. lastField:this.data.lastField,
  161. }
  162. if(this.data.fileList && this.data.fileList.length>0){
  163. data.filePath = this.data.fileList[0].url
  164. }else{
  165. data.filePath = ''
  166. }
  167. this.triggerEvent('search',data)
  168. },
  169. //图片上传
  170. beforeUpload(e){
  171. console.log(e.detail)
  172. if(e.detail.errMsg=="chooseMedia:ok"){
  173. var data = [{
  174. status: 'done',
  175. url: e.detail.tempFiles[0].tempFilePath,
  176. }]
  177. this.setData({ fileList:data })
  178. }
  179. },
  180. onChange(e) {
  181. console.log('1', e)
  182. const { file, fileList } = e.detail
  183. if (file.status === 'uploading') {
  184. this.setData({
  185. progress: 0,
  186. })
  187. wx.showLoading()
  188. } else if (file.status === 'done') {
  189. this.setData({
  190. imageUrl: file.url,
  191. })
  192. }
  193. // Controlled state should set fileList
  194. this.setData({ fileList })
  195. },
  196. //预览
  197. onPreview(e) {
  198. console.log('onPreview', e)
  199. const { file, fileList } = e.detail
  200. wx.previewImage({
  201. current: file.url,
  202. urls: fileList.map((n) => n.url),
  203. })
  204. },
  205. },
  206. /**
  207. * 组件的生命周期函数列表
  208. */
  209. lifetimes: {
  210. // 在组件实例进入页面节点树时执行
  211. attached: function () {
  212. // 初始化操作
  213. // console.log('组件初始化');
  214. this.changeContent(this.properties.value)
  215. // ...
  216. },
  217. // 在组件实例被移除出页面节点树时执行
  218. detached: function () {
  219. // 清理工作
  220. console.log('组件销毁');
  221. // ...
  222. },
  223. // ...
  224. },
  225. /**
  226. * 组件所在页面的生命周期函数可以在 pageLifetimes 中定义
  227. *
  228. * 页面生命周期函数:
  229. * show:页面显示
  230. * hide:页面隐藏
  231. * resize:页面尺寸变化
  232. */
  233. pageLifetimes: {
  234. // ...
  235. }
  236. })