myInput.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. },
  15. placeholder:{
  16. type:String,
  17. value:'请输入'
  18. },
  19. showEdit:{
  20. type:Boolean,
  21. value:true
  22. },
  23. ownner:{
  24. type:Boolean,
  25. value:true
  26. }
  27. },
  28. // 监听传入的变量,当传入的值发生变化时,触发方法
  29. observers: {
  30. 'value': function (val) {
  31. // console.log(val);
  32. }
  33. },
  34. /**
  35. * 页面的初始数据
  36. */
  37. data: {
  38. content:'',
  39. show:false,
  40. fileList:[]
  41. },
  42. methods:{
  43. changeInput(e){
  44. const value = e.detail.value;
  45. this.triggerEvent("input", value);
  46. },
  47. inputBlur(e){
  48. const value = e.detail.value;
  49. if(value == this.data.content){
  50. return false
  51. }
  52. this.changeContent(value)
  53. this.triggerEvent("change", value);
  54. },
  55. changeContent(value){
  56. this.setData(
  57. {
  58. content:value
  59. }
  60. )
  61. },
  62. clickInnerIcon(e){
  63. if(this.properties.showEdit){
  64. // this.data.show = !this.data.show
  65. this.setData(
  66. {
  67. show:!this.data.show
  68. }
  69. )
  70. }
  71. console.log(this.properties.showEdit,this.data.show)
  72. this.triggerEvent('clickInnerIcon',1)
  73. },
  74. buttonClick(e){
  75. if(this.properties.ownner){
  76. var isLogin = api.isLogin()
  77. if(!isLogin){
  78. return false
  79. }
  80. var params = {
  81. key:this.data.content
  82. }
  83. if(this.data.fileList && this.data.fileList.length>0){
  84. params.filePath = this.data.fileList[0].url
  85. }
  86. var product = JSON.stringify(params)
  87. wx.navigateTo({
  88. url: '/pages/searchResults/searchResults?type=0&product='+encodeURIComponent(product),
  89. })
  90. }
  91. // console.log(e,this.properties.value)
  92. this.triggerEvent('search',1)
  93. },
  94. //图片上传
  95. beforeUpload(e){
  96. console.log(e.detail)
  97. if(e.detail.errMsg=="chooseMedia:ok"){
  98. var data = [{
  99. status: 'done',
  100. url: e.detail.tempFiles[0].tempFilePath,
  101. }]
  102. this.setData({ fileList:data })
  103. }
  104. },
  105. onChange(e) {
  106. console.log('1', e)
  107. const { file, fileList } = e.detail
  108. if (file.status === 'uploading') {
  109. this.setData({
  110. progress: 0,
  111. })
  112. wx.showLoading()
  113. } else if (file.status === 'done') {
  114. this.setData({
  115. imageUrl: file.url,
  116. })
  117. }
  118. // Controlled state should set fileList
  119. this.setData({ fileList })
  120. },
  121. //预览
  122. onPreview(e) {
  123. console.log('onPreview', e)
  124. const { file, fileList } = e.detail
  125. wx.previewImage({
  126. current: file.url,
  127. urls: fileList.map((n) => n.url),
  128. })
  129. },
  130. },
  131. /**
  132. * 组件的生命周期函数列表
  133. */
  134. lifetimes: {
  135. // 在组件实例进入页面节点树时执行
  136. attached: function () {
  137. // 初始化操作
  138. // console.log('组件初始化');
  139. this.changeContent(this.properties.value)
  140. // ...
  141. },
  142. // 在组件实例被移除出页面节点树时执行
  143. detached: function () {
  144. // 清理工作
  145. console.log('组件销毁');
  146. // ...
  147. },
  148. // ...
  149. },
  150. /**
  151. * 组件所在页面的生命周期函数可以在 pageLifetimes 中定义
  152. *
  153. * 页面生命周期函数:
  154. * show:页面显示
  155. * hide:页面隐藏
  156. * resize:页面尺寸变化
  157. */
  158. pageLifetimes: {
  159. // ...
  160. }
  161. })