AuthApi.php 82 KB

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