AuthApi.php 84 KB

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