AuthApi.php 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026
  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 = makePathToUrl('routine/code');
  1355. if($path == '')
  1356. return JsonService::fail('生成上传目录失败,请检查权限!');
  1357. $picname = $path.DS.$this->userInfo['uid'].'.jpg';
  1358. $domain = SystemConfigService::get('site_url').'/';
  1359. $domainTop = substr($domain,0,5);
  1360. if($domainTop != 'https') $domain = 'https:'.substr($domain,5,strlen($domain));
  1361. if(file_exists($picname)) return JsonService::successful($domain.$picname);
  1362. else{
  1363. $res = RoutineCode::getCode($this->userInfo['uid'],$picname);
  1364. if($res) file_put_contents($picname,$res);
  1365. else return JsonService::fail('二维码生成失败');
  1366. }
  1367. return JsonService::successful($domain.$picname);
  1368. }
  1369. /**
  1370. * 绑定推荐人
  1371. * @param Request $request
  1372. * @return \think\response\Json
  1373. */
  1374. public function spread_uid(Request $request){
  1375. $data = UtilService::postMore(['spread_uid',0],$request);
  1376. if($data['spread_uid']){
  1377. if(!$this->userInfo['spread_uid']){
  1378. $res = User::edit(['spread_uid'=>$data['spread_uid']],$this->userInfo['uid']);
  1379. if($res) return JsonService::successful('绑定成功');
  1380. else return JsonService::successful('绑定失败');
  1381. }else return JsonService::fail('已存在被推荐人');
  1382. }else return JsonService::fail('没有推荐人');
  1383. }
  1384. /**
  1385. * 获取砍价列表
  1386. * @return \think\response\Json
  1387. */
  1388. public function get_bargain_list($limit = 10){
  1389. $bargain = StoreBargain::getList();
  1390. $bargain = StoreBargainUser::getUserList($bargain,$limit);
  1391. $lovely = GroupDataService::getData('routine_lovely')?:[];//banner图
  1392. $banner = GroupDataService::getData('bargain_banner')?:[];//banner图
  1393. $banner = $banner[0];
  1394. $bargainUser = StoreBargainUser::getBargainUserStatusSuccess();
  1395. $data['bargain'] = $bargain;
  1396. $data['lovely'] = $lovely;
  1397. $data['banner'] = $banner;
  1398. $data['bargainUser'] = $bargainUser;
  1399. return JsonService::successful($data);
  1400. }
  1401. /**
  1402. * 砍价详情
  1403. * @param int $bargainId
  1404. * @return \think\response\Json
  1405. */
  1406. public function get_bargain($bargainId = 0){
  1407. if(!$bargainId) return JsonService::fail('参数错误');
  1408. $bargain = StoreBargain::getBargainTerm($bargainId);
  1409. if(empty($bargain)) return JsonService::fail('砍价已结束');
  1410. $bargain['time'] = time();
  1411. return JsonService::successful($bargain);
  1412. }
  1413. /**
  1414. * 获取人数
  1415. * @param int $count
  1416. * @return \think\response\Json
  1417. */
  1418. public function get_bargain_count($count = 0){
  1419. $data['lookCount'] = StoreBargain::getBargainLook()['look'];//观看人数
  1420. $data['shareCount'] = StoreBargain::getBargainShare()['share'];//观看人数
  1421. $data['userCount'] = StoreBargainUser::count();//参与人数
  1422. return JsonService::successful($data);
  1423. }
  1424. /**
  1425. * 添加砍价分享次数
  1426. * @param int $bargainId
  1427. */
  1428. public function add_share_bargain($bargainId = 0){
  1429. if(!$bargainId) return JsonService::successful();
  1430. StoreBargain::addBargainShare($bargainId);
  1431. return JsonService::successful();
  1432. }
  1433. /**
  1434. * 添加砍价浏览次数
  1435. * @param int $bargainId
  1436. */
  1437. public function add_look_bargain($bargainId = 0){
  1438. if(!$bargainId) return JsonService::successful();
  1439. StoreBargain::addBargainLook($bargainId);
  1440. return JsonService::successful();
  1441. }
  1442. /**
  1443. * 获取砍价帮
  1444. * @param int $bargainId
  1445. * @param int $uid
  1446. * @return \think\response\Json
  1447. */
  1448. public function get_bargain_user($bargainId = 0,$bargainUid = 0,$limit = 15){
  1449. if(!$bargainId || !$bargainUid) return JsonService::fail('参数错误');
  1450. $bargainUserTableId = StoreBargainUser::setUserBargain($bargainId,$bargainUid);
  1451. $storeBargainUserHelp = StoreBargainUserHelp::getList($bargainUserTableId,$limit);
  1452. return JsonService::successful($storeBargainUserHelp);
  1453. }
  1454. /**
  1455. * 我的砍价
  1456. * @param int $bargainId
  1457. * @return \think\response\Json
  1458. */
  1459. public function mycut($bargainId = 0){
  1460. if(!$bargainId ) return JsonService::fail('参数错误');
  1461. $data= StoreBargainUser::where('bargain_id',$bargainId)->where('uid',$this->userInfo['uid'])->where('status',1)->find();
  1462. return JsonService::successful($data);
  1463. }
  1464. /**
  1465. * 参与砍价产品
  1466. * @param int $bargainId
  1467. * @return \think\response\Json
  1468. */
  1469. public function set_bargain($bargainId = 0){
  1470. if(!$bargainId) return JsonService::fail('参数错误');
  1471. $res = StoreBargainUser::setBargain($bargainId,$this->userInfo['uid']);
  1472. if($res) {
  1473. $data['id'] = $res->id;
  1474. return JsonService::successful('参与成功',$data);
  1475. }
  1476. else return JsonService::fail('参与失败');
  1477. }
  1478. /**
  1479. * 判断当前登录人是否参与砍价
  1480. * @param int $bargainId
  1481. * @return \think\response\Json
  1482. */
  1483. public function is_bargain_user($bargainId = 0){
  1484. if(!$bargainId) return JsonService::fail('参数错误');
  1485. $data=StoreBargainUser::isBargainUser($bargainId,$this->userInfo['uid']);
  1486. if($data) return JsonService::successful($data);
  1487. else return JsonService::fail('没有参与砍价');
  1488. }
  1489. /*
  1490. * 已砍掉的金额
  1491. */
  1492. public function speed_of_progress($bargainId = 0){
  1493. if(!$bargainId) return JsonService::fail('参数错误');
  1494. $price= StoreBargainUser::where('bargain_id',$bargainId)->where('status',1)->where('uid',$this->userInfo['uid'])->value('price');
  1495. return JsonService::successful($price);
  1496. }
  1497. /**
  1498. * 帮好友砍价
  1499. * @param int $bargainId
  1500. * @param int $bargainUserId
  1501. * @return \think\response\Json
  1502. */
  1503. public function set_bargain_help($bargainId = 0,$bargainUserId = 0){
  1504. if(!$bargainId || !$bargainUserId) return JsonService::fail('参数错误');
  1505. $res = StoreBargainUserHelp::setBargainUserHelp($bargainId,$bargainUserId,$this->userInfo['uid']);
  1506. if($res) {
  1507. if(!StoreBargainUserHelp::getSurplusPrice($bargainId,$bargainUserId)){
  1508. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId,$bargainUserId);
  1509. $bargain = StoreBargain::where('id',$bargainId)->find()->toArray();
  1510. $bargainUser = StoreBargainUser::where('id',$bargainUserTableId)->find()->toArray();
  1511. RoutineTemplate::sendBargainSuccess($bargain,$bargainUser,$bargainUserId);//发送模板消息
  1512. }
  1513. return JsonService::successful('砍价成功',$res);
  1514. }
  1515. else return JsonService::fail('砍价失败');
  1516. }
  1517. /**
  1518. * 获取砍价帮总人数、剩余金额、进度条
  1519. * @param int $bargainId
  1520. * @param int $bargainUserId
  1521. * @return \think\response\Json
  1522. */
  1523. public function get_bargain_help_count($bargainId = 0,$bargainUserId = 0){
  1524. if(!$bargainId || !$bargainUserId) return JsonService::fail('参数错误');
  1525. $count = StoreBargainUserHelp::getBargainUserHelpPeopleCount($bargainId,$bargainUserId);
  1526. $price = StoreBargainUserHelp::getSurplusPrice($bargainId,$bargainUserId);
  1527. $pricePercent = StoreBargainUserHelp::getSurplusPricePercent($bargainId,$bargainUserId);
  1528. $data['count'] = $count;
  1529. $data['price'] = $price;
  1530. $data['pricePercent'] = $pricePercent;
  1531. return JsonService::successful($data);
  1532. }
  1533. /**
  1534. * 判断当前砍价是否开启
  1535. * @param int $bargainId
  1536. * @return \think\response\Json
  1537. */
  1538. public function is_bargain_status($bargainId = 0){
  1539. if(!$bargainId) return JsonService::fail('参数错误');
  1540. if(!StoreBargain::setBargainStatus($bargainId)) return JsonService::successful();
  1541. else return JsonService::fail();
  1542. }
  1543. /**
  1544. * 判断用户是否可以砍价
  1545. * @param int $bargainId
  1546. * @param int $bargainUserId
  1547. */
  1548. public function is_bargain_user_help($bargainId = 0,$bargainUserId = 0){
  1549. if(!$bargainId || !$bargainUserId) return JsonService::fail('参数错误');
  1550. if(StoreBargainUserHelp::isBargainUserHelpCount($bargainId,$bargainUserId,$this->userInfo['uid'])) return JsonService::successful('请稍后在帮助好友砍价');
  1551. else return JsonService::fail('您不能再帮忙砍价了');
  1552. }
  1553. /**
  1554. * 修改砍价状态为失败
  1555. * @param int $bargainUserTableId
  1556. */
  1557. public function set_user_bargain_status($bargainUserTableId = 0){
  1558. if(!$bargainUserTableId) return JsonService::fail('参数错误');
  1559. if(StoreBargainUser::editBargainUserStatus($bargainUserTableId)) return JsonService::successful('ok');
  1560. else return JsonService::fail('no');
  1561. }
  1562. /**
  1563. * 获取用户信息
  1564. * @param int $uid
  1565. * @return \think\response\Json
  1566. */
  1567. public function get_user_info_uid($userId = 0){
  1568. if(!$userId) return JsonService::fail('参数错误');
  1569. $res = User::getUserInfo($userId);
  1570. if($res) return JsonService::successful($res);
  1571. else return JsonService::fail(User::getErrorInfo());
  1572. }
  1573. /**
  1574. * 获取砍价产品 个人中心 我的砍价
  1575. */
  1576. public function get_user_bargain_all(){
  1577. $list = StoreBargainUser::getBargainUserAll($this->userInfo['uid']);
  1578. if($list){
  1579. foreach ($list as $k=>$v){
  1580. $list[$k]['helpCount'] = StoreBargainUserHelp::getBargainUserHelpPeopleCount($v['bargain_id'],$this->userInfo['uid']);
  1581. }
  1582. return JsonService::successful($list);
  1583. }else return JsonService::fail('暂无参与砍价');
  1584. }
  1585. /*
  1586. * 查物流
  1587. */
  1588. public function express($uid,$uni = '')
  1589. {
  1590. if(!$uni || !($order = StoreOrder::getUserOrderDetail($uid,$uni))) return JsonService::fail('查询订单不存在!');
  1591. if($order['delivery_type'] != 'express' || !$order['delivery_id']) return JsonService::fail('该订单不存在快递单号!');
  1592. $cacheName = $uni.$order['delivery_id'];
  1593. CacheService::rm($cacheName);
  1594. $result = CacheService::get($cacheName,null);
  1595. if($result === NULL){
  1596. $result = Express::query($order['delivery_id']);
  1597. if(is_array($result) &&
  1598. isset($result['result']) &&
  1599. isset($result['result']['deliverystatus']) &&
  1600. $result['result']['deliverystatus'] >= 3)
  1601. $cacheTime = 0;
  1602. else
  1603. $cacheTime = 1800;
  1604. CacheService::set($cacheName,$result,$cacheTime);
  1605. }
  1606. if($result) return JsonService::successful([ 'order'=>$order, 'express'=>$result]);
  1607. }
  1608. /**
  1609. * 收集发送模板信息的formID
  1610. * @param string $formId
  1611. */
  1612. public function get_form_id($formId = ''){
  1613. if((int)$formId == '' || $formId == 'the formId is a mock one') return JsonService::fail('no');
  1614. $data['form_id'] = $formId;
  1615. $data['uid'] = $this->userInfo['uid'];
  1616. $data['status'] = 1;
  1617. $data['stop_time'] = bcadd(time(),bcmul(6,86400,0),0);
  1618. RoutineFormId::set($data);
  1619. return JsonService::successful();
  1620. }
  1621. /**
  1622. * 获取拼团列表
  1623. * @param int $offset
  1624. * @param int $limit
  1625. */
  1626. public function get_combination_list($offset=0,$limit=20){
  1627. $store_combination = StoreCombination::getAll($offset,$limit);
  1628. return JsonService::successful($store_combination);
  1629. }
  1630. public function get_combination_list_banner(){
  1631. $lovely = GroupDataService::getData('routine_lovely')?:[];//banner图
  1632. return JsonService::successful($lovely[2]);
  1633. }
  1634. /**
  1635. * 获取拼团产品详情
  1636. * @param int $id
  1637. */
  1638. public function combination_detail($id = 0){
  1639. if(!$id) return JsonService::fail('拼团不存在或已下架');
  1640. $combinationOne = StoreCombination::getCombinationOne($id);
  1641. if(!$combinationOne) return JsonService::fail('拼团不存在或已下架');
  1642. $combinationOne['images'] = json_decode($combinationOne['images'],true);
  1643. // $combinationOne['userLike'] = StoreProductRelation::isProductRelation($combinationOne['product_id'],$this->userInfo['uid'],'like');
  1644. // $combinationOne['like_num'] = StoreProductRelation::productRelationNum($combinationOne['product_id'],'like');
  1645. $combinationOne['userCollect'] = StoreProductRelation::isProductRelation($id,$this->userInfo['uid'],'collect','pink_product');
  1646. $pink = StorePink::getPinkAll($id);//拼团列表
  1647. $pindAll = array();
  1648. foreach ($pink as $k=>$v){
  1649. $pink[$k]['count'] = StorePink::getPinkPeople($v['id'],$v['people']);
  1650. $pink[$k]['h'] = date('H',$v['stop_time']);
  1651. $pink[$k]['i'] = date('i',$v['stop_time']);
  1652. $pink[$k]['s'] = date('s',$v['stop_time']);
  1653. $pindAll[] = $v['id'];//开团团长ID
  1654. }
  1655. $user = WechatUser::get($this->userInfo['uid'])->toArray();//用户信息
  1656. $data['pink'] = $pink;
  1657. $data['user'] = $user;
  1658. $data['pindAll'] = $pindAll;
  1659. $data['storeInfo'] = $combinationOne;
  1660. $data['reply'] = StoreProductReply::getRecProductReply($combinationOne['product_id']);
  1661. $data['replyCount'] = StoreProductReply::productValidWhere()->where('product_id',$combinationOne['product_id'])->count();
  1662. // $data['mer_id'] = StoreProduct::where('id',$combinationOne['product_id'])->value('mer_id');
  1663. return JsonService::successful($data);
  1664. }
  1665. /**
  1666. * 开团页面
  1667. * @param int $id
  1668. * @return mixed
  1669. */
  1670. public function get_pink($id = 0){
  1671. if(!$id) return JsonService::fail('参数错误');
  1672. $pink = StorePink::getPinkUserOne($id);
  1673. if(isset($pink['is_refund']) && $pink['is_refund']) {
  1674. if($pink['is_refund'] != $pink['id']){
  1675. $id = $pink['is_refund'];
  1676. return $this->get_pink($id);
  1677. }else{
  1678. return JsonService::fail('订单已退款');
  1679. }
  1680. }
  1681. if(!$pink) return JsonService::fail('参数错误');
  1682. $pinkAll = array();//参团人 不包括团长
  1683. $pinkT = array();//团长
  1684. if($pink['k_id']){
  1685. $pinkAll = StorePink::getPinkMember($pink['k_id']);
  1686. $pinkT = StorePink::getPinkUserOne($pink['k_id']);
  1687. }else{
  1688. $pinkAll = StorePink::getPinkMember($pink['id']);
  1689. $pinkT = $pink;
  1690. }
  1691. $store_combination = StoreCombination::getCombinationOne($pink['cid']);//拼团产品
  1692. $count = count($pinkAll)+1;
  1693. $count = (int)$pinkT['people']-$count;//剩余多少人
  1694. $is_ok = 0;//判断拼团是否完成
  1695. $idAll = array();
  1696. $uidAll = array();
  1697. if(!empty($pinkAll)){
  1698. foreach ($pinkAll as $k=>$v){
  1699. $idAll[$k] = $v['id'];
  1700. $uidAll[$k] = $v['uid'];
  1701. }
  1702. }
  1703. $userBool = 0;//判断当前用户是否在团内 0未在 1在
  1704. $pinkBool = 0;//判断当前用户是否在团内 0未在 1在
  1705. $idAll[] = $pinkT['id'];
  1706. $uidAll[] = $pinkT['uid'];
  1707. if($pinkT['status'] == 2){
  1708. $pinkBool = 1;
  1709. $is_ok = 1;
  1710. }else{
  1711. if(!$count){//组团完成
  1712. $is_ok = 1;
  1713. $idAll = implode(',',$idAll);
  1714. $orderPinkStatus = StorePink::setPinkStatus($idAll);
  1715. if($orderPinkStatus){
  1716. if(in_array($this->userInfo['uid'],$uidAll)){
  1717. StorePink::setPinkStopTime($idAll);
  1718. if(StorePink::isTpl($uidAll,$pinkT['id'])) StorePink::orderPinkAfter($uidAll,$pinkT['id']);
  1719. $pinkBool = 1;
  1720. }else $pinkBool = 3;
  1721. }else $pinkBool = 6;
  1722. }
  1723. else{
  1724. if($pinkT['stop_time'] < time()){//拼团时间超时 退款
  1725. if($pinkAll){
  1726. foreach ($pinkAll as $v){
  1727. if($v['uid'] == $this->userInfo['uid']){
  1728. $res = StoreOrder::orderApplyRefund(StoreOrder::where('id',$v['order_id_key'])->value('order_id'),$this->userInfo['uid'],'拼团时间超时');
  1729. if($res){
  1730. if(StorePink::isTpl($v['uid'],$pinkT['id'])) StorePink::orderPinkAfterNo($v['uid'],$v['k_id']);
  1731. $pinkBool = 2;
  1732. }else return JsonService::fail(StoreOrder::getErrorInfo());
  1733. }
  1734. }
  1735. }
  1736. if($pinkT['uid'] == $this->userInfo['uid']){
  1737. $res = StoreOrder::orderApplyRefund(StoreOrder::where('id',$pinkT['order_id_key'])->value('order_id'),$this->userInfo['uid'],'拼团时间超时');
  1738. if($res){
  1739. if(StorePink::isTpl($pinkT['uid'],$pinkT['id'])) StorePink::orderPinkAfterNo($pinkT['uid'],$pinkT['id']);
  1740. $pinkBool = 2;
  1741. }else return JsonService::fail(StoreOrder::getErrorInfo());
  1742. }
  1743. if(!$pinkBool) $pinkBool = 3;
  1744. }
  1745. }
  1746. }
  1747. $store_combination_host = StoreCombination::getCombinationHost();//获取推荐的拼团产品
  1748. if(!empty($pinkAll)){
  1749. foreach ($pinkAll as $v){
  1750. if($v['uid'] == $this->userInfo['uid']) $userBool = 1;
  1751. }
  1752. }
  1753. if($pinkT['uid'] == $this->userInfo['uid']) $userBool = 1;
  1754. $combinationOne = StoreCombination::getCombinationOne($pink['cid']);
  1755. if(!$combinationOne) return JsonService::fail('拼团不存在或已下架');
  1756. $store_combination['userInfo'] = $this->userInfo;
  1757. $data['pinkBool'] = $pinkBool;
  1758. $data['is_ok'] = $is_ok;
  1759. $data['userBool'] = $userBool;
  1760. $data['store_combination'] = $store_combination;
  1761. $data['pinkT'] = $pinkT;
  1762. $data['pinkAll'] = $pinkAll;
  1763. $data['count'] = $count;
  1764. $data['store_combination_host'] = $store_combination_host;
  1765. $data['current_pink_order'] = StorePink::getCurrentPink($id);
  1766. return JsonService::successful($data);
  1767. }
  1768. /**
  1769. * 购物车库存修改
  1770. * @param int $cartId
  1771. * @param int $cartNum
  1772. */
  1773. public function set_buy_cart_num($cartId = 0,$cartNum = 0){
  1774. if(!$cartId) return JsonService::fail('参数错误');
  1775. $res = StoreCart::edit(['cart_num'=>$cartNum],$cartId);
  1776. if($res) return JsonService::successful();
  1777. else return JsonService::fail('修改失败');
  1778. }
  1779. /**
  1780. * 获取后台联系方式
  1781. */
  1782. public function get_site_phone(){
  1783. $data = SystemConfig::getValue('site_service_phone');
  1784. return JsonService::successful($data);
  1785. }
  1786. /**
  1787. * 获取产品链接的二维码
  1788. * @param string $path
  1789. * @param int $width
  1790. */
  1791. // public function get_pages($path = '',$productId = 0,$width = 430){
  1792. // if($path == '' || !$productId) return JsonService::fail('参数错误'); header('content-type:image/jpg');
  1793. // if(!$this->userInfo['uid']) return JsonService::fail('授权失败,请重新授权');
  1794. // $path = 'public/uploads/routinepage/'.$productId.'.jpg';
  1795. // if(file_exists($path)) return JsonService::successful($path);
  1796. // else file_put_contents($path,RoutineCode::getCode($this->userInfo['uid']));
  1797. // return JsonService::successful($path);
  1798. // }
  1799. /**
  1800. * 文章列表
  1801. * @param int $cid
  1802. * @param int $first
  1803. * @param int $limit
  1804. */
  1805. public function get_cid_article($cid = 0,$first = 0,$limit = 8){
  1806. $list = ArticleModel::cidByArticleList($cid,$first,$limit,'id,title,image_input,visit,add_time,synopsis,url')?:[];
  1807. foreach ($list as &$article){
  1808. $article['add_time'] = date('Y-m-d H:i',$article['add_time']);
  1809. }
  1810. $data['list'] = $list;
  1811. return JsonService::successful($list);
  1812. }
  1813. /**
  1814. * 获取热门文章
  1815. */
  1816. public function get_article_hot(){
  1817. $hot = ArticleModel::getArticleListHot('id,title');
  1818. return JsonService::successful($hot);
  1819. }
  1820. /**
  1821. * 获取热门banner文章
  1822. */
  1823. public function get_article_banner(){
  1824. $banner = ArticleModel::getArticleListBanner('id,title,image_input');
  1825. return JsonService::successful($banner);
  1826. }
  1827. /**
  1828. * 获取文章详情
  1829. * @param int $id
  1830. */
  1831. public function visit($id = 0)
  1832. {
  1833. $content = ArticleModel::getArticleOne($id);
  1834. if(!$content || !$content["status"]) return JsonService::fail('此文章已经不存在!');
  1835. $content["visit"] = $content["visit"] + 1;
  1836. $content['add_time'] = date('Y-m-d H:i:s',$content['add_time']);
  1837. ArticleModel::edit(['visit'=>$content["visit"]],$id);//增加浏览次数
  1838. return JsonService::successful($content);
  1839. }
  1840. /**
  1841. * 产品海报二维码
  1842. * @param int $id
  1843. */
  1844. public function product_promotion_code($id = 0){
  1845. if(!$id) return JsonService::fail('参数错误');
  1846. $count = StoreProduct::validWhere()->count();
  1847. if(!$count) return JsonService::fail('参数错误');
  1848. $path = 'public'.DS.'uploads'.DS.'codepath'.DS.'product';
  1849. $codePath = $path.DS.$id.'_'.$this->userInfo['uid'].'.jpg';
  1850. $domain = SystemConfigService::get('site_url').'/';
  1851. if(!file_exists($codePath)){
  1852. if(!is_dir($path)) mkdir($path,0777,true);
  1853. $res = RoutineCode::getCode($this->userInfo['uid'],$codePath,[],'/pages/product-con/index?id='.$id,'product_spread');
  1854. if($res) file_put_contents($codePath,$res);
  1855. else return JsonService::fail('二维码生成失败');
  1856. }
  1857. return JsonService::successful($domain.$codePath);
  1858. }
  1859. public function poster($id = 0){
  1860. if(!$id) return JsonService::fail('参数错误');
  1861. $productInfo = StoreProduct::getValidProduct($id,'store_name,id,price,image,code_path');
  1862. if(empty($productInfo)) return JsonService::fail('参数错误');
  1863. if(strlen($productInfo['code_path'])< 10) {
  1864. $path = 'public'.DS.'uploads'.DS.'codepath'.DS.'product';
  1865. $codePath = $path.DS.$productInfo['id'].'.jpg';
  1866. if(!file_exists($codePath)){
  1867. if(!is_dir($path)) mkdir($path,0777,true);
  1868. $res = file_put_contents($codePath,RoutineCode::getPages('pages/product-con/index?id='.$productInfo['id']));
  1869. }
  1870. $res = StoreProduct::edit(['code_path'=>$codePath],$id);
  1871. if($res) $productInfo['code_path'] = $codePath;
  1872. else return JsonService::fail('没有查看权限');
  1873. }
  1874. $posterPath = createPoster($productInfo);
  1875. return JsonService::successful($posterPath);
  1876. // if(!$id) return JsonService::fail('参数错误');
  1877. // $productInfo = StoreProduct::getValidProduct($id,'store_name,id,price,image,code_path');
  1878. // if(empty($productInfo)) return JsonService::fail('参数错误');
  1879. // if($productInfo['code_path'] == '') {
  1880. // $path = 'public'.DS.'uploads'.DS.'codepath'.DS.'product';
  1881. // $codePath = $path.DS.$productInfo['id'].'.jpg';
  1882. // if(!file_exists($codePath)){
  1883. // //$dir = iconv("UTF-8", "GBK", "public".DS."uploads".DS."codepath".DS."product");
  1884. // if(!is_dir($path))
  1885. // mkdir($path,0777,true);
  1886. // file_put_contents($codePath,RoutineCode::getPages('pages/product-con/index?id='.$productInfo['id']));
  1887. // }
  1888. // $res = StoreProduct::edit(['code_path'=>$codePath],$id);
  1889. // if($res) $productInfo['code_path'] = $codePath;
  1890. // else return JsonService::fail('没有查看权限');
  1891. // }
  1892. // $posterPath = createPoster($productInfo);
  1893. // return JsonService::successful($posterPath);
  1894. }
  1895. /**
  1896. * 刷新数据缓存
  1897. */
  1898. public function refresh_cache(){
  1899. `php think optimize:schema`;
  1900. `php think optimize:autoload`;
  1901. `php think optimize:route`;
  1902. `php think optimize:config`;
  1903. }
  1904. }