AuthApi.php 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977
  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']){
  837. $data['refund_reason_wap_img'] = explode(',',$data['refund_reason_wap_img']);
  838. }
  839. if(!$uni || $data['text'] == '') return JsonService::fail('参数错误!');
  840. $res = StoreOrder::orderApplyRefund($uni,$this->userInfo['uid'],$data['text'],$data['refund_reason_wap_explain'],$data['refund_reason_wap_img']);
  841. if($res)
  842. return JsonService::successful();
  843. else
  844. return JsonService::fail(StoreOrder::getErrorInfo());
  845. }
  846. /**
  847. * 用户确认收货
  848. * @param string $uni
  849. * @return \think\response\Json
  850. */
  851. public function user_take_order($uni = '')
  852. {
  853. if(!$uni) return JsonService::fail('参数错误!');
  854. $res = StoreOrder::takeOrder($uni,$this->userInfo['uid']);
  855. if($res)
  856. return JsonService::successful();
  857. else
  858. return JsonService::fail(StoreOrder::getErrorInfo());
  859. }
  860. /**
  861. * 充值
  862. * @param int $price
  863. * @return \think\response\Json
  864. */
  865. public function user_wechat_recharge($price = 0)
  866. {
  867. if(!$price || $price <=0) return JsonService::fail('参数错误');
  868. $storeMinRecharge = SystemConfigService::get('store_user_min_recharge');
  869. if($price < $storeMinRecharge) return JsonService::fail('充值金额不能低于'.$storeMinRecharge);
  870. $rechargeOrder = UserRecharge::addRecharge($this->userInfo['uid'],$price);
  871. if(!$rechargeOrder) return JsonService::fail('充值订单生成失败!');
  872. try{
  873. return JsonService::successful(UserRecharge::jsPay($rechargeOrder));
  874. }catch (\Exception $e){
  875. return JsonService::fail($e->getMessage());
  876. }
  877. }
  878. /**
  879. * 余额使用记录
  880. * @param int $first
  881. * @param int $limit
  882. * @return \think\response\Json
  883. */
  884. public function user_balance_list($first = 0,$limit = 8)
  885. {
  886. $list = UserBill::where('uid',$this->userInfo['uid'])->where('category','now_money')
  887. ->field('mark,pm,number,add_time')
  888. ->where('status',1)->order('add_time DESC')->limit($first,$limit)->select()->toArray();
  889. foreach ($list as &$v){
  890. $v['add_time'] = date('Y/m/d H:i',$v['add_time']);
  891. }
  892. return JsonService::successful($list);
  893. }
  894. /**
  895. * 积分使用记录
  896. * @param int $first
  897. * @param int $limit
  898. * @return \think\response\Json
  899. */
  900. public function user_integral_list($first = 0,$limit = 8)
  901. {
  902. $list = UserBill::where('uid',$this->userInfo['uid'])->where('category','integral')
  903. ->field('mark,pm,number,add_time')
  904. ->where('status',1)->order('add_time DESC')->limit($first,$limit)->select()->toArray();
  905. foreach ($list as &$v){
  906. $v['add_time'] = date('Y/m/d H:i',$v['add_time']);
  907. $v['number'] = floatval($v['number']);
  908. }
  909. return JsonService::successful($list);
  910. }
  911. /**
  912. * 评价订单
  913. * @param string $unique
  914. * @return \think\response\Json
  915. */
  916. public function user_comment_product($unique = '')
  917. {
  918. if(!$unique) return JsonService::fail('参数错误!');
  919. $cartInfo = StoreOrderCartInfo::where('unique',$unique)->find();
  920. $uid = $this->userInfo['uid'];
  921. if(!$cartInfo || $uid != $cartInfo['cart_info']['uid']) return JsonService::fail('评价产品不存在!');
  922. if(StoreProductReply::be(['oid'=>$cartInfo['oid'],'unique'=>$unique]))
  923. return JsonService::fail('该产品已评价!');
  924. $group = UtilService::postMore([
  925. ['comment',''],['pics',[]],['product_score',5],['service_score',5]
  926. ],Request::instance());
  927. $group['comment'] = htmlspecialchars(trim($group['comment']));
  928. if($group['product_score'] < 1) return JsonService::fail('请为产品评分');
  929. else if($group['service_score'] < 1) return JsonService::fail('请为商家服务评分');
  930. if($cartInfo['cart_info']['combination_id']) $productId = $cartInfo['cart_info']['product_id'];
  931. else if($cartInfo['cart_info']['seckill_id']) $productId = $cartInfo['cart_info']['product_id'];
  932. else if($cartInfo['cart_info']['bargain_id']) $productId = $cartInfo['cart_info']['product_id'];
  933. else $productId = $cartInfo['product_id'];
  934. $group = array_merge($group,[
  935. 'uid'=>$uid,
  936. 'oid'=>$cartInfo['oid'],
  937. 'unique'=>$unique,
  938. 'product_id'=>$productId,
  939. 'reply_type'=>'product'
  940. ]);
  941. StoreProductReply::beginTrans();
  942. $res = StoreProductReply::reply($group,'product');
  943. if(!$res) {
  944. StoreProductReply::rollbackTrans();
  945. return JsonService::fail('评价失败!');
  946. }
  947. try{
  948. // HookService::listen('store_product_order_reply',$group,$cartInfo,false,StoreProductBehavior::class);
  949. StoreOrder::checkOrderOver($cartInfo['oid']);
  950. }catch (\Exception $e){
  951. StoreProductReply::rollbackTrans();
  952. return JsonService::fail($e->getMessage());
  953. }
  954. StoreProductReply::commitTrans();
  955. return JsonService::successful();
  956. }
  957. /**
  958. * 上传图片
  959. * @param string $filename
  960. * @return \think\response\Json
  961. */
  962. public function upload(Request $request)
  963. {
  964. $data = UtilService::postMore([['filename','']],$request);
  965. $res = UploadService::image($data['filename'],'store/comment');
  966. if($res->status == 200)
  967. return JsonService::successful('图片上传成功!',['name'=>$res->fileInfo->getSaveName(),'url'=>UploadService::pathToUrl($res->dir)]);
  968. else
  969. return JsonService::fail($res->error);
  970. }
  971. /**
  972. * 获取一级和二级分类
  973. * @return \think\response\Json
  974. */
  975. public function get_product_category()
  976. {
  977. $parentCategory = StoreCategory::pidByCategory(0,'id,cate_name')->toArray();
  978. foreach ($parentCategory as $k=>$category){
  979. $category['child'] = StoreCategory::pidByCategory($category['id'],'id,cate_name')->toArray();
  980. $parentCategory[$k] = $category;
  981. }
  982. return JsonService::successful($parentCategory);
  983. }
  984. /**
  985. * 获取一级推荐人
  986. * @param int $first
  987. * @param int $limit
  988. * @return \think\response\Json
  989. */
  990. public function get_spread_list($first = 0,$limit = 20)
  991. {
  992. $list = User::where('spread_uid',$this->userInfo['uid'])->field('uid,nickname,avatar,add_time')->limit($first,$limit)->order('add_time DESC')->select()->toArray();
  993. foreach ($list as $k=>$user){
  994. $list[$k]['add_time'] = date('Y/m/d',$user['add_time']);
  995. $list[$k]['price'] = StoreOrder::getUserPrice($user['uid']);
  996. }
  997. $count = User::where('spread_uid',$this->userInfo['uid'])->field('uid,nickname,avatar,add_time')->count();
  998. $data['count'] = $count;
  999. $data['list'] = $list;
  1000. return JsonService::successful($data);
  1001. }
  1002. /**
  1003. * 获取二级推荐人
  1004. * @param int $first
  1005. * @param int $limit
  1006. * @return \think\response\Json
  1007. */
  1008. public function get_spread_list_two($two_uid=0,$first = 0,$limit = 20)
  1009. {
  1010. $list = User::where('spread_uid',$two_uid)->field('uid,nickname,avatar,add_time')->limit($first,$limit)->order('add_time DESC')->select()->toArray();
  1011. foreach ($list as $k=>$user){
  1012. $list[$k]['add_time'] = date('Y/m/d',$user['add_time']);
  1013. $list[$k]['price'] = StoreOrder::getUserPrice($user['uid']);
  1014. }
  1015. $count = User::where('spread_uid',$two_uid)->field('uid,nickname,avatar,add_time')->count();
  1016. $data['count'] = $count;
  1017. $data['list'] = $list;
  1018. return JsonService::successful($data);
  1019. }
  1020. /**
  1021. * 领取优惠券
  1022. * @param string $couponId
  1023. * @return \think\response\Json
  1024. */
  1025. public function user_get_coupon($couponId = '')
  1026. {
  1027. if(!$couponId || !is_numeric($couponId)) return JsonService::fail('参数错误!');
  1028. if(StoreCouponIssue::issueUserCoupon($couponId,$this->userInfo['uid'])){
  1029. return JsonService::successful('领取成功');
  1030. }else{
  1031. return JsonService::fail(StoreCouponIssue::getErrorInfo('领取失败!'));
  1032. }
  1033. }
  1034. /**
  1035. * 获取产品评论
  1036. * @param string $productId
  1037. * @param int $first
  1038. * @param int $limit
  1039. * @param string $filter
  1040. * @return \think\response\Json
  1041. */
  1042. public function product_reply_list($productId = '',$first = 0,$limit = 8, $filter = 'all')
  1043. {
  1044. if(!$productId || !is_numeric($productId)) return JsonService::fail('参数错误!');
  1045. $list = StoreProductReply::getProductReplyList($productId,$filter,$first,$limit);
  1046. if($list){
  1047. foreach ($list as $k=>$v){
  1048. foreach ($v['pics'] as $kk=>$vv){
  1049. $list[$k]['pics'] = explode(',',$vv);
  1050. }
  1051. }
  1052. }
  1053. return JsonService::successful($list);
  1054. }
  1055. /**
  1056. * 获取商品属性数据
  1057. * @param string $productId
  1058. * @return \think\response\Json
  1059. */
  1060. public function product_attr_detail($productId = '')
  1061. {
  1062. if(!$productId || !is_numeric($productId)) return JsonService::fail('参数错误!');
  1063. list($productAttr,$productValue) = StoreProductAttr::getProductAttrDetail($productId);
  1064. return JsonService::successful(compact('productAttr','productValue'));
  1065. }
  1066. /**
  1067. * 获取用户所有地址
  1068. * @return \think\response\Json
  1069. */
  1070. public function user_address_list()
  1071. {
  1072. $list = UserAddress::getUserValidAddressList($this->userInfo['uid'],'id,real_name,phone,province,city,district,detail,is_default');
  1073. return JsonService::successful($list);
  1074. }
  1075. /**
  1076. * 用户通知
  1077. * @param int $page
  1078. * @param int $limit
  1079. * @return \think\response\Json
  1080. */
  1081. public function get_notice_list($page = 0, $limit = 8)
  1082. {
  1083. $list = UserNotice::getNoticeList($this->userInfo['uid'],$page,$limit);
  1084. return JsonService::successful($list);
  1085. }
  1086. /**
  1087. * 修改用户通知为已查看
  1088. * @param $nid
  1089. * @return \think\response\Json
  1090. */
  1091. public function see_notice($nid){
  1092. UserNotice::seeNotice($this->userInfo['uid'],$nid);
  1093. return JsonService::successful();
  1094. }
  1095. /**
  1096. * 客服提醒
  1097. * @param Request $request
  1098. * @return \think\response\Json
  1099. */
  1100. public function refresh_msn(Request $request)
  1101. {
  1102. $params = $request->post();
  1103. $remind_where = "mer_id = ".$params["mer_id"]." AND uid = ".$params["uid"]." AND to_uid = ".$params["to_uid"]." AND type = 0 AND remind = 0";
  1104. $remind_list = StoreServiceLog::where($remind_where)->order("add_time asc")->select();
  1105. foreach ($remind_list as $key => $value) {
  1106. if(time() - $value["add_time"] > 3){
  1107. StoreServiceLog::edit(array("remind"=>1),$value["id"]);
  1108. $now_user = StoreService::field("uid,nickname")->where(array("uid"=>$params["uid"]))->find();
  1109. if(!$now_user)$now_user = User::field("uid,nickname")->where(array("uid"=>$params["uid"]))->find();
  1110. if($params["to_uid"]) {
  1111. $head = '您有新的消息,请注意查收!';
  1112. $head .= $params["mer_id"] > 0 ? "\n商户名称:".Merchant::where('id',$params["mer_id"])->value('mer_name') : '';
  1113. WechatTemplateService::sendTemplate(WechatUser::uidToOpenid($params["to_uid"]),WechatTemplateService::SERVICE_NOTICE,[
  1114. 'first'=>$head,
  1115. 'keyword1'=>$now_user["nickname"],
  1116. 'keyword2'=>"客服提醒",
  1117. 'keyword3'=> preg_replace('/<img.*? \/>/','[图片]',$value["msn"]),
  1118. 'keyword4'=>date('Y-m-d H:i:s',time()),
  1119. 'remark'=>'点击立即查看消息'
  1120. ],Url::build('service/service_ing',['to_uid'=>$now_user["uid"],'mer_id'=>$params["mer_id"]],true,true));
  1121. }
  1122. }
  1123. }
  1124. $where = "mer_id = ".$params["mer_id"]." AND uid = ".$params["to_uid"]." AND to_uid = ".$params["uid"]." AND type = 0";
  1125. $list = StoreServiceLog::where($where)->order("add_time asc")->select()->toArray();
  1126. $ids = [];
  1127. foreach ($list as $key => $value) {
  1128. //设置发送人与接收人区别
  1129. if($value["uid"] == $params["uid"])
  1130. $list[$key]['my'] = "my";
  1131. else
  1132. $list[$key]['my'] = "to";
  1133. array_push($ids,$value["id"]);
  1134. }
  1135. //设置这些消息为已读
  1136. StoreServiceLog::where(array("id"=>array("in",$ids)))->update(array("type"=>1,"remind"=>1));
  1137. return JsonService::successful($list);
  1138. }
  1139. public function add_msn(Request $request){
  1140. $params = $request->post();
  1141. if($params["type"] == "html")
  1142. $data["msn"] = htmlspecialchars_decode($params["msn"]);
  1143. else
  1144. $data["msn"] = $params["msn"];
  1145. $data["uid"] = $params["uid"];
  1146. $data["to_uid"] = $params["to_uid"];
  1147. $data["mer_id"] = $params["mer_id"] > 0 ? $params["mer_id"] : 0;
  1148. $data["add_time"] = time();
  1149. StoreServiceLog::set($data);
  1150. return JsonService::successful();
  1151. }
  1152. public function get_msn(Request $request){
  1153. $params = $request->post();
  1154. $size = 10;
  1155. $page = $params["page"]>=0 ? $params["page"] : 1;
  1156. $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"].")";
  1157. $list = StoreServiceLog::where($where)->limit(($page-1)*$size,$size)->order("add_time desc")->select()->toArray();
  1158. foreach ($list as $key => $value) {
  1159. //设置发送人与接收人区别
  1160. if($value["uid"] == $params["uid"])
  1161. $list[$key]['my'] = "my";
  1162. else
  1163. $list[$key]['my'] = "to";
  1164. //设置这些消息为已读
  1165. if($value["uid"] == $params["to_uid"] && $value["to_uid"] == $params["uid"])StoreServiceLog::edit(array("type"=>1,"remind"=>1),$value["id"]);
  1166. }
  1167. $list=array_reverse($list);
  1168. return JsonService::successful($list);
  1169. }
  1170. public function refresh_msn_new(Request $request){
  1171. $params = $request->post();
  1172. $now_user = User::getUserInfo($this->userInfo['uid']);
  1173. if($params["last_time"] > 0)
  1174. $where = "(uid = ".$now_user["uid"]." OR to_uid = ".$now_user["uid"].") AND add_time>".$params["last_time"];
  1175. else
  1176. $where = "uid = ".$now_user["uid"]." OR to_uid = ".$now_user["uid"];
  1177. $msn_list = StoreServiceLog::where($where)->order("add_time desc")->select()->toArray();
  1178. $info_array = $list = [];
  1179. foreach ($msn_list as $key => $value){
  1180. $to_uid = $value["uid"] == $now_user["uid"] ? $value["to_uid"] : $value["uid"];
  1181. if(!in_array(["to_uid"=>$to_uid,"mer_id"=>$value["mer_id"]],$info_array)){
  1182. $info_array[count($info_array)] = ["to_uid"=>$to_uid,"mer_id"=>$value["mer_id"]];
  1183. $to_user = StoreService::field("uid,nickname,avatar")->where(array("uid"=>$to_uid))->find();
  1184. if(!$to_user)$to_user = User::field("uid,nickname,avatar")->where(array("uid"=>$to_uid))->find();
  1185. $to_user["mer_id"] = $value["mer_id"];
  1186. $to_user["mer_name"] = $value["mer_id"] > 0 ? "[".Merchant::where('id',$value["mer_id"])->value('mer_name')."]" : '';
  1187. $value["to_info"] = $to_user;
  1188. $value["count"] = StoreServiceLog::where(array("mer_id"=>$value["mer_id"],"uid"=>$to_uid,"to_uid"=>$now_user["uid"],"type"=>0))->count();
  1189. $list[count($list)] = $value;
  1190. }
  1191. }
  1192. return JsonService::successful($list);
  1193. }
  1194. public function get_user_brokerage_list($uid, $first = 0,$limit = 8)
  1195. {
  1196. if(!$uid)
  1197. return $this->failed('用户不存在');
  1198. $list = UserBill::field('A.mark,A.add_time,A.number,A.pm')->alias('A')->limit($first,$limit)
  1199. ->where('A.category','now_money')->where('A.type','brokerage')
  1200. ->where('A.uid',$this->userInfo['uid'])
  1201. ->join('__STORE_ORDER__ B','A.link_id = B.id AND B.uid = '.$uid)->select()->toArray();
  1202. return JsonService::successful($list);
  1203. }
  1204. /*
  1205. * 申请提现
  1206. */
  1207. // public function user_extract()
  1208. // {
  1209. // if(UserExtract::userExtract($this->userInfo,UtilService::postMore([
  1210. // ['type','','','extract_type'],'real_name','alipay_code','bank_code','bank_address',['price','','','extract_price']
  1211. // ])))
  1212. // return JsonService::successful('申请提现成功!');
  1213. // else
  1214. // return JsonService::fail(Extract::getErrorInfo());
  1215. // }
  1216. public function user_extract()
  1217. { $request = Request::instance();
  1218. $list=$request->param();
  1219. $data=$list['lists'];
  1220. // dump($data);
  1221. // dump($this->userInfo);
  1222. if(UserExtract::userExtract($this->userInfo,$data))
  1223. return JsonService::successful('申请提现成功!');
  1224. else
  1225. return JsonService::fail(UserExtract::getErrorInfo());
  1226. }
  1227. /*
  1228. * 提现列表
  1229. */
  1230. public function extract($first = 0,$limit = 8)
  1231. {
  1232. $list=UserExtract::where('uid',$this->userInfo['uid'])->order('add_time desc')->limit($first,$limit)->select()->toArray();
  1233. foreach($list as &$v){
  1234. $v['add_time']=date('Y/m/d',$v['add_time']);
  1235. }
  1236. return JsonService::successful($list);
  1237. }
  1238. /**
  1239. * 用户下级的订单
  1240. * @param int $first
  1241. * @param int $limit
  1242. */
  1243. public function subordinateOrderlist($first = 0, $limit = 8)
  1244. { $request = Request::instance();
  1245. $lists=$request->param();
  1246. $xuid=$lists['uid'];$status=$lists['status'];
  1247. if($status==0){
  1248. $type='';
  1249. }elseif($status==1){
  1250. $type=4;
  1251. }elseif($status==2){
  1252. $type=3;
  1253. }else{
  1254. return false;
  1255. }
  1256. if($xuid==0){
  1257. $arr=User::where('spread_uid',$this->userInfo['uid'])->column('uid');
  1258. foreach($arr as $v){
  1259. $list = StoreOrder::getUserOrderList($v,$type,$first,$limit);
  1260. }
  1261. }else{
  1262. $list = StoreOrder::getUserOrderList($xuid,$type,$first,$limit);
  1263. }
  1264. foreach ($list as $k=>$order){
  1265. $list[$k] = StoreOrder::tidyOrder($order,true);
  1266. if($list[$k]['_status']['_type'] == 3){
  1267. foreach ($order['cartInfo']?:[] as $key=>$product){
  1268. $list[$k]['cartInfo'][$key]['is_reply'] = StoreProductReply::isReply($product['unique'],'product');
  1269. }
  1270. }
  1271. }
  1272. return JsonService::successful($list);
  1273. }
  1274. /**
  1275. * 用户下级的订单
  1276. * @param int $first
  1277. * @param int $limit
  1278. */
  1279. public function subordinateOrderlistmoney()
  1280. {
  1281. $arr=User::where('spread_uid',$this->userInfo['uid'])->column('uid');
  1282. foreach($arr as $v){
  1283. $list = StoreOrder::getUserOrderList($v,$type='');
  1284. }
  1285. foreach ($list as $k=>$v){
  1286. $arr[]=$v['pay_price'];
  1287. }
  1288. $cont=count($list);
  1289. $sum=array_sum($arr);
  1290. return JsonService::successful(['cont'=>$cont,'sum'=>$sum]);
  1291. }
  1292. /*
  1293. * 昨日推广佣金
  1294. */
  1295. public function yesterdayCommission(){
  1296. $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');
  1297. $sum= array_sum($money);
  1298. return JsonService::successful($sum);
  1299. }
  1300. /*
  1301. * 累计已提金额
  1302. */
  1303. public function extractsum(){
  1304. $money=UserExtract::where('uid',$this->userInfo['uid'])->where('status',1)->column('extract_price');
  1305. $sum= array_sum($money);
  1306. return JsonService::successful($sum);
  1307. }
  1308. /**
  1309. * 获取一条优惠券
  1310. * @param int $couponId
  1311. * @return \think\response\Json
  1312. */
  1313. public function get_coupon_rope($couponId = 0){
  1314. if(!$couponId) return JsonService::fail('参数错误');
  1315. $couponUser = StoreCouponUser::validAddressWhere()->where('id',$couponId)->where('uid',$this->userInfo['uid'])->find();
  1316. return JsonService::successful($couponUser);
  1317. }
  1318. /**
  1319. * 获取 可以领取的优惠券
  1320. * @param int $limit
  1321. * @return \think\response\Json
  1322. */
  1323. public function get_issue_coupon_list($limit = 2)
  1324. {
  1325. $list = StoreCouponIssue::validWhere('A')->join('__STORE_COUPON__ B','A.cid = B.id')
  1326. ->field('A.*,B.coupon_price,B.use_min_price')->order('B.sort DESC,A.id DESC')->limit($limit)->select()->toArray()?:[];
  1327. foreach ($list as $k=>$v){
  1328. $list[$k]['is_use'] = StoreCouponIssueUser::be(['uid'=>$this->userInfo['uid'],'issue_coupon_id'=>$v['id']]);
  1329. }
  1330. return JsonService::successful($list);
  1331. }
  1332. public function clear_cache($uni = '')
  1333. {
  1334. if($uni)CacheService::clear();
  1335. }
  1336. /**
  1337. * 获取今天正在拼团的人的头像和名称
  1338. * @return \think\response\Json
  1339. */
  1340. public function get_pink_second_one(){
  1341. $addTime = mt_rand(time()-30000,time());
  1342. $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();
  1343. return JsonService::successful($storePink);
  1344. }
  1345. /**
  1346. * 再来一单
  1347. * @param string $uni
  1348. */
  1349. public function order_details($uni = ''){
  1350. if(!$uni) return JsonService::fail('参数错误!');
  1351. $order = StoreOrder::getUserOrderDetail($this->userInfo['uid'],$uni);
  1352. if(!$order) return JsonService::fail('订单不存在!');
  1353. $order = StoreOrder::tidyOrder($order,true);
  1354. $res = array();
  1355. foreach ($order['cartInfo'] as $v) {
  1356. if($v['combination_id']) return JsonService::fail('拼团产品不能再来一单,请在拼团产品内自行下单!');
  1357. 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);
  1358. }
  1359. $cateId = [];
  1360. foreach ($res as $v){
  1361. if(!$v) return JsonService::fail('再来一单失败,请重新下单!');
  1362. $cateId[] = $v['id'];
  1363. }
  1364. return JsonService::successful('ok',implode(',',$cateId));
  1365. }
  1366. /**
  1367. * 获取分销二维码
  1368. * @return \think\response\Json
  1369. */
  1370. public function get_code(){
  1371. header('content-type:image/jpg');
  1372. if(!$this->userInfo['uid']) return JsonService::fail('授权失败,请重新授权');
  1373. $path = 'public/uploads/routine/'.$this->userInfo['uid'].'.jpg';
  1374. $domain=SystemConfigService::get('site_url').'/';
  1375. if(file_exists($path)) return JsonService::successful($domain.$path);
  1376. else file_put_contents($path,RoutineCode::getCode($this->userInfo['uid']));
  1377. return JsonService::successful($domain.$path);
  1378. }
  1379. /**
  1380. * 绑定推荐人
  1381. * @param Request $request
  1382. * @return \think\response\Json
  1383. */
  1384. public function spread_uid(Request $request){
  1385. $data = UtilService::postMore(['spread_uid',0],$request);
  1386. if($data['spread_uid']){
  1387. if(!$this->userInfo['spread_uid']){
  1388. $res = User::edit(['spread_uid'=>$data['spread_uid']],$this->userInfo['uid']);
  1389. if($res) return JsonService::successful('绑定成功');
  1390. else return JsonService::successful('绑定失败');
  1391. }else return JsonService::fail('已存在被推荐人');
  1392. }else return JsonService::fail('没有推荐人');
  1393. }
  1394. /**
  1395. * 获取砍价列表
  1396. * @return \think\response\Json
  1397. */
  1398. public function get_bargain_list($limit = 10){
  1399. $bargain = StoreBargain::getList();
  1400. $bargain = StoreBargainUser::getUserList($bargain,$limit);
  1401. $lovely = GroupDataService::getData('routine_lovely')?:[];//banner图
  1402. $banner = GroupDataService::getData('bargain_banner')?:[];//banner图
  1403. $banner = $banner[0];
  1404. $bargainUser = StoreBargainUser::getBargainUserStatusSuccess();
  1405. $data['bargain'] = $bargain;
  1406. $data['lovely'] = $lovely;
  1407. $data['banner'] = $banner;
  1408. $data['bargainUser'] = $bargainUser;
  1409. return JsonService::successful($data);
  1410. }
  1411. /**
  1412. * 砍价详情
  1413. * @param int $bargainId
  1414. * @return \think\response\Json
  1415. */
  1416. public function get_bargain($bargainId = 0){
  1417. if(!$bargainId) return JsonService::fail('参数错误');
  1418. $bargain = StoreBargain::getBargainTerm($bargainId);
  1419. if(empty($bargain)) return JsonService::fail('砍价已结束');
  1420. $bargain['time'] = time();
  1421. return JsonService::successful($bargain);
  1422. }
  1423. /**
  1424. * 获取人数
  1425. * @param int $count
  1426. * @return \think\response\Json
  1427. */
  1428. public function get_bargain_count($count = 0){
  1429. $data['lookCount'] = StoreBargain::getBargainLook()['look'];//观看人数
  1430. $data['shareCount'] = StoreBargain::getBargainShare()['share'];//观看人数
  1431. $data['userCount'] = StoreBargainUser::count();//参与人数
  1432. return JsonService::successful($data);
  1433. }
  1434. /**
  1435. * 添加砍价分享次数
  1436. * @param int $bargainId
  1437. */
  1438. public function add_share_bargain($bargainId = 0){
  1439. if(!$bargainId) return JsonService::successful();
  1440. StoreBargain::addBargainShare($bargainId);
  1441. return JsonService::successful();
  1442. }
  1443. /**
  1444. * 添加砍价浏览次数
  1445. * @param int $bargainId
  1446. */
  1447. public function add_look_bargain($bargainId = 0){
  1448. if(!$bargainId) return JsonService::successful();
  1449. StoreBargain::addBargainLook($bargainId);
  1450. return JsonService::successful();
  1451. }
  1452. /**
  1453. * 获取砍价帮
  1454. * @param int $bargainId
  1455. * @param int $uid
  1456. * @return \think\response\Json
  1457. */
  1458. public function get_bargain_user($bargainId = 0,$bargainUid = 0,$limit = 15){
  1459. if(!$bargainId || !$bargainUid) return JsonService::fail('参数错误');
  1460. $bargainUserTableId = StoreBargainUser::setUserBargain($bargainId,$bargainUid);
  1461. $storeBargainUserHelp = StoreBargainUserHelp::getList($bargainUserTableId,$limit);
  1462. return JsonService::successful($storeBargainUserHelp);
  1463. }
  1464. /**
  1465. * 我的砍价
  1466. * @param int $bargainId
  1467. * @return \think\response\Json
  1468. */
  1469. public function mycut($bargainId = 0){
  1470. if(!$bargainId ) return JsonService::fail('参数错误');
  1471. $data= StoreBargainUser::where('bargain_id',$bargainId)->where('uid',$this->userInfo['uid'])->where('status',1)->find();
  1472. return JsonService::successful($data);
  1473. }
  1474. /**
  1475. * 参与砍价产品
  1476. * @param int $bargainId
  1477. * @return \think\response\Json
  1478. */
  1479. public function set_bargain($bargainId = 0){
  1480. if(!$bargainId) return JsonService::fail('参数错误');
  1481. $res = StoreBargainUser::setBargain($bargainId,$this->userInfo['uid']);
  1482. if($res) {
  1483. $data['id'] = $res->id;
  1484. return JsonService::successful('参与成功',$data);
  1485. }
  1486. else return JsonService::fail('参与失败');
  1487. }
  1488. /**
  1489. * 判断当前登录人是否参与砍价
  1490. * @param int $bargainId
  1491. * @return \think\response\Json
  1492. */
  1493. public function is_bargain_user($bargainId = 0){
  1494. if(!$bargainId) return JsonService::fail('参数错误');
  1495. $data=StoreBargainUser::isBargainUser($bargainId,$this->userInfo['uid']);
  1496. if($data) return JsonService::successful($data);
  1497. else return JsonService::fail('没有参与砍价');
  1498. }
  1499. /*
  1500. * 已砍掉的金额
  1501. */
  1502. public function speed_of_progress($bargainId = 0){
  1503. if(!$bargainId) return JsonService::fail('参数错误');
  1504. $price= StoreBargainUser::where('bargain_id',$bargainId)->where('status',1)->where('uid',$this->userInfo['uid'])->value('price');
  1505. return JsonService::successful($price);
  1506. }
  1507. /**
  1508. * 帮好友砍价
  1509. * @param int $bargainId
  1510. * @param int $bargainUserId
  1511. * @return \think\response\Json
  1512. */
  1513. public function set_bargain_help($bargainId = 0,$bargainUserId = 0){
  1514. if(!$bargainId || !$bargainUserId) return JsonService::fail('参数错误');
  1515. $res = StoreBargainUserHelp::setBargainUserHelp($bargainId,$bargainUserId,$this->userInfo['uid']);
  1516. if($res) {
  1517. if(!StoreBargainUserHelp::getSurplusPrice($bargainId,$bargainUserId)){
  1518. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId,$bargainUserId);
  1519. $bargain = StoreBargain::where('id',$bargainId)->find()->toArray();
  1520. $bargainUser = StoreBargainUser::where('id',$bargainUserTableId)->find()->toArray();
  1521. RoutineTemplate::sendBargainSuccess($bargain,$bargainUser,$bargainUserId);//发送模板消息
  1522. }
  1523. return JsonService::successful('砍价成功',$res);
  1524. }
  1525. else return JsonService::fail('砍价失败');
  1526. }
  1527. /**
  1528. * 获取砍价帮总人数、剩余金额、进度条
  1529. * @param int $bargainId
  1530. * @param int $bargainUserId
  1531. * @return \think\response\Json
  1532. */
  1533. public function get_bargain_help_count($bargainId = 0,$bargainUserId = 0){
  1534. if(!$bargainId || !$bargainUserId) return JsonService::fail('参数错误');
  1535. $count = StoreBargainUserHelp::getBargainUserHelpPeopleCount($bargainId,$bargainUserId);
  1536. $price = StoreBargainUserHelp::getSurplusPrice($bargainId,$bargainUserId);
  1537. $pricePercent = StoreBargainUserHelp::getSurplusPricePercent($bargainId,$bargainUserId);
  1538. $data['count'] = $count;
  1539. $data['price'] = $price;
  1540. $data['pricePercent'] = $pricePercent;
  1541. return JsonService::successful($data);
  1542. }
  1543. /**
  1544. * 判断当前砍价是否开启
  1545. * @param int $bargainId
  1546. * @return \think\response\Json
  1547. */
  1548. public function is_bargain_status($bargainId = 0){
  1549. if(!$bargainId) return JsonService::fail('参数错误');
  1550. if(!StoreBargain::setBargainStatus($bargainId)) return JsonService::successful();
  1551. else return JsonService::fail();
  1552. }
  1553. /**
  1554. * 判断用户是否可以砍价
  1555. * @param int $bargainId
  1556. * @param int $bargainUserId
  1557. */
  1558. public function is_bargain_user_help($bargainId = 0,$bargainUserId = 0){
  1559. if(!$bargainId || !$bargainUserId) return JsonService::fail('参数错误');
  1560. if(StoreBargainUserHelp::isBargainUserHelpCount($bargainId,$bargainUserId,$this->userInfo['uid'])) return JsonService::successful('请稍后在帮助好友砍价');
  1561. else return JsonService::fail('您不能再帮忙砍价了');
  1562. }
  1563. /**
  1564. * 修改砍价状态为失败
  1565. * @param int $bargainUserTableId
  1566. */
  1567. public function set_user_bargain_status($bargainUserTableId = 0){
  1568. if(!$bargainUserTableId) return JsonService::fail('参数错误');
  1569. if(StoreBargainUser::editBargainUserStatus($bargainUserTableId)) return JsonService::successful('ok');
  1570. else return JsonService::fail('no');
  1571. }
  1572. /**
  1573. * 获取用户信息
  1574. * @param int $uid
  1575. * @return \think\response\Json
  1576. */
  1577. public function get_user_info_uid($userId = 0){
  1578. if(!$userId) return JsonService::fail('参数错误');
  1579. $res = User::getUserInfo($userId);
  1580. if($res) return JsonService::successful($res);
  1581. else return JsonService::fail(User::getErrorInfo());
  1582. }
  1583. /**
  1584. * 获取砍价产品 个人中心 我的砍价
  1585. */
  1586. public function get_user_bargain_all(){
  1587. $list = StoreBargainUser::getBargainUserAll($this->userInfo['uid']);
  1588. if($list){
  1589. foreach ($list as $k=>$v){
  1590. $list[$k]['helpCount'] = StoreBargainUserHelp::getBargainUserHelpPeopleCount($v['bargain_id'],$this->userInfo['uid']);
  1591. }
  1592. return JsonService::successful($list);
  1593. }else return JsonService::fail('暂无参与砍价');
  1594. }
  1595. /*
  1596. * 查物流
  1597. */
  1598. public function express($uid,$uni = '')
  1599. {
  1600. if(!$uni || !($order = StoreOrder::getUserOrderDetail($uid,$uni))) return JsonService::fail('查询订单不存在!');
  1601. if($order['delivery_type'] != 'express' || !$order['delivery_id']) return JsonService::fail('该订单不存在快递单号!');
  1602. $cacheName = $uni.$order['delivery_id'];
  1603. CacheService::rm($cacheName);
  1604. $result = CacheService::get($cacheName,null);
  1605. if($result === NULL){
  1606. $result = Express::query($order['delivery_id']);
  1607. if(is_array($result) &&
  1608. isset($result['result']) &&
  1609. isset($result['result']['deliverystatus']) &&
  1610. $result['result']['deliverystatus'] >= 3)
  1611. $cacheTime = 0;
  1612. else
  1613. $cacheTime = 1800;
  1614. CacheService::set($cacheName,$result,$cacheTime);
  1615. }
  1616. if($result) return JsonService::successful([ 'order'=>$order, 'express'=>$result]);
  1617. }
  1618. /**
  1619. * 收集发送模板信息的formID
  1620. * @param string $formId
  1621. */
  1622. public function get_form_id($formId = ''){
  1623. if((int)$formId == '' || $formId == 'the formId is a mock one') return JsonService::fail('no');
  1624. $data['form_id'] = $formId;
  1625. $data['uid'] = $this->userInfo['uid'];
  1626. $data['status'] = 1;
  1627. $data['stop_time'] = bcadd(time(),bcmul(6,86400,0),0);
  1628. RoutineFormId::set($data);
  1629. return JsonService::successful();
  1630. }
  1631. /**
  1632. * 获取拼团列表
  1633. * @param int $offset
  1634. * @param int $limit
  1635. */
  1636. public function get_combination_list($offset=0,$limit=20){
  1637. $store_combination = StoreCombination::getAll($offset,$limit);
  1638. return JsonService::successful($store_combination);
  1639. }
  1640. public function get_combination_list_banner(){
  1641. $lovely = GroupDataService::getData('routine_lovely')?:[];//banner图
  1642. return JsonService::successful($lovely[2]);
  1643. }
  1644. /**
  1645. * 获取拼团产品详情
  1646. * @param int $id
  1647. */
  1648. public function combination_detail($id = 0){
  1649. if(!$id) return JsonService::fail('拼团不存在或已下架');
  1650. $combinationOne = StoreCombination::getCombinationOne($id);
  1651. if(!$combinationOne) return JsonService::fail('拼团不存在或已下架');
  1652. $combinationOne['images'] = json_decode($combinationOne['images'],true);
  1653. // $combinationOne['userLike'] = StoreProductRelation::isProductRelation($combinationOne['product_id'],$this->userInfo['uid'],'like');
  1654. // $combinationOne['like_num'] = StoreProductRelation::productRelationNum($combinationOne['product_id'],'like');
  1655. $combinationOne['userCollect'] = StoreProductRelation::isProductRelation($id,$this->userInfo['uid'],'collect','pink_product');
  1656. $pink = StorePink::getPinkAll($id);//拼团列表
  1657. $pindAll = array();
  1658. foreach ($pink as $k=>$v){
  1659. $pink[$k]['count'] = StorePink::getPinkPeople($v['id'],$v['people']);
  1660. $pink[$k]['h'] = date('H',$v['stop_time']);
  1661. $pink[$k]['i'] = date('i',$v['stop_time']);
  1662. $pink[$k]['s'] = date('s',$v['stop_time']);
  1663. $pindAll[] = $v['id'];//开团团长ID
  1664. }
  1665. $user = WechatUser::get($this->userInfo['uid'])->toArray();//用户信息
  1666. $data['pink'] = $pink;
  1667. $data['user'] = $user;
  1668. $data['pindAll'] = $pindAll;
  1669. $data['storeInfo'] = $combinationOne;
  1670. $data['reply'] = StoreProductReply::getRecProductReply($combinationOne['product_id']);
  1671. $data['replyCount'] = StoreProductReply::productValidWhere()->where('product_id',$combinationOne['product_id'])->count();
  1672. // $data['mer_id'] = StoreProduct::where('id',$combinationOne['product_id'])->value('mer_id');
  1673. return JsonService::successful($data);
  1674. }
  1675. /**
  1676. * 开团页面
  1677. * @param int $id
  1678. * @return mixed
  1679. */
  1680. public function get_pink($id = 0){
  1681. if(!$id) return JsonService::fail('参数错误');
  1682. $pink = StorePink::getPinkUserOne($id);
  1683. if(isset($pink['is_refund']) && $pink['is_refund']) {
  1684. if($pink['is_refund'] != $pink['id']){
  1685. $id = $pink['is_refund'];
  1686. return $this->get_pink($id);
  1687. }else{
  1688. return JsonService::fail('订单已退款');
  1689. }
  1690. }
  1691. if(!$pink) return JsonService::fail('参数错误');
  1692. $pinkAll = array();//参团人 不包括团长
  1693. $pinkT = array();//团长
  1694. if($pink['k_id']){
  1695. $pinkAll = StorePink::getPinkMember($pink['k_id']);
  1696. $pinkT = StorePink::getPinkUserOne($pink['k_id']);
  1697. }else{
  1698. $pinkAll = StorePink::getPinkMember($pink['id']);
  1699. $pinkT = $pink;
  1700. }
  1701. $store_combination = StoreCombination::getCombinationOne($pink['cid']);//拼团产品
  1702. $count = count($pinkAll)+1;
  1703. $count = (int)$pinkT['people']-$count;//剩余多少人
  1704. $is_ok = 0;//判断拼团是否完成
  1705. $idAll = array();
  1706. $uidAll = array();
  1707. if(!empty($pinkAll)){
  1708. foreach ($pinkAll as $k=>$v){
  1709. $idAll[$k] = $v['id'];
  1710. $uidAll[$k] = $v['uid'];
  1711. }
  1712. }
  1713. $userBool = 0;//判断当前用户是否在团内 0未在 1在
  1714. $pinkBool = 0;//判断当前用户是否在团内 0未在 1在
  1715. $idAll[] = $pinkT['id'];
  1716. $uidAll[] = $pinkT['uid'];
  1717. if($pinkT['status'] == 2){
  1718. $pinkBool = 1;
  1719. $is_ok = 1;
  1720. }else{
  1721. if(!$count){//组团完成
  1722. $is_ok = 1;
  1723. $idAll = implode(',',$idAll);
  1724. $orderPinkStatus = StorePink::setPinkStatus($idAll);
  1725. if($orderPinkStatus){
  1726. if(in_array($this->userInfo['uid'],$uidAll)){
  1727. StorePink::setPinkStopTime($idAll);
  1728. if(StorePink::isTpl($uidAll,$pinkT['id'])) StorePink::orderPinkAfter($uidAll,$pinkT['id']);
  1729. $pinkBool = 1;
  1730. }else $pinkBool = 3;
  1731. }else $pinkBool = 6;
  1732. }
  1733. else{
  1734. if($pinkT['stop_time'] < time()){//拼团时间超时 退款
  1735. if($pinkAll){
  1736. foreach ($pinkAll as $v){
  1737. if($v['uid'] == $this->userInfo['uid']){
  1738. $res = StoreOrder::orderApplyRefund(StoreOrder::where('id',$v['order_id_key'])->value('order_id'),$this->userInfo['uid'],'拼团时间超时');
  1739. if($res){
  1740. if(StorePink::isTpl($v['uid'],$pinkT['id'])) StorePink::orderPinkAfterNo($v['uid'],$v['k_id']);
  1741. $pinkBool = 2;
  1742. }else return JsonService::fail(StoreOrder::getErrorInfo());
  1743. }
  1744. }
  1745. }
  1746. if($pinkT['uid'] == $this->userInfo['uid']){
  1747. $res = StoreOrder::orderApplyRefund(StoreOrder::where('id',$pinkT['order_id_key'])->value('order_id'),$this->userInfo['uid'],'拼团时间超时');
  1748. if($res){
  1749. if(StorePink::isTpl($pinkT['uid'],$pinkT['id'])) StorePink::orderPinkAfterNo($pinkT['uid'],$pinkT['id']);
  1750. $pinkBool = 2;
  1751. }else return JsonService::fail(StoreOrder::getErrorInfo());
  1752. }
  1753. if(!$pinkBool) $pinkBool = 3;
  1754. }
  1755. }
  1756. }
  1757. $store_combination_host = StoreCombination::getCombinationHost();//获取推荐的拼团产品
  1758. if(!empty($pinkAll)){
  1759. foreach ($pinkAll as $v){
  1760. if($v['uid'] == $this->userInfo['uid']) $userBool = 1;
  1761. }
  1762. }
  1763. if($pinkT['uid'] == $this->userInfo['uid']) $userBool = 1;
  1764. $combinationOne = StoreCombination::getCombinationOne($pink['cid']);
  1765. if(!$combinationOne) return JsonService::fail('拼团不存在或已下架');
  1766. $store_combination['userInfo'] = $this->userInfo;
  1767. $data['pinkBool'] = $pinkBool;
  1768. $data['is_ok'] = $is_ok;
  1769. $data['userBool'] = $userBool;
  1770. $data['store_combination'] = $store_combination;
  1771. $data['pinkT'] = $pinkT;
  1772. $data['pinkAll'] = $pinkAll;
  1773. $data['count'] = $count;
  1774. $data['store_combination_host'] = $store_combination_host;
  1775. $data['current_pink_order'] = StorePink::getCurrentPink($id);
  1776. return JsonService::successful($data);
  1777. }
  1778. /**
  1779. * 购物车库存修改
  1780. * @param int $cartId
  1781. * @param int $cartNum
  1782. */
  1783. public function set_buy_cart_num($cartId = 0,$cartNum = 0){
  1784. if(!$cartId) return JsonService::fail('参数错误');
  1785. $res = StoreCart::edit(['cart_num'=>$cartNum],$cartId);
  1786. if($res) return JsonService::successful();
  1787. else return JsonService::fail('修改失败');
  1788. }
  1789. /**
  1790. * 获取后台联系方式
  1791. */
  1792. public function get_site_phone(){
  1793. $data = SystemConfig::getValue('site_service_phone');
  1794. return JsonService::successful($data);
  1795. }
  1796. /**
  1797. * 获取产品链接的二维码
  1798. * @param string $path
  1799. * @param int $width
  1800. */
  1801. public function get_pages($path = '',$productId = 0,$width = 430){
  1802. if($path == '' || !$productId) return JsonService::fail('参数错误'); header('content-type:image/jpg');
  1803. if(!$this->userInfo['uid']) return JsonService::fail('授权失败,请重新授权');
  1804. $path = 'public/uploads/routinepage/'.$productId.'.jpg';
  1805. if(file_exists($path)) return JsonService::successful($path);
  1806. else file_put_contents($path,RoutineCode::getCode($this->userInfo['uid']));
  1807. return JsonService::successful($path);
  1808. }
  1809. /**
  1810. * 文章列表
  1811. * @param int $cid
  1812. * @param int $first
  1813. * @param int $limit
  1814. */
  1815. public function get_cid_article($cid = 0,$first = 0,$limit = 8){
  1816. $list = ArticleModel::cidByArticleList($cid,$first,$limit,'id,title,image_input,visit,add_time,synopsis,url')?:[];
  1817. foreach ($list as &$article){
  1818. $article['add_time'] = date('Y-m-d H:i',$article['add_time']);
  1819. }
  1820. $data['list'] = $list;
  1821. return JsonService::successful($list);
  1822. }
  1823. /**
  1824. * 获取热门文章
  1825. */
  1826. public function get_article_hot(){
  1827. $hot = ArticleModel::getArticleListHot('id,title');
  1828. return JsonService::successful($hot);
  1829. }
  1830. /**
  1831. * 获取热门banner文章
  1832. */
  1833. public function get_article_banner(){
  1834. $banner = ArticleModel::getArticleListBanner('id,title,image_input');
  1835. return JsonService::successful($banner);
  1836. }
  1837. /**
  1838. * 获取文章详情
  1839. * @param int $id
  1840. */
  1841. public function visit($id = 0)
  1842. {
  1843. $content = ArticleModel::getArticleOne($id);
  1844. if(!$content || !$content["status"]) return JsonService::fail('此文章已经不存在!');
  1845. $content["visit"] = $content["visit"] + 1;
  1846. $content['add_time'] = date('Y-m-d H:i:s',$content['add_time']);
  1847. ArticleModel::edit(['visit'=>$content["visit"]],$id);//增加浏览次数
  1848. return JsonService::successful($content);
  1849. }
  1850. /**
  1851. * 刷新数据缓存
  1852. */
  1853. public function refresh_cache(){
  1854. `php think optimize:schema`;
  1855. `php think optimize:autoload`;
  1856. `php think optimize:route`;
  1857. `php think optimize:config`;
  1858. }
  1859. }