index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. top: "", //排序
  24. images: [], //商品轮播图
  25. html: "", //富文本
  26. // image: "https://file-cloud.fmode.cn/lb486SzXLP/20220326/7fibag051234.png", //商品首图
  27. Introduction: "",
  28. image: [],
  29. sales: "",
  30. total: '',
  31. imageList: [],
  32. licenseList: [],
  33. },
  34. blur(e) {
  35. let name = e.currentTarget.dataset.name
  36. this.setData({
  37. [name]: e.detail.value
  38. })
  39. },
  40. // shopsubmit() {
  41. // this.setData({
  42. // twoModal: true
  43. // })
  44. // },
  45. onChangeos(event) {
  46. console.log(event)
  47. const { picker, value, index } = event.detail;
  48. picker.setColumnValues(1, citys[value[0]]);
  49. },
  50. onCancel() {
  51. this.setData({
  52. twoModal: false
  53. })
  54. },
  55. onChange({ detail }) {
  56. // 需要手动对 checked 状态进行更新
  57. this.setData({ checked: detail });
  58. },
  59. oChange({ detail }) {
  60. // 需要手动对 checked 状态进行更新
  61. this.setData({ check: detail });
  62. },
  63. specifications() {
  64. wx.navigateTo({
  65. url: '/nova-shop/pages/nova-shopadmin/pages/goods/specifications/index'
  66. });
  67. },
  68. goodsimage() {
  69. // let details = this.data.goods.details
  70. // let img = this.data.goods.images.join(',');
  71. // console.log(details, 123, img);
  72. let id = this.data.id
  73. wx.navigateTo({
  74. url: `/nova-shop/pages/nova-shopadmin/pages/goods/goods-image/index?id=${id}`
  75. });
  76. },
  77. showCategory() {
  78. this.setData({
  79. showPicker: true
  80. })
  81. },
  82. onConfirm(event) {
  83. const { value, index } = event.detail;
  84. console.log(value, index, event.detail)
  85. this.setData({
  86. pickerIndex: index,
  87. category: {
  88. name: value.name,
  89. value: {
  90. __type: "Pointer",
  91. className: "Category",
  92. objectId: value.objectId
  93. }
  94. },
  95. showPicker: false
  96. })
  97. },
  98. changeFile(e) {
  99. if (e.detail && e.detail.length > 0) {
  100. this.setData({
  101. image: [e.detail[0].url]
  102. })
  103. } else {
  104. this.setData({
  105. image: []
  106. })
  107. }
  108. },
  109. onCancel() {
  110. console.log('取消')
  111. this.setData({
  112. showPicker: false
  113. })
  114. },
  115. getCategory() {
  116. let Category = new Parse.Query("Category")
  117. Category.equalTo("company", company)
  118. Category.equalTo("type", "shop")
  119. Category.ascending('index')
  120. Category.select('name')
  121. Category.find().then(res => {
  122. let columns = []
  123. res.forEach(item => {
  124. let category = item.toJSON()
  125. columns.push(category)
  126. })
  127. this.setData({
  128. columns
  129. })
  130. })
  131. },
  132. // 新建商品
  133. async submit() {
  134. console.log(this.data.html, this.data.images);
  135. const _this = this
  136. let ShopStore = new Parse.Query('ShopStore')
  137. ShopStore.equalTo('user', this.data.merchant.objectId)
  138. let res = await ShopStore.first()
  139. let id = res.toJSON().objectId
  140. console.log(this.data.name, this.data.price, this.data.originalPrice, id, this.data.top, this.data.sales, this.data.html, this.data.image[0])
  141. if (!this.data.name || !this.data.price || !this.data.originalPrice || !this.data.top || !this.data.image) {
  142. wx.showToast({
  143. title: '请填写完整信息',
  144. icon: 'none'
  145. })
  146. return
  147. }
  148. let shopGoods
  149. if (this.data.id) {
  150. let query = new Parse.Query("ShopGoods")
  151. shopGoods = await query.get(this.data.id)
  152. } else {
  153. let ShopGoods = Parse.Object.extend("ShopGoods")
  154. shopGoods = new ShopGoods()
  155. shopGoods.set("company", {
  156. __type: "Pointer",
  157. className: "Company",
  158. objectId: company
  159. })
  160. }
  161. shopGoods.set("user", {
  162. __type: "Pointer",
  163. className: "_User",
  164. objectId: Parse.User.current().id
  165. })
  166. shopGoods.set("name", this.data.name)
  167. if (this.data.html) {
  168. shopGoods.set("details", this.data.html)
  169. }
  170. if (!this.data.html) {
  171. shopGoods.set("details", this.data.goods.details)
  172. }
  173. shopGoods.set("price", Number(this.data.price))
  174. shopGoods.set("originalPrice", Number(this.data.originalPrice))
  175. shopGoods.set("top", Number(this.data.top))
  176. // shopGoods.set("image", this.data.image)
  177. shopGoods.set("image", this.data.image[0])
  178. if (this.data.images) {
  179. shopGoods.set("images", this.data.images)
  180. }
  181. if (!this.data.images) {
  182. shopGoods.set("images", this.data.goods.images)
  183. }
  184. shopGoods.set("status", this.data.checked)
  185. shopGoods.set("isRecom", this.data.check)
  186. shopGoods.set("type", "meal")
  187. shopGoods.set("services", this.data.services)
  188. shopGoods.set("total", Number(this.data.total))
  189. shopGoods.set("sales", Number(this.data.sales))
  190. shopGoods.set("shopStore", {
  191. __type: "Pointer",
  192. className: "ShopStore",
  193. objectId: id
  194. })
  195. shopGoods.save().then(res => {
  196. console.log(res)
  197. if (this.data.id) {
  198. wx.showToast({
  199. title: '修改成功',
  200. icon: 'none'
  201. })
  202. } else {
  203. wx.showToast({
  204. title: '创建成功',
  205. icon: 'none'
  206. })
  207. }
  208. wx.setStorageSync('images', undefined)
  209. wx.setStorageSync('html', undefined)
  210. _this.setData({
  211. name: '',
  212. images: [],
  213. image: '',
  214. price: '',
  215. originalPrice: '',
  216. total: '',
  217. category: '',
  218. Introduction: "",
  219. })
  220. wx.navigateBack({
  221. delta: 1,
  222. })
  223. // ({
  224. // url: '/nova-tourism/pages/my/merchant/merchant-home/package/index'
  225. // });
  226. })
  227. },
  228. //从本地获取数据
  229. getGoodsList() {
  230. let ShopGoods = new Parse.Query("ShopGoods")
  231. console.log(this.data.id);
  232. ShopGoods.equalTo("objectId", this.data.id)
  233. ShopGoods.include("shopStore")
  234. ShopGoods.first().then(res => {
  235. let goods = res.toJSON()
  236. // order.orderTime = dateF.formatTime("YYYY-mm-dd ", order.createdAt)
  237. console.log(goods);
  238. this.setData({
  239. goods: goods,
  240. services: goods.services,
  241. // image: goods.image,
  242. // images: goods.images,
  243. checked: goods.status,
  244. check: goods.isRecom,
  245. top: goods.top,
  246. sales: goods.sales,
  247. total: goods.total,
  248. name: goods.name,
  249. price: goods.price,
  250. originalPrice: goods.originalPrice,
  251. })
  252. })
  253. },
  254. showPopup() {
  255. this.setData({ show: true })
  256. },
  257. images() {
  258. console.log(wx.getStorageSync('images'))
  259. let images = wx.getStorageSync('images')
  260. let html = wx.getStorageSync('html')
  261. this.setData({
  262. images: images,
  263. html: html
  264. });
  265. console.log(html);
  266. },
  267. onClose: function () {
  268. this.setData({ show: false })
  269. },
  270. twoonConfirm: function () {
  271. let item = this.data.services
  272. let service = this.data.service
  273. console.log(service);
  274. item.push({
  275. 'check': false,
  276. 'value': service,
  277. })
  278. console.log(item)
  279. this.setData({
  280. service: null,
  281. })
  282. console.log(service);
  283. this.setData({
  284. show: false,
  285. services: item
  286. });
  287. },
  288. /**
  289. * 生命周期函数--监听页面加载
  290. */
  291. onLoad: async function (options) {
  292. let merchant = wx.getStorageSync('merchant'); //用户
  293. this.setData({ merchant: merchant })
  294. const _this = this
  295. this.getCategory()
  296. let id = (options.id)
  297. console.log(id)
  298. this.setData({ id: id });
  299. // id && this.getGoodsList()
  300. if (id) {
  301. let imageList = []
  302. let ShopGoods = new Parse.Query("ShopGoods")
  303. console.log(id);
  304. ShopGoods.include("shopStore")
  305. let Goods = await ShopGoods.get(id)
  306. let goods = Goods.toJSON()
  307. // order.orderTime = dateF.formatTime("YYYY-mm-dd ", order.createdAt)
  308. imageList.push({
  309. url: goods.image
  310. })
  311. console.log(imageList);
  312. this.setData({
  313. goods: goods,
  314. services: goods.services,
  315. // image: goods.image,
  316. images: goods.images,
  317. checked: goods.status,
  318. check: goods.isRecom,
  319. top: goods.top,
  320. sales: goods.sales,
  321. total: goods.total,
  322. name: goods.name,
  323. price: goods.price,
  324. originalPrice: goods.originalPrice,
  325. imageList: imageList,
  326. })
  327. }
  328. },
  329. /**
  330. * 生命周期函数--监听页面初次渲染完成
  331. */
  332. onReady: function () {
  333. },
  334. /**
  335. * 生命周期函数--监听页面显示
  336. */
  337. onShow: function () {
  338. this.images();
  339. },
  340. /**
  341. * 生命周期函数--监听页面隐藏
  342. */
  343. onHide: function () {
  344. },
  345. /**
  346. * 生命周期函数--监听页面卸载
  347. */
  348. onUnload: function () {
  349. },
  350. /**
  351. * 页面相关事件处理函数--监听用户下拉动作
  352. */
  353. onPullDownRefresh: function () {
  354. },
  355. /**
  356. * 页面上拉触底事件的处理函数
  357. */
  358. onReachBottom: function () {
  359. },
  360. /**
  361. * 用户点击右上角分享
  362. */
  363. onShareAppMessage: function () {
  364. }
  365. })