AuthApi.php 79 KB

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