index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import {
  2. ApiModel
  3. } from "../../utils/api";
  4. import {
  5. TestVerify
  6. } from "../../utils/verify";
  7. const _Http = new ApiModel();
  8. const _Verify = new TestVerify();
  9. Component({
  10. /**
  11. * 组件的属性列表
  12. */
  13. properties: {
  14. productData: {
  15. type: Object
  16. }
  17. },
  18. /**
  19. * 组件的初始数据
  20. */
  21. data: {
  22. newlyBuilt: true, //是否为新增产品,非新增会修改为false 新增请求修改接口后改为false 如果页面关闭为true 会删除该商品
  23. productName: "", //产品名称
  24. serialNumber: "", //产品编号
  25. fileList: [], //图片列表
  26. productLabel: "", //产品标签
  27. productExplain: "", //产品说明
  28. checked: Boolean, //是否上架
  29. /* 必填项目 */
  30. inputVerify: {
  31. productName: false, //产品名称
  32. serialNumber: false, //身份
  33. },
  34. tagents_productid: 0, //产品ID
  35. fileListLength: 0, //记录进入图片数量
  36. },
  37. lifetimes: {
  38. ready: function () {
  39. if (this.data.productData) {
  40. const {
  41. productData
  42. } = this.data
  43. let checked = Boolean;
  44. if (productData.fisonsale == 1) {
  45. checked = true;
  46. } else {
  47. checked = false;
  48. }
  49. this.manageImage(productData.attinfos)
  50. /* */
  51. this.setData({
  52. productName: productData.fprodname,
  53. serialNumber: productData.fprodnum,
  54. productLabel: productData.ftag,
  55. tagents_productid: productData.tagents_productid,
  56. checked: checked,
  57. newlyBuilt: false,
  58. })
  59. } else {
  60. /* 新增产品 */
  61. _Http.basic({
  62. "accesstoken": wx.getStorageSync('token'),
  63. "classname": "customer.products.products",
  64. "method": "insertOrModifyProducts",
  65. "content": {
  66. "tagents_productid": 0
  67. }
  68. }).then(s => {
  69. if (s.msg != "成功") return;
  70. this.setData({
  71. tagents_productid: s.data[0].tagents_productid,
  72. checked: true
  73. })
  74. })
  75. }
  76. },
  77. detached() {
  78. //新增账号未保存会删除账号
  79. if (!this.data.newlyBuilt) return;
  80. //删除账号
  81. _Http.basic({
  82. "accesstoken": wx.getStorageSync('token'),
  83. "classname": "customer.products.products",
  84. "method": "deleteProducts",
  85. "content": {
  86. "tagents_productid": this.data.tagents_productid
  87. }
  88. }).then(s => {
  89. console.log(s)
  90. })
  91. }
  92. },
  93. methods: {
  94. /* 获焦恢复 */
  95. inputFocus(e) {
  96. let inputVerify = this.data.inputVerify;
  97. const {
  98. name
  99. } = e.currentTarget.dataset;
  100. inputVerify[name] = false;
  101. this.setData({
  102. inputVerify
  103. })
  104. },
  105. /* 失焦验证 */
  106. inputBlur(e) {
  107. let inputVerify = this.data.inputVerify;
  108. const {
  109. name
  110. } = e.currentTarget.dataset;
  111. /* 手机号验证 */
  112. if (name == 'cellPhoneNum' && !_Verify.phoneNumber(this.data.cellPhoneNum, true)) {
  113. inputVerify[name] = true;
  114. this.setData({
  115. inputVerify
  116. })
  117. }
  118. /* 验证 */
  119. if (e.detail.value != "") return;
  120. inputVerify[name] = true;
  121. this.setData({
  122. inputVerify
  123. })
  124. },
  125. /* 验证 */
  126. submitVerify() {
  127. let inputVerify = this.data.inputVerify,
  128. nextStep = true;
  129. if (!_Verify.required(this.data.productName)) {
  130. inputVerify.productName = true;
  131. nextStep = false;
  132. }
  133. if(this.data.fileList.length<1){
  134. nextStep = false;
  135. }
  136. if (!_Verify.required(this.data.serialNumber)) {
  137. inputVerify.serialNumber = true
  138. nextStep = false;
  139. }
  140. if (!nextStep) {
  141. this.setData({
  142. inputVerify
  143. })
  144. }
  145. return nextStep;
  146. },
  147. /* 提交 */
  148. submit() {
  149. /* 验证必填项 */
  150. if (!this.submitVerify()) return wx.showToast({
  151. title: '请检查输入框内容!',
  152. icon: "none"
  153. })
  154. /* 发送请求 */
  155. _Http.basic({
  156. "accesstoken": wx.getStorageSync('token'),
  157. "classname": "customer.products.products",
  158. "method": "insertOrModifyProducts",
  159. "content": {
  160. "tagents_productid": this.data.tagents_productid,
  161. "fprodnum": this.data.serialNumber,
  162. "fprodname": this.data.productName,
  163. // "fprice": 100,
  164. // "fprodclassname": "墙布",
  165. "fintroduction": this.data.productExplain,
  166. "ftag": this.data.productLabel,
  167. // "fnotes": "备注"
  168. }
  169. }).then(s => {
  170. if (s.msg != "成功") return;
  171. let putaway = 1;
  172. if (this.data.checked) {
  173. putaway = 1
  174. } else {
  175. putaway = 0
  176. }
  177. /* 设置是否上架 */
  178. _Http.basic({
  179. "accesstoken": wx.getStorageSync('token'),
  180. "classname": "customer.products.products",
  181. "method": "updatesalestatus",
  182. "content": {
  183. "productlist": [{
  184. "tagents_productid": this.data.tagents_productid,
  185. "fisonsale": putaway
  186. }]
  187. }
  188. }).then(s => {
  189. console.log(s)
  190. })
  191. this.setData({
  192. newlyBuilt: false
  193. })
  194. wx.showToast({
  195. title: s.msg,
  196. })
  197. setTimeout(() => {
  198. wx.navigateBack({
  199. delta: 1
  200. })
  201. }, 500)
  202. })
  203. },
  204. /* 是否上架 */
  205. switchOnChange() {
  206. this.setData({
  207. checked: !this.data.checked
  208. })
  209. },
  210. /* 上传图片 */
  211. afterRead(event) {
  212. // 初始化数据
  213. this.setData({
  214. logoErrTips: ""
  215. })
  216. var that = this
  217. const {
  218. file
  219. } = event.detail;
  220. var index = file.url.lastIndexOf(".");
  221. var ext = file.url.substr(index + 1);
  222. const fileName = file.url.split('/'),
  223. name = fileName[fileName.length - 1];
  224. //验证文件格式大小
  225. const Uploader = _Verify.verifyUploader({
  226. file
  227. });
  228. if (Uploader == "发送请求") {
  229. wx.getFileSystemManager().readFile({
  230. filePath: file.url,
  231. // encoding:'utf-8',
  232. success: result => {
  233. //返回临时文件路径
  234. const fileData = result.data
  235. _Http.basic({
  236. "accesstoken": wx.getStorageSync('token'),
  237. "classname": "system.system.docManage",
  238. "method": "getFileName",
  239. "content": {
  240. "filename": name,
  241. "filetype": ext,
  242. "ownertable": "tagents_product",
  243. "ownerid": that.data.tagents_productid,
  244. "ftype": "default"
  245. }
  246. }).then(res => {
  247. that.uploadFile(res, fileData)
  248. }).catch(err => {
  249. console.log(err)
  250. })
  251. },
  252. fail: console.error
  253. })
  254. } else {
  255. this.setData({
  256. logoErrTips: Uploader
  257. })
  258. }
  259. },
  260. /* 上传成功反馈 */
  261. uploadFile(res, data) {
  262. let that = this;
  263. wx.request({
  264. url: res.data.obsuploadurl,
  265. method: "PUT",
  266. data: data,
  267. header: {
  268. 'content-type': 'application/octet-stream' // 默认值
  269. },
  270. success() {
  271. _Http.basic({
  272. "accesstoken": wx.getStorageSync('token'),
  273. "classname": "system.system.docManage",
  274. "method": "uploadSuccess",
  275. "content": {
  276. "obsfilename": res.data.obsfilename
  277. }
  278. }).then(res => {
  279. if (res.msg != "成功") return;
  280. let data = {
  281. url: res.data[0].fobsurl,
  282. ownerid: res.data[0].ownerid,
  283. tattachmentid: res.data[0].tattachmentid
  284. };
  285. let fileList = that.data.fileList
  286. fileList.push(data)
  287. that.setData({
  288. fileList
  289. })
  290. }).catch(err => {
  291. console.log(err)
  292. })
  293. }
  294. })
  295. },
  296. /* 加工处理图片列表 */
  297. manageImage(data) {
  298. let fileList = [];
  299. for (let i = 0; i < data.length && i < 3; i++) {
  300. let a1 = {
  301. url: data[i].fobsurl,
  302. ownerid: data[i].ownerid,
  303. tattachmentid: data[i].tattachmentid
  304. }
  305. fileList.push(a1)
  306. }
  307. this.setData({
  308. fileList,
  309. fileListLength: data.length
  310. })
  311. },
  312. /* 删除图片 */
  313. imageDelete(e) {
  314. const index = e.detail.index
  315. _Http.basic({
  316. "accesstoken": wx.getStorageSync('token'),
  317. "classname": "system.system.docManage",
  318. "method": "deleteDoc",
  319. "content": {
  320. "ownertable": "tagents_product",
  321. "ownerid": this.data.fileList[index].ownerid,
  322. "tattachmentid": this.data.fileList[index].tattachmentid
  323. }
  324. }).then(s => {
  325. if (s.msg != "成功") return;
  326. this.manageImage(s.data)
  327. })
  328. },
  329. }
  330. })