1
0

groupDetail.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. var app = getApp();
  2. var WxParse = require('../../lib/wxParse/wxParse.js');
  3. var util = require('../../utils/util.js');
  4. var api = require('../../config/api.js');
  5. Page({
  6. data: {
  7. winHeight: "",
  8. id: 0,
  9. referrer: 0,
  10. goods: {},
  11. group: {},
  12. gallery: [],
  13. attribute: [],
  14. issueList: [],
  15. comment: [],
  16. brand: {},
  17. specificationList: [],
  18. productList: [],
  19. relatedGoods: [],
  20. cartGoodsCount: 0,
  21. userHasCollect: 0,
  22. crashList: {},
  23. groupOpenList: [],
  24. number: 1,
  25. checkedSpecText: '请选择规格数量',
  26. openAttr: false,
  27. noCollectImage: "/static/images/icon_collect.png",
  28. hasCollectImage: "/static/images/icon_collect_checked.png",
  29. collectBackImage: "/static/images/icon_collect.png",
  30. showNavList: false
  31. },
  32. toggleNav(){
  33. this.setData({
  34. showNavList: !this.data.showNavList
  35. })
  36. },
  37. switchNav(event){
  38. let name = event.currentTarget.dataset.name;
  39. wx.switchTab({
  40. url: `/pages/${name}/${name}`,
  41. });
  42. },
  43. getGoodsInfo: function () {
  44. let that = this;
  45. util.request(api.GroupDetail, { id: that.data.id }).then(function (res) {
  46. if (res.errno === 0) {
  47. that.setData({
  48. group: res.data.groupVo,
  49. goods: res.data.info,
  50. gallery: res.data.gallery,
  51. attribute: res.data.attribute,
  52. issueList: res.data.issue,
  53. comment: res.data.comment,
  54. // brand: res.data.brand,
  55. specificationList: res.data.specificationList,
  56. productList: res.data.productList,
  57. userHasCollect: res.data.userHasCollect
  58. });
  59. //
  60. let _specificationList = res.data.specificationList;
  61. for (let i = 0; i < _specificationList.length; i++) {
  62. if (_specificationList[i].valueList.length == 1) {
  63. //如果已经选中,则反选
  64. _specificationList[i].valueList[0].checked = true;
  65. }
  66. }
  67. that.setData({
  68. 'specificationList': _specificationList
  69. });
  70. that.getGroupOpenList(res.data.groupVo.id)
  71. if (res.data.userHasCollect == 1) {
  72. that.setData({
  73. 'collectBackImage': that.data.hasCollectImage
  74. });
  75. } else {
  76. that.setData({
  77. 'collectBackImage': that.data.noCollectImage
  78. });
  79. }
  80. wx.hideLoading()
  81. WxParse.wxParse('goodsDetail', 'html', res.data.info.goods_desc, that);
  82. util.countdown(that,res.data.groupVo,'group',null)
  83. }
  84. });
  85. },
  86. //获取拼团列表
  87. getGroupOpenList(groupId) {
  88. let that = this;
  89. util.request(api.GroupOpenList, { groupId: groupId, type:1}).then(function (res) {
  90. if (res.errno === 0) {
  91. that.setData({
  92. groupOpenList: res.data
  93. });
  94. res.data.forEach((item,num)=>{
  95. util.countdown(that, res.data, 'groupOpenList', num)
  96. })
  97. }
  98. });
  99. },
  100. clickSkuValue: function (event) {
  101. let that = this;
  102. let specNameId = event.currentTarget.dataset.nameId;
  103. let specValueId = event.currentTarget.dataset.valueId;
  104. //判断是否可以点击
  105. //TODO 性能优化,可在wx:for中添加index,可以直接获取点击的属性名和属性值,不用循环
  106. let _specificationList = this.data.specificationList;
  107. for (let i = 0; i < _specificationList.length; i++) {
  108. if (_specificationList[i].specification_id == specNameId) {
  109. for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  110. if (_specificationList[i].valueList[j].id == specValueId) {
  111. //如果已经选中,则反选
  112. if (_specificationList[i].valueList[j].checked) {
  113. _specificationList[i].valueList[j].checked = false;
  114. } else {
  115. _specificationList[i].valueList[j].checked = true;
  116. }
  117. } else {
  118. _specificationList[i].valueList[j].checked = false;
  119. }
  120. }
  121. }
  122. }
  123. this.setData({
  124. 'specificationList': _specificationList
  125. });
  126. //重新计算spec改变后的信息
  127. this.changeSpecInfo();
  128. //重新计算哪些值不可以点击
  129. },
  130. //获取选中的规格信息
  131. getCheckedSpecValue: function () {
  132. let checkedValues = [];
  133. let _specificationList = this.data.specificationList;
  134. for (let i = 0; i < _specificationList.length; i++) {
  135. let _checkedObj = {
  136. nameId: _specificationList[i].specification_id,
  137. valueId: 0,
  138. valueText: ''
  139. };
  140. for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  141. if (_specificationList[i].valueList[j].checked) {
  142. _checkedObj.valueId = _specificationList[i].valueList[j].id;
  143. _checkedObj.valueText = _specificationList[i].valueList[j].value;
  144. }
  145. }
  146. checkedValues.push(_checkedObj);
  147. }
  148. return checkedValues;
  149. },
  150. //根据已选的值,计算其它值的状态
  151. setSpecValueStatus: function () {
  152. },
  153. //判断规格是否选择完整
  154. isCheckedAllSpec: function () {
  155. return !this.getCheckedSpecValue().some(function (v) {
  156. if (v.valueId == 0) {
  157. util.showErrorToast('请选齐规格');
  158. return true;
  159. }
  160. });
  161. },
  162. getCheckedSpecKey: function () {
  163. let checkedValue = this.getCheckedSpecValue().map(function (v) {
  164. return v.valueId;
  165. });
  166. return checkedValue.join('_');
  167. },
  168. changeSpecInfo: function () {
  169. let checkedNameValue = this.getCheckedSpecValue();
  170. //设置选择的信息
  171. let checkedValue = checkedNameValue.filter(function (v) {
  172. if (v.valueId != 0) {
  173. return true;
  174. } else {
  175. return false;
  176. }
  177. }).map(function (v) {
  178. return v.valueText;
  179. });
  180. if (checkedValue.length > 0) {
  181. this.setData({
  182. 'checkedSpecText': checkedValue.join(' ')
  183. });
  184. } else {
  185. this.setData({
  186. 'checkedSpecText': '请选择规格数量'
  187. });
  188. }
  189. },
  190. getCheckedProductItem: function (key) {
  191. if (this.data.productList.length ==1){
  192. return this.data.productList;
  193. }
  194. return this.data.productList.filter(function (v) {
  195. if (v.goods_specification_ids == key) {
  196. return true;
  197. } else {
  198. return false;
  199. }
  200. });
  201. },
  202. onLoad: function (options) {
  203. wx.showLoading({
  204. mask: true
  205. })
  206. // 页面初始化 options为页面跳转所带来的参数
  207. this.setData({
  208. id: parseInt(options.id),
  209. referrer: (null!=options.referrer?parseInt(options.referrer):0)
  210. });
  211. var that = this;
  212. this.getGoodsInfo();
  213. var that = this
  214. // 高度自适应
  215. wx.getSystemInfo({
  216. success: function (res) {
  217. var clientHeight = res.windowHeight,
  218. clientWidth = res.windowWidth,
  219. rpxR = 750 / clientWidth;
  220. var calc = clientHeight * rpxR - 100;
  221. console.log(calc)
  222. that.setData({
  223. winHeight: calc
  224. });
  225. }
  226. });
  227. },
  228. onReady: function () {
  229. let that = this;
  230. // 页面渲染完成
  231. },
  232. onShow: function () {
  233. // 页面显示
  234. },
  235. onHide: function () {
  236. //页面隐藏
  237. this.setData({
  238. openAttr: false,
  239. showNavList: false
  240. })
  241. },
  242. onUnload: function () {
  243. // 页面关闭
  244. },
  245. switchAttrPop: function () {
  246. this.setData({
  247. openAttr: !this.data.openAttr
  248. })
  249. },
  250. closeAttrOrCollect: function () {
  251. let that = this;
  252. //添加或是取消收藏
  253. util.request(api.CollectAddOrDelete, { typeId: 0, valueId: this.data.id }, "POST")
  254. .then(function (res) {
  255. let _res = res;
  256. if (_res.errno == 0) {
  257. if (_res.data.type == 'add') {
  258. that.setData({
  259. 'collectBackImage': that.data.hasCollectImage
  260. });
  261. } else {
  262. that.setData({
  263. 'collectBackImage': that.data.noCollectImage
  264. });
  265. }
  266. } else {
  267. wx.showToast({
  268. image: '/static/images/icon_error.png',
  269. title: _res.errmsg,
  270. mask: true
  271. });
  272. }
  273. });
  274. },
  275. cutNumber: function () {
  276. this.setData({
  277. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  278. });
  279. },
  280. addNumber: function () {
  281. this.setData({
  282. number: this.data.number + 1
  283. });
  284. },
  285. onShareAppMessage: function () {
  286. var that = this;
  287. // console.log("url:" + that.data.goods.list_pic_url);
  288. // var userId = wx.getStorageSync('userId');
  289. // console.log("userId:" + userId);
  290. return {
  291. title: that.data.goods.name,
  292. desc: '商业版',
  293. imageUrl: that.data.goods.list_pic_url,
  294. path: '/pages/groupDetail/groupDetail?id=' + that.data.id + '&&referrer=' + wx.getStorageSync('userId'),
  295. success: function (res) {
  296. console.log("转发成功");
  297. // 转发成功
  298. },
  299. fail: function (res) {
  300. // 转发失败
  301. console.log("转发失败");
  302. }
  303. }
  304. },
  305. checkProduct: function () {
  306. var that = this;
  307. if (this.data.openAttr == false) {
  308. //打开规格选择窗口
  309. this.setData({
  310. openAttr: !this.data.openAttr,
  311. collectBackImage: "/static/images/detail_back.png"
  312. });
  313. } else {
  314. //提示选择完整规格
  315. if (!this.isCheckedAllSpec()) {
  316. return false;
  317. }
  318. //根据选中的规格,判断是否有对应的sku信息
  319. let checkedProduct = this.getCheckedProductItem(this.getCheckedSpecKey());
  320. if (!checkedProduct || checkedProduct.length <= 0) {
  321. //找不到对应的product信息,提示没有库存
  322. return false;
  323. }
  324. //验证库存
  325. if (checkedProduct.goods_number < this.data.number) {
  326. //找不到对应的product信息,提示没有库存
  327. return false;
  328. }
  329. wx.redirectTo({
  330. url: '/pages/shopping/groupcheck/groupcheck?number=' + this.data.number + '&productId=' + checkedProduct[0].id
  331. + '&groupId=' + this.data.id,
  332. })
  333. }
  334. },
  335. switchNav(event) {
  336. wx.switchTab({
  337. url: '/pages/index/index'
  338. });
  339. },
  340. })