My.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/21
  6. */
  7. namespace app\wap\controller;
  8. use Api\Express;
  9. use app\admin\model\system\SystemConfig;
  10. use app\wap\model\store\StoreBargainUser;
  11. use app\wap\model\store\StoreBargainUserHelp;
  12. use app\wap\model\store\StoreCombination;
  13. use app\wap\model\store\StoreOrderCartInfo;
  14. use app\wap\model\store\StorePink;
  15. use app\wap\model\store\StoreProduct;
  16. use app\wap\model\store\StoreProductRelation;
  17. use app\wap\model\store\StoreProductReply;
  18. use app\wap\model\store\StoreCouponUser;
  19. use app\wap\model\store\StoreOrder;
  20. use app\wap\model\user\User;
  21. use app\wap\model\user\UserBill;
  22. use app\wap\model\user\UserExtract;
  23. use app\wap\model\user\UserNotice;
  24. use service\GroupDataService;
  25. use app\wap\model\user\UserAddress;
  26. use app\wap\model\user\UserSign;
  27. use service\CacheService;
  28. use service\SystemConfigService;
  29. use think\Request;
  30. use think\Url;
  31. class My extends AuthController
  32. {
  33. public function user_cut(){
  34. $list = StoreBargainUser::getBargainUserAll($this->userInfo['uid']);
  35. if($list){
  36. foreach ($list as $k=>$v){
  37. $list[$k]['con_price'] = bcsub($v['bargain_price'],$v['price'],2);
  38. $list[$k]['helpCount'] = StoreBargainUserHelp::getBargainUserHelpPeopleCount($v['bargain_id'],$this->userInfo['uid']);
  39. }
  40. $this->assign('list',$list);
  41. }else return $this->failed('暂无参与砍价',Url::build('My/index'));
  42. return $this->fetch();
  43. }
  44. public function index()
  45. {
  46. // echo date('Y-m-d,H:i:s',1521516681);
  47. $this->assign([
  48. 'menus'=>GroupDataService::getData('my_index_menu')?:[],
  49. 'orderStatusNum'=>StoreOrder::getOrderStatusNum($this->userInfo['uid']),
  50. 'notice'=>UserNotice::getNotice($this->userInfo['uid']),
  51. 'statu' =>(int)SystemConfig::getValue('store_brokerage_statu'),
  52. ]);
  53. return $this->fetch();
  54. }
  55. public function sign_in()
  56. {
  57. $signed = UserSign::checkUserSigned($this->userInfo['uid']);
  58. $signCount = UserSign::userSignedCount($this->userInfo['uid']);
  59. $signList = UserSign::userSignBillWhere($this->userInfo['uid'])
  60. ->field('number,add_time')->order('id DESC')
  61. ->limit(30)->select()->toArray();
  62. $goodsList = StoreProduct::getNewProduct('image,price,sales,store_name,id','0,20')->toArray();
  63. $this->assign(compact('signed','signCount','signList','goodsList'));
  64. return $this->fetch();
  65. }
  66. public function coupon()
  67. {
  68. $uid = $this->userInfo['uid'];
  69. $couponList = StoreCouponUser::all(function($query) use($uid){
  70. $query->where('status','0')->where('uid',$uid)->order('is_fail ASC,status ASC,add_time DESC')->whereOr(function($query) use($uid){
  71. $query->where('uid',$uid)->where('status','<>',0)->where('end_time','>',time()-(7*86400));
  72. });
  73. })->toArray();
  74. $couponList = StoreCouponUser::tidyCouponList($couponList);
  75. $this->assign([
  76. 'couponList'=>$couponList
  77. ]);
  78. return $this->fetch();
  79. }
  80. public function collect()
  81. {
  82. return $this->fetch();
  83. }
  84. public function address()
  85. {
  86. $this->assign([
  87. 'address'=>UserAddress::getUserValidAddressList($this->userInfo['uid'],'id,real_name,phone,province,city,district,detail,is_default')
  88. ]);
  89. return $this->fetch();
  90. }
  91. public function recharge()
  92. {
  93. return $this->fetch();
  94. }
  95. public function edit_address($addressId = '')
  96. {
  97. if($addressId && is_numeric($addressId) && UserAddress::be(['is_del'=>0,'id'=>$addressId,'uid'=>$this->userInfo['uid']])){
  98. $addressInfo = UserAddress::find($addressId)->toArray();
  99. }else{
  100. $addressInfo = [];
  101. }
  102. $this->assign(compact('addressInfo'));
  103. return $this->fetch();
  104. }
  105. public function order($uni = '')
  106. {
  107. if(!$uni || !$order = StoreOrder::getUserOrderDetail($this->userInfo['uid'],$uni)) return $this->redirect(Url::build('order_list'));
  108. $this->assign([
  109. 'order'=>StoreOrder::tidyOrder($order,true)
  110. ]);
  111. return $this->fetch();
  112. }
  113. public function orderPinkOld($uni = '')
  114. {
  115. if(!$uni || !$order = StoreOrder::getUserOrderDetail($this->userInfo['uid'],$uni)) return $this->redirect(Url::build('order_list'));
  116. $this->assign([
  117. 'order'=>StoreOrder::tidyOrder($order,true)
  118. ]);
  119. return $this->fetch('order');
  120. }
  121. public function order_list()
  122. {
  123. return $this->fetch();
  124. }
  125. public function order_reply($unique = '')
  126. {
  127. if(!$unique || !StoreOrderCartInfo::be(['unique'=>$unique]) || !($cartInfo = StoreOrderCartInfo::where('unique',$unique)->find())) return $this->failed('评价产品不存在!');
  128. $this->assign(['cartInfo'=>$cartInfo]);
  129. return $this->fetch();
  130. }
  131. public function balance()
  132. {
  133. $this->assign([
  134. 'userMinRecharge'=>SystemConfigService::get('store_user_min_recharge')
  135. ]);
  136. return $this->fetch();
  137. }
  138. public function integral()
  139. {
  140. return $this->fetch();
  141. }
  142. public function spread_list()
  143. {
  144. $statu = (int)SystemConfig::getValue('store_brokerage_statu');
  145. if($statu == 1){
  146. if(!User::be(['uid'=>$this->userInfo['uid'],'is_promoter'=>1]))
  147. return $this->failed('没有权限访问!');
  148. }
  149. $this->assign([
  150. 'total'=>User::where('spread_uid',$this->userInfo['uid'])->count()
  151. ]);
  152. return $this->fetch();
  153. }
  154. public function notice()
  155. {
  156. return $this->fetch();
  157. }
  158. public function express($uni = '')
  159. {
  160. if(!$uni || !($order = StoreOrder::getUserOrderDetail($this->userInfo['uid'],$uni))) return $this->failed('查询订单不存在!');
  161. if($order['delivery_type'] != 'express' || !$order['delivery_id']) return $this->failed('该订单不存在快递单号!');
  162. $cacheName = $uni.$order['delivery_id'];
  163. $result = CacheService::get($cacheName,null);
  164. if($result === null){
  165. $result = Express::query($order['delivery_id']);
  166. if(is_array($result) &&
  167. isset($result['result']) &&
  168. isset($result['result']['deliverystatus']) &&
  169. $result['result']['deliverystatus'] >= 3)
  170. $cacheTime = 0;
  171. else
  172. $cacheTime = 1800;
  173. CacheService::set($cacheName,$result,$cacheTime);
  174. }
  175. $this->assign([
  176. 'order'=>$order,
  177. 'express'=>$result
  178. ]);
  179. return $this->fetch();
  180. }
  181. public function user_pro()
  182. {
  183. $statu = (int)SystemConfig::getValue('store_brokerage_statu');
  184. if($statu == 1){
  185. if(!User::be(['uid'=>$this->userInfo['uid'],'is_promoter'=>1]))
  186. return $this->failed('没有权限访问!');
  187. }
  188. $userBill = new UserBill();
  189. $number = $userBill->where('uid',$this->userInfo['uid'])
  190. ->where('add_time','BETWEEN',[strtotime('today -1 day'),strtotime('today')])
  191. ->where('category','now_money')
  192. ->where('type','brokerage')
  193. ->value('SUM(number)')?:0;
  194. $allNumber = $userBill
  195. ->where('uid',$this->userInfo['uid'])
  196. ->where('category','now_money')
  197. ->where('type','brokerage')
  198. ->value('SUM(number)')?:0;
  199. $extractNumber = UserExtract::userExtractTotalPrice($this->userInfo['uid']);
  200. $this->assign([
  201. 'number'=>$number,
  202. 'allnumber'=>$allNumber,
  203. 'extractNumber'=>$extractNumber
  204. ]);
  205. return $this->fetch();
  206. }
  207. public function commission()
  208. {
  209. $uid = (int)Request::instance()->get('uid',0);
  210. if(!$uid) return $this->failed('用户不存在!');
  211. $this->assign(['uid'=>$uid]);
  212. return $this->fetch();
  213. }
  214. public function extract()
  215. {
  216. $minExtractPrice = floatval(SystemConfigService::get('user_extract_min_price'))?:0;
  217. $extractInfo = UserExtract::userLastInfo($this->userInfo['uid'])?:[
  218. 'extract_type'=>'bank',
  219. 'real_name'=>'',
  220. 'bank_code'=>'',
  221. 'bank_address'=>'',
  222. 'alipay_code'=>''
  223. ];
  224. $this->assign(compact('minExtractPrice','extractInfo'));
  225. return $this->fetch();
  226. }
  227. /**
  228. * 创建拼团
  229. * @param string $uni
  230. */
  231. // public function createPink($uni = ''){
  232. // if(!$uni || !$order = StoreOrder::getUserOrderDetail($this->userInfo['uid'],$uni)) return $this->redirect(Url::build('order_list'));
  233. // $order = StoreOrder::tidyOrder($order,true)->toArray();
  234. // if($order['pink_id']){//拼团存在
  235. // $res = false;
  236. // $pink['uid'] = $order['uid'];//用户id
  237. // if(StorePink::isPinkBe($pink,$order['pink_id'])) return $this->redirect('order_pink',['id'=>$order['pink_id']]);
  238. // $pink['order_id'] = $order['order_id'];//订单id 生成
  239. // $pink['order_id_key'] = $order['id'];//订单id 数据库id
  240. // $pink['total_num'] = $order['total_num'];//购买个数
  241. // $pink['total_price'] = $order['pay_price'];//总金额
  242. // $pink['k_id'] = $order['pink_id'];//拼团id
  243. // foreach ($order['cartInfo'] as $v){
  244. // $pink['cid'] = $v['combination_id'];//拼团产品id
  245. // $pink['pid'] = $v['product_id'];//产品id
  246. // $pink['people'] = StoreCombination::where('id',$v['combination_id'])->value('people');//几人拼团
  247. // $pink['price'] = $v['productInfo']['price'];//单价
  248. // $pink['stop_time'] = 0;//结束时间
  249. // $pink['add_time'] = time();//开团时间
  250. // $res = StorePink::set($pink)->toArray();
  251. // }
  252. // if($res) $this->redirect('order_pink',['id'=>$res['id']]);
  253. // else $this->failed('创建拼团失败,请退款后再次拼团',Url::build('my/index'));
  254. // $this->redirect('order_pink',['id'=>$order['pink_id']]);
  255. // }else{
  256. // $res = false;
  257. // $pink['uid'] = $order['uid'];//用户id
  258. // $pink['order_id'] = $order['order_id'];//订单id 生成
  259. // $pink['order_id_key'] = $order['id'];//订单id 数据库id
  260. // $pink['total_num'] = $order['total_num'];//购买个数
  261. // $pink['total_price'] = $order['pay_price'];//总金额
  262. // $pink['k_id'] = 0;//拼团id
  263. // foreach ($order['cartInfo'] as $v){
  264. // $pink['cid'] = $v['combination_id'];//拼团产品id
  265. // $pink['pid'] = $v['product_id'];//产品id
  266. // $pink['people'] = StoreCombination::where('id',$v['combination_id'])->value('people');//几人拼团
  267. // $pink['price'] = $v['productInfo']['price'];//单价
  268. // $pink['stop_time'] = time()+86400;//结束时间
  269. // $pink['add_time'] = time();//开团时间
  270. // $res1 = StorePink::set($pink)->toArray();
  271. // $res2 = StoreOrder::where('id',$order['id'])->update(['pink_id'=>$res1['id']]);
  272. // $res = $res1 && $res2;
  273. // }
  274. // if($res) $this->redirect('order_pink',['id'=>$res1['id']]);
  275. // else $this->failed('创建拼团失败,请退款后再次拼团',Url::build('my/index'));
  276. // }
  277. // }
  278. /**
  279. * 参团详情页
  280. */
  281. public function order_pink($id = 0){
  282. if(!$id) return $this->failed('参数错误',Url::build('my/index'));
  283. $pink = StorePink::getPinkUserOne($id);
  284. if(isset($pink['is_refund']) && $pink['is_refund']) {
  285. if($pink['is_refund'] != $pink['id']){
  286. $id = $pink['is_refund'];
  287. return $this->order_pink($id);
  288. }else{
  289. return $this->failed('订单已退款',Url::build('store/combination_detail',['id'=>$pink['cid']]));
  290. }
  291. }
  292. if(!$pink) return $this->failed('参数错误',Url::build('my/index'));
  293. $pinkAll = array();//参团人 不包括团长
  294. $pinkT = array();//团长
  295. if($pink['k_id']){
  296. $pinkAll = StorePink::getPinkMember($pink['k_id']);
  297. $pinkT = StorePink::getPinkUserOne($pink['k_id']);
  298. }else{
  299. $pinkAll = StorePink::getPinkMember($pink['id']);
  300. $pinkT = $pink;
  301. }
  302. $store_combination = StoreCombination::getCombinationOne($pink['cid']);//拼团产品
  303. $count = count($pinkAll)+1;
  304. $count = (int)$pinkT['people']-$count;//剩余多少人
  305. $is_ok = 0;//判断拼团是否完成
  306. $idAll = array();
  307. $uidAll = array();
  308. if(!empty($pinkAll)){
  309. foreach ($pinkAll as $k=>$v){
  310. $idAll[$k] = $v['id'];
  311. $uidAll[$k] = $v['uid'];
  312. }
  313. }
  314. $userBool = 0;//判断当前用户是否在团内 0未在 1在
  315. $pinkBool = 0;//判断当前用户是否在团内 0未在 1在
  316. $idAll[] = $pinkT['id'];
  317. $uidAll[] = $pinkT['uid'];
  318. if($pinkT['status'] == 2){
  319. $pinkBool = 1;
  320. }else{
  321. if(!$count){//组团完成
  322. $idAll = implode(',',$idAll);
  323. $orderPinkStatus = StorePink::setPinkStatus($idAll);
  324. if($orderPinkStatus){
  325. if(in_array($this->uid,$uidAll)){
  326. StorePink::setPinkStopTime($idAll);
  327. if(StorePink::isTpl($uidAll,$pinkT['id'])) StorePink::orderPinkAfter($uidAll,$pinkT['id']);
  328. $pinkBool = 1;
  329. }else $pinkBool = 3;
  330. }else $pinkBool = 6;
  331. }
  332. else{
  333. if($pinkT['stop_time'] < time()){//拼团时间超时 退款
  334. if($pinkAll){
  335. foreach ($pinkAll as $v){
  336. if($v['uid'] == $this->uid){
  337. $res = StoreOrder::orderApplyRefund(StoreOrder::where('id',$v['order_id_key'])->value('order_id'),$this->uid,'拼团时间超时');
  338. if($res){
  339. if(StorePink::isTpl($v['uid'],$pinkT['id'])) StorePink::orderPinkAfterNo($v['uid'],$v['k_id']);
  340. $pinkBool = 2;
  341. }else return $this->failed(StoreOrder::getErrorInfo(),Url::build('index'));
  342. }
  343. }
  344. }
  345. if($pinkT['uid'] == $this->uid){
  346. $res = StoreOrder::orderApplyRefund(StoreOrder::where('id',$pinkT['order_id_key'])->value('order_id'),$this->uid,'拼团时间超时');
  347. if($res){
  348. if(StorePink::isTpl($pinkT['uid'],$pinkT['id'])) StorePink::orderPinkAfterNo($pinkT['uid'],$pinkT['id']);
  349. $pinkBool = 2;
  350. }else return $this->failed(StoreOrder::getErrorInfo(),Url::build('index'));
  351. }
  352. if(!$pinkBool) $pinkBool = 3;
  353. }
  354. }
  355. }
  356. $store_combination_host = StoreCombination::getCombinationHost();//获取推荐的拼团产品
  357. if(!empty($pinkAll)){
  358. foreach ($pinkAll as $v){
  359. if($v['uid'] == $this->uid) $userBool = 1;
  360. }
  361. }
  362. if($pinkT['uid'] == $this->uid) $userBool = 1;
  363. $combinationOne = StoreCombination::getCombinationOne($pink['cid']);
  364. if(!$combinationOne) return $this->failed('拼团不存在或已下架');
  365. $combinationOne['images'] = json_decode($combinationOne['images'],true);
  366. $combinationOne['userLike'] = StoreProductRelation::isProductRelation($combinationOne['product_id'],$this->userInfo['uid'],'like');
  367. $combinationOne['like_num'] = StoreProductRelation::productRelationNum($combinationOne['product_id'],'like');
  368. $combinationOne['userCollect'] = StoreProductRelation::isProductRelation($combinationOne['product_id'],$this->userInfo['uid'],'collect');
  369. $this->assign('storeInfo',$combinationOne);
  370. $this->assign('current_pink_order',StorePink::getCurrentPink($id));
  371. $this->assign(compact('pinkBool','is_ok','userBool','store_combination','pinkT','pinkAll','count','store_combination_host'));
  372. return $this->fetch();
  373. }
  374. /**
  375. * 参团详情页 失败或者成功展示页
  376. */
  377. public function order_pink_after($id = 0){
  378. if(!$id) return $this->failed('参数错误',Url::build('my/index'));
  379. $pink = StorePink::getPinkUserOne($id);
  380. if(!$pink) return $this->failed('参数错误',Url::build('my/index'));
  381. $pinkAll = array();//参团人 不包括团长
  382. $pinkT = array();//团长
  383. if($pink['k_id']){
  384. $pinkAll = StorePink::getPinkMember($pink['k_id']);
  385. $pinkT = StorePink::getPinkUserOne($pink['k_id']);
  386. }else{
  387. $pinkAll = StorePink::getPinkMember($pink['id']);
  388. $pinkT = $pink;
  389. }
  390. $store_combination = StoreCombination::getCombinationOne($pink['cid']);//拼团产品
  391. $count = count($pinkAll)+1;
  392. $count = (int)$pinkT['people']-$count;//剩余多少人
  393. $idAll = array();
  394. $uidAll = array();
  395. if(!empty($pinkAll)){
  396. foreach ($pinkAll as $k=>$v){
  397. $idAll[$k] = $v['id'];
  398. $uidAll[$k] = $v['uid'];
  399. }
  400. }
  401. $idAll[] = $pinkT['id'];
  402. $uidAll[] = $pinkT['uid'];
  403. $userBool = 0;//判断当前用户是否在团内是否完成拼团
  404. if(!$count) $userBool = 1;//组团完成
  405. $store_combination_host = StoreCombination::getCombinationHost();//获取推荐的拼团产品
  406. $combinationOne = StoreCombination::getCombinationOne($pink['cid']);
  407. if(!$combinationOne) return $this->failed('拼团不存在或已下架');
  408. $combinationOne['images'] = json_decode($combinationOne['images'],true);
  409. $combinationOne['userLike'] = StoreProductRelation::isProductRelation($combinationOne['product_id'],$this->userInfo['uid'],'like');
  410. $combinationOne['like_num'] = StoreProductRelation::productRelationNum($combinationOne['product_id'],'like');
  411. $combinationOne['userCollect'] = StoreProductRelation::isProductRelation($combinationOne['product_id'],$this->userInfo['uid'],'collect');
  412. $this->assign('storeInfo',$combinationOne);
  413. $this->assign(compact('userBool','store_combination','pinkT','pinkAll','count','store_combination_host'));
  414. return $this->fetch();
  415. }
  416. /**
  417. * 售后服务 退款订单
  418. * @return mixed
  419. */
  420. public function order_customer(){
  421. return $this->fetch();
  422. }
  423. }