AuthApi.php 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983
  1. <?php
  2. namespace app\routine\controller;
  3. use Api\Express;
  4. use app\routine\model\routine\RoutineCode;
  5. use app\routine\model\routine\RoutineFormId;
  6. use app\routine\model\routine\RoutineTemplate;
  7. use app\routine\model\store\StoreCombination;
  8. use behavior\routine\RoutineBehavior;
  9. use service\JsonService;
  10. use service\GroupDataService;
  11. use service\RoutineBizDataCrypt;
  12. use service\SystemConfigService;
  13. use service\UploadService;
  14. use service\UtilService;
  15. use think\Request;
  16. use behavior\wap\StoreProductBehavior;
  17. use service\WechatTemplateService;
  18. use service\CacheService;
  19. use service\HookService;
  20. use think\Url;
  21. use app\routine\model\store\StoreCouponUser;
  22. use app\routine\model\store\StoreOrder;
  23. use app\routine\model\store\StoreProductRelation;
  24. use app\routine\model\store\StoreProductAttr;
  25. use app\routine\model\store\StoreProductAttrValue;
  26. use app\routine\model\store\StoreProductReply;
  27. use app\routine\model\store\StoreCart;
  28. use app\routine\model\store\StoreCategory;
  29. use app\routine\model\store\StoreProduct;
  30. use app\routine\model\store\StoreSeckill;
  31. use app\routine\model\user\User;
  32. use app\routine\model\user\UserNotice;
  33. use app\routine\model\store\StoreCouponIssue;
  34. use app\routine\model\store\StoreCouponIssueUser;
  35. use app\routine\model\store\StoreOrderCartInfo;
  36. use app\routine\model\store\StorePink;
  37. use app\routine\model\store\StoreService;
  38. use app\routine\model\store\StoreServiceLog;
  39. use app\routine\model\user\UserAddress;
  40. use app\routine\model\user\UserBill;
  41. use app\routine\model\user\UserExtract;
  42. use app\routine\model\user\UserRecharge;
  43. use app\routine\model\user\UserSign;
  44. use app\routine\model\user\WechatUser;
  45. use app\admin\model\system\SystemConfig;
  46. use app\routine\model\store\StoreBargain;
  47. use app\routine\model\store\StoreBargainUser;
  48. use app\routine\model\store\StoreBargainUserHelp;
  49. use app\routine\model\article\Article as ArticleModel;
  50. /**
  51. * 小程序接口
  52. * Class AuthApi
  53. * @package app\routine\controller
  54. *
  55. *
  56. *
  57. *
  58. * https://qipei.9gt.net/routine/auth_api/index 首页
  59. * https://qipei.9gt.net/routine/auth_api/store 分类页面
  60. * https://qipei.9gt.net/routine/auth_api/get_pid_cate 一级分类
  61. * https://qipei.9gt.net/routine/auth_api/get_id_cate 二级分类
  62. * https://qipei.9gt.net/routine/auth_api/get_product_list 分类页面产品
  63. */
  64. class AuthApi extends AuthController{
  65. /**
  66. * 获取用户信息
  67. * @return \think\response\Json
  68. */
  69. public function get_user_info(){
  70. return JsonService::successful($this->userInfo);
  71. }
  72. /**
  73. * 获取退款理由
  74. */
  75. public function get_refund_reason(){
  76. $reason = SystemConfig::getValue('refund_reason')?:[];//退款理由
  77. $reason = explode('=',$reason);
  78. return JsonService::successful($reason);
  79. }
  80. /**
  81. * 获取提现银行
  82. */
  83. public function get_user_extract_bank(){
  84. $extractBank = SystemConfig::getValue('user_extract_bank')?:[];//提现银行
  85. $extractBank = explode('=',$extractBank);
  86. return JsonService::successful($extractBank);
  87. }
  88. /**
  89. * 首页
  90. */
  91. public function index(){
  92. $banner = GroupDataService::getData('routine_home_banner')?:[];//banner图
  93. $menus = GroupDataService::getData('routine_home_menus')?:[];//banner图
  94. $lovely = GroupDataService::getData('routine_lovely')?:[];//猜你喜欢图
  95. $best = StoreProduct::getBestProduct('id,image,store_name,cate_id,price,unit_name,sort',8);//精品推荐
  96. $new = StoreProduct::getNewProduct('id,image,store_name,cate_id,price,unit_name,sort',3);//今日上新
  97. $hot = StoreProduct::getHotProduct('id,image,store_name,cate_id,price,unit_name,sort',6);//猜你喜欢
  98. $data['banner'] = $banner;
  99. $data['lovely'] = $lovely;
  100. $data['menus'] = $menus;
  101. $data['best'] = $best;
  102. $data['new'] = $new;
  103. $data['hot'] = $hot;
  104. return JsonService::successful($data);
  105. }
  106. /**
  107. * 猜你喜欢 加载
  108. * @param Request $request
  109. */
  110. public function get_hot_product(Request $request){
  111. $data = UtilService::postMore([['offset',0],['limit',0]],$request);
  112. $hot = StoreProduct::getHotProductLoading('id,image,store_name,cate_id,price,unit_name,sort',$data['offset'],$data['limit']);//猜你喜欢
  113. return JsonService::successful($hot);
  114. }
  115. /**
  116. * 分类搜索页面
  117. * @param Request $request
  118. * @return \think\response\Json
  119. */
  120. public function store(){
  121. $model = StoreProduct::validWhere();
  122. if($_GET){$data = $_GET['value'];
  123. if($data!=''){
  124. $model = $model->where('store_name','LIKE',"%$data%")->whereOr('keyword','LIKE',"%$data%");
  125. if((int)$data) $model = $model->whereOr('id',$data);
  126. }
  127. $list = $model->field('id,store_name,cate_id,image,sales,price,stock')->select()->toArray();
  128. return JsonService::successful($list);
  129. }
  130. }
  131. /**
  132. * 分类页面
  133. * @param Request $request
  134. * @return \think\response\Json
  135. */
  136. public function store1(Request $request){
  137. $data = UtilService::postMore([['keyword',''],['cid',''],['sid','']],$request);
  138. $keyword = addslashes($data['keyword']);
  139. $cid = intval($data['cid']);
  140. $sid = intval($data['sid']);
  141. $category = null;
  142. if($sid) $category = StoreCategory::get($sid);
  143. if($cid && !$category) $category = StoreCategory::get($cid);
  144. $data['keyword'] = $keyword;
  145. $data['cid'] = $cid;
  146. $data['sid'] = $sid;
  147. return JsonService::successful($data);
  148. }
  149. /**
  150. * 一级分类
  151. * @return \think\response\Json
  152. */
  153. public function get_pid_cate(){
  154. $data = StoreCategory::pidByCategory(0,'id,cate_name');//一级分类
  155. return JsonService::successful($data);
  156. }
  157. /**
  158. * 最小提现金额
  159. * @return \think\response\Json
  160. */
  161. public function minmoney(){
  162. $data = SystemConfig::getValue('user_extract_min_price');//最小提现金额
  163. return JsonService::successful($data);
  164. }
  165. /**
  166. * 二级分类
  167. * @param Request $request
  168. * @return \think\response\Json
  169. */
  170. public function get_id_cate(Request $request){
  171. $data = UtilService::postMore([['id',0]],$request);
  172. $dataCateA = [];
  173. $dataCateA[0]['id'] = $data['id'];
  174. $dataCateA[0]['cate_name'] = '全部商品';
  175. $dataCateA[0]['pid'] = 0;
  176. $dataCateE = StoreCategory::pidBySidList($data['id']);//根据一级分类获取二级分类
  177. if($dataCateE) $dataCateE = $dataCateE->toArray();
  178. $dataCate = [];
  179. $dataCate = array_merge_recursive($dataCateA,$dataCateE);
  180. return JsonService::successful($dataCate);
  181. }
  182. /**
  183. * 分类页面产品
  184. * @param string $keyword
  185. * @param int $cId
  186. * @param int $sId
  187. * @param string $priceOrder
  188. * @param string $salesOrder
  189. * @param int $news
  190. * @param int $first
  191. * @param int $limit
  192. * @return \think\response\Json
  193. */
  194. public function get_product_list(Request $request)
  195. {
  196. $data = UtilService::getMore([
  197. ['sid',0],
  198. ['cid',0],
  199. ['keyword',''],
  200. ['priceOrder',''],
  201. ['salesOrder',''],
  202. ['news',0],
  203. ['first',0],
  204. ['limit',0]
  205. ],$request);
  206. $sId = $data['sid'];
  207. $cId = $data['cid'];
  208. $keyword = $data['keyword'];
  209. $priceOrder = $data['priceOrder'];
  210. $salesOrder = $data['salesOrder'];
  211. $news = $data['news'];
  212. $first = $data['first'];
  213. $limit = $data['limit'];
  214. $model = StoreProduct::validWhere();
  215. if($sId){
  216. $model->where('cate_id',$sId);
  217. }elseif($cId){
  218. $sids = StoreCategory::pidBySidList($cId)?:[];
  219. if($sids){
  220. $sidsr = [];
  221. foreach($sids as $v){
  222. $sidsr[] = $v['id'];
  223. }
  224. $model->where('cate_id','IN',$sidsr);
  225. }
  226. }
  227. if(!empty($keyword)) $model->where('keyword|store_name','LIKE',htmlspecialchars("%$keyword%"));
  228. if($news!=0) $model->where('is_new',1);
  229. $baseOrder = '';
  230. if($priceOrder) $baseOrder = $priceOrder == 'desc' ? 'price DESC' : 'price ASC';
  231. if($salesOrder) $baseOrder = $salesOrder == 'desc' ? 'sales DESC' : 'sales ASC';
  232. if($baseOrder) $baseOrder .= ', ';
  233. $model->order($baseOrder.'sort DESC, add_time DESC');
  234. $list = $model->limit($first,$limit)->field('id,store_name,cate_id,image,sales,price,stock')->select()->toArray();
  235. return JsonService::successful($list);
  236. }
  237. /**
  238. * 购物车
  239. * @return \think\response\Json
  240. */
  241. public function get_cart_list(){
  242. return JsonService::successful(StoreCart::getUserProductCartList($this->userInfo['uid']));
  243. }
  244. /**
  245. * 商品详情页
  246. * @param Request $request
  247. */
  248. public function details(Request $request){
  249. $data = UtilService::postMore(['id'],$request);
  250. $id = $data['id'];
  251. if(!$id || !($storeInfo = StoreProduct::getValidProduct($id))) return JsonService::fail('商品不存在或已下架');
  252. $storeInfo['userCollect'] = StoreProductRelation::isProductRelation($id,$this->userInfo['uid'],'collect');
  253. list($productAttr,$productValue) = StoreProductAttr::getProductAttrDetail($id);
  254. setView($this->userInfo['uid'],$id,$storeInfo['cate_id'],'viwe');
  255. foreach ($productAttr as $k=>$v){
  256. $attr = $v['attr_values'];
  257. // unset($productAttr[$k]['attr_values']);
  258. foreach ($attr as $kk=>$vv){
  259. $productAttr[$k]['attr_value'][$kk]['attr'] = $vv;
  260. $productAttr[$k]['attr_value'][$kk]['check'] = false;
  261. }
  262. }
  263. $data['storeInfo'] = $storeInfo;
  264. $data['similarity'] = StoreProduct::cateIdBySimilarityProduct($storeInfo['cate_id'],'id,store_name,image,price,sales',4);
  265. $data['productAttr'] = $productAttr;
  266. $data['productValue'] = $productValue;
  267. $data['reply'] = StoreProductReply::getRecProductReply($storeInfo['id']);
  268. $data['replyCount'] = StoreProductReply::productValidWhere()->where('product_id',$storeInfo['id'])->count();
  269. $data['mer_id'] = StoreProduct::where('id',$storeInfo['id'])->value('mer_id');
  270. return JsonService::successful($data);
  271. }
  272. /**
  273. * 获取产品评论
  274. * @param int $productId
  275. * @return \think\response\Json
  276. */
  277. public function get_product_reply($productId = 0){
  278. if(!$productId) return JsonService::fail('参数错误');
  279. $replyCount = StoreProductReply::productValidWhere()->where('product_id',$productId)->count();
  280. $reply = StoreProductReply::getRecProductReply($productId);
  281. return JsonService::successful(['replyCount'=>$replyCount,'reply'=>$reply]);
  282. }
  283. /**
  284. * 订单页面
  285. * @param Request $request
  286. * @return \think\response\Json
  287. */
  288. public function confirm_order(Request $request){
  289. $data = UtilService::postMore(['cartId'],$request);
  290. $cartId = $data['cartId'];
  291. if(!is_string($cartId) || !$cartId ) return JsonService::fail('请提交购买的商品');
  292. $cartGroup = StoreCart::getUserProductCartList($this->userInfo['uid'],$cartId,1);
  293. if(count($cartGroup['invalid'])) return JsonService::fail($cartGroup['invalid'][0]['productInfo']['store_name'].'已失效!');
  294. if(!$cartGroup['valid']) return JsonService::fail('请提交购买的商品');
  295. $cartInfo = $cartGroup['valid'];
  296. $priceGroup = StoreOrder::getOrderPriceGroup($cartInfo);
  297. $other = [
  298. 'offlinePostage'=>SystemConfigService::get('offline_postage'),
  299. 'integralRatio'=>SystemConfigService::get('integral_ratio')
  300. ];
  301. $usableCoupon = StoreCouponUser::beUsableCoupon($this->userInfo['uid'],$priceGroup['totalPrice']);
  302. $cartIdA = explode(',',$cartId);
  303. if(count($cartIdA) > 1) $seckill_id=0;
  304. else{
  305. $seckillinfo = StoreCart::where('id',$cartId)->find();
  306. if((int)$seckillinfo['seckill_id']>0) $seckill_id=$seckillinfo['seckill_id'];
  307. else $seckill_id=0;
  308. }
  309. $data['usableCoupon'] = $usableCoupon;
  310. $data['seckill_id'] = $seckill_id;
  311. $data['cartInfo'] = $cartInfo;
  312. $data['priceGroup'] = $priceGroup;
  313. $data['orderKey'] = StoreOrder::cacheOrderInfo($this->userInfo['uid'],$cartInfo,$priceGroup,$other);
  314. $data['offlinePostage'] = $other['offlinePostage'];
  315. $data['userInfo'] = User::getUserInfo($this->userInfo['uid']);
  316. $data['integralRatio'] = $other['integralRatio'];
  317. return JsonService::successful($data);
  318. }
  319. /**
  320. * 获取可以使用的优惠券
  321. * @param int $totalPrice
  322. * @return \think\response\Json
  323. */
  324. public function get_use_coupon_order($totalPrice = 0){
  325. return JsonService::successful(StoreCouponUser::beUsableCouponList($this->userInfo['uid'],$totalPrice));
  326. }
  327. /**
  328. * 秒杀列表页
  329. * @return \think\response\Json
  330. */
  331. public function seckill_index(){
  332. $lovely = GroupDataService::getData('routine_lovely')?:[];//banner图
  333. $seckill = StoreSeckill::where('is_del',0)->where('status',1)->where('start_time','<',time())->where('stop_time','>',time())->order('sort desc')->select()->toArray();
  334. $data['seckill'] = $seckill;
  335. $data['lovely'] = $lovely;
  336. return JsonService::successful($data);
  337. }
  338. /**
  339. * 秒杀详情页
  340. * @param Request $request
  341. * @return \think\response\Json
  342. */
  343. public function seckill_detail(Request $request){
  344. $data = UtilService::postMore(['id'],$request);
  345. $id = $data['id'];
  346. if(!$id || !($storeInfo = StoreSeckill::getValidProduct($id))) return JsonService::fail('商品不存在或已下架!');
  347. $storeInfo['userLike'] = StoreProductRelation::isProductRelation($storeInfo['product_id'],$this->userInfo['uid'],'like','product_seckill');
  348. $storeInfo['like_num'] = StoreProductRelation::productRelationNum($storeInfo['product_id'],'like','product_seckill');
  349. $storeInfo['userCollect'] = StoreProductRelation::isProductRelation($storeInfo['product_id'],$this->userInfo['uid'],'collect','product_seckill');
  350. $data['storeInfo'] = $storeInfo;
  351. setView($this->userInfo['uid'],$id,$storeInfo['product_id'],'viwe');
  352. $data['reply'] = StoreProductReply::getRecProductReply($storeInfo['product_id']);
  353. $data['replyCount'] = StoreProductReply::productValidWhere()->where('product_id',$storeInfo['id'])->count();
  354. return JsonService::successful($data);
  355. }
  356. /**
  357. * 个人中心
  358. * @return \think\response\Json
  359. */
  360. public function my(){
  361. $this->userInfo['couponCount'] = StoreCouponUser::getUserValidCouponCount($this->userInfo['uid']);
  362. $this->userInfo['like'] = StoreProductRelation::getUserIdCollect($this->userInfo['uid']);;
  363. $this->userInfo['orderStatusNum'] = StoreOrder::getOrderStatusNum($this->userInfo['uid']);
  364. $this->userInfo['notice'] = UserNotice::getNotice($this->userInfo['uid']);
  365. $this->userInfo['statu'] = (int)SystemConfig::getValue('store_brokerage_statu');
  366. // $this->userInfo['service_phone'] = SystemConfig::getValue('service_phone');
  367. // $this->userInfo['service_phone_str'] = SystemConfig::getValue('service_phone_str');
  368. return JsonService::successful($this->userInfo);
  369. }
  370. /**
  371. * 用户签到
  372. * @return \think\response\Json
  373. */
  374. public function user_sign()
  375. {
  376. $signed = UserSign::checkUserSigned($this->userInfo['uid']);
  377. if($signed) return JsonService::fail('已签到');
  378. if(false !== $integral = UserSign::sign($this->userInfo))
  379. return JsonService::successful('签到获得'.floatval($integral).'积分');
  380. else
  381. return JsonService::fail('签到失败!');
  382. }
  383. /**
  384. * 过度查$uniqueId
  385. * @param string $productId
  386. * @param int $cartNum
  387. * @param string $uniqueId
  388. * @return \think\response\Json
  389. */
  390. public function unique(){
  391. $productId=$_GET['productId'];
  392. if(!$productId || !is_numeric($productId)) return JsonService::fail('参数错误');
  393. $uniqueId=StoreProductAttrValue::where('product_id',$productId)->value('unique');
  394. $data=$this->set_cart($productId,$cartNum = 1,$uniqueId);
  395. if($data==true){
  396. return JsonService::successful('ok');
  397. }
  398. }
  399. /**
  400. * 加入到购物车
  401. * @param string $productId
  402. * @param int $cartNum
  403. * @param string $uniqueId
  404. * @return \think\response\Json
  405. */
  406. public function set_cart($productId = '',$cartNum = 1,$uniqueId = ''){
  407. if(!$productId || !is_numeric($productId)) return JsonService::fail('参数错误');
  408. $res = StoreCart::setCart($this->userInfo['uid'],$productId,$cartNum,$uniqueId,'product');
  409. if(!$res) return JsonService::fail(StoreCart::getErrorInfo());
  410. else{
  411. // HookService::afterListen('store_product_set_cart_after',$res,$this->userInfo,false,StoreProductBehavior::class);
  412. return JsonService::successful('ok',['cartId'=>$res->id]);
  413. }
  414. }
  415. /**
  416. * 拼团 秒杀 砍价 加入到购物车
  417. * @param string $productId
  418. * @param int $cartNum
  419. * @param string $uniqueId
  420. * @param int $combinationId
  421. * @param int $secKillId
  422. * @return \think\response\Json
  423. */
  424. public function now_buy($productId = '',$cartNum = 1,$uniqueId = '',$combinationId = 0,$secKillId = 0,$bargainId = 0){
  425. if(!$productId || !is_numeric($productId)) return JsonService::fail('参数错误');
  426. if($bargainId && StoreBargainUserHelp::getSurplusPrice($bargainId,$this->userInfo['uid'])) return JsonService::fail('请先砍价');
  427. $res = StoreCart::setCart($this->userInfo['uid'],$productId,$cartNum,$uniqueId,'product',1,$combinationId,$secKillId,$bargainId);
  428. if(!$res) return JsonService::fail(StoreCart::getErrorInfo());
  429. else return JsonService::successful('ok',['cartId'=>$res->id]);
  430. }
  431. /**
  432. * 添加点赞
  433. * @param string $productId
  434. * @param string $category
  435. * @return \think\response\Json
  436. */
  437. public function like_product($productId = '',$category = 'product'){
  438. if(!$productId || !is_numeric($productId)) return JsonService::fail('参数错误');
  439. $res = StoreProductRelation::productRelation($productId,$this->userInfo['uid'],'like',$category);
  440. if(!$res) return JsonService::fail(StoreProductRelation::getErrorInfo());
  441. else return JsonService::successful();
  442. }
  443. /**
  444. * 取消点赞
  445. * @param string $productId
  446. * @param string $category
  447. * @return \think\response\Json
  448. */
  449. public function unlike_product($productId = '',$category = 'product'){
  450. if(!$productId || !is_numeric($productId)) return JsonService::fail('参数错误');
  451. $res = StoreProductRelation::unProductRelation($productId,$this->userInfo['uid'],'like',$category);
  452. if(!$res) return JsonService::fail(StoreProductRelation::getErrorInfo());
  453. else return JsonService::successful();
  454. }
  455. /**
  456. * 添加收藏
  457. * @param $productId
  458. * @param string $category
  459. * @return \think\response\Json
  460. */
  461. public function collect_product($productId,$category = 'product'){
  462. if(!$productId || !is_numeric($productId)) return JsonService::fail('参数错误');
  463. $res = StoreProductRelation::productRelation($productId,$this->userInfo['uid'],'collect',$category);
  464. if(!$res) return JsonService::fail(StoreProductRelation::getErrorInfo());
  465. else return JsonService::successful();
  466. }
  467. /**
  468. * 批量收藏
  469. * @param string $productId
  470. * @param string $category
  471. * @return \think\response\Json
  472. */
  473. public function collect_product_all($productId = '',$category = 'product'){
  474. if($productId == '') return JsonService::fail('参数错误');
  475. $productIdS = explode(',',$productId);
  476. $res = StoreProductRelation::productRelationAll($productIdS,$this->userInfo['uid'],'collect',$category);
  477. if(!$res) return JsonService::fail(StoreProductRelation::getErrorInfo());
  478. else return JsonService::successful();
  479. }
  480. /**
  481. * 取消收藏
  482. * @param $productId
  483. * @param string $category
  484. * @return \think\response\Json
  485. */
  486. public function uncollect_product($productId,$category = 'product'){
  487. if(!$productId || !is_numeric($productId)) return JsonService::fail('参数错误');
  488. $res = StoreProductRelation::unProductRelation($productId,$this->userInfo['uid'],'collect',$category);
  489. if(!$res) return JsonService::fail(StoreProductRelation::getErrorInfo());
  490. else return JsonService::successful();
  491. }
  492. /**
  493. * 获取购物车数量
  494. * @return \think\response\Json
  495. */
  496. public function get_cart_num(){
  497. return JsonService::successful('ok',StoreCart::getUserCartNum($this->userInfo['uid'],'product'));
  498. }
  499. /**
  500. * 修改购物车产品数量
  501. * @param string $cartId
  502. * @param string $cartNum
  503. * @return \think\response\Json
  504. */
  505. public function change_cart_num($cartId = '',$cartNum = ''){
  506. if(!$cartId || !$cartNum || !is_numeric($cartId) || !is_numeric($cartNum)) return JsonService::fail('参数错误!');
  507. StoreCart::changeUserCartNum($cartId,$cartNum,$this->userInfo['uid']);
  508. return JsonService::successful();
  509. }
  510. /**
  511. * 删除购物车产品
  512. * @param string $ids
  513. * @return \think\response\Json
  514. */
  515. public function remove_cart($ids=''){
  516. if(!$ids) return JsonService::fail('参数错误!');
  517. StoreCart::removeUserCart($this->userInfo['uid'],$ids);
  518. return JsonService::successful();
  519. }
  520. /**
  521. * 获取用户优惠券
  522. * @return \think\response\Json
  523. */
  524. public function get_use_coupons(){
  525. if($_GET){
  526. if($_GET['types']==0||$_GET['types']==''){
  527. $list= StoreCouponUser::getUserAllCoupon($this->userInfo['uid']);
  528. }elseif($_GET['types']==1){
  529. $list=StoreCouponUser::getUserValidCoupon($this->userInfo['uid']);
  530. }elseif($_GET['types']==2){
  531. $list=StoreCouponUser::getUserAlreadyUsedCoupon($this->userInfo['uid']);
  532. }else{
  533. $list=StoreCouponUser::getUserBeOverdueCoupon($this->userInfo['uid']);
  534. }
  535. foreach ($list as &$v){
  536. $v['add_time'] = date('Y/m/d',$v['add_time']);
  537. $v['end_time'] = date('Y/m/d',$v['end_time']);
  538. }
  539. return JsonService::successful($list);
  540. }
  541. }
  542. /**
  543. * 获取用户优惠券
  544. * @return \think\response\Json
  545. */
  546. public function get_use_coupon(){
  547. return JsonService::successful('',StoreCouponUser::getUserAllCoupon($this->userInfo['uid']));
  548. }
  549. /**
  550. * 获取收藏产品
  551. * @param int $first
  552. * @param int $limit
  553. * @return \think\response\Json
  554. */
  555. public function get_user_collect_product($first = 0,$limit = 8)
  556. {
  557. $list = StoreProductRelation::where('A.uid',$this->userInfo['uid'])
  558. ->field('B.id pid,B.store_name,B.price,B.ot_price,B.sales,B.image,B.is_del,B.is_show')->alias('A')
  559. ->where('A.type','collect')->where('A.category','product')
  560. ->order('A.add_time DESC')->join('__STORE_PRODUCT__ B','A.product_id = B.id')
  561. ->limit($first,$limit)->select()->toArray();
  562. foreach ($list as $k=>$product){
  563. if($product['pid']){
  564. $list[$k]['is_fail'] = $product['is_del'] && $product['is_show'];
  565. }else{
  566. unset($list[$k]);
  567. }
  568. }
  569. return JsonService::successful($list);
  570. }
  571. /**
  572. * 获取收藏产品删除
  573. * @param int $first
  574. * @param int $limit
  575. * @return \think\response\Json
  576. */
  577. public function get_user_collect_product_del()
  578. {
  579. if($_GET){
  580. $list = StoreProductRelation::where('uid',$this->userInfo['uid'])->where('product_id',$_GET['pid'])->delete();
  581. return JsonService::successful($list);
  582. }
  583. }
  584. /**
  585. * 设置为默认地址
  586. * @param string $addressId
  587. * @return \think\response\Json
  588. */
  589. public function set_user_default_address($addressId = '')
  590. {
  591. if(!$addressId || !is_numeric($addressId)) return JsonService::fail('参数错误!');
  592. if(!UserAddress::be(['is_del'=>0,'id'=>$addressId,'uid'=>$this->userInfo['uid']]))
  593. return JsonService::fail('地址不存在!');
  594. $res = UserAddress::setDefaultAddress($addressId,$this->userInfo['uid']);
  595. if(!$res)
  596. return JsonService::fail('地址不存在!');
  597. else
  598. return JsonService::successful();
  599. }
  600. /**
  601. * 修改收货地址
  602. * @return \think\response\Json
  603. */
  604. public function edit_user_address()
  605. {
  606. $request = Request::instance();
  607. if(!$request->isPost()) return JsonService::fail('参数错误!');
  608. $addressInfo = UtilService::postMore([
  609. ['address',[]],
  610. ['is_default',false],
  611. ['real_name',''],
  612. ['post_code',''],
  613. ['phone',''],
  614. ['detail',''],
  615. ['id',0]
  616. ],$request);
  617. $addressInfo['province'] = $addressInfo['address']['province'];
  618. $addressInfo['city'] = $addressInfo['address']['city'];
  619. $addressInfo['district'] = $addressInfo['address']['district'];
  620. $addressInfo['is_default'] = $addressInfo['is_default'] == true ? 1 : 0;
  621. $addressInfo['uid'] = $this->userInfo['uid'];
  622. unset($addressInfo['address']);
  623. if($addressInfo['id'] && UserAddress::be(['id'=>$addressInfo['id'],'uid'=>$this->userInfo['uid'],'is_del'=>0])){
  624. $id = $addressInfo['id'];
  625. unset($addressInfo['id']);
  626. if(UserAddress::edit($addressInfo,$id,'id')){
  627. if($addressInfo['is_default'])
  628. UserAddress::setDefaultAddress($id,$this->userInfo['uid']);
  629. return JsonService::successful();
  630. }else
  631. return JsonService::fail('编辑收货地址失败!');
  632. }else{
  633. if($address = UserAddress::set($addressInfo)){
  634. if($addressInfo['is_default'])
  635. UserAddress::setDefaultAddress($address->id,$this->userInfo['uid']);
  636. return JsonService::successful();
  637. }else
  638. return JsonService::fail('添加收货地址失败!');
  639. }
  640. }
  641. /**
  642. * 获取一条用户地址
  643. * @param string $addressId
  644. * @return \think\response\Json
  645. */
  646. public function get_user_address($addressId = ''){
  647. $addressInfo = [];
  648. if($addressId && is_numeric($addressId) && UserAddress::be(['is_del'=>0,'id'=>$addressId,'uid'=>$this->userInfo['uid']])){
  649. $addressInfo = UserAddress::find($addressId);
  650. }
  651. return JsonService::successful($addressInfo);
  652. }
  653. /**
  654. * 获取默认地址
  655. * @return \think\response\Json
  656. */
  657. public function user_default_address()
  658. {
  659. $defaultAddress = UserAddress::getUserDefaultAddress($this->userInfo['uid'],'id,real_name,phone,province,city,district,detail,is_default');
  660. if($defaultAddress) return JsonService::successful('ok',$defaultAddress);
  661. else return JsonService::successful('empty',[]);
  662. }
  663. /**
  664. * 删除地址
  665. * @param string $addressId
  666. * @return \think\response\Json
  667. */
  668. public function remove_user_address($addressId = '')
  669. {
  670. if(!$addressId || !is_numeric($addressId)) return JsonService::fail('参数错误!');
  671. if(!UserAddress::be(['is_del'=>0,'id'=>$addressId,'uid'=>$this->userInfo['uid']]))
  672. return JsonService::fail('地址不存在!');
  673. if(UserAddress::edit(['is_del'=>'1'],$addressId,'id'))
  674. return JsonService::successful();
  675. else
  676. return JsonService::fail('删除地址失败!');
  677. }
  678. /**
  679. * 创建订单
  680. * @param string $key
  681. * @return \think\response\Json
  682. */
  683. public function create_order($key = '')
  684. {
  685. if(!$key) return JsonService::fail('参数错误!');
  686. if(StoreOrder::be(['order_id|unique'=>$key,'uid'=>$this->userInfo['uid'],'is_del'=>0]))
  687. return JsonService::status('extend_order','订单已生成',['orderId'=>$key,'key'=>$key]);
  688. list($addressId,$couponId,$payType,$useIntegral,$mark,$combinationId,$pinkId,$seckill_id,$formId,$bargainId) = UtilService::postMore([
  689. 'addressId','couponId','payType','useIntegral','mark',['combinationId',0],['pinkId',0],['seckill_id',0],['formId',''],['bargainId','']
  690. ],Request::instance(),true);
  691. $payType = strtolower($payType);
  692. if($bargainId) StoreBargainUser::setBargainUserStatus($bargainId,$this->userInfo['uid']);//修改砍价状态
  693. if($pinkId) if(StorePink::getIsPinkUid($pinkId,$this->userInfo['uid'])) return JsonService::status('ORDER_EXIST','订单生成失败,你已经在该团内不能再参加了',['orderId'=>StoreOrder::getStoreIdPink($pinkId,$this->userInfo['uid'])]);
  694. if($pinkId) if(StoreOrder::getIsOrderPink($pinkId,$this->userInfo['uid'])) return JsonService::status('ORDER_EXIST','订单生成失败,你已经参加该团了,请先支付订单',['orderId'=>StoreOrder::getStoreIdPink($pinkId,$this->userInfo['uid'])]);
  695. $order = StoreOrder::cacheKeyCreateOrder($this->userInfo['uid'],$key,$addressId,$payType,$useIntegral,$couponId,$mark,$combinationId,$pinkId,$seckill_id,$bargainId);
  696. $orderId = $order['order_id'];
  697. $info = compact('orderId','key');
  698. if($orderId){
  699. if($payType == 'weixin'){
  700. $orderInfo = StoreOrder::where('order_id',$orderId)->find();
  701. if(!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  702. if($orderInfo['paid']) exception('支付已支付!');
  703. if(bcsub((float)$orderInfo['pay_price'],0,2) <= 0){
  704. if(StoreOrder::jsPayPrice($orderId,$this->userInfo['uid'],$formId))
  705. return JsonService::status('success','微信支付成功',$info);
  706. else
  707. return JsonService::status('pay_error',StoreOrder::getErrorInfo());
  708. }else{
  709. try{
  710. $jsConfig = StoreOrder::jsPay($orderId);
  711. }catch (\Exception $e){
  712. return JsonService::status('pay_error',$e->getMessage(),$info);
  713. }
  714. $info['jsConfig'] = $jsConfig;
  715. return JsonService::status('wechat_pay','订单创建成功',$info);
  716. }
  717. }else if($payType == 'yue'){
  718. if(StoreOrder::yuePay($orderId,$this->userInfo['uid'],$formId))
  719. return JsonService::status('success','余额支付成功',$info);
  720. else
  721. return JsonService::status('pay_error',StoreOrder::getErrorInfo());
  722. }else if($payType == 'offline'){
  723. // RoutineTemplate::sendOrderSuccess($formId,$orderId);//发送模板消息
  724. return JsonService::status('success','订单创建成功',$info);
  725. }
  726. }else{
  727. return JsonService::fail(StoreOrder::getErrorInfo('订单生成失败!'));
  728. }
  729. }
  730. /**
  731. * 获取订单列表
  732. * @param string $type
  733. * @param int $first
  734. * @param int $limit
  735. * @param string $search
  736. * @return \think\response\Json
  737. */
  738. public function get_user_order_list($type = '',$first = 0, $limit = 8,$search = '')
  739. {
  740. // StoreOrder::delCombination();//删除拼团未支付订单
  741. if($search){
  742. $order = StoreOrder::searchUserOrder($this->userInfo['uid'],$search)?:[];
  743. $list = $order == false ? [] : [$order];
  744. }else{
  745. $list = StoreOrder::getUserOrderList($this->userInfo['uid'],$type,$first,$limit);
  746. }
  747. foreach ($list as $k=>$order){
  748. $list[$k] = StoreOrder::tidyOrder($order,true);
  749. if($list[$k]['_status']['_type'] == 3){
  750. foreach ($order['cartInfo']?:[] as $key=>$product){
  751. $list[$k]['cartInfo'][$key]['is_reply'] = StoreProductReply::isReply($product['unique'],'product');
  752. }
  753. }
  754. }
  755. return JsonService::successful($list);
  756. }
  757. /**
  758. * 订单详情页
  759. * @param string $order_id
  760. * @return \think\response\Json
  761. */
  762. public function get_order($uni = ''){
  763. if($uni == '') return JsonService::fail('参数错误');
  764. $order = StoreOrder::getUserOrderDetail($this->userInfo['uid'],$uni);
  765. $order = $order->toArray();
  766. $order['add_time'] = date('Y-m-d H:i:s',$order['add_time']);
  767. if(!$order) return JsonService::fail('订单不存在');
  768. return JsonService::successful(StoreOrder::tidyOrder($order,true));
  769. }
  770. /**
  771. * 获取订单内的某个产品信息
  772. * @param string $uni
  773. * @param string $productId
  774. * @return \think\response\Json
  775. */
  776. public function get_order_product($unique = ''){
  777. if(!$unique || !StoreOrderCartInfo::be(['unique'=>$unique]) || !($cartInfo = StoreOrderCartInfo::where('unique',$unique)->find())) return JsonService::fail('评价产品不存在!');
  778. return JsonService::successful($cartInfo);
  779. }
  780. /**
  781. * 删除订单
  782. * @param string $uni
  783. * @return \think\response\Json
  784. */
  785. public function user_remove_order($uni = '')
  786. {
  787. if(!$uni) return JsonService::fail('参数错误!');
  788. $res = StoreOrder::removeOrder($uni,$this->userInfo['uid']);
  789. if($res)
  790. return JsonService::successful();
  791. else
  792. return JsonService::fail(StoreOrder::getErrorInfo());
  793. }
  794. /**
  795. * 支付订单
  796. * @param string $uni
  797. * @return \think\response\Json
  798. */
  799. public function pay_order($uni = '')
  800. {
  801. if(!$uni) return JsonService::fail('参数错误!');
  802. $order= StoreOrder::getUserOrderDetail($this->userInfo['uid'],$uni);
  803. if(!$order) return JsonService::fail('订单不存在!');
  804. if($order['paid']) return JsonService::fail('该订单已支付!');
  805. if($order['pink_id']) if(StorePink::isPinkStatus($order['pink_id'])) return JsonService::fail('该订单已失效!');
  806. if($order['pay_type'] == 'weixin'){
  807. try{
  808. $jsConfig = StoreOrder::jsPay($order);
  809. }catch (\Exception $e){
  810. return JsonService::fail($e->getMessage());
  811. }
  812. return JsonService::status('wechat_pay',['jsConfig'=>$jsConfig,'order_id'=>$order['order_id']]);
  813. }else if($order['pay_type'] == 'yue'){
  814. if($res = StoreOrder::yuePay($order['order_id'],$this->userInfo['uid']))
  815. return JsonService::successful('余额支付成功');
  816. else
  817. return JsonService::fail(StoreOrder::getErrorInfo());
  818. }else if($order['pay_type'] == 'offline'){
  819. StoreOrder::createOrderTemplate($order);
  820. return JsonService::successful('订单创建成功');
  821. }
  822. }
  823. /**
  824. * 申请退款
  825. * @param string $uni
  826. * @param string $text
  827. * @return \think\response\Json
  828. */
  829. public function apply_order_refund(Request $request,$uni = '')
  830. {
  831. $data = UtilService::postMore([
  832. ['text',''],
  833. ['refund_reason_wap_img',''],
  834. ['refund_reason_wap_explain',''],
  835. ],$request);
  836. if($data['refund_reason_wap_img']) $data['refund_reason_wap_img'] = explode(',',$data['refund_reason_wap_img']);
  837. if(!$uni || $data['text'] == '') return JsonService::fail('参数错误!');
  838. $res = StoreOrder::orderApplyRefund($uni,$this->userInfo['uid'],$data['text'],$data['refund_reason_wap_explain'],$data['refund_reason_wap_img']);
  839. if($res)
  840. return JsonService::successful();
  841. else
  842. return JsonService::fail(StoreOrder::getErrorInfo());
  843. }
  844. /**
  845. * 用户确认收货
  846. * @param string $uni
  847. * @return \think\response\Json
  848. */
  849. public function user_take_order($uni = '')
  850. {
  851. if(!$uni) return JsonService::fail('参数错误!');
  852. $res = StoreOrder::takeOrder($uni,$this->userInfo['uid']);
  853. if($res)
  854. return JsonService::successful();
  855. else
  856. return JsonService::fail(StoreOrder::getErrorInfo());
  857. }
  858. /**
  859. * 充值
  860. * @param int $price
  861. * @return \think\response\Json
  862. */
  863. public function user_wechat_recharge($price = 0)
  864. {
  865. if(!$price || $price <=0) return JsonService::fail('参数错误');
  866. $storeMinRecharge = SystemConfigService::get('store_user_min_recharge');
  867. if($price < $storeMinRecharge) return JsonService::fail('充值金额不能低于'.$storeMinRecharge);
  868. $rechargeOrder = UserRecharge::addRecharge($this->userInfo['uid'],$price);
  869. if(!$rechargeOrder) return JsonService::fail('充值订单生成失败!');
  870. try{
  871. return JsonService::successful(UserRecharge::jsPay($rechargeOrder));
  872. }catch (\Exception $e){
  873. return JsonService::fail($e->getMessage());
  874. }
  875. }
  876. /**
  877. * 余额使用记录
  878. * @param int $first
  879. * @param int $limit
  880. * @return \think\response\Json
  881. */
  882. public function user_balance_list($first = 0,$limit = 8)
  883. {
  884. $list = UserBill::where('uid',$this->userInfo['uid'])->where('category','now_money')
  885. ->field('mark,pm,number,add_time')
  886. ->where('status',1)->order('add_time DESC')->limit($first,$limit)->select()->toArray();
  887. foreach ($list as &$v){
  888. $v['add_time'] = date('Y/m/d H:i',$v['add_time']);
  889. }
  890. return JsonService::successful($list);
  891. }
  892. /**
  893. * 积分使用记录
  894. * @param int $first
  895. * @param int $limit
  896. * @return \think\response\Json
  897. */
  898. public function user_integral_list($first = 0,$limit = 8)
  899. {
  900. $list = UserBill::where('uid',$this->userInfo['uid'])->where('category','integral')
  901. ->field('mark,pm,number,add_time')
  902. ->where('status',1)->order('add_time DESC')->limit($first,$limit)->select()->toArray();
  903. foreach ($list as &$v){
  904. $v['add_time'] = date('Y/m/d H:i',$v['add_time']);
  905. $v['number'] = floatval($v['number']);
  906. }
  907. return JsonService::successful($list);
  908. }
  909. /**
  910. * 评价订单
  911. * @param string $unique
  912. * @return \think\response\Json
  913. */
  914. public function user_comment_product($unique = '')
  915. {
  916. if(!$unique) return JsonService::fail('参数错误!');
  917. $cartInfo = StoreOrderCartInfo::where('unique',$unique)->find();
  918. $uid = $this->userInfo['uid'];
  919. if(!$cartInfo || $uid != $cartInfo['cart_info']['uid']) return JsonService::fail('评价产品不存在!');
  920. if(StoreProductReply::be(['oid'=>$cartInfo['oid'],'unique'=>$unique]))
  921. return JsonService::fail('该产品已评价!');
  922. $group = UtilService::postMore([
  923. ['comment',''],['pics',[]],['product_score',5],['service_score',5]
  924. ],Request::instance());
  925. $group['comment'] = htmlspecialchars(trim($group['comment']));
  926. if($group['product_score'] < 1) return JsonService::fail('请为产品评分');
  927. else if($group['service_score'] < 1) return JsonService::fail('请为商家服务评分');
  928. if($cartInfo['cart_info']['combination_id']) $productId = $cartInfo['cart_info']['product_id'];
  929. else if($cartInfo['cart_info']['seckill_id']) $productId = $cartInfo['cart_info']['product_id'];
  930. else if($cartInfo['cart_info']['bargain_id']) $productId = $cartInfo['cart_info']['product_id'];
  931. else $productId = $cartInfo['product_id'];
  932. $group = array_merge($group,[
  933. 'uid'=>$uid,
  934. 'oid'=>$cartInfo['oid'],
  935. 'unique'=>$unique,
  936. 'product_id'=>$productId,
  937. 'reply_type'=>'product'
  938. ]);
  939. StoreProductReply::beginTrans();
  940. $res = StoreProductReply::reply($group,'product');
  941. if(!$res) {
  942. StoreProductReply::rollbackTrans();
  943. return JsonService::fail('评价失败!');
  944. }
  945. try{
  946. // HookService::listen('store_product_order_reply',$group,$cartInfo,false,StoreProductBehavior::class);
  947. StoreOrder::checkOrderOver($cartInfo['oid']);
  948. }catch (\Exception $e){
  949. StoreProductReply::rollbackTrans();
  950. return JsonService::fail($e->getMessage());
  951. }
  952. StoreProductReply::commitTrans();
  953. return JsonService::successful();
  954. }
  955. /**
  956. * 上传图片
  957. * @param string $filename
  958. * @return \think\response\Json
  959. */
  960. public function upload(Request $request)
  961. {
  962. $data = UtilService::postMore([['filename','']],$request);
  963. $res = UploadService::image($data['filename'],'store/comment');
  964. if($res->status == 200)
  965. return JsonService::successful('图片上传成功!',['name'=>$res->fileInfo->getSaveName(),'url'=>UploadService::pathToUrl($res->dir)]);
  966. else
  967. return JsonService::fail($res->error);
  968. }
  969. /**
  970. * 获取一级和二级分类
  971. * @return \think\response\Json
  972. */
  973. public function get_product_category()
  974. {
  975. $parentCategory = StoreCategory::pidByCategory(0,'id,cate_name')->toArray();
  976. foreach ($parentCategory as $k=>$category){
  977. $category['child'] = StoreCategory::pidByCategory($category['id'],'id,cate_name')->toArray();
  978. $parentCategory[$k] = $category;
  979. }
  980. return JsonService::successful($parentCategory);
  981. }
  982. /**
  983. * 获取一级推荐人
  984. * @param int $first
  985. * @param int $limit
  986. * @return \think\response\Json
  987. */
  988. public function get_spread_list($first = 0,$limit = 20)
  989. {
  990. $list = User::where('spread_uid',$this->userInfo['uid'])->field('uid,nickname,avatar,add_time')->limit($first,$limit)->order('add_time DESC')->select()->toArray();
  991. foreach ($list as $k=>$user){
  992. $list[$k]['add_time'] = date('Y/m/d',$user['add_time']);
  993. $list[$k]['price'] = StoreOrder::getUserPrice($user['uid']);
  994. }
  995. $count = User::where('spread_uid',$this->userInfo['uid'])->field('uid,nickname,avatar,add_time')->count();
  996. $data['count'] = $count;
  997. $data['list'] = $list;
  998. return JsonService::successful($data);
  999. }
  1000. /**
  1001. * 获取二级推荐人
  1002. * @param int $first
  1003. * @param int $limit
  1004. * @return \think\response\Json
  1005. */
  1006. public function get_spread_list_two($two_uid=0,$first = 0,$limit = 20)
  1007. {
  1008. $list = User::where('spread_uid',$two_uid)->field('uid,nickname,avatar,add_time')->limit($first,$limit)->order('add_time DESC')->select()->toArray();
  1009. foreach ($list as $k=>$user){
  1010. $list[$k]['add_time'] = date('Y/m/d',$user['add_time']);
  1011. $list[$k]['price'] = StoreOrder::getUserPrice($user['uid']);
  1012. }
  1013. $count = User::where('spread_uid',$two_uid)->field('uid,nickname,avatar,add_time')->count();
  1014. $data['count'] = $count;
  1015. $data['list'] = $list;
  1016. return JsonService::successful($data);
  1017. }
  1018. /**
  1019. * 领取优惠券
  1020. * @param string $couponId
  1021. * @return \think\response\Json
  1022. */
  1023. public function user_get_coupon($couponId = '')
  1024. {
  1025. if(!$couponId || !is_numeric($couponId)) return JsonService::fail('参数错误!');
  1026. if(StoreCouponIssue::issueUserCoupon($couponId,$this->userInfo['uid'])){
  1027. return JsonService::successful('领取成功');
  1028. }else{
  1029. return JsonService::fail(StoreCouponIssue::getErrorInfo('领取失败!'));
  1030. }
  1031. }
  1032. /**
  1033. * 获取产品评论
  1034. * @param string $productId
  1035. * @param int $first
  1036. * @param int $limit
  1037. * @param string $filter
  1038. * @return \think\response\Json
  1039. */
  1040. public function product_reply_list($productId = '',$first = 0,$limit = 8, $filter = 'all')
  1041. {
  1042. if(!$productId || !is_numeric($productId)) return JsonService::fail('参数错误!');
  1043. $list = StoreProductReply::getProductReplyList($productId,$filter,$first,$limit);
  1044. if($list){
  1045. foreach ($list as $k=>$v){
  1046. foreach ($v['pics'] as $kk=>$vv){
  1047. $list[$k]['pics'] = explode(',',$vv);
  1048. }
  1049. }
  1050. }
  1051. return JsonService::successful($list);
  1052. }
  1053. /**
  1054. * 获取商品属性数据
  1055. * @param string $productId
  1056. * @return \think\response\Json
  1057. */
  1058. public function product_attr_detail($productId = '')
  1059. {
  1060. if(!$productId || !is_numeric($productId)) return JsonService::fail('参数错误!');
  1061. list($productAttr,$productValue) = StoreProductAttr::getProductAttrDetail($productId);
  1062. return JsonService::successful(compact('productAttr','productValue'));
  1063. }
  1064. /**
  1065. * 获取用户所有地址
  1066. * @return \think\response\Json
  1067. */
  1068. public function user_address_list()
  1069. {
  1070. $list = UserAddress::getUserValidAddressList($this->userInfo['uid'],'id,real_name,phone,province,city,district,detail,is_default');
  1071. return JsonService::successful($list);
  1072. }
  1073. /**
  1074. * 用户通知
  1075. * @param int $page
  1076. * @param int $limit
  1077. * @return \think\response\Json
  1078. */
  1079. public function get_notice_list($page = 0, $limit = 8)
  1080. {
  1081. $list = UserNotice::getNoticeList($this->userInfo['uid'],$page,$limit);
  1082. return JsonService::successful($list);
  1083. }
  1084. /**
  1085. * 修改用户通知为已查看
  1086. * @param $nid
  1087. * @return \think\response\Json
  1088. */
  1089. public function see_notice($nid){
  1090. UserNotice::seeNotice($this->userInfo['uid'],$nid);
  1091. return JsonService::successful();
  1092. }
  1093. /**
  1094. * 客服提醒
  1095. * @param Request $request
  1096. * @return \think\response\Json
  1097. */
  1098. public function refresh_msn(Request $request)
  1099. {
  1100. $params = $request->post();
  1101. $remind_where = "mer_id = ".$params["mer_id"]." AND uid = ".$params["uid"]." AND to_uid = ".$params["to_uid"]." AND type = 0 AND remind = 0";
  1102. $remind_list = StoreServiceLog::where($remind_where)->order("add_time asc")->select();
  1103. foreach ($remind_list as $key => $value) {
  1104. if(time() - $value["add_time"] > 3){
  1105. StoreServiceLog::edit(array("remind"=>1),$value["id"]);
  1106. $now_user = StoreService::field("uid,nickname")->where(array("uid"=>$params["uid"]))->find();
  1107. if(!$now_user)$now_user = User::field("uid,nickname")->where(array("uid"=>$params["uid"]))->find();
  1108. if($params["to_uid"]) {
  1109. $head = '您有新的消息,请注意查收!';
  1110. $head .= $params["mer_id"] > 0 ? "\n商户名称:".Merchant::where('id',$params["mer_id"])->value('mer_name') : '';
  1111. WechatTemplateService::sendTemplate(WechatUser::uidToOpenid($params["to_uid"]),WechatTemplateService::SERVICE_NOTICE,[
  1112. 'first'=>$head,
  1113. 'keyword1'=>$now_user["nickname"],
  1114. 'keyword2'=>"客服提醒",
  1115. 'keyword3'=> preg_replace('/<img.*? \/>/','[图片]',$value["msn"]),
  1116. 'keyword4'=>date('Y-m-d H:i:s',time()),
  1117. 'remark'=>'点击立即查看消息'
  1118. ],Url::build('service/service_ing',['to_uid'=>$now_user["uid"],'mer_id'=>$params["mer_id"]],true,true));
  1119. }
  1120. }
  1121. }
  1122. $where = "mer_id = ".$params["mer_id"]." AND uid = ".$params["to_uid"]." AND to_uid = ".$params["uid"]." AND type = 0";
  1123. $list = StoreServiceLog::where($where)->order("add_time asc")->select()->toArray();
  1124. $ids = [];
  1125. foreach ($list as $key => $value) {
  1126. //设置发送人与接收人区别
  1127. if($value["uid"] == $params["uid"])
  1128. $list[$key]['my'] = "my";
  1129. else
  1130. $list[$key]['my'] = "to";
  1131. array_push($ids,$value["id"]);
  1132. }
  1133. //设置这些消息为已读
  1134. StoreServiceLog::where(array("id"=>array("in",$ids)))->update(array("type"=>1,"remind"=>1));
  1135. return JsonService::successful($list);
  1136. }
  1137. public function add_msn(Request $request){
  1138. $params = $request->post();
  1139. if($params["type"] == "html")
  1140. $data["msn"] = htmlspecialchars_decode($params["msn"]);
  1141. else
  1142. $data["msn"] = $params["msn"];
  1143. $data["uid"] = $params["uid"];
  1144. $data["to_uid"] = $params["to_uid"];
  1145. $data["mer_id"] = $params["mer_id"] > 0 ? $params["mer_id"] : 0;
  1146. $data["add_time"] = time();
  1147. StoreServiceLog::set($data);
  1148. return JsonService::successful();
  1149. }
  1150. public function get_msn(Request $request){
  1151. $params = $request->post();
  1152. $size = 10;
  1153. $page = $params["page"]>=0 ? $params["page"] : 1;
  1154. $where = "(mer_id = ".$params["mer_id"]." AND uid = ".$params["uid"]." AND to_uid = ".$params["to_uid"].") OR (mer_id = ".$params["mer_id"]." AND uid = ".$params["to_uid"]." AND to_uid = ".$params["uid"].")";
  1155. $list = StoreServiceLog::where($where)->limit(($page-1)*$size,$size)->order("add_time desc")->select()->toArray();
  1156. foreach ($list as $key => $value) {
  1157. //设置发送人与接收人区别
  1158. if($value["uid"] == $params["uid"])
  1159. $list[$key]['my'] = "my";
  1160. else
  1161. $list[$key]['my'] = "to";
  1162. //设置这些消息为已读
  1163. if($value["uid"] == $params["to_uid"] && $value["to_uid"] == $params["uid"])StoreServiceLog::edit(array("type"=>1,"remind"=>1),$value["id"]);
  1164. }
  1165. $list=array_reverse($list);
  1166. return JsonService::successful($list);
  1167. }
  1168. public function refresh_msn_new(Request $request){
  1169. $params = $request->post();
  1170. $now_user = User::getUserInfo($this->userInfo['uid']);
  1171. if($params["last_time"] > 0)
  1172. $where = "(uid = ".$now_user["uid"]." OR to_uid = ".$now_user["uid"].") AND add_time>".$params["last_time"];
  1173. else
  1174. $where = "uid = ".$now_user["uid"]." OR to_uid = ".$now_user["uid"];
  1175. $msn_list = StoreServiceLog::where($where)->order("add_time desc")->select()->toArray();
  1176. $info_array = $list = [];
  1177. foreach ($msn_list as $key => $value){
  1178. $to_uid = $value["uid"] == $now_user["uid"] ? $value["to_uid"] : $value["uid"];
  1179. if(!in_array(["to_uid"=>$to_uid,"mer_id"=>$value["mer_id"]],$info_array)){
  1180. $info_array[count($info_array)] = ["to_uid"=>$to_uid,"mer_id"=>$value["mer_id"]];
  1181. $to_user = StoreService::field("uid,nickname,avatar")->where(array("uid"=>$to_uid))->find();
  1182. if(!$to_user)$to_user = User::field("uid,nickname,avatar")->where(array("uid"=>$to_uid))->find();
  1183. $to_user["mer_id"] = $value["mer_id"];
  1184. $to_user["mer_name"] = $value["mer_id"] > 0 ? "[".Merchant::where('id',$value["mer_id"])->value('mer_name')."]" : '';
  1185. $value["to_info"] = $to_user;
  1186. $value["count"] = StoreServiceLog::where(array("mer_id"=>$value["mer_id"],"uid"=>$to_uid,"to_uid"=>$now_user["uid"],"type"=>0))->count();
  1187. $list[count($list)] = $value;
  1188. }
  1189. }
  1190. return JsonService::successful($list);
  1191. }
  1192. public function get_user_brokerage_list($uid, $first = 0,$limit = 8)
  1193. {
  1194. if(!$uid)
  1195. return $this->failed('用户不存在');
  1196. $list = UserBill::field('A.mark,A.add_time,A.number,A.pm')->alias('A')->limit($first,$limit)
  1197. ->where('A.category','now_money')->where('A.type','brokerage')
  1198. ->where('A.uid',$this->userInfo['uid'])
  1199. ->join('__STORE_ORDER__ B','A.link_id = B.id AND B.uid = '.$uid)->select()->toArray();
  1200. return JsonService::successful($list);
  1201. }
  1202. public function user_extract()
  1203. { $request = Request::instance();
  1204. $list=$request->param();
  1205. $data=$list['lists'];
  1206. if(UserExtract::userExtract($this->userInfo,$data))
  1207. return JsonService::successful('申请提现成功!');
  1208. else
  1209. return JsonService::fail(UserExtract::getErrorInfo());
  1210. }
  1211. /*
  1212. * 提现列表
  1213. */
  1214. public function extract($first = 0,$limit = 8)
  1215. {
  1216. $list=UserExtract::where('uid',$this->userInfo['uid'])->order('add_time desc')->limit($first,$limit)->select()->toArray();
  1217. foreach($list as &$v){
  1218. $v['add_time']=date('Y/m/d',$v['add_time']);
  1219. }
  1220. return JsonService::successful($list);
  1221. }
  1222. /**
  1223. * 用户下级的订单
  1224. * @param int $first
  1225. * @param int $limit
  1226. */
  1227. public function subordinateOrderlist($first = 0, $limit = 8){
  1228. $request = Request::instance();
  1229. $lists=$request->param();
  1230. $xUid = $lists['uid'];
  1231. $status = $lists['status'];
  1232. if($status == 0) $type='';
  1233. elseif($status == 1) $type=4;
  1234. elseif($status == 2) $type=3;
  1235. else return false;
  1236. $list = [];
  1237. if(!$xUid){
  1238. $arr = User::where('spread_uid',$this->userInfo['uid'])->column('uid');
  1239. foreach($arr as $v) $list = StoreOrder::getUserOrderList($v,$type,$first,$limit);
  1240. }else $list = StoreOrder::getUserOrderList($xUid,$type,$first,$limit);
  1241. foreach ($list as $k=>$order){
  1242. $list[$k] = StoreOrder::tidyOrder($order,true);
  1243. if($list[$k]['_status']['_type'] == 3){
  1244. foreach ($order['cartInfo']?:[] as $key=>$product){
  1245. $list[$k]['cartInfo'][$key]['is_reply'] = StoreProductReply::isReply($product['unique'],'product');
  1246. }
  1247. }
  1248. }
  1249. return JsonService::successful($list);
  1250. }
  1251. /**
  1252. * 用户下级的订单
  1253. * @param int $first
  1254. * @param int $limit
  1255. */
  1256. public function subordinateOrderlistmoney()
  1257. {
  1258. $request = Request::instance();
  1259. $lists=$request->param();
  1260. $status = $lists['status'];
  1261. $type = '';
  1262. if($status == 1) $type = 4;
  1263. elseif($status == 2) $type = 3;
  1264. $arr = User::where('spread_uid',$this->userInfo['uid'])->column('uid');
  1265. $list = StoreOrder::getUserOrderCount(implode(',',$arr),$type);
  1266. $price = [];
  1267. if(!empty($list)) foreach ($list as $k=>$v) $price[]=$v['pay_price'];
  1268. $cont = count($list);
  1269. $sum = array_sum($price);
  1270. return JsonService::successful(['cont'=>$cont,'sum'=>$sum]);
  1271. }
  1272. /*
  1273. * 昨日推广佣金
  1274. */
  1275. public function yesterdayCommission(){
  1276. $money=UserBill::where('uid',$this->userInfo['uid'])->where('category','now_money')->where('type','brokerage')->where('pm',1)->where('status',1)->whereTime('add_time', 'yesterday')->column('number');
  1277. $sum= array_sum($money);
  1278. return JsonService::successful($sum);
  1279. }
  1280. /*
  1281. * 累计已提金额
  1282. */
  1283. public function extractsum(){
  1284. $money=UserExtract::where('uid',$this->userInfo['uid'])->where('status',1)->column('extract_price');
  1285. $sum= array_sum($money);
  1286. return JsonService::successful($sum);
  1287. }
  1288. /**
  1289. * 获取一条优惠券
  1290. * @param int $couponId
  1291. * @return \think\response\Json
  1292. */
  1293. public function get_coupon_rope($couponId = 0){
  1294. if(!$couponId) return JsonService::fail('参数错误');
  1295. $couponUser = StoreCouponUser::validAddressWhere()->where('id',$couponId)->where('uid',$this->userInfo['uid'])->find();
  1296. return JsonService::successful($couponUser);
  1297. }
  1298. /**
  1299. * 获取 可以领取的优惠券
  1300. * @param int $limit
  1301. * @return \think\response\Json
  1302. */
  1303. public function get_issue_coupon_list($limit = 2)
  1304. {
  1305. $list = StoreCouponIssue::validWhere('A')->join('__STORE_COUPON__ B','A.cid = B.id')
  1306. ->field('A.*,B.coupon_price,B.use_min_price')->order('B.sort DESC,A.id DESC')->limit($limit)->select()->toArray()?:[];
  1307. foreach ($list as $k=>$v){
  1308. $list[$k]['is_use'] = StoreCouponIssueUser::be(['uid'=>$this->userInfo['uid'],'issue_coupon_id'=>$v['id']]);
  1309. }
  1310. return JsonService::successful($list);
  1311. }
  1312. public function clear_cache($uni = '')
  1313. {
  1314. if($uni)CacheService::clear();
  1315. }
  1316. /**
  1317. * 获取今天正在拼团的人的头像和名称
  1318. * @return \think\response\Json
  1319. */
  1320. public function get_pink_second_one(){
  1321. $addTime = mt_rand(time()-30000,time());
  1322. $storePink = StorePink::where('p.add_time','GT',$addTime)->alias('p')->where('p.status',1)->join('User u','u.uid=p.uid')->field('u.nickname,u.avatar as src')->find();
  1323. return JsonService::successful($storePink);
  1324. }
  1325. /**
  1326. * 再来一单
  1327. * @param string $uni
  1328. */
  1329. public function order_details($uni = ''){
  1330. if(!$uni) return JsonService::fail('参数错误!');
  1331. $order = StoreOrder::getUserOrderDetail($this->userInfo['uid'],$uni);
  1332. if(!$order) return JsonService::fail('订单不存在!');
  1333. $order = StoreOrder::tidyOrder($order,true);
  1334. $res = array();
  1335. foreach ($order['cartInfo'] as $v) {
  1336. if($v['combination_id']) return JsonService::fail('拼团产品不能再来一单,请在拼团产品内自行下单!');
  1337. else $res[] = StoreCart::setCart($this->userInfo['uid'], $v['product_id'], $v['cart_num'], isset($v['productInfo']['attrInfo']['unique']) ? $v['productInfo']['attrInfo']['unique'] : '', 'product', 0, 0);
  1338. }
  1339. $cateId = [];
  1340. foreach ($res as $v){
  1341. if(!$v) return JsonService::fail('再来一单失败,请重新下单!');
  1342. $cateId[] = $v['id'];
  1343. }
  1344. return JsonService::successful('ok',implode(',',$cateId));
  1345. }
  1346. /**
  1347. * 获取分销二维码
  1348. * @return \think\response\Json
  1349. */
  1350. public function get_code(){
  1351. header('content-type:image/jpg');
  1352. if(!$this->userInfo['uid']) return JsonService::fail('授权失败,请重新授权');
  1353. $path = 'public/uploads/routine/'.$this->userInfo['uid'].'.jpg';
  1354. $domain = SystemConfigService::get('site_url').'/';
  1355. $domainTop = substr($domain,0,5);
  1356. if($domainTop != 'https') $domain = 'https:'.substr($domain,5,strlen($domain));
  1357. if(file_exists($path)) return JsonService::successful($domain.$path);
  1358. else{
  1359. if(!file_exists('public/uploads/routine')){
  1360. $dir = iconv("UTF-8", "GBK", "public/uploads/routine");
  1361. mkdir ($dir,0775,true);
  1362. }
  1363. file_put_contents($path,RoutineCode::getCode($this->userInfo['uid']));
  1364. }
  1365. return JsonService::successful($domain.$path);
  1366. }
  1367. /**
  1368. * 绑定推荐人
  1369. * @param Request $request
  1370. * @return \think\response\Json
  1371. */
  1372. public function spread_uid(Request $request){
  1373. $data = UtilService::postMore(['spread_uid',0],$request);
  1374. if($data['spread_uid']){
  1375. if(!$this->userInfo['spread_uid']){
  1376. $res = User::edit(['spread_uid'=>$data['spread_uid']],$this->userInfo['uid']);
  1377. if($res) return JsonService::successful('绑定成功');
  1378. else return JsonService::successful('绑定失败');
  1379. }else return JsonService::fail('已存在被推荐人');
  1380. }else return JsonService::fail('没有推荐人');
  1381. }
  1382. /**
  1383. * 获取砍价列表
  1384. * @return \think\response\Json
  1385. */
  1386. public function get_bargain_list($limit = 10){
  1387. $bargain = StoreBargain::getList();
  1388. $bargain = StoreBargainUser::getUserList($bargain,$limit);
  1389. $lovely = GroupDataService::getData('routine_lovely')?:[];//banner图
  1390. $banner = GroupDataService::getData('bargain_banner')?:[];//banner图
  1391. $banner = $banner[0];
  1392. $bargainUser = StoreBargainUser::getBargainUserStatusSuccess();
  1393. $data['bargain'] = $bargain;
  1394. $data['lovely'] = $lovely;
  1395. $data['banner'] = $banner;
  1396. $data['bargainUser'] = $bargainUser;
  1397. return JsonService::successful($data);
  1398. }
  1399. /**
  1400. * 砍价详情
  1401. * @param int $bargainId
  1402. * @return \think\response\Json
  1403. */
  1404. public function get_bargain($bargainId = 0){
  1405. if(!$bargainId) return JsonService::fail('参数错误');
  1406. $bargain = StoreBargain::getBargainTerm($bargainId);
  1407. if(empty($bargain)) return JsonService::fail('砍价已结束');
  1408. $bargain['time'] = time();
  1409. return JsonService::successful($bargain);
  1410. }
  1411. /**
  1412. * 获取人数
  1413. * @param int $count
  1414. * @return \think\response\Json
  1415. */
  1416. public function get_bargain_count($count = 0){
  1417. $data['lookCount'] = StoreBargain::getBargainLook()['look'];//观看人数
  1418. $data['shareCount'] = StoreBargain::getBargainShare()['share'];//观看人数
  1419. $data['userCount'] = StoreBargainUser::count();//参与人数
  1420. return JsonService::successful($data);
  1421. }
  1422. /**
  1423. * 添加砍价分享次数
  1424. * @param int $bargainId
  1425. */
  1426. public function add_share_bargain($bargainId = 0){
  1427. if(!$bargainId) return JsonService::successful();
  1428. StoreBargain::addBargainShare($bargainId);
  1429. return JsonService::successful();
  1430. }
  1431. /**
  1432. * 添加砍价浏览次数
  1433. * @param int $bargainId
  1434. */
  1435. public function add_look_bargain($bargainId = 0){
  1436. if(!$bargainId) return JsonService::successful();
  1437. StoreBargain::addBargainLook($bargainId);
  1438. return JsonService::successful();
  1439. }
  1440. /**
  1441. * 获取砍价帮
  1442. * @param int $bargainId
  1443. * @param int $uid
  1444. * @return \think\response\Json
  1445. */
  1446. public function get_bargain_user($bargainId = 0,$bargainUid = 0,$limit = 15){
  1447. if(!$bargainId || !$bargainUid) return JsonService::fail('参数错误');
  1448. $bargainUserTableId = StoreBargainUser::setUserBargain($bargainId,$bargainUid);
  1449. $storeBargainUserHelp = StoreBargainUserHelp::getList($bargainUserTableId,$limit);
  1450. return JsonService::successful($storeBargainUserHelp);
  1451. }
  1452. /**
  1453. * 我的砍价
  1454. * @param int $bargainId
  1455. * @return \think\response\Json
  1456. */
  1457. public function mycut($bargainId = 0){
  1458. if(!$bargainId ) return JsonService::fail('参数错误');
  1459. $data= StoreBargainUser::where('bargain_id',$bargainId)->where('uid',$this->userInfo['uid'])->where('status',1)->find();
  1460. return JsonService::successful($data);
  1461. }
  1462. /**
  1463. * 参与砍价产品
  1464. * @param int $bargainId
  1465. * @return \think\response\Json
  1466. */
  1467. public function set_bargain($bargainId = 0){
  1468. if(!$bargainId) return JsonService::fail('参数错误');
  1469. $res = StoreBargainUser::setBargain($bargainId,$this->userInfo['uid']);
  1470. if($res) {
  1471. $data['id'] = $res->id;
  1472. return JsonService::successful('参与成功',$data);
  1473. }
  1474. else return JsonService::fail('参与失败');
  1475. }
  1476. /**
  1477. * 判断当前登录人是否参与砍价
  1478. * @param int $bargainId
  1479. * @return \think\response\Json
  1480. */
  1481. public function is_bargain_user($bargainId = 0){
  1482. if(!$bargainId) return JsonService::fail('参数错误');
  1483. $data=StoreBargainUser::isBargainUser($bargainId,$this->userInfo['uid']);
  1484. if($data) return JsonService::successful($data);
  1485. else return JsonService::fail('没有参与砍价');
  1486. }
  1487. /*
  1488. * 已砍掉的金额
  1489. */
  1490. public function speed_of_progress($bargainId = 0){
  1491. if(!$bargainId) return JsonService::fail('参数错误');
  1492. $price= StoreBargainUser::where('bargain_id',$bargainId)->where('status',1)->where('uid',$this->userInfo['uid'])->value('price');
  1493. return JsonService::successful($price);
  1494. }
  1495. /**
  1496. * 帮好友砍价
  1497. * @param int $bargainId
  1498. * @param int $bargainUserId
  1499. * @return \think\response\Json
  1500. */
  1501. public function set_bargain_help($bargainId = 0,$bargainUserId = 0){
  1502. if(!$bargainId || !$bargainUserId) return JsonService::fail('参数错误');
  1503. $res = StoreBargainUserHelp::setBargainUserHelp($bargainId,$bargainUserId,$this->userInfo['uid']);
  1504. if($res) {
  1505. if(!StoreBargainUserHelp::getSurplusPrice($bargainId,$bargainUserId)){
  1506. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId,$bargainUserId);
  1507. $bargain = StoreBargain::where('id',$bargainId)->find()->toArray();
  1508. $bargainUser = StoreBargainUser::where('id',$bargainUserTableId)->find()->toArray();
  1509. RoutineTemplate::sendBargainSuccess($bargain,$bargainUser,$bargainUserId);//发送模板消息
  1510. }
  1511. return JsonService::successful('砍价成功',$res);
  1512. }
  1513. else return JsonService::fail('砍价失败');
  1514. }
  1515. /**
  1516. * 获取砍价帮总人数、剩余金额、进度条
  1517. * @param int $bargainId
  1518. * @param int $bargainUserId
  1519. * @return \think\response\Json
  1520. */
  1521. public function get_bargain_help_count($bargainId = 0,$bargainUserId = 0){
  1522. if(!$bargainId || !$bargainUserId) return JsonService::fail('参数错误');
  1523. $count = StoreBargainUserHelp::getBargainUserHelpPeopleCount($bargainId,$bargainUserId);
  1524. $price = StoreBargainUserHelp::getSurplusPrice($bargainId,$bargainUserId);
  1525. $pricePercent = StoreBargainUserHelp::getSurplusPricePercent($bargainId,$bargainUserId);
  1526. $data['count'] = $count;
  1527. $data['price'] = $price;
  1528. $data['pricePercent'] = $pricePercent;
  1529. return JsonService::successful($data);
  1530. }
  1531. /**
  1532. * 判断当前砍价是否开启
  1533. * @param int $bargainId
  1534. * @return \think\response\Json
  1535. */
  1536. public function is_bargain_status($bargainId = 0){
  1537. if(!$bargainId) return JsonService::fail('参数错误');
  1538. if(!StoreBargain::setBargainStatus($bargainId)) return JsonService::successful();
  1539. else return JsonService::fail();
  1540. }
  1541. /**
  1542. * 判断用户是否可以砍价
  1543. * @param int $bargainId
  1544. * @param int $bargainUserId
  1545. */
  1546. public function is_bargain_user_help($bargainId = 0,$bargainUserId = 0){
  1547. if(!$bargainId || !$bargainUserId) return JsonService::fail('参数错误');
  1548. if(StoreBargainUserHelp::isBargainUserHelpCount($bargainId,$bargainUserId,$this->userInfo['uid'])) return JsonService::successful('请稍后在帮助好友砍价');
  1549. else return JsonService::fail('您不能再帮忙砍价了');
  1550. }
  1551. /**
  1552. * 修改砍价状态为失败
  1553. * @param int $bargainUserTableId
  1554. */
  1555. public function set_user_bargain_status($bargainUserTableId = 0){
  1556. if(!$bargainUserTableId) return JsonService::fail('参数错误');
  1557. if(StoreBargainUser::editBargainUserStatus($bargainUserTableId)) return JsonService::successful('ok');
  1558. else return JsonService::fail('no');
  1559. }
  1560. /**
  1561. * 获取用户信息
  1562. * @param int $uid
  1563. * @return \think\response\Json
  1564. */
  1565. public function get_user_info_uid($userId = 0){
  1566. if(!$userId) return JsonService::fail('参数错误');
  1567. $res = User::getUserInfo($userId);
  1568. if($res) return JsonService::successful($res);
  1569. else return JsonService::fail(User::getErrorInfo());
  1570. }
  1571. /**
  1572. * 获取砍价产品 个人中心 我的砍价
  1573. */
  1574. public function get_user_bargain_all(){
  1575. $list = StoreBargainUser::getBargainUserAll($this->userInfo['uid']);
  1576. if($list){
  1577. foreach ($list as $k=>$v){
  1578. $list[$k]['helpCount'] = StoreBargainUserHelp::getBargainUserHelpPeopleCount($v['bargain_id'],$this->userInfo['uid']);
  1579. }
  1580. return JsonService::successful($list);
  1581. }else return JsonService::fail('暂无参与砍价');
  1582. }
  1583. /*
  1584. * 查物流
  1585. */
  1586. public function express($uid,$uni = '')
  1587. {
  1588. if(!$uni || !($order = StoreOrder::getUserOrderDetail($uid,$uni))) return JsonService::fail('查询订单不存在!');
  1589. if($order['delivery_type'] != 'express' || !$order['delivery_id']) return JsonService::fail('该订单不存在快递单号!');
  1590. $cacheName = $uni.$order['delivery_id'];
  1591. CacheService::rm($cacheName);
  1592. $result = CacheService::get($cacheName,null);
  1593. if($result === NULL){
  1594. $result = Express::query($order['delivery_id']);
  1595. if(is_array($result) &&
  1596. isset($result['result']) &&
  1597. isset($result['result']['deliverystatus']) &&
  1598. $result['result']['deliverystatus'] >= 3)
  1599. $cacheTime = 0;
  1600. else
  1601. $cacheTime = 1800;
  1602. CacheService::set($cacheName,$result,$cacheTime);
  1603. }
  1604. if($result) return JsonService::successful([ 'order'=>$order, 'express'=>$result]);
  1605. }
  1606. /**
  1607. * 收集发送模板信息的formID
  1608. * @param string $formId
  1609. */
  1610. public function get_form_id($formId = ''){
  1611. if((int)$formId == '' || $formId == 'the formId is a mock one') return JsonService::fail('no');
  1612. $data['form_id'] = $formId;
  1613. $data['uid'] = $this->userInfo['uid'];
  1614. $data['status'] = 1;
  1615. $data['stop_time'] = bcadd(time(),bcmul(6,86400,0),0);
  1616. RoutineFormId::set($data);
  1617. return JsonService::successful();
  1618. }
  1619. /**
  1620. * 获取拼团列表
  1621. * @param int $offset
  1622. * @param int $limit
  1623. */
  1624. public function get_combination_list($offset=0,$limit=20){
  1625. $store_combination = StoreCombination::getAll($offset,$limit);
  1626. return JsonService::successful($store_combination);
  1627. }
  1628. public function get_combination_list_banner(){
  1629. $lovely = GroupDataService::getData('routine_lovely')?:[];//banner图
  1630. return JsonService::successful($lovely[2]);
  1631. }
  1632. /**
  1633. * 获取拼团产品详情
  1634. * @param int $id
  1635. */
  1636. public function combination_detail($id = 0){
  1637. if(!$id) return JsonService::fail('拼团不存在或已下架');
  1638. $combinationOne = StoreCombination::getCombinationOne($id);
  1639. if(!$combinationOne) return JsonService::fail('拼团不存在或已下架');
  1640. $combinationOne['images'] = json_decode($combinationOne['images'],true);
  1641. // $combinationOne['userLike'] = StoreProductRelation::isProductRelation($combinationOne['product_id'],$this->userInfo['uid'],'like');
  1642. // $combinationOne['like_num'] = StoreProductRelation::productRelationNum($combinationOne['product_id'],'like');
  1643. $combinationOne['userCollect'] = StoreProductRelation::isProductRelation($id,$this->userInfo['uid'],'collect','pink_product');
  1644. $pink = StorePink::getPinkAll($id);//拼团列表
  1645. $pindAll = array();
  1646. foreach ($pink as $k=>$v){
  1647. $pink[$k]['count'] = StorePink::getPinkPeople($v['id'],$v['people']);
  1648. $pink[$k]['h'] = date('H',$v['stop_time']);
  1649. $pink[$k]['i'] = date('i',$v['stop_time']);
  1650. $pink[$k]['s'] = date('s',$v['stop_time']);
  1651. $pindAll[] = $v['id'];//开团团长ID
  1652. }
  1653. $user = WechatUser::get($this->userInfo['uid'])->toArray();//用户信息
  1654. $data['pink'] = $pink;
  1655. $data['user'] = $user;
  1656. $data['pindAll'] = $pindAll;
  1657. $data['storeInfo'] = $combinationOne;
  1658. $data['reply'] = StoreProductReply::getRecProductReply($combinationOne['product_id']);
  1659. $data['replyCount'] = StoreProductReply::productValidWhere()->where('product_id',$combinationOne['product_id'])->count();
  1660. // $data['mer_id'] = StoreProduct::where('id',$combinationOne['product_id'])->value('mer_id');
  1661. return JsonService::successful($data);
  1662. }
  1663. /**
  1664. * 开团页面
  1665. * @param int $id
  1666. * @return mixed
  1667. */
  1668. public function get_pink($id = 0){
  1669. if(!$id) return JsonService::fail('参数错误');
  1670. $pink = StorePink::getPinkUserOne($id);
  1671. if(isset($pink['is_refund']) && $pink['is_refund']) {
  1672. if($pink['is_refund'] != $pink['id']){
  1673. $id = $pink['is_refund'];
  1674. return $this->get_pink($id);
  1675. }else{
  1676. return JsonService::fail('订单已退款');
  1677. }
  1678. }
  1679. if(!$pink) return JsonService::fail('参数错误');
  1680. $pinkAll = array();//参团人 不包括团长
  1681. $pinkT = array();//团长
  1682. if($pink['k_id']){
  1683. $pinkAll = StorePink::getPinkMember($pink['k_id']);
  1684. $pinkT = StorePink::getPinkUserOne($pink['k_id']);
  1685. }else{
  1686. $pinkAll = StorePink::getPinkMember($pink['id']);
  1687. $pinkT = $pink;
  1688. }
  1689. $store_combination = StoreCombination::getCombinationOne($pink['cid']);//拼团产品
  1690. $count = count($pinkAll)+1;
  1691. $count = (int)$pinkT['people']-$count;//剩余多少人
  1692. $is_ok = 0;//判断拼团是否完成
  1693. $idAll = array();
  1694. $uidAll = array();
  1695. if(!empty($pinkAll)){
  1696. foreach ($pinkAll as $k=>$v){
  1697. $idAll[$k] = $v['id'];
  1698. $uidAll[$k] = $v['uid'];
  1699. }
  1700. }
  1701. $userBool = 0;//判断当前用户是否在团内 0未在 1在
  1702. $pinkBool = 0;//判断当前用户是否在团内 0未在 1在
  1703. $idAll[] = $pinkT['id'];
  1704. $uidAll[] = $pinkT['uid'];
  1705. if($pinkT['status'] == 2){
  1706. $pinkBool = 1;
  1707. $is_ok = 1;
  1708. }else{
  1709. if(!$count){//组团完成
  1710. $is_ok = 1;
  1711. $idAll = implode(',',$idAll);
  1712. $orderPinkStatus = StorePink::setPinkStatus($idAll);
  1713. if($orderPinkStatus){
  1714. if(in_array($this->userInfo['uid'],$uidAll)){
  1715. StorePink::setPinkStopTime($idAll);
  1716. if(StorePink::isTpl($uidAll,$pinkT['id'])) StorePink::orderPinkAfter($uidAll,$pinkT['id']);
  1717. $pinkBool = 1;
  1718. }else $pinkBool = 3;
  1719. }else $pinkBool = 6;
  1720. }
  1721. else{
  1722. if($pinkT['stop_time'] < time()){//拼团时间超时 退款
  1723. if($pinkAll){
  1724. foreach ($pinkAll as $v){
  1725. if($v['uid'] == $this->userInfo['uid']){
  1726. $res = StoreOrder::orderApplyRefund(StoreOrder::where('id',$v['order_id_key'])->value('order_id'),$this->userInfo['uid'],'拼团时间超时');
  1727. if($res){
  1728. if(StorePink::isTpl($v['uid'],$pinkT['id'])) StorePink::orderPinkAfterNo($v['uid'],$v['k_id']);
  1729. $pinkBool = 2;
  1730. }else return JsonService::fail(StoreOrder::getErrorInfo());
  1731. }
  1732. }
  1733. }
  1734. if($pinkT['uid'] == $this->userInfo['uid']){
  1735. $res = StoreOrder::orderApplyRefund(StoreOrder::where('id',$pinkT['order_id_key'])->value('order_id'),$this->userInfo['uid'],'拼团时间超时');
  1736. if($res){
  1737. if(StorePink::isTpl($pinkT['uid'],$pinkT['id'])) StorePink::orderPinkAfterNo($pinkT['uid'],$pinkT['id']);
  1738. $pinkBool = 2;
  1739. }else return JsonService::fail(StoreOrder::getErrorInfo());
  1740. }
  1741. if(!$pinkBool) $pinkBool = 3;
  1742. }
  1743. }
  1744. }
  1745. $store_combination_host = StoreCombination::getCombinationHost();//获取推荐的拼团产品
  1746. if(!empty($pinkAll)){
  1747. foreach ($pinkAll as $v){
  1748. if($v['uid'] == $this->userInfo['uid']) $userBool = 1;
  1749. }
  1750. }
  1751. if($pinkT['uid'] == $this->userInfo['uid']) $userBool = 1;
  1752. $combinationOne = StoreCombination::getCombinationOne($pink['cid']);
  1753. if(!$combinationOne) return JsonService::fail('拼团不存在或已下架');
  1754. $store_combination['userInfo'] = $this->userInfo;
  1755. $data['pinkBool'] = $pinkBool;
  1756. $data['is_ok'] = $is_ok;
  1757. $data['userBool'] = $userBool;
  1758. $data['store_combination'] = $store_combination;
  1759. $data['pinkT'] = $pinkT;
  1760. $data['pinkAll'] = $pinkAll;
  1761. $data['count'] = $count;
  1762. $data['store_combination_host'] = $store_combination_host;
  1763. $data['current_pink_order'] = StorePink::getCurrentPink($id);
  1764. return JsonService::successful($data);
  1765. }
  1766. /**
  1767. * 购物车库存修改
  1768. * @param int $cartId
  1769. * @param int $cartNum
  1770. */
  1771. public function set_buy_cart_num($cartId = 0,$cartNum = 0){
  1772. if(!$cartId) return JsonService::fail('参数错误');
  1773. $res = StoreCart::edit(['cart_num'=>$cartNum],$cartId);
  1774. if($res) return JsonService::successful();
  1775. else return JsonService::fail('修改失败');
  1776. }
  1777. /**
  1778. * 获取后台联系方式
  1779. */
  1780. public function get_site_phone(){
  1781. $data = SystemConfig::getValue('site_service_phone');
  1782. return JsonService::successful($data);
  1783. }
  1784. /**
  1785. * 获取产品链接的二维码
  1786. * @param string $path
  1787. * @param int $width
  1788. */
  1789. // public function get_pages($path = '',$productId = 0,$width = 430){
  1790. // if($path == '' || !$productId) return JsonService::fail('参数错误'); header('content-type:image/jpg');
  1791. // if(!$this->userInfo['uid']) return JsonService::fail('授权失败,请重新授权');
  1792. // $path = 'public/uploads/routinepage/'.$productId.'.jpg';
  1793. // if(file_exists($path)) return JsonService::successful($path);
  1794. // else file_put_contents($path,RoutineCode::getCode($this->userInfo['uid']));
  1795. // return JsonService::successful($path);
  1796. // }
  1797. /**
  1798. * 文章列表
  1799. * @param int $cid
  1800. * @param int $first
  1801. * @param int $limit
  1802. */
  1803. public function get_cid_article($cid = 0,$first = 0,$limit = 8){
  1804. $list = ArticleModel::cidByArticleList($cid,$first,$limit,'id,title,image_input,visit,add_time,synopsis,url')?:[];
  1805. foreach ($list as &$article){
  1806. $article['add_time'] = date('Y-m-d H:i',$article['add_time']);
  1807. }
  1808. $data['list'] = $list;
  1809. return JsonService::successful($list);
  1810. }
  1811. /**
  1812. * 获取热门文章
  1813. */
  1814. public function get_article_hot(){
  1815. $hot = ArticleModel::getArticleListHot('id,title');
  1816. return JsonService::successful($hot);
  1817. }
  1818. /**
  1819. * 获取热门banner文章
  1820. */
  1821. public function get_article_banner(){
  1822. $banner = ArticleModel::getArticleListBanner('id,title,image_input');
  1823. return JsonService::successful($banner);
  1824. }
  1825. /**
  1826. * 获取文章详情
  1827. * @param int $id
  1828. */
  1829. public function visit($id = 0)
  1830. {
  1831. $content = ArticleModel::getArticleOne($id);
  1832. if(!$content || !$content["status"]) return JsonService::fail('此文章已经不存在!');
  1833. $content["visit"] = $content["visit"] + 1;
  1834. $content['add_time'] = date('Y-m-d H:i:s',$content['add_time']);
  1835. ArticleModel::edit(['visit'=>$content["visit"]],$id);//增加浏览次数
  1836. return JsonService::successful($content);
  1837. }
  1838. public function poster($id = 0){
  1839. if(!$id) return JsonService::fail('参数错误');
  1840. $productInfo = StoreProduct::getValidProduct($id,'store_name,id,price,image,code_path');
  1841. if(empty($productInfo)) return JsonService::fail('参数错误');
  1842. if($productInfo['code_path'] == '') {
  1843. $codePath = 'public/uploads/codepath/product/'.$productInfo['id'].'.jpg';
  1844. if(!file_exists($codePath)){
  1845. $dir = iconv("UTF-8", "GBK", "public/uploads/codepath/product");
  1846. mkdir($dir,0775,true);
  1847. file_put_contents($codePath,RoutineCode::getPages('pages/product-con/index?id='.$productInfo['id']));
  1848. }
  1849. $res = StoreProduct::edit(['code_path'=>$codePath],$id);
  1850. if($res) $productInfo['code_path'] = $codePath;
  1851. else return JsonService::fail('没有查看权限');
  1852. }
  1853. $posterPath = createPoster($productInfo);
  1854. return JsonService::successful($posterPath);
  1855. }
  1856. /**
  1857. * 刷新数据缓存
  1858. */
  1859. public function refresh_cache(){
  1860. `php think optimize:schema`;
  1861. `php think optimize:autoload`;
  1862. `php think optimize:route`;
  1863. `php think optimize:config`;
  1864. }
  1865. }