addProduct.js 6.8 KB

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