addProduct.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. labelWidth:350,
  16. fileList:[],
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad(options) {
  22. const eventChannel = this.getOpenerEventChannel()
  23. var that = this
  24. eventChannel.on('acceptDataFromOpenerPage', function(data) {
  25. var form = {
  26. searchCondition:data.key,
  27. concernType:data.concernType
  28. }
  29. var file = []
  30. if(data.filePath){
  31. file.push(
  32. {
  33. status: 'done',
  34. url: filePath,
  35. }
  36. )
  37. }
  38. that.setData(
  39. {
  40. form:form,
  41. fileList:file
  42. }
  43. )
  44. if(data.filePath){
  45. that.uploadFile(data.filePath)
  46. }
  47. })
  48. },
  49. /**
  50. * 生命周期函数--监听页面初次渲染完成
  51. */
  52. onReady() {
  53. },
  54. /**
  55. * 生命周期函数--监听页面显示
  56. */
  57. onShow() {
  58. },
  59. /**
  60. * 页面方法集合
  61. */
  62. //表单栏位值发生变化时
  63. onValuesChange(e) {
  64. console.log(e)
  65. const { changedValues, allValues } = e.detail
  66. // this.setValues(changedValues)
  67. Object.keys(changedValues).forEach((field) => {
  68. this.setData({
  69. ['form.'+field]: changedValues[field],
  70. })
  71. })
  72. console.log('onValuesChange \n', changedValues, allValues)
  73. },
  74. //图片上传
  75. beforeUpload(e){
  76. console.log(e)
  77. if(e.detail.errMsg=="chooseMedia:ok"){
  78. // var data = [{
  79. // status: 'done',
  80. // url: e.detail.tempFiles[0].tempFilePath,
  81. // }]
  82. var data = this.data.fileList
  83. var url = e.detail.tempFiles[0].tempFilePath
  84. data.push(
  85. {
  86. status: 'done',
  87. url: url,
  88. }
  89. )
  90. this.setData({ fileList:data })
  91. this.uploadFile(url)
  92. }
  93. },
  94. onRemove(e) {
  95. console.log(e)
  96. const { file, fileList } = e.detail
  97. wx.showModal({
  98. content: '确定删除?',
  99. success: (res) => {
  100. if (res.confirm) {
  101. var data = this.data.fileList
  102. this.setData({
  103. fileList: data.filter((n) => n.url !== file.url),
  104. })
  105. }
  106. },
  107. })
  108. },
  109. uploadFile(tempFilePaths){
  110. var that = this
  111. upload.upload(tempFilePaths).then(res=>{
  112. if(res.code == 200){
  113. var guid = res.data[0]
  114. var index = that.data.fileList.findIndex(item=>{
  115. return item.url == tempFilePaths
  116. })
  117. that.setData(
  118. {
  119. ["fileList["+index+"].guid"]:guid
  120. }
  121. )
  122. // var arr = that.data.form.fileGuids || []
  123. // arr.push(res.data[0])
  124. // that.setData(
  125. // {
  126. // ["form.fileGuids"]:arr
  127. // }
  128. // )
  129. }
  130. })
  131. },
  132. //上传文件
  133. // uploadFile(){
  134. // const that = this;
  135. // wx.chooseMessageFile({
  136. // count: 1, // 默认9,表示一次最多可以选择的文件个数
  137. // type: 'file', // 可以指定是文件
  138. // success(res) {
  139. // console.log(res)
  140. // var obj = res.tempFiles[0]
  141. // var data = that.data.fileList
  142. // data.push(obj)
  143. // // that.setData(
  144. // // {
  145. // // fileList:data
  146. // // }
  147. // // )
  148. // // 返回选定文件的本地文件路径列表,tempFilePath可以作为文件上传的标识
  149. // const tempFilePaths = obj.path;
  150. // upload.upload(tempFilePaths).then(res=>{
  151. // if(res.code == 200){
  152. // var arr = that.data.form.fileGuids
  153. // if(!arr){
  154. // arr = []
  155. // }
  156. // arr.push(res.data[0])
  157. // that.setData(
  158. // {
  159. // ["form.fileGuids"]:arr,
  160. // fileList:data
  161. // }
  162. // )
  163. // }
  164. // })
  165. // }
  166. // });
  167. // },
  168. //输入备注
  169. changRemark(e){
  170. this.setData(
  171. {
  172. ["form.description"]:e.detail.value
  173. }
  174. )
  175. },
  176. //修改专利无法导出?
  177. onChange(e){
  178. this.setData(
  179. {
  180. ["form.patentExport"]:e.detail
  181. }
  182. )
  183. },
  184. //提交工单
  185. submit(){
  186. var key = api.isLogin()
  187. if(!key){
  188. return false
  189. }
  190. var that = this
  191. const { validateFields } = $wuxForm()
  192. validateFields((err, values) => {
  193. console.log(err)
  194. if (!err) {
  195. var fileGuids = that.data.fileList.map(item=>{
  196. return item.guid
  197. })
  198. var params = {
  199. ...that.data.form,
  200. fileGuids:fileGuids
  201. }
  202. wx.showLoading({
  203. title: '加载中',
  204. })
  205. api.addOrUpdateProduct(params).then(res=>{
  206. if(res.code == 200){
  207. var pages = getCurrentPages()
  208. var prepage = pages[pages.length-2]
  209. var product = {
  210. concernType : params.concernType,
  211. id:res.data[0],
  212. key:params.searchCondition,
  213. filePath:that.data.fileList[0]?.url
  214. }
  215. prepage.setData({
  216. product : product,
  217. showDialog:false,
  218. followType:null,
  219. })
  220. wx.navigateBack()
  221. }
  222. })
  223. }else{
  224. var error = Object.keys(err)
  225. var key = error[0]
  226. var len = err[key].errors.length
  227. var message = err[key].errors[len-1].message
  228. wx.showModal({
  229. title: '提示',
  230. content: message,
  231. complete: (res) => {
  232. if (res.cancel) {
  233. }
  234. if (res.confirm) {
  235. }
  236. }
  237. })
  238. }
  239. })
  240. },
  241. /**
  242. * 生命周期函数--监听页面隐藏
  243. */
  244. onHide() {
  245. },
  246. /**
  247. * 生命周期函数--监听页面卸载
  248. */
  249. onUnload() {
  250. },
  251. /**
  252. * 页面相关事件处理函数--监听用户下拉动作
  253. */
  254. onPullDownRefresh() {
  255. },
  256. /**
  257. * 页面上拉触底事件的处理函数
  258. */
  259. onReachBottom() {
  260. },
  261. /**
  262. * 用户点击右上角分享
  263. */
  264. onShareAppMessage() {
  265. }
  266. })