index.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // nova-shopAdmin/pages/goods/goods.js
  2. const company = getApp().globalData.company
  3. const Parse = getApp().Parse;
  4. const app = getApp();
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. merchant: null,
  11. services: [],
  12. service: "",
  13. goods: {},
  14. id: "",
  15. category: {}, //商品分类
  16. value: '',
  17. checked: true, //上架状态
  18. check: false, //推荐状态
  19. twoModal: false,
  20. name: "", //商品名称
  21. price: "", //价格
  22. originalPrice: "", //原价
  23. desc: null, //排序
  24. image: null,
  25. sales: "",
  26. total: '',
  27. imageList: [],
  28. showPicker: false,
  29. item: null,
  30. },
  31. blur(e) {
  32. let name = e.currentTarget.dataset.name
  33. this.setData({
  34. [name]: e.detail.value
  35. })
  36. },
  37. onChange({ detail }) {
  38. // 需要手动对 checked 状态进行更新
  39. this.setData({ checked: detail });
  40. },
  41. oChange({ detail }) {
  42. // 需要手动对 checked 状态进行更新
  43. this.setData({ check: detail });
  44. },
  45. specifications() {
  46. wx.navigateTo({
  47. url: '/nova-shop/pages/nova-shopadmin/pages/goods/specifications/index'
  48. });
  49. },
  50. showCategory() {
  51. this.setData({
  52. showPicker: true
  53. })
  54. },
  55. changeFile(e) {
  56. if (e.detail && e.detail.length > 0) {
  57. this.setData({
  58. image: e.detail[0].url
  59. })
  60. } else {
  61. this.setData({
  62. image: ''
  63. })
  64. }
  65. },
  66. onCancel() {
  67. console.log('取消')
  68. this.setData({
  69. showPicker: false
  70. })
  71. },
  72. getCategory() {
  73. let Category = new Parse.Query("Category")
  74. Category.equalTo("company", company)
  75. Category.equalTo("type", "shop")
  76. Category.ascending('index')
  77. Category.select('name')
  78. Category.find().then(res => {
  79. let columns = []
  80. res.forEach(item => {
  81. let category = item.toJSON()
  82. columns.push(category)
  83. })
  84. this.setData({
  85. columns
  86. })
  87. })
  88. },
  89. onConfirm(event) {
  90. const { value, index } = event.detail;
  91. console.log(event.detail)
  92. this.setData({
  93. time: value.objectId,
  94. category: {
  95. name: value.name,
  96. value: {
  97. __type: "Pointer",
  98. className: "Category",
  99. objectId: value.objectId
  100. }
  101. },
  102. showPicker: false
  103. })
  104. },
  105. // 新建商品
  106. async submit() {
  107. const _this = this
  108. let id = this.data.store.objectId
  109. console.log(this.data.name, this.data.price, this.data.originalPrice, id, this.data.image, this.data.desc)
  110. if (!this.data.name || !this.data.price || !this.data.originalPrice || !this.data.image || !this.data.desc) {
  111. wx.showToast({
  112. title: '请填写完整信息',
  113. icon: 'none'
  114. })
  115. return
  116. }
  117. if(!this.data.category) {
  118. wx.showToast({
  119. title: '请选择商品分类',
  120. icon: 'none'
  121. })
  122. return
  123. }
  124. let shopGoods
  125. if (this.data.id) {
  126. let query = new Parse.Query("ShopGoods")
  127. shopGoods = await query.get(this.data.id)
  128. } else {
  129. let ShopGoods = Parse.Object.extend("ShopGoods")
  130. shopGoods = new ShopGoods()
  131. shopGoods.set("company", {
  132. __type: "Pointer",
  133. className: "Company",
  134. objectId: company
  135. })
  136. }
  137. shopGoods.set("name", this.data.name)
  138. shopGoods.set("price", Number(this.data.price))
  139. shopGoods.set("originalPrice", Number(this.data.originalPrice))
  140. shopGoods.set("image", this.data.image)
  141. shopGoods.set("status", this.data.checked)
  142. shopGoods.set("isRecom", this.data.check)
  143. shopGoods.set("type", "shop")
  144. shopGoods.set("services", this.data.services)
  145. shopGoods.set("total", Number(this.data.total))
  146. shopGoods.set("sales", Number(this.data.sales))
  147. shopGoods.set("shopStore", {
  148. __type: "Pointer",
  149. className: "ShopStore",
  150. objectId: id
  151. })
  152. shopGoods.set("category", this.data.category.value)
  153. shopGoods.save().then(res => {
  154. console.log(res)
  155. if (this.data.id) {
  156. wx.showToast({
  157. title: '修改成功',
  158. icon: 'none'
  159. })
  160. } else {
  161. wx.showToast({
  162. title: '创建成功',
  163. icon: 'none'
  164. })
  165. }
  166. _this.setData({
  167. name: '',
  168. images: [],
  169. image: '',
  170. price: '',
  171. originalPrice: '',
  172. total: '',
  173. category: '',
  174. Introduction: "",
  175. })
  176. wx.navigateBack({
  177. delta: 1
  178. })
  179. })
  180. },
  181. //从本地获取数据
  182. getGoodsList() {
  183. let ShopGoods = new Parse.Query("ShopGoods")
  184. console.log(this.data.id);
  185. ShopGoods.equalTo("objectId", this.data.id)
  186. ShopGoods.include("shopStore")
  187. ShopGoods.first().then(res => {
  188. let goods = res.toJSON()
  189. // order.orderTime = dateF.formatTime("YYYY-mm-dd ", order.createdAt)
  190. console.log(goods);
  191. this.setData({
  192. goods: goods,
  193. services: goods.services,
  194. image: goods.image,
  195. checked: goods.status,
  196. check: goods.isRecom,
  197. desc: goods.desc,
  198. sales: goods.sales,
  199. total: goods.total,
  200. name: goods.name,
  201. price: goods.price,
  202. originalPrice: goods.originalPrice,
  203. })
  204. })
  205. },
  206. twoonConfirm: function () {
  207. let item = this.data.services
  208. let service = this.data.service
  209. console.log(service);
  210. item.push({
  211. 'check': false,
  212. 'value': service,
  213. })
  214. console.log(item)
  215. this.setData({
  216. service: null,
  217. })
  218. console.log(service);
  219. this.setData({
  220. show: false,
  221. services: item
  222. });
  223. },
  224. /**
  225. * 生命周期函数--监听页面加载
  226. */
  227. onLoad: async function (options) {
  228. let merchant = wx.getStorageSync('merchant'); //用户
  229. let store = wx.getStorageSync('store'); //用户
  230. this.setData({ merchant: merchant,store:store })
  231. this.getCategory()
  232. let id = (options.id)
  233. console.log(id)
  234. this.setData({ id: id });
  235. if (id) {
  236. let imageList = []
  237. let ShopGoods = new Parse.Query("ShopGoods")
  238. console.log(id);
  239. ShopGoods.include("shopStore")
  240. ShopGoods.include("category")
  241. let Goods = await ShopGoods.get(id)
  242. let goods = Goods.toJSON()
  243. imageList.push({
  244. url: goods.image
  245. })
  246. this.setData({
  247. goods: goods,
  248. services: goods.services,
  249. image: goods.image,
  250. checked: goods.status,
  251. check: goods.isRecom,
  252. desc: goods.desc,
  253. sales: goods.sales,
  254. total: goods.total,
  255. name: goods.name,
  256. price: goods.price,
  257. originalPrice: goods.originalPrice,
  258. imageList: imageList,
  259. time: goods.category.objectId,
  260. category: goods.category
  261. })
  262. }
  263. },
  264. /**
  265. * 生命周期函数--监听页面初次渲染完成
  266. */
  267. onReady: function () {
  268. },
  269. /**
  270. * 生命周期函数--监听页面显示
  271. */
  272. onShow: function () {
  273. },
  274. /**
  275. * 生命周期函数--监听页面隐藏
  276. */
  277. onHide: function () {
  278. },
  279. /**
  280. * 生命周期函数--监听页面卸载
  281. */
  282. onUnload: function () {
  283. },
  284. /**
  285. * 页面相关事件处理函数--监听用户下拉动作
  286. */
  287. onPullDownRefresh: function () {
  288. },
  289. /**
  290. * 页面上拉触底事件的处理函数
  291. */
  292. onReachBottom: function () {
  293. },
  294. /**
  295. * 用户点击右上角分享
  296. */
  297. onShareAppMessage: function () {
  298. }
  299. })