AuthApi.php 33 KB

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