AuthApi.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/12
  6. */
  7. namespace app\wap\controller;
  8. use Api\Express;
  9. use app\core\util\WechatService;
  10. use app\wap\model\store\StoreBargain;
  11. use app\wap\model\store\StoreBargainUser;
  12. use app\wap\model\store\StoreBargainUserHelp;
  13. use app\wap\model\store\StoreCouponIssue;
  14. use app\wap\model\store\StoreCouponIssueUser;
  15. use app\wap\model\store\StoreOrderCartInfo;
  16. use app\wap\model\store\StorePink;
  17. use app\wap\model\store\StoreProductReply;
  18. use app\wap\model\store\StoreService;
  19. use app\wap\model\store\StoreServiceLog;
  20. use app\wap\model\store\StoreCart;
  21. use app\wap\model\store\StoreCategory;
  22. use app\wap\model\store\StoreCouponUser;
  23. use app\wap\model\store\StoreOrder;
  24. use app\wap\model\store\StoreProduct;
  25. use app\wap\model\store\StoreProductAttr;
  26. use app\wap\model\store\StoreProductRelation;
  27. use app\wap\model\user\User;
  28. use app\wap\model\user\UserAddress;
  29. use app\wap\model\user\UserBill;
  30. use app\wap\model\user\UserExtract;
  31. use app\wap\model\user\UserRecharge;
  32. use app\wap\model\user\UserNotice;
  33. use app\wap\model\user\UserSign;
  34. use app\wap\model\user\WechatUser;
  35. use behavior\wap\StoreProductBehavior;
  36. use app\core\util\WechatTemplateService;
  37. use service\CacheService;
  38. use service\HookService;
  39. use service\JsonService;
  40. use app\core\util\SystemConfigService;
  41. use service\UtilService;
  42. use think\Cache;
  43. use think\Request;
  44. use think\Url;
  45. class AuthApi extends AuthController
  46. {
  47. public function user_sign()
  48. {
  49. $signed = UserSign::checkUserSigned($this->userInfo['uid']);
  50. if($signed) return JsonService::fail('已签到');
  51. if(false !== $integral = UserSign::sign($this->userInfo))
  52. return JsonService::successful('签到获得'.floatval($integral).'积分');
  53. else
  54. return JsonService::fail('签到失败!');
  55. }
  56. public function set_cart($productId = '',$cartNum = 1,$uniqueId = '')
  57. {
  58. if(!$productId || !is_numeric($productId)) return $this->failed('参数错误!');
  59. $res = StoreCart::setCart($this->userInfo['uid'],$productId,$cartNum,$uniqueId,'product');
  60. if(!$res)
  61. return $this->failed(StoreCart::getErrorInfo('加入购物车失败!'));
  62. else{
  63. HookService::afterListen('store_product_set_cart_after',$res,$this->userInfo,false,StoreProductBehavior::class);
  64. return $this->successful('ok',['cartId'=>$res->id]);
  65. }
  66. }
  67. public function now_buy($productId = '',$cartNum = 1,$uniqueId = '',$combinationId = 0,$secKillId=0,$bargainId = 0)
  68. {
  69. if($productId == '') return $this->failed('参数错误!');
  70. if($bargainId && StoreBargainUserHelp::getSurplusPrice($bargainId,$this->userInfo['uid'])) return JsonService::fail('请先砍价');
  71. $res = StoreCart::setCart($this->userInfo['uid'],$productId,$cartNum,$uniqueId,'product',1,$combinationId,$secKillId,$bargainId);
  72. if(!$res)
  73. return $this->failed(StoreCart::getErrorInfo('订单生成失败!'));
  74. else {
  75. return $this->successful('ok', ['cartId' => $res->id]);
  76. }
  77. }
  78. public function like_product($productId = '',$category = 'product')
  79. {
  80. if(!$productId || !is_numeric($productId)) return $this->failed('参数错误!');
  81. $res = StoreProductRelation::productRelation($productId,$this->userInfo['uid'],'like',$category);
  82. if(!$res)
  83. return $this->failed(StoreProductRelation::getErrorInfo('点赞失败!'));
  84. else
  85. return $this->successful();
  86. }
  87. public function unlike_product($productId = '',$category = 'product')
  88. {
  89. if(!$productId || !is_numeric($productId)) return $this->failed('参数错误!');
  90. $res = StoreProductRelation::unProductRelation($productId,$this->userInfo['uid'],'like',$category);
  91. if(!$res)
  92. return $this->failed(StoreProductRelation::getErrorInfo('取消点赞失败!'));
  93. else
  94. return $this->successful();
  95. }
  96. public function collect_product($productId,$category = 'product')
  97. {
  98. if(!$productId || !is_numeric($productId)) return $this->failed('参数错误!');
  99. $res = StoreProductRelation::productRelation($productId,$this->userInfo['uid'],'collect',$category);
  100. if(!$res)
  101. return $this->failed(StoreProductRelation::getErrorInfo('收藏失败!'));
  102. else
  103. return $this->successful();
  104. }
  105. public function uncollect_product($productId,$category = 'product')
  106. {
  107. if(!$productId || !is_numeric($productId)) return $this->failed('参数错误!');
  108. $res = StoreProductRelation::unProductRelation($productId,$this->userInfo['uid'],'collect',$category);
  109. if(!$res)
  110. return $this->failed(StoreProductRelation::getErrorInfo('取消收藏失败!'));
  111. else
  112. return $this->successful();
  113. }
  114. public function get_cart_num()
  115. {
  116. return JsonService::successful('ok',StoreCart::getUserCartNum($this->userInfo['uid'],'product'));
  117. }
  118. public function get_cart_list()
  119. {
  120. return JsonService::successful('ok',StoreCart::getUserProductCartList($this->userInfo['uid']));
  121. }
  122. public function change_cart_num($cartId = '',$cartNum = '')
  123. {
  124. if(!$cartId || !$cartNum || !is_numeric($cartId) || !is_numeric($cartNum)) return JsonService::fail('参数错误!');
  125. StoreCart::changeUserCartNum($cartId,$cartNum,$this->userInfo['uid']);
  126. return JsonService::successful();
  127. }
  128. public function remove_cart($ids='')
  129. {
  130. if(!$ids) return JsonService::fail('参数错误!');
  131. StoreCart::removeUserCart($this->userInfo['uid'],$ids);
  132. return JsonService::successful();
  133. }
  134. public function get_use_coupon()
  135. {
  136. return JsonService::successful('',StoreCouponUser::getUserValidCoupon($this->userInfo['uid']));
  137. }
  138. public function get_user_collect_product($first = 0,$limit = 8)
  139. {
  140. $productList = StoreProductRelation::getProductRelation($this->userInfo['uid'], $first, $limit);
  141. $seckillList = StoreProductRelation::getSeckillRelation($this->userInfo['uid'], $first, $limit);
  142. $sort = [];
  143. $list = [];
  144. foreach ($productList as $key=>&$product){
  145. if($product['pid']){
  146. $product['is_fail'] = $product['is_del'] && $product['is_show'];
  147. $sort[] = $product['add_time'];
  148. array_push($list,$product);
  149. }else{
  150. unset($productList[$key]);
  151. }
  152. }
  153. foreach ($seckillList as $key=>&$seckill){
  154. if($seckill['pid']){
  155. $seckill['is_fail'] = $seckill['is_del'] && $seckill['is_show'];
  156. $sort[] = $seckill['add_time'];
  157. array_push($list,$seckill);
  158. }else{
  159. unset($seckillList[$key]);
  160. }
  161. }
  162. array_multisort($sort,SORT_DESC,SORT_NUMERIC,$list);
  163. return JsonService::successful($list);
  164. }
  165. public function remove_user_collect_product($productId = '')
  166. {
  167. if(!$productId || !is_numeric($productId)) return JsonService::fail('参数错误!');
  168. StoreProductRelation::unProductRelation($productId,$this->userInfo['uid'],'collect','product');
  169. return JsonService::successful();
  170. }
  171. public function set_user_default_address($addressId = '')
  172. {
  173. if(!$addressId || !is_numeric($addressId)) return JsonService::fail('参数错误!');
  174. if(!UserAddress::be(['is_del'=>0,'id'=>$addressId,'uid'=>$this->userInfo['uid']]))
  175. return JsonService::fail('地址不存在!');
  176. $res = UserAddress::setDefaultAddress($addressId,$this->userInfo['uid']);
  177. if(!$res)
  178. return JsonService::fail('地址不存在!');
  179. else
  180. return JsonService::successful();
  181. }
  182. public function edit_user_address()
  183. {
  184. $request = Request::instance();
  185. if(!$request->isPost()) return JsonService::fail('参数错误!');
  186. $addressInfo = UtilService::postMore([
  187. ['address',[]],
  188. ['is_default',false],
  189. ['real_name',''],
  190. ['post_code',''],
  191. ['phone',''],
  192. ['detail',''],
  193. ['id',0]
  194. ],$request);
  195. $addressInfo['province'] = $addressInfo['address']['province'];
  196. $addressInfo['city'] = $addressInfo['address']['city'];
  197. $addressInfo['district'] = $addressInfo['address']['district'];
  198. $addressInfo['is_default'] = $addressInfo['is_default'] == true ? 1 : 0;
  199. $addressInfo['uid'] = $this->userInfo['uid'];
  200. unset($addressInfo['address']);
  201. if($addressInfo['id'] && UserAddress::be(['id'=>$addressInfo['id'],'uid'=>$this->userInfo['uid'],'is_del'=>0])){
  202. $id = $addressInfo['id'];
  203. unset($addressInfo['id']);
  204. if(UserAddress::edit($addressInfo,$id,'id')){
  205. if($addressInfo['is_default'])
  206. UserAddress::setDefaultAddress($id,$this->userInfo['uid']);
  207. return JsonService::successful();
  208. }else
  209. return JsonService::fail('编辑收货地址失败!');
  210. }else{
  211. if($address = UserAddress::set($addressInfo)){
  212. if($addressInfo['is_default'])
  213. UserAddress::setDefaultAddress($address->id,$this->userInfo['uid']);
  214. return JsonService::successful();
  215. }else
  216. return JsonService::fail('添加收货地址失败!');
  217. }
  218. }
  219. public function user_default_address()
  220. {
  221. $defaultAddress = UserAddress::getUserDefaultAddress($this->userInfo['uid'],'id,real_name,phone,province,city,district,detail,is_default');
  222. if($defaultAddress)
  223. return JsonService::successful('ok',$defaultAddress);
  224. else
  225. return JsonService::successful('empty',[]);
  226. }
  227. public function remove_user_address($addressId = '')
  228. {
  229. if(!$addressId || !is_numeric($addressId)) return JsonService::fail('参数错误!');
  230. if(!UserAddress::be(['is_del'=>0,'id'=>$addressId,'uid'=>$this->userInfo['uid']]))
  231. return JsonService::fail('地址不存在!');
  232. if(UserAddress::edit(['is_del'=>'1'],$addressId,'id'))
  233. return JsonService::successful();
  234. else
  235. return JsonService::fail('删除地址失败!');
  236. }
  237. /**
  238. * 创建订单
  239. * @param string $key
  240. * @return \think\response\Json
  241. */
  242. public function create_order($key = '')
  243. {
  244. if(!$key) return JsonService::fail('参数错误!');
  245. if(StoreOrder::be(['order_id|unique'=>$key,'uid'=>$this->userInfo['uid'],'is_del'=>0]))
  246. return JsonService::status('extend_order','订单已生成',['orderId'=>$key,'key'=>$key]);
  247. list($addressId,$couponId,$payType,$useIntegral,$mark,$combinationId,$pinkId,$seckill_id,$bargainId) = UtilService::postMore([
  248. 'addressId','couponId','payType','useIntegral','mark',['combinationId',0],['pinkId',0],['seckill_id',0],['bargainId',0]
  249. ],Request::instance(),true);
  250. $payType = strtolower($payType);
  251. if($bargainId) StoreBargainUser::setBargainUserStatus($bargainId,$this->userInfo['uid']);//修改砍价状态
  252. if($pinkId) if(StorePink::getIsPinkUid($pinkId)) return JsonService::status('ORDER_EXIST','订单生成失败,你已经在该团内不能再参加了',['orderId'=>StoreOrder::getStoreIdPink($pinkId)]);
  253. if($pinkId) if(StoreOrder::getIsOrderPink($pinkId)) return JsonService::status('ORDER_EXIST','订单生成失败,你已经参加该团了,请先支付订单',['orderId'=>StoreOrder::getStoreIdPink($pinkId)]);
  254. $order = StoreOrder::cacheKeyCreateOrder($this->userInfo['uid'],$key,$addressId,$payType,$useIntegral,$couponId,$mark,$combinationId,$pinkId,$seckill_id,$bargainId);
  255. $orderId = $order['order_id'];
  256. $info = compact('orderId','key');
  257. if($orderId){
  258. if($payType == 'weixin'){
  259. $orderInfo = StoreOrder::where('order_id',$orderId)->find();
  260. if(!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  261. if($orderInfo['paid']) exception('支付已支付!');
  262. if(bcsub((float)$orderInfo['pay_price'],0,2) <= 0){
  263. if(StoreOrder::jsPayPrice($orderId,$this->userInfo['uid']))
  264. return JsonService::status('success','微信支付成功',$info);
  265. else
  266. return JsonService::status('pay_error',StoreOrder::getErrorInfo());
  267. }else{
  268. try{
  269. $jsConfig = StoreOrder::jsPay($orderId);
  270. }catch (\Exception $e){
  271. return JsonService::status('pay_error',$e->getMessage(),$info);
  272. }
  273. $info['jsConfig'] = $jsConfig;
  274. return JsonService::status('wechat_pay','订单创建成功',$info);
  275. }
  276. }else if($payType == 'yue'){
  277. if(StoreOrder::yuePay($orderId,$this->userInfo['uid']))
  278. return JsonService::status('success','余额支付成功',$info);
  279. else
  280. return JsonService::status('pay_error',StoreOrder::getErrorInfo());
  281. }else if($payType == 'offline'){
  282. StoreOrder::createOrderTemplate($order);
  283. return JsonService::status('success','订单创建成功',$info);
  284. }
  285. }else{
  286. return JsonService::fail(StoreOrder::getErrorInfo('订单生成失败!'));
  287. }
  288. }
  289. public function get_user_order_list($type = '',$first = 0, $limit = 8,$search = '')
  290. {
  291. // StoreOrder::delCombination();//删除拼团未支付订单
  292. $type=='null' && $type='';
  293. if($search){
  294. $order = StoreOrder::searchUserOrder($this->userInfo['uid'],$search)?:[];
  295. $list = $order == false ? [] : [$order];
  296. }else{
  297. if(!is_numeric($type)) $type = '';
  298. $list = StoreOrder::getUserOrderList($this->userInfo['uid'],$type,$first,$limit);
  299. }
  300. foreach ($list as $k=>$order){
  301. $list[$k] = StoreOrder::tidyOrder($order,true);
  302. if($list[$k]['_status']['_type'] == 3){
  303. foreach ($order['cartInfo']?:[] as $key=>$product){
  304. $list[$k]['cartInfo'][$key]['is_reply'] = StoreProductReply::isReply($product['unique'],'product');
  305. }
  306. }
  307. }
  308. return JsonService::successful($list);
  309. }
  310. public function user_remove_order($uni = '')
  311. {
  312. if(!$uni) return JsonService::fail('参数错误!');
  313. $res = StoreOrder::removeOrder($uni,$this->userInfo['uid']);
  314. if($res)
  315. return JsonService::successful();
  316. else
  317. return JsonService::fail(StoreOrder::getErrorInfo());
  318. }
  319. /**
  320. * 支付订单
  321. * @param string $uni
  322. * @return \think\response\Json
  323. */
  324. public function pay_order($uni = '')
  325. {
  326. if(!$uni) return JsonService::fail('参数错误!');
  327. $order= StoreOrder::getUserOrderDetail($this->userInfo['uid'],$uni);
  328. if(!$order) return JsonService::fail('订单不存在!');
  329. if($order['paid']) return JsonService::fail('该订单已支付!');
  330. if($order['pink_id']) if(StorePink::isPinkStatus($order['pink_id'])) return JsonService::fail('该订单已失效!');
  331. if($order['pay_type'] == 'weixin'){
  332. try{
  333. $jsConfig = StoreOrder::jsPay($order);
  334. }catch (\Exception $e){
  335. return JsonService::fail($e->getMessage());
  336. }
  337. return JsonService::status('wechat_pay',['jsConfig'=>$jsConfig,'order_id'=>$order['order_id']]);
  338. }else if($order['pay_type'] == 'yue'){
  339. if($res = StoreOrder::yuePay($order['order_id'],$this->userInfo['uid']))
  340. return JsonService::successful('余额支付成功');
  341. else
  342. return JsonService::fail(StoreOrder::getErrorInfo());
  343. }else if($order['pay_type'] == 'offline'){
  344. StoreOrder::createOrderTemplate($order);
  345. return JsonService::successful('订单创建成功');
  346. }
  347. }
  348. public function apply_order_refund($uni = '',$text = '')
  349. {
  350. if(!$uni || $text == '') return JsonService::fail('参数错误!');
  351. $res = StoreOrder::orderApplyRefund($uni,$this->userInfo['uid'],$text);
  352. if($res)
  353. return JsonService::successful();
  354. else
  355. return JsonService::fail(StoreOrder::getErrorInfo());
  356. }
  357. public function user_take_order($uni = '')
  358. {
  359. if(!$uni) return JsonService::fail('参数错误!');
  360. $res = StoreOrder::takeOrder($uni,$this->userInfo['uid']);
  361. if($res)
  362. return JsonService::successful();
  363. else
  364. return JsonService::fail(StoreOrder::getErrorInfo());
  365. }
  366. public function user_wechat_recharge($price = 0)
  367. {
  368. if(!$price || $price <=0) return JsonService::fail('参数错误');
  369. $storeMinRecharge = SystemConfigService::get('store_user_min_recharge');
  370. if($price < $storeMinRecharge) return JsonService::fail('充值金额不能低于'.$storeMinRecharge);
  371. $rechargeOrder = UserRecharge::addRecharge($this->userInfo['uid'],$price);
  372. if(!$rechargeOrder) return JsonService::fail('充值订单生成失败!');
  373. try{
  374. return JsonService::successful(UserRecharge::jsPay($rechargeOrder));
  375. }catch (\Exception $e){
  376. return JsonService::fail($e->getMessage());
  377. }
  378. }
  379. public function user_balance_list($first = 0,$limit = 8)
  380. {
  381. $list = UserBill::where('uid',$this->userInfo['uid'])->where('category','now_money')
  382. ->field('mark,pm,number,add_time')
  383. ->where('status',1)->order('add_time DESC')->limit($first,$limit)->select()->toArray();
  384. foreach ($list as &$v){
  385. $v['add_time'] = date('Y/m/d H:i',$v['add_time']);
  386. }
  387. return JsonService::successful($list);
  388. }
  389. public function user_integral_list($first = 0,$limit = 8)
  390. {
  391. $list = UserBill::where('uid',$this->userInfo['uid'])->where('category','integral')
  392. ->field('mark,pm,number,add_time')
  393. ->where('status',1)->order('add_time DESC')->limit($first,$limit)->select()->toArray();
  394. foreach ($list as &$v){
  395. $v['add_time'] = date('Y/m/d H:i',$v['add_time']);
  396. $v['number'] = floatval($v['number']);
  397. }
  398. return JsonService::successful($list);
  399. }
  400. public function user_comment_product($unique = '')
  401. {
  402. if(!$unique) return JsonService::fail('参数错误!');
  403. $cartInfo = StoreOrderCartInfo::where('unique',$unique)->find();
  404. $uid = $this->userInfo['uid'];
  405. if(!$cartInfo || $uid != $cartInfo['cart_info']['uid']) return JsonService::fail('评价产品不存在!');
  406. if(StoreProductReply::be(['oid'=>$cartInfo['oid'],'unique'=>$unique]))
  407. return JsonService::fail('该产品已评价!');
  408. $group = UtilService::postMore([
  409. ['comment',''],['pics',[]],['product_score',5],['service_score',5]
  410. ],Request::instance());
  411. $group['comment'] = htmlspecialchars(trim($group['comment']));
  412. if(sensitive_words_filter($group['comment'])) return JsonService::fail('请注意您的用词,谢谢!!');
  413. if($group['product_score'] < 1) return JsonService::fail('请为产品评分');
  414. else if($group['service_score'] < 1) return JsonService::fail('请为商家服务评分');
  415. $group = array_merge($group,[
  416. 'uid'=>$uid,
  417. 'oid'=>$cartInfo['oid'],
  418. 'unique'=>$unique,
  419. 'product_id'=>$cartInfo['product_id'],
  420. 'reply_type'=>'product'
  421. ]);
  422. StoreProductReply::beginTrans();
  423. $res = StoreProductReply::reply($group,'product');
  424. if(!$res) {
  425. StoreProductReply::rollbackTrans();
  426. return JsonService::fail('评价失败!');
  427. }
  428. try{
  429. HookService::listen('store_product_order_reply',$group,$cartInfo,false,StoreProductBehavior::class);
  430. }catch (\Exception $e){
  431. StoreProductReply::rollbackTrans();
  432. return JsonService::fail($e->getMessage());
  433. }
  434. StoreProductReply::commitTrans();
  435. return JsonService::successful();
  436. }
  437. public function get_product_category()
  438. {
  439. $parentCategory = StoreCategory::pidByCategory(0,'id,cate_name')->toArray();
  440. foreach ($parentCategory as $k=>$category){
  441. $category['child'] = StoreCategory::pidByCategory($category['id'],'id,cate_name')->toArray();
  442. $parentCategory[$k] = $category;
  443. }
  444. return JsonService::successful($parentCategory);
  445. }
  446. public function get_spread_list($first = 0,$limit = 20)
  447. {
  448. $list = User::where('spread_uid',$this->userInfo['uid'])->field('uid,nickname,avatar,add_time')->limit($first,$limit)->order('add_time DESC')->select()->toArray();
  449. foreach ($list as $k=>$user){
  450. $list[$k]['add_time'] = date('Y/m/d',$user['add_time']);
  451. }
  452. return JsonService::successful($list);
  453. }
  454. public function get_product_list($keyword = '', $cId = 0,$sId = 0,$priceOrder = '', $salesOrder = '', $news = 0, $first = 0, $limit = 8)
  455. {
  456. if(!empty($keyword)){
  457. $encodedData = str_replace(' ','+',$keyword);
  458. $keyword = base64_decode(htmlspecialchars($encodedData));
  459. }
  460. $model = StoreProduct::validWhere();
  461. if($cId){
  462. $sids = StoreCategory::pidBySidList($cId);
  463. $sids[] = $cId;
  464. if($sId) $sids[] = $sId;
  465. $sId = implode(',',$sids);
  466. }
  467. if($sId){
  468. $product_ids = \think\Db::name('store_product_cate')->where('cate_id','IN',$sId)->column('product_id');
  469. if(count($product_ids))
  470. $model=$model->where('id',"in",$product_ids);
  471. else
  472. $model=$model->where('cate_id',-1);
  473. }
  474. if(!empty($keyword)) $model->where('keyword|store_name','LIKE',"%$keyword%");
  475. if($news) $model->where('is_new',1);
  476. $baseOrder = '';
  477. if($priceOrder) $baseOrder = $priceOrder == 'desc' ? 'price DESC' : 'price ASC';
  478. // if($salesOrder) $baseOrder = $salesOrder == 'desc' ? 'sales DESC' : 'sales ASC';
  479. if($salesOrder) $baseOrder = $salesOrder == 'desc' ? 'ficti DESC' : 'ficti ASC';
  480. if($baseOrder) $baseOrder .= ', ';
  481. $model->order($baseOrder.'sort DESC, add_time DESC');
  482. $list = $model->limit($first,$limit)->field('id,store_name,image,sales,ficti,price,stock')->select()->toArray();
  483. if($list) setView($this->uid,0,$sId,'search','product',$keyword);
  484. return JsonService::successful($list);
  485. }
  486. public function user_get_coupon($couponId = '')
  487. {
  488. if(!$couponId || !is_numeric($couponId)) return JsonService::fail('参数错误!');
  489. if(StoreCouponIssue::issueUserCoupon($couponId,$this->userInfo['uid'])){
  490. return JsonService::successful('领取成功');
  491. }else{
  492. return JsonService::fail(StoreCouponIssue::getErrorInfo('领取失败!'));
  493. }
  494. }
  495. public function product_reply_list($productId = '',$first = 0,$limit = 8, $filter = 'all')
  496. {
  497. if(!$productId || !is_numeric($productId)) return JsonService::fail('参数错误!');
  498. $list = StoreProductReply::getProductReplyList($productId,$filter,$first,$limit);
  499. return JsonService::successful($list);
  500. }
  501. public function product_attr_detail($productId = '')
  502. {
  503. if(!$productId || !is_numeric($productId)) return JsonService::fail('参数错误!');
  504. list($productAttr,$productValue) = StoreProductAttr::getProductAttrDetail($productId);
  505. return JsonService::successful(compact('productAttr','productValue'));
  506. }
  507. public function user_address_list()
  508. {
  509. $list = UserAddress::getUserValidAddressList($this->userInfo['uid'],'id,real_name,phone,province,city,district,detail,is_default');
  510. return JsonService::successful($list);
  511. }
  512. public function get_notice_list($page = 0, $limit = 8)
  513. {
  514. $list = UserNotice::getNoticeList($this->userInfo['uid'],$page,$limit);
  515. return JsonService::successful($list);
  516. }
  517. public function see_notice($nid){
  518. UserNotice::seeNotice($this->userInfo['uid'],$nid);
  519. return JsonService::successful();
  520. }
  521. public function refresh_msn(Request $request)
  522. {
  523. $params = $request->post();
  524. $remind_where = "mer_id = ".$params["mer_id"]." AND uid = ".$params["uid"]." AND to_uid = ".$params["to_uid"]." AND type = 0 AND remind = 0";
  525. $remind_list = StoreServiceLog::where($remind_where)->order("add_time asc")->select();
  526. foreach ($remind_list as $key => $value) {
  527. if(time() - $value["add_time"] > 3){
  528. StoreServiceLog::edit(array("remind"=>1),$value["id"]);
  529. $now_user = StoreService::field("uid,nickname")->where(array("uid"=>$params["uid"]))->find();
  530. if(!$now_user)$now_user = User::field("uid,nickname")->where(array("uid"=>$params["uid"]))->find();
  531. if($params["to_uid"]) {
  532. $userInfo = WechatUser::where('uid',$params["to_uid"])->field('nickname,subscribe,openid,headimgurl')->find();
  533. $head = '客服提醒';
  534. $description = '您有新的消息,请注意查收!';
  535. $url = Url::build('service/service_ing',['to_uid'=>$now_user["uid"],'mer_id'=>$params["mer_id"]],true,true);
  536. $message = WechatService::newsMessage($head,$description,$url,$userInfo['headimgurl']);
  537. if($userInfo){
  538. $userInfo = $userInfo->toArray();
  539. if($userInfo['subscribe'] && $userInfo['openid']){
  540. try {
  541. WechatService::staffService()->message($message)->to($userInfo['openid'])->send();
  542. } catch (\Exception $e) {
  543. $errorLog = $userInfo['nickname'].'发送失败'.$e->getMessage();
  544. }
  545. }
  546. }
  547. // WechatTemplateService::sendTemplate(WechatUser::uidToOpenid($params["to_uid"]),WechatTemplateService::SERVICE_NOTICE,[
  548. // 'first'=>$head,
  549. // 'keyword1'=>$now_user["nickname"],
  550. // 'keyword2'=>"客服提醒",
  551. // 'keyword3'=> preg_replace('/<img.*? \/>/','[图片]',$value["msn"]),
  552. // 'keyword4'=>date('Y-m-d H:i:s',time()),
  553. // 'remark'=>'点击立即查看消息'
  554. // ],Url::build('service/service_ing',['to_uid'=>$now_user["uid"],'mer_id'=>$params["mer_id"]],true,true));
  555. }
  556. }
  557. }
  558. $where = "mer_id = ".$params["mer_id"]." AND uid = ".$params["to_uid"]." AND to_uid = ".$params["uid"]." AND type = 0";
  559. $list = StoreServiceLog::where($where)->order("add_time asc")->select()->toArray();
  560. $ids = [];
  561. foreach ($list as $key => $value) {
  562. //设置发送人与接收人区别
  563. if($value["uid"] == $params["uid"])
  564. $list[$key]['my'] = "my";
  565. else
  566. $list[$key]['my'] = "to";
  567. array_push($ids,$value["id"]);
  568. }
  569. //设置这些消息为已读
  570. StoreServiceLog::where(array("id"=>array("in",$ids)))->update(array("type"=>1,"remind"=>1));
  571. return JsonService::successful($list);
  572. }
  573. public function add_msn(Request $request){
  574. $params = $request->post();
  575. if($params["type"] == "html")
  576. $data["msn"] = htmlspecialchars_decode($params["msn"]);
  577. else
  578. $data["msn"] = $params["msn"];
  579. $data["uid"] = $params["uid"];
  580. $data["to_uid"] = $params["to_uid"];
  581. $data["mer_id"] = $params["mer_id"] > 0 ? $params["mer_id"] : 0;
  582. $data["add_time"] = time();
  583. StoreServiceLog::set($data);
  584. return JsonService::successful();
  585. }
  586. public function get_msn(Request $request){
  587. $params = $request->post();
  588. $size = 10;
  589. $page = $params["page"]>=0 ? $params["page"] : 1;
  590. $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"].")";
  591. $list = StoreServiceLog::where($where)->limit(($page-1)*$size,$size)->order("add_time desc")->select()->toArray();
  592. foreach ($list as $key => $value) {
  593. //设置发送人与接收人区别
  594. if($value["uid"] == $params["uid"])
  595. $list[$key]['my'] = "my";
  596. else
  597. $list[$key]['my'] = "to";
  598. //设置这些消息为已读
  599. if($value["uid"] == $params["to_uid"] && $value["to_uid"] == $params["uid"])StoreServiceLog::edit(array("type"=>1,"remind"=>1),$value["id"]);
  600. }
  601. $list=array_reverse($list);
  602. return JsonService::successful($list);
  603. }
  604. public function refresh_msn_new(Request $request){
  605. $params = $request->post();
  606. $now_user = User::getUserInfo($this->userInfo['uid']);
  607. if($params["last_time"] > 0)
  608. $where = "(uid = ".$now_user["uid"]." OR to_uid = ".$now_user["uid"].") AND add_time>".$params["last_time"];
  609. else
  610. $where = "uid = ".$now_user["uid"]." OR to_uid = ".$now_user["uid"];
  611. $msn_list = StoreServiceLog::where($where)->order("add_time desc")->select()->toArray();
  612. $info_array = $list = [];
  613. foreach ($msn_list as $key => $value){
  614. $to_uid = $value["uid"] == $now_user["uid"] ? $value["to_uid"] : $value["uid"];
  615. if(!in_array(["to_uid"=>$to_uid,"mer_id"=>$value["mer_id"]],$info_array)){
  616. $info_array[count($info_array)] = ["to_uid"=>$to_uid,"mer_id"=>$value["mer_id"]];
  617. $to_user = StoreService::field("uid,nickname,avatar")->where(array("uid"=>$to_uid))->find();
  618. if(!$to_user)$to_user = User::field("uid,nickname,avatar")->where(array("uid"=>$to_uid))->find();
  619. $to_user["mer_id"] = $value["mer_id"];
  620. $to_user["mer_name"] = '';
  621. $value["to_info"] = $to_user;
  622. $value["count"] = StoreServiceLog::where(array("mer_id"=>$value["mer_id"],"uid"=>$to_uid,"to_uid"=>$now_user["uid"],"type"=>0))->count();
  623. $list[count($list)] = $value;
  624. }
  625. }
  626. return JsonService::successful($list);
  627. }
  628. public function get_user_brokerage_list($uid, $first = 0,$limit = 8)
  629. {
  630. if(!$uid)
  631. return $this->failed('用户不存在');
  632. $list = UserBill::field('A.mark,A.add_time,A.number,A.pm')->alias('A')->limit($first,$limit)
  633. ->where('A.category','now_money')->where('A.type','brokerage')
  634. ->where('A.uid',$this->userInfo['uid'])
  635. ->join('__STORE_ORDER__ B','A.link_id = B.id AND B.uid = '.$uid)->select()->toArray();
  636. return JsonService::successful($list);
  637. }
  638. public function user_extract()
  639. {
  640. if(UserExtract::userExtract($this->userInfo,UtilService::postMore([
  641. ['type','','','extract_type'],'real_name','alipay_code','bank_code','bank_address',['price','','','extract_price']
  642. ])))
  643. return JsonService::successful('申请提现成功!');
  644. else
  645. return JsonService::fail(Extract::getErrorInfo());
  646. }
  647. public function get_issue_coupon_list($limit = 2)
  648. {
  649. $list = StoreCouponIssue::validWhere('A')->join('__STORE_COUPON__ B','A.cid = B.id')
  650. ->field('A.*,B.coupon_price,B.use_min_price')->order('B.sort DESC,A.id DESC')->limit($limit)->select()->toArray()?:[];
  651. $list_coupon=[];
  652. foreach ($list as $k=>&$v){
  653. if(!($v['is_use']=StoreCouponIssueUser::be(['uid'=>$this->userInfo['uid'],'issue_coupon_id'=>$v['id']]))){
  654. if($v['is_permanent'] == 0 && $v['total_count'] > 0 && $v['remain_count'] >0){
  655. array_push($list_coupon,$v);
  656. }else if($v['is_permanent'] == 1){
  657. array_push($list_coupon,$v);
  658. }
  659. }
  660. }
  661. return JsonService::successful($list_coupon);
  662. }
  663. public function clear_cache($uni = '')
  664. {
  665. if($uni)CacheService::clear();
  666. }
  667. /**
  668. * 获取今天正在拼团的人的头像和名称
  669. * @return \think\response\Json
  670. */
  671. public function get_pink_second_one(){
  672. $addTime = mt_rand(time()-30000,time());
  673. $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();
  674. return JsonService::successful($storePink);
  675. }
  676. public function order_details($uni = ''){
  677. if(!$uni) return JsonService::fail('参数错误!');
  678. $order = StoreOrder::getUserOrderDetail($this->userInfo['uid'],$uni);
  679. if(!$order) return JsonService::fail('订单不存在!');
  680. $order = StoreOrder::tidyOrder($order,true);
  681. $res = array();
  682. foreach ($order['cartInfo'] as $v) {
  683. if($v['combination_id']) return JsonService::fail('拼团产品不能再来一单,请在拼团产品内自行下单!');
  684. 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);
  685. }
  686. $cateId = [];
  687. foreach ($res as $v){
  688. if(!$v) return JsonService::fail('再来一单失败,请重新下单!');
  689. $cateId[] = $v['id'];
  690. }
  691. return JsonService::successful('ok',implode(',',$cateId));
  692. }
  693. /**
  694. * 帮好友砍价
  695. * @param int $bargainId
  696. * @param int $bargainUserId
  697. * @return \think\response\Json
  698. */
  699. public function set_bargain_help(){
  700. list($bargainId,$bargainUserId) = UtilService::postMore([
  701. 'bargainId','bargainUserId'],Request::instance(),true);
  702. if(!$bargainId || !$bargainUserId) return JsonService::fail('参数错误');
  703. $res = StoreBargainUserHelp::setBargainUserHelp($bargainId,$bargainUserId,$this->userInfo['uid']);
  704. if($res) {
  705. if(!StoreBargainUserHelp::getSurplusPrice($bargainId,$bargainUserId)){//砍价成功,发模板消息
  706. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId,$bargainUserId);
  707. $bargain = StoreBargain::where('id',$bargainId)->find()->toArray();
  708. $bargainUser = StoreBargainUser::where('id',$bargainUserTableId)->find()->toArray();
  709. }
  710. return JsonService::status('SUCCESS','砍价成功');
  711. }
  712. else return JsonService::status('ERROR','砍价失败,请稍后再帮助朋友砍价');
  713. }
  714. /**
  715. * 砍价分享添加次数
  716. * @param int $bargainId
  717. */
  718. public function add_bargain_share($bargainId = 0){
  719. if(!$bargainId) return JsonService::successful();
  720. StoreBargain::addBargainShare($bargainId);
  721. return JsonService::successful();
  722. }
  723. }