addProduct.js 6.6 KB

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