AuthApi.php 33 KB

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