AuthApi.php 78 KB

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