AuthApi.php 79 KB

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