change.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import {
  2. ApiModel
  3. } from "../../utils/api";
  4. const _Http = new ApiModel();
  5. import {
  6. TestVerify
  7. } from "../../utils/verify";
  8. const _Verify = new TestVerify();
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. fprodname: "", //产品名称
  15. fprodnum: "", //产品编码
  16. attinfos: [], //附件列表
  17. fintroduction: "", //产品说明
  18. ftag: [], //产品标签
  19. showSaleprod: "", //标签显示
  20. tagents_productid: 0, //识别产品ID
  21. checked: false, //开关控件
  22. newAdd: false, //新增标识,新增时true,在保存后会更改为false 如果退出页面仍为true 改产品会被删除
  23. errTips: {
  24. fprodname: false,
  25. fprodnum: false,
  26. showSaleprod: false,
  27. }
  28. },
  29. /**
  30. * 生命周期函数--监听页面加载
  31. */
  32. onLoad: function (options) {
  33. if (options.data) {
  34. const data = JSON.parse(options.data)
  35. let imageList = []
  36. console.log(data)
  37. /* 格式化图片列表 */
  38. for (let i = 0; i < data.attinfos.length; i++) {
  39. let obj = {
  40. url: data.attinfos[i].fobsurl,
  41. ownerid: data.attinfos[i].ownerid,
  42. tattachmentid: data.attinfos[i].tattachmentid
  43. }
  44. imageList.push(obj)
  45. };
  46. /* 是否上架 */
  47. if (data.fisonsale == 1) {
  48. this.setData({
  49. checked: true
  50. })
  51. }
  52. let showSaleprod = "",
  53. ftag = data.ftag
  54. /* 空标签转换为数组格式 */
  55. if (data.ftag == null || data.ftag == '') {
  56. console.log(21)
  57. ftag = []
  58. } else {
  59. /* 字符串数组转换数组 */
  60. for (var i = 0; i < ftag.length; i++) {
  61. showSaleprod += ftag[i] + ' ';
  62. }
  63. }
  64. /* 渲染数据 */
  65. this.setData({
  66. fprodname: data.fprodname, //产品名称
  67. fprodnum: data.fprodnum, //产品编码
  68. attinfos: imageList, //附件列表
  69. fintroduction: data.fintroduction, //产品说明
  70. fisonsale: data.fisonsale, //是否上架
  71. ftag: ftag, //产品标签
  72. tagents_productid: data.tagents_productid, //识别产品ID
  73. showSaleprod, // 显示标签
  74. });
  75. } else {
  76. //新增产品,先新建一个产品,未保存退出页面会删除
  77. this.submit();
  78. this.setData({
  79. newAdd: true
  80. })
  81. }
  82. },
  83. /**
  84. * 生命周期函数--监听页面初次渲染完成
  85. */
  86. onReady: function () {
  87. },
  88. /* 上架开关 */
  89. switchChange() {
  90. const checked = !this.data.checked;
  91. this.setData({
  92. checked
  93. });
  94. const fisonsale = (checked) ? 1 : 0;
  95. _Http.basic({
  96. "accesstoken": wx.getStorageSync('userData').token,
  97. "classname": "customer.products.products",
  98. "method": "updatesalestatus",
  99. "content": {
  100. "productlist": [{
  101. "tagents_productid": this.data.tagents_productid,
  102. "fisonsale": fisonsale
  103. }]
  104. }
  105. }).then(res => {
  106. console.log(res)
  107. if (res.msg != "成功") return wx.showToast({
  108. title: res.data,
  109. icon: "error"
  110. })
  111. })
  112. },
  113. /* 表单验证 */
  114. formVerify() {
  115. let errTips = this.data.errTips,
  116. verify = true;
  117. /* 验证产品名称 */
  118. if (!_Verify.required(this.data.fprodname)) {
  119. errTips.fprodname = true;
  120. verify = false;
  121. }
  122. /* 验证产品编码 */
  123. if (!_Verify.required(this.data.fprodnum)) {
  124. errTips.fprodnum = true;
  125. verify = false;
  126. }
  127. /* 验证附件列表 */
  128. if (!_Verify.required(this.data.attinfos, "请上传产品图片")) {
  129. errTips.fprodnum = true;
  130. }
  131. this.setData({
  132. errTips
  133. })
  134. return verify;
  135. },
  136. /* 提交 */
  137. submit() {
  138. if (!this.formVerify()) return;
  139. let content = {};
  140. //请求参数
  141. if (this.data.tagents_productid != 0) {
  142. content = {
  143. "tagents_productid": this.data.tagents_productid,
  144. "fprodnum": this.data.fprodnum,
  145. "fprodname": this.data.fprodname,
  146. "fintroduction": this.data.fintroduction,
  147. "ftag": this.data.ftag,
  148. }
  149. } else {
  150. content = {
  151. "tagents_productid": 0
  152. }
  153. };
  154. _Http.basic({
  155. "accesstoken": wx.getStorageSync('userData').token,
  156. "classname": "customer.products.products",
  157. "method": "insertOrModifyProducts",
  158. "content": content
  159. }).then(res => {
  160. console.log(res)
  161. if (res.msg == "成功") {
  162. if (this.data.tagents_productid != 0) {
  163. //非新增项目
  164. wx.showToast({
  165. title: '保存成功',
  166. })
  167. this.setData({
  168. newAdd: false
  169. })
  170. setTimeout(() => {
  171. wx.navigateBack({
  172. delta: 1,
  173. })
  174. }, 500)
  175. } else {
  176. //新增未保存项目
  177. this.setData({
  178. tagents_productid: res.data[0].tagents_productid
  179. })
  180. }
  181. }
  182. })
  183. },
  184. /* 获取焦点 */
  185. inputFocus(e) {
  186. const {
  187. name
  188. } = e.currentTarget.dataset;
  189. let errTips = this.data.errTips;
  190. errTips[name] = false;
  191. /* 经营类目提示框 */
  192. if (name == 'showSaleprod') {
  193. errTips[name] = true;
  194. console.log(23)
  195. }
  196. this.setData({
  197. errTips
  198. })
  199. },
  200. /* 失去焦点 */
  201. inputBlur(e) {
  202. const {
  203. name
  204. } = e.currentTarget.dataset;
  205. const {
  206. value
  207. } = e.detail;
  208. /* 经营类目提示框,字符串转化数组 */
  209. if (name == 'showSaleprod') {
  210. let errTips = this.data.errTips;
  211. const ftag = this.data.showSaleprod.split(" ");
  212. errTips[name] = false;
  213. this.setData({
  214. ftag,
  215. errTips
  216. })
  217. };
  218. if (value.trim() == "") {
  219. let errTips = this.data.errTips;
  220. errTips[name] = true;
  221. this.setData({
  222. errTips
  223. })
  224. }
  225. },
  226. /**
  227. * 生命周期函数--监听页面显示
  228. */
  229. onShow: function () {
  230. },
  231. /**
  232. * 生命周期函数--监听页面隐藏
  233. */
  234. onHide: function () {
  235. },
  236. /**
  237. * 生命周期函数--监听页面卸载
  238. */
  239. onUnload: function () {
  240. if (this.data.newAdd) {
  241. _Http.basic({
  242. "accesstoken": wx.getStorageSync('userData').token,
  243. "classname": "customer.products.products",
  244. "method": "deleteProducts",
  245. "content": {
  246. "tagents_productid": this.data.tagents_productid
  247. }
  248. }).then(res => {
  249. console.log(res)
  250. })
  251. }
  252. },
  253. /**
  254. * 页面相关事件处理函数--监听用户下拉动作
  255. */
  256. onPullDownRefresh: function () {
  257. },
  258. /**
  259. * 页面上拉触底事件的处理函数
  260. */
  261. onReachBottom: function () {
  262. },
  263. /**
  264. * 用户点击右上角分享
  265. */
  266. onShareAppMessage: function () {
  267. }
  268. })