AuthApi.php 82 KB

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