addProduct.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // pages/form/addProduct/addProduct.js
  2. const upload = require("../../../utils/upload")
  3. const api = require('../../../api/index')
  4. import { $wuxForm} from '../../../miniprogram_npm/wux-weapp/index'
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. validateMessages: {
  11. required: '%s 字段为必填',
  12. },
  13. form:{
  14. },
  15. funType:null,
  16. labelWidth:350,
  17. fileList:[],
  18. concernType:[
  19. {
  20. type:1,
  21. label:'白名单',
  22. icon:'myIconanquanbangzhu'
  23. },
  24. {
  25. type:2,
  26. label:'灰名单',
  27. icon:'myIcongroup43'
  28. },
  29. {
  30. type:0,
  31. label:'黑名单',
  32. icon:'myIconicon-test'
  33. },
  34. ]
  35. },
  36. /**
  37. * 生命周期函数--监听页面加载
  38. */
  39. onLoad(options) {
  40. const eventChannel = this.getOpenerEventChannel()
  41. var that = this
  42. eventChannel.on('acceptDataFromOpenerPage', function(data) {
  43. var form = {
  44. searchCondition:data.key,
  45. concernType:data.concernType,
  46. monitorPeriod:'7天'
  47. }
  48. var file = []
  49. if(data.filePath){
  50. file.push(
  51. {
  52. status: 'done',
  53. url: filePath,
  54. }
  55. )
  56. }
  57. that.setData(
  58. {
  59. form:form,
  60. fileList:file,
  61. funType:data.method
  62. }
  63. )
  64. if(data.filePath){
  65. that.uploadFile(data.filePath)
  66. }
  67. })
  68. },
  69. /**
  70. * 生命周期函数--监听页面初次渲染完成
  71. */
  72. onReady() {
  73. },
  74. /**
  75. * 生命周期函数--监听页面显示
  76. */
  77. onShow() {
  78. },
  79. /**
  80. * 页面方法集合
  81. */
  82. //表单栏位值发生变化时
  83. onValuesChange(e) {
  84. console.log(e)
  85. const { changedValues, allValues } = e.detail
  86. // this.setValues(changedValues)
  87. Object.keys(changedValues).forEach((field) => {
  88. this.setData({
  89. ['form.'+field]: changedValues[field],
  90. })
  91. })
  92. console.log('onValuesChange \n', changedValues, allValues)
  93. },
  94. //修改关注类型
  95. changeConcernType(e){
  96. this.setData(
  97. {
  98. ["form.concernType"]:e.detail
  99. }
  100. )
  101. },
  102. //图片上传
  103. beforeUpload(e){
  104. console.log(e)
  105. if(e.detail.errMsg=="chooseMedia:ok"){
  106. // var data = [{
  107. // status: 'done',
  108. // url: e.detail.tempFiles[0].tempFilePath,
  109. // }]
  110. var data = this.data.fileList
  111. var url = e.detail.tempFiles[0].tempFilePath
  112. data.push(
  113. {
  114. status: 'done',
  115. url: url,
  116. }
  117. )
  118. this.setData({ fileList:data })
  119. this.uploadFile(url)
  120. }
  121. },
  122. onRemove(e) {
  123. console.log(e)
  124. const { file, fileList } = e.detail
  125. wx.showModal({
  126. content: '确定删除?',
  127. success: (res) => {
  128. if (res.confirm) {
  129. var data = this.data.fileList
  130. this.setData({
  131. fileList: data.filter((n) => n.url !== file.url),
  132. })
  133. }
  134. },
  135. })
  136. },
  137. uploadFile(tempFilePaths){
  138. var that = this
  139. upload.upload(tempFilePaths).then(res=>{
  140. if(res.code == 200){
  141. var guid = res.data[0]
  142. var index = that.data.fileList.findIndex(item=>{
  143. return item.url == tempFilePaths
  144. })
  145. that.setData(
  146. {
  147. ["fileList["+index+"].guid"]:guid
  148. }
  149. )
  150. // var arr = that.data.form.fileGuids || []
  151. // arr.push(res.data[0])
  152. // that.setData(
  153. // {
  154. // ["form.fileGuids"]:arr
  155. // }
  156. // )
  157. }
  158. })
  159. },
  160. //输入备注
  161. changRemark(e){
  162. this.setData(
  163. {
  164. ["form.description"]:e.detail.value
  165. }
  166. )
  167. },
  168. //提交工单
  169. submit(){
  170. var key = api.isLogin()
  171. if(!key){
  172. return false
  173. }
  174. // var that = this
  175. const { validateFields } = $wuxForm()
  176. validateFields((err, values) => {
  177. console.log(err)
  178. if (!err) {
  179. var fileGuids = this.data.fileList.map(item=>{
  180. return item.guid
  181. })
  182. var params = {
  183. ...this.data.form,
  184. fileGuids:fileGuids
  185. }
  186. if(this.data.funType == '监控'){
  187. this.addMonitoring(params)
  188. return
  189. }
  190. wx.showLoading({
  191. title: '加载中',
  192. })
  193. var that = this
  194. api.throttle(
  195. api.addOrUpdateProduct(params).then(res=>{
  196. if(res.code == 200){
  197. wx.showToast({
  198. title: '关注成功',
  199. icon: 'success',
  200. duration: 2000
  201. })
  202. that.updateMessage(params,res.data[0])
  203. }
  204. })
  205. )
  206. }else{
  207. var error = Object.keys(err)
  208. var key = error[0]
  209. var len = err[key].errors.length
  210. var message = err[key].errors[len-1].message
  211. wx.showModal({
  212. title: '提示',
  213. content: message,
  214. complete: (res) => {
  215. if (res.cancel) {
  216. }
  217. if (res.confirm) {
  218. }
  219. }
  220. })
  221. }
  222. })
  223. },
  224. //修改上一页信息
  225. updateMessage(params,id){
  226. var pages = getCurrentPages()
  227. var prepage = pages[pages.length-2]
  228. var product = {
  229. concernType : params.concernType,
  230. id:id,
  231. key:params.searchCondition,
  232. filePath:this.data.fileList[0]?.url
  233. }
  234. prepage.setData({
  235. product : product,
  236. showDialog:false,
  237. followType:null,
  238. })
  239. prepage.collectPatent()
  240. wx.navigateBack()
  241. },
  242. //监控产品接口
  243. addMonitoring(product){
  244. wx.showLoading({
  245. title: '加载中',
  246. })
  247. api.addMonitoring(product).then(res=>{
  248. wx.showToast({
  249. title: '监控成功',
  250. icon: 'success',
  251. duration: 2000
  252. })
  253. this.updateMessage(params,res.data[0])
  254. })
  255. },
  256. /**
  257. * 生命周期函数--监听页面隐藏
  258. */
  259. onHide() {
  260. },
  261. /**
  262. * 生命周期函数--监听页面卸载
  263. */
  264. onUnload() {
  265. },
  266. /**
  267. * 页面相关事件处理函数--监听用户下拉动作
  268. */
  269. onPullDownRefresh() {
  270. },
  271. /**
  272. * 页面上拉触底事件的处理函数
  273. */
  274. onReachBottom() {
  275. },
  276. /**
  277. * 用户点击右上角分享
  278. */
  279. onShareAppMessage() {
  280. }
  281. })