myInput.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. },
  31. // 监听传入的变量,当传入的值发生变化时,触发方法
  32. observers: {
  33. 'value': function (val) {
  34. // console.log(val);
  35. }
  36. },
  37. /**
  38. * 页面的初始数据
  39. */
  40. data: {
  41. searchType:[
  42. {
  43. label:'按产品查',
  44. value:'key',
  45. placeholder:'请输入产品相关关键词'
  46. },
  47. {
  48. label:'按公司查',
  49. value:'companyName',
  50. placeholder:'请输入公司名称'
  51. }
  52. ],
  53. field:'key',
  54. lastField:'',
  55. placeholder:'请输入产品相关关键词',
  56. content:'',
  57. show:false,
  58. fileList:[]
  59. },
  60. methods:{
  61. changeSearchType(e){
  62. // var field = e.detail
  63. var lastField = this.data.field
  64. var {field} = e.currentTarget.dataset
  65. var obj = this.data.searchType.find(item=>{
  66. return item.value == field
  67. })
  68. this.setData(
  69. {
  70. field:field,
  71. lastField:lastField,
  72. placeholder:obj.placeholder
  73. }
  74. )
  75. this.triggerEvent('changeSearchType',field)
  76. },
  77. changeInput(e){
  78. const value = e.detail.value;
  79. this.changeContent(value)
  80. this.triggerEvent("input", value,this.data.field);
  81. },
  82. inputBlur(e){
  83. const value = e.detail.value;
  84. if(value == this.data.content){
  85. return false
  86. }
  87. this.changeContent(value)
  88. this.triggerEvent("change", value,this.data.field);
  89. },
  90. changeContent(value){
  91. this.setData(
  92. {
  93. content:value
  94. }
  95. )
  96. },
  97. clickInnerIcon(e){
  98. if(this.properties.showEdit){
  99. // this.data.show = !this.data.show
  100. this.setData(
  101. {
  102. show:!this.data.show
  103. }
  104. )
  105. }
  106. console.log(this.properties.showEdit,this.data.show)
  107. this.triggerEvent('clickInnerIcon',1)
  108. },
  109. buttonClick(e){
  110. if(this.properties.ownner){
  111. var isLogin = api.isLogin()
  112. if(!isLogin){
  113. return false
  114. }
  115. var params = {
  116. // key:this.data.content
  117. }
  118. params[this.data.field] = this.data.content
  119. if(this.data.fileList && this.data.fileList.length>0){
  120. params.filePath = this.data.fileList[0].url
  121. }
  122. var product = JSON.stringify(params)
  123. console.log(product)
  124. wx.navigateTo({
  125. url: '/pages/searchResults/searchResults?type=0&product='+encodeURIComponent(product),
  126. })
  127. }
  128. // console.log(e,this.properties.value)
  129. var data = {
  130. value:this.data.content,
  131. field:this.data.field,
  132. lastField:this.data.lastField,
  133. }
  134. if(this.data.fileList && this.data.fileList.length>0){
  135. data.filePath = this.data.fileList[0].url
  136. }
  137. this.triggerEvent('search',data)
  138. },
  139. //图片上传
  140. beforeUpload(e){
  141. console.log(e.detail)
  142. if(e.detail.errMsg=="chooseMedia:ok"){
  143. var data = [{
  144. status: 'done',
  145. url: e.detail.tempFiles[0].tempFilePath,
  146. }]
  147. this.setData({ fileList:data })
  148. }
  149. },
  150. onChange(e) {
  151. console.log('1', e)
  152. const { file, fileList } = e.detail
  153. if (file.status === 'uploading') {
  154. this.setData({
  155. progress: 0,
  156. })
  157. wx.showLoading()
  158. } else if (file.status === 'done') {
  159. this.setData({
  160. imageUrl: file.url,
  161. })
  162. }
  163. // Controlled state should set fileList
  164. this.setData({ fileList })
  165. },
  166. //预览
  167. onPreview(e) {
  168. console.log('onPreview', e)
  169. const { file, fileList } = e.detail
  170. wx.previewImage({
  171. current: file.url,
  172. urls: fileList.map((n) => n.url),
  173. })
  174. },
  175. },
  176. /**
  177. * 组件的生命周期函数列表
  178. */
  179. lifetimes: {
  180. // 在组件实例进入页面节点树时执行
  181. attached: function () {
  182. // 初始化操作
  183. // console.log('组件初始化');
  184. this.changeContent(this.properties.value)
  185. // ...
  186. },
  187. // 在组件实例被移除出页面节点树时执行
  188. detached: function () {
  189. // 清理工作
  190. console.log('组件销毁');
  191. // ...
  192. },
  193. // ...
  194. },
  195. /**
  196. * 组件所在页面的生命周期函数可以在 pageLifetimes 中定义
  197. *
  198. * 页面生命周期函数:
  199. * show:页面显示
  200. * hide:页面隐藏
  201. * resize:页面尺寸变化
  202. */
  203. pageLifetimes: {
  204. // ...
  205. }
  206. })