index.js 7.4 KB

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