1
0

index.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. const util = require('../../utils/util.js');
  2. const api = require('../../config/api.js');
  3. const user = require('../../services/user.js');
  4. //获取应用实例
  5. const app = getApp();
  6. Page({
  7. data: {
  8. groupGoods: [],
  9. hotGoods: [],
  10. topics: [],
  11. brands: [],
  12. floorGoods: [],
  13. banner: [],
  14. channel: [],
  15. groupBanner: {},
  16. storeName: '',
  17. showPop: false, //活动弹窗
  18. couponVo: {},
  19. storeId: '',
  20. page: 1,
  21. size: 3,
  22. list: [],
  23. openAttr: false,
  24. isMapShow: '', //第三方商户是否启用地图显示
  25. isStartLocation: null,
  26. isSystemLocation: null
  27. },
  28. onLoad: function(options) {
  29. let that = this;
  30. // console.log('http://120.76.26.84:80/group1/M00/00/03/rBJEdVv1LDWAAjsfABCztmpupr8780.mp4')
  31. // console.log(wx.getFileSystemManager().readFile())
  32. wx.setStorageSync("navUrl", "/pages/index/index");
  33. // options.scene = 148;
  34. // options.scene = 93;
  35. // options.scene = 38;
  36. options.scene = 67;
  37. // options.scene = 28;
  38. // options.scene = 8;
  39. if (options.scene) {
  40. // console.log("have scene");
  41. var scene = decodeURIComponent(options.scene);
  42. console.log("scene is ", scene);
  43. that.setData({
  44. storeId: scene
  45. });
  46. wx.setStorageSync('storeId', scene);
  47. //扫码进入的小程序则清除是否分享缓存
  48. wx.removeStorageSync('isShare');
  49. } else {
  50. //是否分享过商品,有数据则分享过;从首页进入页面则没有分享过,从详情页进入则分享过
  51. if (wx.getStorageSync('isShare') == 'true') {
  52. console.log('是分享进入首页门店id:' + wx.getStorageSync('storeId') + ',从商品页获取的缓存')
  53. console.log('是分享进入首页:' + wx.getStorageSync('isShare'))
  54. } else {
  55. //小程序入口进入的主页,则清除storeId,获取当前地址最近的门店
  56. wx.removeStorageSync('storeId');
  57. // console.log('不是分享进入首页门店id:【' + wx.getStorageSync('storeId') + '】,获取最近门店')
  58. // console.log('不是分享进入首页:' + wx.getStorageSync('isShare'))
  59. }
  60. }
  61. wx.getLocation({
  62. success: function(location) {
  63. // that.successLoadStore(location);
  64. },
  65. fail: function() {
  66. wx.hideLoading();
  67. wx.getSetting({
  68. success: function(res) {
  69. if (!res.authSetting['scope.userLocation']) {
  70. wx.showModal({
  71. title: '',
  72. content: '检测到您没有开启获取地理位置权限,是否开启',
  73. confirmText: '确定',
  74. success: function(resConfirm) {
  75. if (resConfirm.confirm) {
  76. wx.openSetting();
  77. } else {
  78. that.setData({
  79. isStartLocation: false
  80. });
  81. }
  82. }
  83. })
  84. } else {
  85. //用户已授权,但是获取地理位置失败,提示用户去系统设置中打开定位
  86. wx.showModal({
  87. title: '',
  88. content: '请在系统设置中打开定位服务',
  89. confirmText: '确定',
  90. showCancel: false,
  91. success: function(resConfirm2) {
  92. that.setData({
  93. isSystemLocation: false
  94. });
  95. }
  96. })
  97. }
  98. }
  99. })
  100. }
  101. });
  102. },
  103. onReady: function() {
  104. // 页面渲染完成
  105. },
  106. onShow: function() {
  107. // 页面显示
  108. let that = this;
  109. wx.setStorageSync("navUrl", "/pages/index/index");
  110. wx.setStorageSync("isLocationIndex", "false"); //购物车是否已跳转登录页,到首页重置字段为false未跳转
  111. if (!wx.getStorageSync('isRefusedLogin')) {
  112. if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
  113. console.log('缓存是否授权为空,用户已授权:' + wx.getStorageSync('isRefusedLogin'))
  114. wx.setStorageSync('isRefusedLogin', 'false');
  115. } else {
  116. console.log('缓存是否授权为空,用户未授权:' + wx.getStorageSync('isRefusedLogin'))
  117. wx.setStorageSync('isRefusedLogin', 'true'); //拒绝授权
  118. }
  119. } else {
  120. console.log('授权不为空:' + wx.getStorageSync('isRefusedLogin'))
  121. wx.setStorageSync('isRefusedLogin', wx.getStorageSync('isRefusedLogin'));
  122. }
  123. that.syncStore();
  124. // if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
  125. // } else {
  126. // // console.log('拒绝授权:' + wx.getStorageSync('isRefusedLogin'))
  127. // if (wx.getStorageSync('isRefusedLogin') != 'true') {
  128. // wx.navigateTo({
  129. // url: '/pages/auth/btnAuth/btnAuth',
  130. // })
  131. // }
  132. // }
  133. },
  134. onHide: function() {
  135. // 页面隐藏
  136. },
  137. onUnload: function() {
  138. // 页面关闭
  139. },
  140. showCouponPop() {
  141. let that = this;
  142. this.setData({
  143. showPop: false
  144. });
  145. // wx.showToast({
  146. // title: '恭喜获取优惠券一张' + that.data.couponVo.name,
  147. // duration: 2000
  148. // });
  149. wx.showModal({
  150. title: '获取优惠券一张',
  151. showCancel: false,
  152. content: that.data.couponVo.name
  153. })
  154. },
  155. onShareAppMessage: function() {
  156. var that = this;
  157. // console.log('分享:' + that.data.storeId)
  158. return {
  159. title: '中网跨境电商主页',
  160. desc: '中网跨境电商主页',
  161. path: '/pages/index/index?scene=' + that.data.storeId
  162. }
  163. },
  164. // 同步门店
  165. syncStore: function() {
  166. let that = this;
  167. // console.log(wx.getStorageSync('merchSn'));
  168. //获取附件门店信息
  169. // util.getLocation((lng, lat) => {
  170. wx.getLocation({
  171. success: function(location) {
  172. // console.log(location)
  173. that.successLoadStore(location);
  174. },
  175. fail: function() {
  176. wx.hideLoading();
  177. wx.getSetting({
  178. success: function(res) {
  179. if (!res.authSetting['scope.userLocation']) {
  180. that.setData({
  181. isStartLocation: false
  182. });
  183. } else {
  184. //用户已授权,但是获取地理位置失败,提示用户去系统设置中打开定位
  185. that.setData({
  186. isSystemLocation: false
  187. });
  188. // wx.showModal({
  189. // title: '',
  190. // content: '请在系统设置中打开定位服务',
  191. // confirmText: '确定',
  192. // showCancel: false,
  193. // success: function (resConfirm2) {
  194. // that.setData({
  195. // isSystemLocation: false
  196. // });
  197. // }
  198. // })
  199. }
  200. }
  201. });
  202. }
  203. });
  204. },
  205. /**
  206. * 打开微信定位
  207. */
  208. openSetLocation: function() {
  209. wx.openSetting();
  210. },
  211. /**
  212. * 定位确定授权加载附近门店信息
  213. */
  214. successLoadStore: function(location) {
  215. let that = this;
  216. that.setData({
  217. isStartLocation: true,
  218. isSystemLocation: true
  219. });
  220. var lng = location.longitude;
  221. var lat = location.latitude;
  222. wx.setStorageSync('location', JSON.stringify({
  223. lng,
  224. lat
  225. }));
  226. let storeId = wx.getStorageSync('storeId');
  227. util.request(api.NearbyList, {
  228. longitude: lng,
  229. latitude: lat,
  230. storeId: storeId,
  231. thirdPartyMerchCode: ''
  232. }).then((res) => {
  233. let nlist = res.data;
  234. // wx.removeStorageSync('nearStoreList');
  235. // wx.removeStorageSync('storeId');
  236. // wx.removeStorageSync('storeVo');
  237. // wx.setStorageSync('storeId', storeId);
  238. if (!wx.getStorageSync('currentCategory')) {
  239. wx.removeStorageSync('currentCategory');
  240. }
  241. if (!nlist.length) {
  242. wx.removeStorageSync('nearStoreList');
  243. wx.removeStorageSync('mapIndexStoreId');
  244. } else {
  245. wx.setStorageSync('nearStoreList', JSON.stringify(nlist));
  246. wx.setStorageSync('mapIndexStoreId', nlist[0].id);
  247. }
  248. if (!nlist.length) {
  249. // wx.removeStorageSync('storeId');
  250. wx.removeStorageSync('storeVo');
  251. that.setData({
  252. storeName: '附近暂无门店'
  253. })
  254. } else {
  255. that.setData({
  256. storeName: nlist[0].storeName,
  257. storeId: nlist[0].id
  258. })
  259. that.checkLoginTokenByUpd(nlist);
  260. wx.setStorageSync('storeVo', JSON.stringify(nlist[0]));
  261. }
  262. })
  263. },
  264. /**
  265. * 校验token是否失效,失效则更新新token
  266. */
  267. checkLoginTokenByUpd: function (nlist){
  268. var that = this;
  269. util.request(api.checkToken, {
  270. token: wx.getStorageSync('token'),
  271. isRefusedLogin: wx.getStorageSync('isRefusedLogin'),
  272. userId: wx.getStorageSync('userId')+'',
  273. storeId: parseInt(that.data.storeId)
  274. }, 'POST').then(function (res) {
  275. if (res.data.errno == 401) {
  276. console.log(res.data.errmsg)
  277. console.log(wx.getStorageSync('token'))
  278. wx.getSetting({
  279. success: (setRes) => {
  280. if (setRes.authSetting['scope.userInfo'] == undefined) {
  281. wx.setStorageSync('isRefusedLogin', 'true');//未授权
  282. wx.request({
  283. url: api.AuthLoginSaveToken,
  284. data: {
  285. storeId: that.data.storeId,
  286. merchSn: nlist[0].merchSn
  287. },
  288. method: 'POST',
  289. header: {
  290. 'Content-Type': 'application/json',
  291. 'isRefusedLogin': wx.getStorageSync('isRefusedLogin')
  292. },
  293. success: function (wxRes) {
  294. // console.log(wxRes)
  295. if (wxRes.data.errno === 0) {
  296. //存储用户信息
  297. // wx.setStorageSync('userInfo', wxRes.data.data.userInfo);
  298. wx.setStorageSync('token', wxRes.data.data.token);
  299. console.log('微信用户未授权,获取一个新的token' + wx.getStorageSync('token'))
  300. // wx.setStorageSync('userId', wxRes.data.data.userId);
  301. that.chooseStore(nlist[0].id, nlist[0].merchSn);
  302. }
  303. },
  304. fail: function (err) {
  305. console.log("failed");
  306. }
  307. });
  308. } else {
  309. console.log('微信用户点击授权操作,过了一段时间token失效,更新token')
  310. wx.login({
  311. success: function (loginRes) {
  312. if (loginRes.code) {
  313. //登录远程服务器
  314. wx.request({
  315. url: api.AuthLoginSaveToken,
  316. data: {
  317. code: loginRes.code,
  318. userInfo: wx.getStorageSync('userInfo'),
  319. storeId: that.data.storeId,
  320. merchSn: nlist[0].merchSn
  321. },
  322. method: 'POST',
  323. header: {
  324. 'Content-Type': 'application/json'
  325. },
  326. success: function (wxRes) {
  327. // console.log(wxRes)
  328. if (wxRes.data.errno === 0) {
  329. //存储用户信息
  330. wx.setStorageSync('userInfo', wxRes.data.data.userInfo);
  331. wx.setStorageSync('token', wxRes.data.data.token);
  332. wx.setStorageSync('userId', wxRes.data.data.userId);
  333. wx.setStorageSync('isRefusedLogin', 'false');//允许授权
  334. console.log("登录成功");
  335. that.chooseStore(nlist[0].id, nlist[0].merchSn);
  336. }
  337. },
  338. fail: function (err) {
  339. console.log("failed");
  340. }
  341. });
  342. } else {
  343. console.log("failed");
  344. }
  345. },
  346. fail: function (err) {
  347. console.log("failed");
  348. }
  349. });
  350. }
  351. }
  352. });
  353. } else {
  354. console.log('token未失效');
  355. // console.log(res)
  356. that.chooseStore(nlist[0].id, nlist[0].merchSn);
  357. }
  358. });
  359. },
  360. // 更新门店Id
  361. chooseStore: function(storeId, merchSn) {
  362. let that = this;
  363. util.request(api.ChooseStoreId, {
  364. storeId: storeId,
  365. merchSn: merchSn,
  366. isRefusedLogin: wx.getStorageSync('isRefusedLogin')
  367. }, 'POST').then(function (res) {
  368. if (res.errno === 0) {
  369. wx.setStorageSync('storeId', storeId);
  370. wx.setStorageSync('merchSn', merchSn);
  371. console.log(res.data.token)
  372. wx.setStorageSync('token', res.data.token);
  373. // wx.getSetting({
  374. // success: (setRes) => {
  375. // if (setRes.authSetting['scope.userInfo'] == undefined) {
  376. // }else{
  377. // wx.setStorageSync('token', res.data.token);
  378. // }
  379. // }
  380. // });
  381. if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
  382. that.reLoad();
  383. } else {
  384. wx.setStorageSync('isLoadGoods', '0'); //1:代表每次点首页都会重新加载商品,0:否
  385. }
  386. that.getIndexData();
  387. that.enableActivity();
  388. that.getGroupData();
  389. }
  390. });
  391. },
  392. reLoad: function() {
  393. let that = this;
  394. // console.log(wx.getStorageSync('userId'));
  395. // console.log(wx.getStorageSync('storeId'));
  396. // console.log(wx.getStorageSync('merchSn'));
  397. if (wx.getStorageSync('storeId')) {
  398. if (wx.getStorageSync('userId')) {
  399. wx.request({
  400. url: api.updateLoginUser,
  401. data: {
  402. userId: wx.getStorageSync('userId'),
  403. storeId: wx.getStorageSync('storeId'),
  404. merchSn: wx.getStorageSync('merchSn')
  405. },
  406. method: 'POST',
  407. header: {
  408. 'Content-Type': 'application/json'
  409. },
  410. success: function(wxRes) {
  411. if (wxRes.data.errno === 0) {
  412. that.setData({
  413. isMapShow: wxRes.data.data.isMapShow
  414. });
  415. // console.log(wxRes.data.data.isLoadGoods);
  416. wx.setStorageSync('isLoadGoods', wxRes.data.data.isLoadGoods);
  417. wx.setStorageSync('thirdPartyMerchCode', wxRes.data.data.thirdPartyMerchCode);
  418. // console.log(that.data.isMapShow)
  419. // console.log("用户信息更新成功");
  420. }
  421. },
  422. fail: function(err) {
  423. console.log("failed");
  424. }
  425. });
  426. }
  427. }
  428. },
  429. getIndexData: function() {
  430. setTimeout(function() {}, 350)
  431. wx.showLoading({
  432. title: '加载中...',
  433. })
  434. let that = this;
  435. util.request(api.IndexUrl).then(function(res) {
  436. if (res.errno === 0) {
  437. // console.log(res.data.banner);
  438. that.setData({
  439. // newGoods: res.data.newGoodsList,
  440. // hotGoods: res.data.hotGoodsList,
  441. // topics: res.data.topicList,
  442. // brand: res.data.brandList,
  443. // floorGoods: res.data.categoryList,
  444. banner: res.data.banner,
  445. // groupBanner: res.data.groupBanner,
  446. channel: res.data.channel
  447. });
  448. // console.log(that.data.hotGoods.length)
  449. // console.log(wx.getStorageSync('storeId'))
  450. // console.log(wx.getStorageSync('mapIndexStoreId'))
  451. // if (that.data.hotGoods.length == 0) {
  452. // that.getGoodsList();
  453. // }
  454. // console.log("mapIndexStoreId:" + wx.getStorageSync('mapIndexStoreId'))
  455. // console.log("storeId:" + wx.getStorageSync('storeId'))
  456. // console.log("isShare:" + wx.getStorageSync('isShare'))
  457. // console.log("isLoadGoods:" + wx.getStorageSync('isLoadGoods'))
  458. //当切换了门店地图(mapIndexStoreId),或是由用户分享进入的商品详情切换进入的首页(isShare:true)
  459. if (wx.getStorageSync('storeId') != wx.getStorageSync('mapIndexStoreId') || wx.getStorageSync('isShare') == 'true' || that.data.hotGoods.length == 0 || wx.getStorageSync('isLoadGoods') == '1') {
  460. that.setData({
  461. hotGoods: [],
  462. page: 1
  463. });
  464. that.getGoodsList();
  465. if (wx.getStorageSync('isLoadGoods') == '1') {
  466. util.request(api.UpdateStoreLoadGoods, {
  467. storeId: wx.getStorageSync('storeId')
  468. }).then(function(res) {});
  469. }
  470. wx.setStorageSync('isShare', 'false');
  471. }
  472. }
  473. wx.hideLoading();
  474. });
  475. },
  476. getGoodsList: function() {
  477. let that = this;
  478. util.request(api.GoodsHot).then(function(res) {
  479. if (res.errno == 0) {
  480. util.request(api.HotGoodsList, {
  481. isHot: 1,
  482. page: that.data.page,
  483. size: that.data.size,
  484. categoryId: 0
  485. })
  486. .then(function(res) {
  487. if (res.errno == 0) {
  488. let goodsList = that.data.hotGoods.concat(res.data.goodsList);
  489. that.setData({
  490. hotGoods: goodsList,
  491. list: res.data.goodsList
  492. });
  493. }
  494. });
  495. wx.hideLoading();
  496. }
  497. });
  498. },
  499. getGroupData: function() {
  500. let that = this;
  501. util.request(api.GroupList).then(function(res) {
  502. if (res.errno === 0) {
  503. that.setData({
  504. groupGoods: res.data.data,
  505. });
  506. }
  507. });
  508. },
  509. handleStore() {
  510. wx.navigateTo({
  511. url: '../map/map',
  512. })
  513. },
  514. goSearch() {
  515. wx.navigateTo({
  516. url: '../search/search',
  517. })
  518. },
  519. goCatalog: function(e) {
  520. let url = '';
  521. // console.log('dataset.goodsBizType:' + e.currentTarget.dataset.goodsBizType);
  522. app.globalData.appGoodsBizType = e.currentTarget.dataset.goodsBizType;
  523. // console.log('appgoodsBizType1:' + app.globalData.appGoodsBizType);
  524. wx.setStorageSync('isSwitchCatalog', 'true'); //主页跳转至分类页加载商品,
  525. wx.switchTab({
  526. url: '/pages/catalog/catalog',
  527. });
  528. },
  529. onReachBottom: function() {
  530. if (this.data.bottomLoadDone === true || this.data.bottomLoading === true) {
  531. return false;
  532. }
  533. this.setData({
  534. bottomLoading: true
  535. });
  536. // this.getFloorCategory();
  537. },
  538. //购物车减少
  539. cutNumber: function(e) {
  540. let that = this;
  541. var goodsId = e.currentTarget.dataset.goodsId;
  542. var productId = e.currentTarget.dataset.productId;
  543. var hotGoods = that.data.hotGoods;
  544. // hotGoods.forEach(function (val, index, arr) {
  545. // if (val.product_id == productId) {
  546. // val.cart_num = val.cart_num - 1;
  547. // if (val.cart_num >= 0) {
  548. // hotGoods[index] = val;
  549. // }
  550. // }
  551. // });
  552. // that.setData({ hotGoods: hotGoods });
  553. util.request(api.CartMinus, {
  554. goodsId: goodsId,
  555. productId: productId,
  556. number: 1
  557. }, 'POST').then(function(res) {
  558. if (res.errno === 0 && null != res.data) {
  559. var hotGoods = that.data.hotGoods;
  560. hotGoods.forEach(function(val, index, arr) {
  561. if (val.product_id == productId) {
  562. val.cart_num = res.data;
  563. hotGoods[index] = val;
  564. that.setData({
  565. hotGoods: hotGoods
  566. });
  567. }
  568. }, that);
  569. }
  570. });
  571. },
  572. //购物车增加
  573. addNumber: function(e) {
  574. let that = this;
  575. var goodsId = e.currentTarget.dataset.goodsId;
  576. var productId = e.currentTarget.dataset.productId;
  577. var hotGoods = that.data.hotGoods;
  578. // hotGoods.forEach(function (val, index, arr) {
  579. // if (val.product_id == productId) {
  580. // val.cart_num = val.cart_num + 1;
  581. // hotGoods[index] = val;
  582. // }
  583. // });
  584. // that.setData({ hotGoods: hotGoods });
  585. util.request(api.CartAdd, {
  586. goodsId: goodsId,
  587. productId: productId,
  588. number: 1
  589. }, 'POST').then(function(res) {
  590. if (res.errno === 0 && null != res.data) {
  591. hotGoods.forEach(function(val, index, arr) {
  592. res.data.cartList.forEach(function(cartVal, cartIndex, cartArr) {
  593. if (val.product_id == cartVal.product_id) {
  594. val.cart_num = cartVal.number;
  595. hotGoods[index] = val;
  596. }
  597. });
  598. that.setData({
  599. hotGoods: hotGoods
  600. });
  601. }, that);
  602. wx.showToast({
  603. title: '添加成功'
  604. })
  605. } else {
  606. wx.showToast({
  607. title: res.errmsg,
  608. icon: 'none'
  609. })
  610. }
  611. });
  612. },
  613. // 查询是否有活动
  614. enableActivity: function() {
  615. let that = this;
  616. let couponIds = wx.getStorageSync('couponIds');
  617. if (!couponIds) {
  618. couponIds = new Array();
  619. }
  620. // util.request(api.EnableActivity, { couponIds: couponIds }).then(function (res) {
  621. // // if (res.errno === 0 && null != res.data.showCoupon) {
  622. // // if (couponIds.contains(res.data.showCoupon.id)) {
  623. // // return;
  624. // // }
  625. // // couponIds.push(res.data.showCoupon.id);
  626. // // wx.setStorageSync('couponIds', couponIds);
  627. // // that.setData({
  628. // // couponVo: res.data.showCoupon,
  629. // // showPop: true
  630. // // });
  631. // // } else
  632. // if (res.errno === 0 && null != res.data.takeCoupon && null != res.data.takeCoupon.id) {
  633. // that.setData({
  634. // couponVo: res.data.takeCoupon,
  635. // showPop: true
  636. // });
  637. // }
  638. // });
  639. },
  640. // 商品扫码
  641. scanGoodsCode: function(e) {
  642. var that = this;
  643. var code;
  644. var value;
  645. var substrValue;
  646. var scanType;
  647. // 调起客户端扫码界面进行扫码
  648. wx.scanCode({
  649. // 是否只能从相机扫码
  650. onlyFromCamera: true,
  651. // 扫码类型, barCode:一维码, qrCode:二维码
  652. scanType: ['barCode', 'qrCode'],
  653. success: function(res) {
  654. that.code = "结果:" + res.result + ",路径:" + res.path + ",编码:" + res.rawData;
  655. that.value = res.result;
  656. that.scanType = res.scanType;
  657. that.setData({
  658. goodsCode: that.code
  659. });
  660. if (that.scanType == 'QR_CODE') { //二维码
  661. that.substrValue = that.value.substring(0, 5);
  662. that.value = that.value.substring(5, that.value.length);
  663. // var goodId = that.value.substring(18, that.value.length);
  664. var scanArray = that.value.split('&');
  665. // console.log(scanArray.length);
  666. if (scanArray.length < 2) {
  667. wx.showModal({
  668. title: '',
  669. content: '您所扫描的商品无效',
  670. showCancel: false
  671. });
  672. return;
  673. }
  674. var goodId = scanArray[0].substring(18, that.value.length);
  675. var storeId = scanArray[1].substring(8, that.value.length);
  676. // console.log("storeId:" + storeId);
  677. // console.log("goodId:" + goodId);
  678. if (that.substrValue != 'emato') { //../goods/goods?id=&merchSn=
  679. wx.showModal({
  680. title: '',
  681. content: '您所扫描的商品无效',
  682. showCancel: false,
  683. success: function(res) {
  684. if (res.confirm) {
  685. console.log('用户点击确定')
  686. } else if (res.cancel) {
  687. console.log('用户点击取消')
  688. }
  689. }
  690. });
  691. return;
  692. }
  693. if (storeId != wx.getStorageSync('storeId')) {
  694. wx.showModal({
  695. title: '扫描结果',
  696. content: '该商品不属于当前门店',
  697. showCancel: false
  698. });
  699. return;
  700. }
  701. util.request(api.GoodsDetail, {
  702. id: goodId,
  703. storeId: storeId,
  704. referrer: ''
  705. }).then(function(res) {
  706. if (res.errno === 0) {
  707. // console.log(res);
  708. // 跳转页面
  709. setTimeout(function() {
  710. wx.navigateTo({
  711. url: that.value,
  712. success: function(e) {
  713. console.log('跳转成功');
  714. },
  715. fail: function(e) {
  716. console.log('跳转失败');
  717. }
  718. })
  719. }, 350)
  720. } else {
  721. wx.showModal({
  722. title: '扫描结果',
  723. content: '商品不存在',
  724. showCancel: false
  725. });
  726. }
  727. });
  728. } else { //其他码
  729. //弹框显示结果
  730. wx.showModal({
  731. title: '扫描结果',
  732. content: that.value,
  733. showCancel: false
  734. });
  735. }
  736. },
  737. fail: function() {
  738. // 显示提示框
  739. wx.showToast({
  740. title: '扫码失败',
  741. icon: 'none',
  742. // 提示的延迟时间
  743. duration: 3000
  744. })
  745. }
  746. })
  747. },
  748. imgOnLoad: function(e) {
  749. let that = this;
  750. // console.log('图片加载完成');
  751. // var realthumb = e.target.dataset.thumb;
  752. // let list = that.data.list
  753. // for (var i = 0; i < list.length; i++) {
  754. // if (list[i].thumb == realthumb) {
  755. // list[i].loaded = true
  756. // }
  757. // that.setData({
  758. // list: list
  759. // })
  760. // }
  761. },
  762. switchAttrPop: function() {
  763. this.setData({
  764. openAttr: !this.data.openAttr
  765. })
  766. },
  767. hideSwitchAttrPop: function() {
  768. this.setData({
  769. openAttr: false
  770. })
  771. },
  772. //购物车增加
  773. addCart: function(e) {
  774. let that = this;
  775. that.setData({
  776. number: 1
  777. });
  778. var goodsId = e.currentTarget.dataset.goodsId;
  779. var retailPrice = e.currentTarget.dataset.retailPrice;
  780. util.request(api.GoodsSku, {
  781. goodsId: goodsId
  782. }).then(function(res) {
  783. if (res.errno === 0 && null != res.data) {
  784. that.setData({
  785. goodsVo: res.data.goodsVo,
  786. specificationList: res.data.specificationList,
  787. productList: res.data.productList,
  788. openAttr: !that.data.openAttr,
  789. retailPrice: retailPrice,
  790. stockNum: res.data.stockNum,
  791. cartNumber: res.data.cartNumber,
  792. checkedSpecText: res.data.specificationList[0].valueList[0].value
  793. });
  794. //
  795. let _specificationList = res.data.specificationList;
  796. for (let i = 0; i < _specificationList.length; i++) {
  797. if (_specificationList[i].valueList.length == 1) {
  798. //如果已经选中,则反选
  799. _specificationList[i].valueList[0].checked = true;
  800. }
  801. }
  802. that.setData({
  803. 'specificationList': _specificationList
  804. });
  805. }
  806. });
  807. },
  808. shows: function(e) {
  809. // wx.redirectTo({
  810. // url: '/pages/images/images'
  811. // })
  812. // wx.navigateTo({
  813. // url: '../ucenter/campMinus/campMinus?campMinusId=' + 9 + '&&storeId=' + 8 + '&&campName=' + '满200减50' + "&&isShare=1"
  814. // })
  815. wx.redirectTo({
  816. url: '/pages/checkVideo/checkVideo'
  817. })
  818. },
  819. onReachBottom() {
  820. var that = this;
  821. if (that.data.list.length > 0) {
  822. // console.log(that.data.list)
  823. wx.showLoading({
  824. title: '加载中...',
  825. })
  826. }
  827. that.setData({
  828. page: that.data.page + 1
  829. });
  830. that.getGoodsList();
  831. }
  832. })