store.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. (function(global,factory){
  2. typeof define == 'function' && define('store',['axios','helper'],factory);
  3. })(this,function(axios,$h){
  4. axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
  5. axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
  6. return {
  7. baseGet:function(url,successCallback,errorCallback,isMsg){
  8. axios.get(url).then(function(res){
  9. if(res.status == 200 && res.data.code == 200){
  10. successCallback && successCallback(res);
  11. }else{
  12. var err = res.data.msg || '请求失败!';
  13. errorCallback && errorCallback(err);
  14. isMsg || $h.pushMsgOnce(err);
  15. }
  16. }).catch(function(err){
  17. errorCallback && errorCallback(err);
  18. $h.pushMsgOnce(err)
  19. });
  20. },
  21. basePost:function(url,data,successCallback,errorCallback,isMsg){
  22. axios.post(url,data).then(function(res){
  23. if(res.status == 200 && res.data.code == 200){
  24. successCallback && successCallback(res);
  25. }else{
  26. var err = res.data.msg || '请求失败!';
  27. errorCallback && errorCallback(err);
  28. isMsg || $h.pushMsgOnce(err);
  29. }
  30. }).catch(function(err){
  31. errorCallback && errorCallback(err);
  32. $h.pushMsgOnce(err)
  33. });
  34. },
  35. setCart:function(opt,successCallback,errorCallback){
  36. axios.get($h.U({
  37. c:"auth_api",
  38. a:"set_cart",
  39. p:opt
  40. })).then(function(res){
  41. if(res.status == 200 && res.data.code == 200)
  42. successCallback && successCallback();
  43. else{
  44. var error = res.data.msg || '加入购物车失败!';
  45. errorCallback && errorCallback(error);
  46. $h.pushMsg(error);
  47. }
  48. }).catch(function(err){
  49. errorCallback && errorCallback(err);
  50. $h.pushMsg(err);
  51. });
  52. },
  53. goBuy:function(opt,successCallback,errorCallback){
  54. axios.get($h.U({
  55. c:"auth_api",
  56. a:"now_buy",
  57. p:opt
  58. })).then(function(res){
  59. if(res.status == 200 && res.data.code == 200)
  60. successCallback && successCallback(res.data.data.cartId);
  61. else{
  62. var error = res.data.msg || '订单生成失败!';
  63. errorCallback && errorCallback(error);
  64. $h.pushMsg(error);
  65. }
  66. }).catch(function(err){
  67. errorCallback && errorCallback(err);
  68. $h.pushMsg(err);
  69. });
  70. },
  71. addBargainShare:function(opt,successCallback,errorCallback){
  72. axios.get($h.U({
  73. c:"auth_api",
  74. a:"add_bargain_share",
  75. p:opt
  76. })).then(function(res){
  77. }).catch(function(err){
  78. });
  79. },
  80. likeProduct:function(productId,category,successCallback,errorCallback) {
  81. axios.get($h.U({
  82. c:"auth_api",
  83. a:"like_product",
  84. p:{productId:productId,category:category}
  85. })).then(function(res){
  86. if(res.status == 200 && res.data.code == 200){
  87. successCallback && successCallback(res.data);
  88. }else{
  89. var error = res.data.msg || '点赞失败!';
  90. errorCallback && errorCallback(error);
  91. $h.pushMsg(error);
  92. }
  93. }).catch(function(err){
  94. errorCallback && errorCallback(err);
  95. $h.pushMsg(err)
  96. });
  97. },
  98. bargainFriends:function(bargain,successCallback,errorCallback){
  99. this.basePost($h.U({
  100. c:'auth_api',
  101. a:'set_bargain_help'
  102. }),bargain,successCallback,errorCallback)
  103. },
  104. unlikeProduct:function(productId,category,successCallback,errorCallback) {
  105. axios.get($h.U({
  106. c:"auth_api",
  107. a:"unlike_product",
  108. p:{productId:productId,category:category}
  109. })).then(function(res){
  110. if(res.status == 200 && res.data.code == 200){
  111. successCallback && successCallback(res.data);
  112. }else{
  113. var error = res.data.msg || '取消点赞失败!';
  114. errorCallback && errorCallback(error);
  115. $h.pushMsg(error);
  116. }
  117. }).catch(function(err){
  118. errorCallback && errorCallback(err);
  119. $h.pushMsg(err)
  120. });
  121. },
  122. collectProduct(productId,category,successCallback,errorCallback){
  123. axios.get($h.U({
  124. c:'auth_api',
  125. a:'collect_product',
  126. p:{productId:productId,category:category}
  127. })).then(function(res){
  128. if(res.status == 200 && res.data.code == 200){
  129. successCallback && successCallback(res.data);
  130. }else{
  131. var error = res.data.msg || '收藏失败!';
  132. errorCallback && errorCallback(error);
  133. $h.pushMsg(error);
  134. }
  135. }).catch(function(err){
  136. errorCallback && errorCallback(err);
  137. $h.pushMsg(err)
  138. });
  139. },
  140. unCollectProduct(productId,category,successCallback,errorCallback){
  141. axios.get($h.U({
  142. c:'auth_api',
  143. a:'uncollect_product',
  144. p:{productId:productId,category:category}
  145. })).then(function(res){
  146. if(res.status == 200 && res.data.code == 200){
  147. successCallback && successCallback(res.data);
  148. }else{
  149. var error = res.data.msg || '取消收藏失败!';
  150. errorCallback && errorCallback(error);
  151. $h.pushMsg(error);
  152. }
  153. }).catch(function(err){
  154. errorCallback && errorCallback(err);
  155. $h.pushMsg(err)
  156. });
  157. },
  158. getCartNum:function(callback){
  159. axios.get($h.U({
  160. c:'auth_api',
  161. a:'get_cart_num'
  162. })).then(function(res){
  163. if(res.status == 200 && res.data.code == 200){
  164. callback && callback(res.data.data);
  165. }else{
  166. callback && callback(0);
  167. }
  168. }).catch(function(){
  169. callback && callback(0);
  170. });
  171. },
  172. changeCartNum:function(cartId,cartNum,successCallback,errorCallback){
  173. this.baseGet($h.U({
  174. c:'auth_api',
  175. a:'change_cart_num',
  176. p:{cartId:cartId,cartNum:cartNum}
  177. }),successCallback,errorCallback);
  178. },
  179. getCartList:function(successCallback,errorCallback){
  180. axios.get($h.U({
  181. c:'auth_api',
  182. a:'get_cart_list'
  183. })).then(function(res){
  184. if(res.status == 200 && res.data.code == 200){
  185. successCallback && successCallback(res.data.data,res.data);
  186. }else{
  187. var error = res.data.msg || '获取购物车数据失败!';
  188. errorCallback && errorCallback(error);
  189. $h.pushMsg(error);
  190. }
  191. }).catch(function(err){
  192. errorCallback && errorCallback(err);
  193. $h.pushMsg(err)
  194. });
  195. },
  196. removeCart:function(cartId,successCallback,errorCallback){
  197. axios.get($h.U({
  198. c:'auth_api',
  199. a:'remove_cart',
  200. p:{ids:cartId}
  201. })).then(function(res){
  202. if(res.status == 200 && res.data.code == 200){
  203. successCallback && successCallback(res.data.data,res.data);
  204. }else{
  205. var error = res.data.msg || '删除失败!';
  206. errorCallback && errorCallback(error);
  207. $h.pushMsg(error);
  208. }
  209. }).catch(function(err){
  210. errorCallback && errorCallback(err);
  211. $h.pushMsg(err)
  212. });
  213. },
  214. getUseCoupon:function(successCallback,errorCallback){
  215. this.baseGet($h.U({
  216. c:'auth_api',
  217. a:'get_use_coupon'
  218. }),successCallback,errorCallback);
  219. },
  220. getArticleList:function(p,successCallback,errorCallback){
  221. this.baseGet($h.U({
  222. c:'public_api',
  223. a:'get_cid_article',
  224. p:p
  225. }),successCallback,errorCallback)
  226. },
  227. getVideoList:function(p,successCallback,errorCallback){
  228. this.baseGet($h.U({
  229. c:'public_api',
  230. a:'get_video_list',
  231. p:p
  232. }),successCallback,errorCallback)
  233. },
  234. getCollectProduct:function(p,successCallback,errorCallback){
  235. this.baseGet($h.U({
  236. c:'auth_api',
  237. a:'get_user_collect_product',
  238. p:p
  239. }),successCallback,errorCallback)
  240. },
  241. removeCollectProduct:function(productId,successCallback,errorCallback){
  242. this.baseGet($h.U({
  243. c:'auth_api',
  244. a:'remove_user_collect_product',
  245. p:{productId:productId}
  246. }),successCallback,errorCallback)
  247. },
  248. editUserAddress:function(addressInfo,successCallback,errorCallback){
  249. this.basePost($h.U({
  250. c:'auth_api',
  251. a:'edit_user_address'
  252. }),addressInfo,successCallback,errorCallback)
  253. },
  254. getUserDefaultAddress:function(successCallback,errorCallback){
  255. this.baseGet($h.U({
  256. c:'auth_api',
  257. a:'user_default_address'
  258. }),successCallback,errorCallback)
  259. },
  260. setUserDefaultAddress:function(addressId,successCallback,errorCallback){
  261. this.baseGet($h.U({
  262. c:'auth_api',
  263. a:'set_user_default_address',
  264. p:{addressId:addressId}
  265. }),successCallback,errorCallback)
  266. },
  267. removeUserAddress:function(addressId,successCallback,errorCallback){
  268. this.baseGet($h.U({
  269. c:'auth_api',
  270. a:'remove_user_address',
  271. p:{addressId:addressId}
  272. }),successCallback,errorCallback)
  273. },
  274. submitOrder:function(key,order,successCallback,errorCallback){
  275. this.basePost($h.U({
  276. c:'auth_api',
  277. a:'create_order',
  278. p:{key:key}
  279. }),order,successCallback,errorCallback)
  280. },
  281. getUserOrderList:function(p,successCallback,errorCallback){
  282. this.baseGet($h.U({
  283. c:'auth_api',
  284. a:'get_user_order_list',
  285. p:p
  286. }),successCallback,errorCallback);
  287. },
  288. removeUserOrder:function(uni,successCallback,errorCallback){
  289. this.baseGet($h.U({
  290. c:'auth_api',
  291. a:'user_remove_order',
  292. p:{uni:uni}
  293. }),successCallback,errorCallback);
  294. },
  295. payOrder:function(uni,successCallback,errorCallback){
  296. this.baseGet($h.U({
  297. c:'auth_api',
  298. a:'pay_order',
  299. p:{uni:uni}
  300. }),successCallback,errorCallback);
  301. },
  302. orderApplyRefund:function(uni,text,successCallback,errorCallback){
  303. this.baseGet($h.U({
  304. c:'auth_api',
  305. a:'apply_order_refund',
  306. p:{uni:uni,text:text}
  307. }),successCallback,errorCallback);
  308. },
  309. orderDetails:function(uni,successCallback,errorCallback){
  310. this.baseGet($h.U({
  311. c:'auth_api',
  312. a:'order_details',
  313. p:{uni:uni}
  314. }),successCallback,errorCallback);
  315. },
  316. userTakeOrder:function(uni,successCallback,errorCallback) {
  317. this.baseGet($h.U({
  318. c:'auth_api',
  319. a:'user_take_order',
  320. p:{uni:uni}
  321. }),successCallback,errorCallback);
  322. },
  323. getProductCategory:function(successCallback,errorCallback) {
  324. this.baseGet($h.U({
  325. c:'auth_api',
  326. a:'get_product_category'
  327. }),successCallback,errorCallback);
  328. },
  329. userCommentProduct:function(unique,data,successCallback,errorCallback){
  330. this.basePost($h.U({
  331. c:'auth_api',
  332. a:'user_comment_product',
  333. p:{unique:unique}
  334. }),data,successCallback,errorCallback)
  335. },
  336. getSpreadList:function(p,successCallback,errorCallback){
  337. this.baseGet($h.U({
  338. c:'auth_api',
  339. a:'get_spread_list',
  340. p:p
  341. }),successCallback,errorCallback);
  342. },
  343. getProductList:function(search,successCallback,errorCallback){
  344. this.baseGet($h.U({
  345. c:'auth_api',
  346. a:'get_product_list',
  347. p:search
  348. }),successCallback,errorCallback);
  349. },
  350. getUserBalanceList:function(p,successCallback,errorCallback){
  351. this.baseGet($h.U({
  352. c:'auth_api',
  353. a:'user_balance_list',
  354. p:p
  355. }),successCallback,errorCallback);
  356. },
  357. getUserIntegralList:function(p,successCallback,errorCallback){
  358. this.baseGet($h.U({
  359. c:'auth_api',
  360. a:'user_integral_list',
  361. p:p
  362. }),successCallback,errorCallback);
  363. },
  364. getProductReply:function(p,successCallback,errorCallback){
  365. this.baseGet($h.U({
  366. c:'auth_api',
  367. a:'product_reply_list',
  368. p:p
  369. }),successCallback,errorCallback);
  370. },
  371. getUserAddress:function(successCallback,errorCallback){
  372. this.baseGet($h.U({
  373. c:'auth_api',
  374. a:'user_address_list'
  375. }),successCallback,errorCallback);
  376. },
  377. getProductAttr:function(productId,successCallback,errorCallback){
  378. this.baseGet($h.U({
  379. c:'auth_api',
  380. a:'product_attr_detail',
  381. p:{productId:productId}
  382. }),successCallback,errorCallback);
  383. },
  384. userWechatRecharge:function(price,successCallback,errorCallback) {
  385. this.baseGet($h.U({
  386. c:'auth_api',
  387. a:'user_wechat_recharge',
  388. p:{price:price}
  389. }),successCallback,errorCallback);
  390. },
  391. getNoticeList:function(p,successCallback,errorCallback){
  392. this.baseGet($h.U({
  393. c:'auth_api',
  394. a:'get_notice_list',
  395. p:p
  396. }),successCallback,errorCallback);
  397. },
  398. seeNotice: function(p,successCallback,errorCallback){
  399. this.baseGet($h.U({
  400. c:'auth_api',
  401. a:'see_notice',
  402. p:p
  403. }),successCallback,errorCallback);
  404. },
  405. getIssueCouponList:function(limit,successCallback,errorCallback){
  406. this.baseGet($h.U({
  407. c:'auth_api',
  408. a:'get_issue_coupon_list',
  409. p:{limit:limit}
  410. }),successCallback,errorCallback);
  411. },
  412. getCategoryProductList:function(limit,successCallback,errorCallback){
  413. this.baseGet($h.U({
  414. c:'public_api',
  415. a:'get_category_product_list',
  416. p:{limit:limit}
  417. }),successCallback,errorCallback);
  418. },
  419. getBestProductList:function(p,successCallback,errorCallback){
  420. this.baseGet($h.U({
  421. c:'public_api',
  422. a:'get_best_product_list',
  423. p:p
  424. }),successCallback,errorCallback);
  425. },
  426. userGetCoupon:function(couponId,successCallback,errorCallback){
  427. this.baseGet($h.U({
  428. c:'auth_api',
  429. a:'user_get_coupon',
  430. p:{couponId:couponId}
  431. }),successCallback,errorCallback);
  432. },
  433. isLogin:function(){
  434. return $h.getCookie('is_login') == 1;
  435. },
  436. goLogin:function(){
  437. if(!this.isLogin()){
  438. $h.pushMsg('未登录,立即登陆',function(){
  439. location.href = $h.U({
  440. c:'login',
  441. a:'index',
  442. p:{ref:window.btoa(unescape(encodeURIComponent( location.href )))}
  443. });
  444. });
  445. return false;
  446. }
  447. return true;
  448. },
  449. wechatUploadImg:function(wxApi,count,successCallback,errorCallback){
  450. wxApi.chooseImage({count:count,sizeType:['compressed']},function(localIds){
  451. $h.prompt('图片上传中...');
  452. wxApi.uploadImage(localIds,function(serverIds){
  453. axios.get($h.U({
  454. c:"public_api",
  455. a:"wechat_media_id_by_image",
  456. p:{mediaIds:serverIds}
  457. })).then(function(result){
  458. $h.promptClear();
  459. if(result.status == 200 && result.data.code == 200)
  460. return Promise.resolve(result.data.data);
  461. else
  462. return Promise.reject('上传失败!');
  463. }).then(function(picList){
  464. if(!picList) return Promise.reject('请选择上传图片!');
  465. successCallback && successCallback(picList);
  466. }).catch(function(err){
  467. $h.promptClear();
  468. $h.pushMsgOnce(err);
  469. errorCallback && errorCallback(err);
  470. });
  471. })
  472. });
  473. }
  474. }
  475. });