index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. var util = require('../../utils/util.js');
  2. var app = getApp()
  3. function count_down(that, total_micro_second) {
  4. that.setData({
  5. clock: date_format(total_micro_second)
  6. });
  7. if (total_micro_second <= 0) {
  8. that.setData({
  9. clock: "00:00:00"
  10. });
  11. return;
  12. }
  13. setTimeout(function () {
  14. total_micro_second -= 1000;
  15. count_down(that, total_micro_second);
  16. }, 1000)
  17. }
  18. // 时间格式化输出
  19. function date_format(micro_second) {
  20. var second = Math.floor(micro_second / 1000);
  21. var hr = Math.floor(second / 3600);
  22. var min = fill_zero_prefix(Math.floor((second - hr * 3600) / 60));
  23. var sec = fill_zero_prefix((second - hr * 3600 - min * 60));
  24. return hr + ":" + min + ":" + sec;
  25. }
  26. // 位数不足补零
  27. function fill_zero_prefix(num) {
  28. return num < 10 ? "0" + num : num
  29. }
  30. Page({
  31. data: {
  32. list: {},
  33. is_show: false,
  34. goods: {},
  35. theme_type: '',
  36. loadover: false,
  37. goods_info: {},
  38. pin_info: {},
  39. pin_order_arr: {},
  40. options: {},
  41. sku: [],
  42. skustate: 0,
  43. order: {},
  44. hide_new: true,
  45. me_take_in: 0,
  46. interface_get_time: 0,
  47. order_id: 0,
  48. isHidenotice: true,
  49. is_login: false,
  50. share_title: '',
  51. jarray: [],
  52. clock: ''
  53. },
  54. onLoad: function (options) {
  55. var that = this;
  56. var token = wx.getStorageSync('token');
  57. if (util.check_login()) {
  58. this.setData({ is_login: true })
  59. }
  60. wx.showLoading({
  61. })
  62. wx.hideLoading();
  63. this.setData({ is_show: true, loadover: true })
  64. //
  65. //is_show
  66. var is_show = options.is_show;
  67. if (is_show != undefined && is_show == 1) {
  68. wx.showToast({
  69. title: '支付成功',
  70. })
  71. }
  72. //id = 24 & is_show=1
  73. app.util.request({
  74. 'url': 'entry/wxapp/index',
  75. 'data': {
  76. controller: 'user.group_info',
  77. 'token': token,
  78. 'order_id': options.id
  79. },
  80. dataType: 'json',
  81. success: function (res) {
  82. that.setData({
  83. goods: res.data.data.order_goods,
  84. goods_info: res.data.data.goods_info,
  85. options: res.data.data.options,
  86. pin_info: res.data.data.pin_info,
  87. share_title: res.data.data.share_title,
  88. pin_order_arr: res.data.data.pin_order_arr,
  89. order: {
  90. goods_id: res.data.data.order_goods.goods_id,
  91. pin_id: res.data.data.pin_info.pin_id,
  92. },
  93. me_take_in: res.data.data.me_take_in,
  94. is_me: res.data.data.is_me,
  95. interface_get_time: res.data.data.interface_get_time,
  96. order_id: res.data.data.order_id,
  97. })
  98. var seconds = (that.data.pin_info.end_time - that.data.interface_get_time) * 1000;
  99. if (seconds > 0) {
  100. count_down(that, seconds);
  101. }
  102. }
  103. })
  104. app.util.request({
  105. 'url': 'entry/wxapp/index',
  106. 'data': {
  107. controller: 'index.load_index_pintuan'
  108. },
  109. dataType: 'json',
  110. success: function (res) {
  111. that.setData({
  112. list: res.data.data,
  113. })
  114. }
  115. })
  116. this.socketmsg();
  117. },
  118. bindGetUserInfo: function (e) {
  119. var id = this.data.order_id;
  120. util.login('/Snailfish_shop/pages/share/index?id=' + id);
  121. },
  122. cancle_tip: function () {
  123. this.setData({
  124. hide_new: true
  125. })
  126. },
  127. goBuy: function (event) {
  128. var that = this;
  129. var order = that.data.order;
  130. order.buy_type = 'pin';
  131. order.quantity = 1;
  132. that.setData({
  133. order: order
  134. })
  135. if (that.data.options.list.length > 0) {
  136. let list = that.data.options.list;
  137. let arr = [];
  138. for (let i = 0; i < list.length; i++) {
  139. let sku = list[i]['option_value'][0];
  140. let temp = {
  141. name: sku['name'],
  142. id: sku['option_value_id'],
  143. index: i,
  144. idx: 0
  145. };
  146. arr.push(temp);
  147. }
  148. that.setData({
  149. sku: arr,
  150. skustate: 1
  151. })
  152. } else {
  153. that.goOrder();
  154. }
  155. },
  156. socketmsg: function () {
  157. wx.closeSocket();
  158. var domain = util.getdomain();
  159. var self = this;
  160. wx.connectSocket({
  161. url: 'wss://mall.shiziyu888.com/wss',
  162. header: {
  163. 'content-type': 'application/json'
  164. },
  165. method: "GET"
  166. })
  167. wx.onSocketOpen(function (res) {
  168. var login_data = '{ "type":"membre_login","domain":"' + domain + '"} ';
  169. wx.sendSocketMessage({
  170. data: login_data
  171. });
  172. setTimeout(function () {
  173. var ck_lo_pt = util.api() + "/index.php?s=/Apigoods/notify_order/rt/28163.html";
  174. wx.request({
  175. url: ck_lo_pt,
  176. type: 'get',
  177. dataType: 'json',
  178. success: function (res) {
  179. if (res.data.ret == 1) {
  180. self.send_bao_notify(res.data);
  181. }
  182. }
  183. })
  184. }, 1000);
  185. setInterval(function () {
  186. if (self.data.jarray.length > 0) {
  187. var res = self.data.jarray.pop();
  188. self.show_or(res);
  189. }
  190. }, 4000);
  191. })
  192. wx.onSocketMessage(function (res) {
  193. var data = util.stringToJson(res.data);
  194. switch (data.type) {
  195. // 服务端ping客户端
  196. case 'ping':
  197. var pong_data = '{"type":"pong"}';
  198. wx.sendSocketMessage({ data: pong_data });
  199. break;
  200. case 'member_buy_msg':
  201. var jarray_str = self.data.jarray;
  202. jarray_str.push(data);
  203. self.setData({
  204. jarray: jarray_str
  205. });
  206. //this.on_member_buy_msg(data);
  207. break;
  208. }
  209. })
  210. wx.onSocketError(function (res) {
  211. console.log('WebSocket连接打开失败,请检查!')
  212. })
  213. },
  214. send_bao_notify: function (res) {
  215. var buy_data = '{ "type":"member_buy","avatar":"' + res.avatar + '","miao":"' + res.miao + '","username":"' + res.username + '","order_id":"' + res.order_id + '","order_url":"' + res.order_url + '"} ';
  216. wx.sendSocketMessage({
  217. data: buy_data
  218. });
  219. },
  220. show_or: function (res) {
  221. this.setData({
  222. notice_orderid: res.order_id,
  223. notice_avatar: res.avatar,
  224. notice_name: res.username,
  225. notice_miao: res.miao,
  226. isHidenotice: false
  227. });
  228. var self = this;
  229. setTimeout(function () {
  230. self.setData({
  231. isHidenotice: true
  232. })
  233. setTimeout(function () {
  234. if (self.data.jarray.length > 0) {
  235. var res = self.data.jarray.pop();
  236. self.show_or(res);
  237. }
  238. }, 3000);
  239. }, 2000);
  240. },
  241. noticego: function (e) {
  242. var orderid = e.currentTarget.dataset.orderid;
  243. var pages_all = getCurrentPages();
  244. if (pages_all.length > 3) {
  245. wx.redirectTo({
  246. url: '/Snailfish_shop/pages/share/index?id=' + orderid
  247. })
  248. } else {
  249. wx.navigateTo({
  250. url: '/Snailfish_shop/pages/share/index?id=' + orderid
  251. })
  252. }
  253. },
  254. goOrder: function () {
  255. var token = wx.getStorageSync('token');
  256. var that = this;
  257. if (that.data.options.list.length > 0) {
  258. var id = '';
  259. let arr = that.data.sku;
  260. for (let i = 0; i < arr.length; i++) {
  261. if (i == arr.length - 1) {
  262. id = id + arr[i]['id'];
  263. } else {
  264. id = id + arr[i]['id'] + "_";
  265. }
  266. }
  267. var order = that.data.order;
  268. order.sku_str = id;
  269. that.setData({
  270. order: order
  271. })
  272. }
  273. app.util.request({
  274. 'url': 'entry/wxapp/index',
  275. 'data': {
  276. controller: 'car.add',
  277. 'token': token,
  278. "goods_id": that.data.order.goods_id,
  279. "quantity": that.data.order.quantity,
  280. "sku_str": that.data.order.sku_str,
  281. "buy_type": that.data.order.buy_type,
  282. "pin_id": that.data.order.pin_id,
  283. },
  284. dataType: 'json',
  285. success: function (res) {
  286. if (res.data.code == 3) {
  287. wx.showToast({
  288. title: '库存不足',
  289. icon: 'loading',
  290. duration: 2000
  291. })
  292. } else if (res.data.code == 4) {
  293. wx.showToast({
  294. title: '您未登录',
  295. icon: 'loading',
  296. duration: 2000
  297. })
  298. }
  299. else if (res.data.code == 6) {
  300. var msg = res.data.msg;
  301. wx.showToast({
  302. title: msg,
  303. duration: 2000
  304. })
  305. }
  306. else if (res.data.code == 5) {
  307. that.setData({
  308. hide_new: false
  309. })
  310. }
  311. else {
  312. var pages_all = getCurrentPages();
  313. if (pages_all.length > 3) {
  314. wx.redirectTo({
  315. url: '/Snailfish_shop/pages/buy/index?type=' + that.data.order.buy_type
  316. })
  317. } else {
  318. wx.navigateTo({
  319. url: '/Snailfish_shop/pages/buy/index?type=' + that.data.order.buy_type
  320. })
  321. }
  322. }
  323. }
  324. })
  325. },
  326. selectSku: function (event) {
  327. var that = this;
  328. let str = event.currentTarget.dataset.type;
  329. let obj = str.split("_");
  330. let arr = that.data.sku;
  331. let temp = {
  332. name: obj[3],
  333. id: obj[2],
  334. index: obj[0],
  335. idx: obj[1]
  336. };
  337. arr.splice(obj[0], 1, temp);
  338. that.setData({
  339. sku: arr
  340. })
  341. var id = '';
  342. for (let i = 0; i < arr.length; i++) {
  343. if (i == arr.length - 1) {
  344. id = id + arr[i]['id'];
  345. } else {
  346. id = id + arr[i]['id'] + "_";
  347. }
  348. }
  349. app.util.request({
  350. 'url': 'entry/wxapp/index',
  351. 'data': {
  352. controller: 'goods.get_goods_option_data',
  353. 'token': token,
  354. "id": that.data.goods.goods_id,
  355. "sku_str": id
  356. },
  357. dataType: 'json',
  358. success: function (res) {
  359. var goods = that.data.goods;
  360. goods.quantity = res.data.data.value.quantity;
  361. goods.image = res.data.data.value.image;
  362. goods.price = res.data.data.value.pin_price;
  363. that.setData({
  364. goods: goods
  365. })
  366. }
  367. })
  368. },
  369. openSku: function () {
  370. var that = this;
  371. if (that.data.skustate == 1) {
  372. that.setData({
  373. skustate: 0
  374. })
  375. } else {
  376. that.setData({
  377. skustate: 1
  378. })
  379. }
  380. },
  381. goGoods: function (event) {
  382. let obj = event.currentTarget.dataset.type;
  383. let data = this.data.list[obj];
  384. var goods_id = this.data.goods.goods_id;
  385. wx.setStorage({
  386. key: "goods",
  387. data: data,
  388. success: function () {
  389. var pages_all = getCurrentPages();
  390. if (pages_all.length > 3) {
  391. wx.redirectTo({
  392. url: '/Snailfish_shop/pages/goods/index?id=' + goods_id
  393. })
  394. } else {
  395. wx.navigateTo({
  396. url: '/Snailfish_shop/pages/goods/index?id=' + goods_id
  397. })
  398. }
  399. }
  400. })
  401. },
  402. goOrders: function () {
  403. var pages_all = getCurrentPages();
  404. if (pages_all.length > 3) {
  405. wx.redirectTo({
  406. url: '/Snailfish_shop/pages/order/order?id=' + this.data.order_id
  407. })
  408. } else {
  409. wx.navigateTo({
  410. url: '/Snailfish_shop/pages/order/order?id=' + this.data.order_id
  411. })
  412. }
  413. },
  414. goGoods2: function (e) {
  415. var goods_id = e.currentTarget.dataset.type;
  416. var pages_all = getCurrentPages();
  417. if (pages_all.length > 3) {
  418. wx.redirectTo({
  419. url: '/Snailfish_shop/pages/goods/index?id=' + goods_id
  420. })
  421. } else {
  422. wx.navigateTo({
  423. url: '/Snailfish_shop/pages/goods/index?id=' + goods_id
  424. })
  425. }
  426. },
  427. goIndex: function (e) {
  428. var pages_all = getCurrentPages();
  429. if (pages_all.length > 3) {
  430. wx.redirectTo({
  431. url: '/Snailfish_shop/pages/index/index'
  432. })
  433. } else {
  434. wx.navigateTo({
  435. url: '/Snailfish_shop/pages/index/index'
  436. })
  437. }
  438. },
  439. goLink: function (e) {
  440. var link = e.currentTarget.dataset.link;
  441. wx.reLaunch({
  442. url: link,
  443. })
  444. },
  445. setNum: function (event) {
  446. let types = event.currentTarget.dataset.type;
  447. var num = 1;
  448. if (types == 'add') {
  449. num = this.data.order.quantity + 1;
  450. } else if (types == 'decrease') {
  451. if (this.data.order.quantity > 1) {
  452. num = this.data.order.quantity - 1;
  453. }
  454. }
  455. var order = this.data.order;
  456. order.quantity = num;
  457. this.setData({
  458. order: order
  459. })
  460. },
  461. onShareAppMessage: function (res) {
  462. var that = this;
  463. var share_id = wx.getStorageSync('member_id');
  464. var share_path = 'Snailfish_shop/pages/share/index?id=' + that.data.order_id + '&share_id=' + share_id;
  465. return {
  466. title: that.data.share_title,
  467. path: share_path,
  468. success: function (res) {
  469. // 转发成功
  470. },
  471. fail: function (res) {
  472. // 转发失败
  473. }
  474. }
  475. }
  476. })