UserApi.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. <?php
  2. namespace app\ebapi\controller;
  3. use Api\Express;
  4. use app\admin\model\system\SystemAttachment;
  5. use app\core\model\routine\RoutineQrcode;
  6. use app\core\model\user\UserLevel;
  7. use app\core\model\user\UserSign;
  8. use app\core\model\routine\RoutineCode;//待完善
  9. use app\core\model\routine\RoutineFormId;//待完善
  10. use app\ebapi\model\store\StoreBargain;
  11. use app\ebapi\model\store\StoreCombination;
  12. use app\ebapi\model\store\StoreCouponUser;
  13. use app\ebapi\model\store\StoreOrder;
  14. use app\ebapi\model\store\StoreOrderCartInfo;
  15. use app\ebapi\model\store\StoreProductRelation;
  16. use app\ebapi\model\store\StoreProductReply;
  17. use app\ebapi\model\store\StoreSeckill;
  18. use app\ebapi\model\user\User;
  19. use app\ebapi\model\user\UserAddress;
  20. use app\core\model\user\UserBill;
  21. use app\ebapi\model\user\UserExtract;
  22. use app\ebapi\model\user\UserNotice;
  23. use app\ebapi\model\user\UserRecharge;
  24. use service\CacheService;
  25. use app\core\util\GroupDataService;
  26. use service\JsonService;
  27. use app\core\util\SystemConfigService;
  28. use service\UploadService;
  29. use service\UtilService;
  30. use think\Request;
  31. use think\Cache;
  32. /**
  33. * 小程序个人中心api接口
  34. * Class UserApi
  35. * @package app\ebapi\controller
  36. *
  37. */
  38. class UserApi extends AuthController
  39. {
  40. /*
  41. * 获取签到按月份查找
  42. * @param int $page 页码
  43. * @param int $limit 显示条数
  44. * @return json
  45. * */
  46. public function get_sign_month_list($page=1,$limit=10)
  47. {
  48. return JsonService::successful(UserSign::getSignMonthList($this->uid,$page,$limit));
  49. }
  50. /*
  51. * 获取用户签到记录列表
  52. *
  53. * */
  54. public function get_sign_list($page=1,$limit=10)
  55. {
  56. return JsonService::successful(UserSign::getSignList($this->uid,$page,$limit));
  57. }
  58. /*
  59. * 获取图片储存位置
  60. *
  61. * */
  62. public function picture_storage_location()
  63. {
  64. return JsonService::successful((int)SystemConfigService::get('upload_type'));
  65. }
  66. /*
  67. * 获取当前登录的用户信息
  68. * */
  69. public function get_my_user_info()
  70. {
  71. list($isSgin,$isIntegral,$isall)=UtilService::getMore([
  72. ['isSgin',0],
  73. ['isIntegral',0],
  74. ['isall',0],
  75. ],$this->request,true);
  76. //是否统计签到
  77. if($isSgin || $isall){
  78. $this->userInfo['sum_sgin_day']=UserSign::getSignSumDay($this->uid);
  79. $this->userInfo['is_day_sgin']=UserSign::getToDayIsSign($this->uid);
  80. $this->userInfo['is_YesterDay_sgin']=UserSign::getYesterDayIsSign($this->uid);
  81. if(!$this->userInfo['is_day_sgin'] && !$this->userInfo['is_YesterDay_sgin']){
  82. $this->userInfo['sign_num']=0;
  83. }
  84. }
  85. //是否统计积分使用情况
  86. if($isIntegral || $isall){
  87. $this->userInfo['sum_integral']=(int)UserBill::getRecordCount($this->uid,'integral','sign,system_add,gain');
  88. $this->userInfo['deduction_integral']=(int)UserBill::getRecordCount($this->uid,'integral','deduction') ? : 0;
  89. $this->userInfo['today_integral']=(int)UserBill::getRecordCount($this->uid,'integral','sign,system_add,gain','today');
  90. }
  91. unset($this->userInfo['pwd']);
  92. $this->userInfo['integral']=(int)$this->userInfo['integral'];
  93. if(!$this->userInfo['is_promoter']){
  94. $this->userInfo['is_promoter']=(int)SystemConfigService::get('store_brokerage_statu') == 2 ? true : false;
  95. }
  96. return JsonService::successful($this->userInfo);
  97. }
  98. /**
  99. * 获取用户信息
  100. * @param int $userId 用户uid
  101. * @return \think\response\Json
  102. */
  103. public function get_user_info_uid($userId = 0){
  104. if(!$userId) return JsonService::fail('参数错误');
  105. $res = User::getUserInfo($userId);
  106. if($res) return JsonService::successful($res);
  107. else return JsonService::fail(User::getErrorInfo());
  108. }
  109. /**
  110. * 个人中心
  111. * @return \think\response\Json
  112. */
  113. public function my(){
  114. $this->userInfo['couponCount'] = StoreCouponUser::getUserValidCouponCount($this->userInfo['uid']);
  115. $this->userInfo['like'] = StoreProductRelation::getUserIdCollect($this->userInfo['uid']);;
  116. $this->userInfo['orderStatusNum'] = StoreOrder::getOrderStatusNum($this->userInfo['uid']);
  117. $this->userInfo['notice'] = UserNotice::getNotice($this->userInfo['uid']);
  118. $this->userInfo['brokerage'] = UserBill::getBrokerage($this->uid);//获取总佣金
  119. $this->userInfo['recharge'] = UserBill::getRecharge($this->uid);//累计充值
  120. $this->userInfo['orderStatusSum'] = StoreOrder::getOrderStatusSum($this->uid);//累计消费
  121. $this->userInfo['extractTotalPrice'] = UserExtract::userExtractTotalPrice($this->uid);//累计提现
  122. if($this->userInfo['brokerage'] > $this->userInfo['extractTotalPrice']) {
  123. $orderYuePrice = StoreOrder::getOrderStatusYueSum($this->uid);//余额累计消费
  124. $systemAdd = UserBill::getSystemAdd($this->uid);//后台添加余额
  125. $yueCount = bcadd($this->userInfo['recharge'],$systemAdd,2);// 后台添加余额 + 累计充值 = 非佣金的总金额
  126. $orderYuePrice = $yueCount > $orderYuePrice ? 0 : bcsub($orderYuePrice,$yueCount,2);// 余额累计消费(使用佣金消费的金额)
  127. $this->userInfo['brokerage'] = bcsub($this->userInfo['brokerage'],$this->userInfo['extractTotalPrice'],2);//减去已提现金额
  128. $extract_price = UserExtract::userExtractTotalPrice($this->uid,0);
  129. $this->userInfo['brokerage'] = $extract_price < $this->userInfo['brokerage'] ? bcsub($this->userInfo['brokerage'],$extract_price,2) : 0;//减去审核中的提现金额
  130. $this->userInfo['brokerage'] = $this->userInfo['brokerage'] > $orderYuePrice ? bcsub($this->userInfo['brokerage'],$orderYuePrice,2) : 0;//减掉余额支付
  131. }else{
  132. $this->userInfo['brokerage']=0;
  133. }
  134. $this->userInfo['extractPrice'] = (float)bcsub($this->userInfo['brokerage'],$this->userInfo['extractTotalPrice'],2) > 0 ?
  135. bcsub($this->userInfo['brokerage'],$this->userInfo['extractTotalPrice'],2) : 0;//可提现
  136. $this->userInfo['statu'] = (int)SystemConfigService::get('store_brokerage_statu');
  137. $vipId=UserLevel::getUserLevel($this->uid);
  138. $this->userInfo['vip']=$vipId !==false ? true : false;
  139. if($this->userInfo['vip']){
  140. $this->userInfo['vip_id']=$vipId;
  141. $this->userInfo['vip_icon']=UserLevel::getUserLevelInfo($vipId,'icon');
  142. $this->userInfo['vip_name']=UserLevel::getUserLevelInfo($vipId,'name');
  143. }
  144. if(!SystemConfigService::get('vip_open')) $this->userInfo['vip']=false;
  145. unset($this->userInfo['pwd']);
  146. return JsonService::successful($this->userInfo);
  147. }
  148. /**
  149. * 用户签到
  150. * @return \think\response\Json
  151. */
  152. public function user_sign()
  153. {
  154. $signed = UserSign::getToDayIsSign($this->userInfo['uid']);
  155. if($signed) return JsonService::fail('已签到');
  156. if(false !== $integral = UserSign::sign($this->uid))
  157. return JsonService::successful('签到获得'.floatval($integral).'积分',['integral'=>$integral]);
  158. else
  159. return JsonService::fail(UserSign::getErrorInfo('签到失败'));
  160. }
  161. /**
  162. * 获取一条用户地址
  163. * @param string $addressId 地址id
  164. * @return \think\response\Json
  165. */
  166. public function get_user_address($addressId = ''){
  167. $addressInfo = [];
  168. if($addressId && is_numeric($addressId) && UserAddress::be(['is_del'=>0,'id'=>$addressId,'uid'=>$this->userInfo['uid']])){
  169. $addressInfo = UserAddress::find($addressId);
  170. }
  171. return JsonService::successful($addressInfo);
  172. }
  173. /**
  174. * 获取默认地址
  175. * @return \think\response\Json
  176. */
  177. public function user_default_address()
  178. {
  179. $defaultAddress = UserAddress::getUserDefaultAddress($this->userInfo['uid'],'id,real_name,phone,province,city,district,detail,is_default');
  180. if($defaultAddress) return JsonService::successful('ok',$defaultAddress);
  181. else return JsonService::successful('empty',[]);
  182. }
  183. /**
  184. * 删除地址
  185. * @param string $addressId 地址id
  186. * @return \think\response\Json
  187. */
  188. public function remove_user_address($addressId = '')
  189. {
  190. if(!$addressId || !is_numeric($addressId)) return JsonService::fail('参数错误!');
  191. if(!UserAddress::be(['is_del'=>0,'id'=>$addressId,'uid'=>$this->userInfo['uid']]))
  192. return JsonService::fail('地址不存在!');
  193. if(UserAddress::edit(['is_del'=>'1'],$addressId,'id'))
  194. return JsonService::successful();
  195. else
  196. return JsonService::fail('删除地址失败!');
  197. }
  198. /**
  199. * 个人中心 获取订单列表
  200. * @param string $type
  201. * @param int $first
  202. * @param int $limit
  203. * @param string $search
  204. * @return \think\response\Json
  205. */
  206. public function get_user_order_list()
  207. {
  208. list($type,$page,$limit,$search)=UtilService::getMore([
  209. ['type',''],
  210. ['page',''],
  211. ['limit',''],
  212. ['search',''],
  213. ],$this->request,true);
  214. return JsonService::successful(StoreOrder::getUserOrderSearchList($this->uid,$type,$page,$limit,$search));
  215. }
  216. /**
  217. * 个人中心 订单详情页
  218. * @param string $order_id
  219. * @return \think\response\Json
  220. */
  221. public function get_order($uni = ''){
  222. if($uni == '') return JsonService::fail('参数错误');
  223. $order = StoreOrder::getUserOrderDetail($this->userInfo['uid'],$uni);
  224. $order = $order->toArray();
  225. $order['add_time_y'] = date('Y-m-d',$order['add_time']);
  226. $order['add_time_h'] = date('H:i:s',$order['add_time']);
  227. if(!$order) return JsonService::fail('订单不存在');
  228. return JsonService::successful(StoreOrder::tidyOrder($order,true,true));
  229. }
  230. /**
  231. * 个人中心 删除订单
  232. * @param string $uni
  233. * @return \think\response\Json
  234. */
  235. public function user_remove_order($uni = '')
  236. {
  237. if(!$uni) return JsonService::fail('参数错误!');
  238. $res = StoreOrder::removeOrder($uni,$this->userInfo['uid']);
  239. if($res)
  240. return JsonService::successful();
  241. else
  242. return JsonService::fail(StoreOrder::getErrorInfo());
  243. }
  244. /**
  245. * 获取用户手机号码
  246. * @param Request $request
  247. * @return \think\response\Json
  248. */
  249. public function bind_mobile(Request $request){
  250. list($iv,$cache_key,$encryptedData) = UtilService::postMore([
  251. ['iv',''],
  252. ['cache_key',''],
  253. ['encryptedData',''],
  254. ],$request,true);
  255. $iv = urldecode(urlencode($iv));
  256. try{
  257. if(!Cache::has('eb_api_code_'.$cache_key)) return JsonService::fail('获取手机号失败');
  258. $session_key=Cache::get('eb_api_code_'.$cache_key);
  259. $userInfo = \app\core\util\MiniProgramService::encryptor($session_key,$iv,$encryptedData);
  260. if(!empty($userInfo['purePhoneNumber'])){
  261. if(User::edit(['phone'=>$userInfo['purePhoneNumber']],$this->userInfo['uid']))
  262. return JsonService::successful('绑定成功',['phone'=>$userInfo['purePhoneNumber']]);
  263. else
  264. return JsonService::fail('绑定失败');
  265. }else
  266. return JsonService::fail('获取手机号失败');
  267. }catch (\Exception $e){
  268. return JsonService::fail('error',$e->getMessage());
  269. }
  270. }
  271. /**
  272. * 个人中心 用户确认收货
  273. * @param string $uni
  274. * @return \think\response\Json
  275. */
  276. public function user_take_order($uni = '')
  277. {
  278. if(!$uni) return JsonService::fail('参数错误!');
  279. $res = StoreOrder::takeOrder($uni,$this->userInfo['uid']);
  280. if($res)
  281. return JsonService::successful();
  282. else
  283. return JsonService::fail(StoreOrder::getErrorInfo());
  284. }
  285. /**
  286. * 个人中心 充值
  287. * @param int $price
  288. * @return \think\response\Json
  289. */
  290. public function user_wechat_recharge($price = 0)
  291. {
  292. if(!$price || $price <=0) return JsonService::fail('参数错误');
  293. $storeMinRecharge = SystemConfigService::get('store_user_min_recharge');
  294. if($price < $storeMinRecharge) return JsonService::fail('充值金额不能低于'.$storeMinRecharge);
  295. $rechargeOrder = UserRecharge::addRecharge($this->userInfo['uid'],$price,'routine');
  296. if(!$rechargeOrder) return JsonService::fail('充值订单生成失败!');
  297. try{
  298. return JsonService::successful(UserRecharge::jsPay($rechargeOrder));
  299. }catch (\Exception $e){
  300. return JsonService::fail($e->getMessage());
  301. }
  302. }
  303. /**
  304. * 个人中心 余额使用记录
  305. * @param int $first
  306. * @param int $limit
  307. * @return \think\response\Json
  308. */
  309. public function user_balance_list($first = 0,$limit = 8)
  310. {
  311. return JsonService::successful(UserBill::userBillList($this->uid,$first,$limit,'now_money'));
  312. }
  313. /**
  314. * 个人中心 积分使用记录
  315. * @param int $first
  316. * @param int $limit
  317. * @return \think\response\Json
  318. */
  319. public function user_integral_list($page = 0,$limit = 8)
  320. {
  321. return JsonService::successful(UserBill::userBillList($this->uid,$page,$limit));
  322. }
  323. /**
  324. * 个人中心 获取一级推荐人
  325. * @param int $first
  326. * @param int $limit
  327. * @return \think\response\Json
  328. */
  329. public function get_spread_list($first = 0,$limit = 20)
  330. {
  331. return JsonService::successful(User::getSpreadList($this->uid,$first,$limit));
  332. }
  333. /**
  334. * 个人中心 获取二级推荐人
  335. * @param int $first
  336. * @param int $limit
  337. * @return \think\response\Json
  338. */
  339. public function get_spread_list_two($two_uid=0,$first = 0,$limit = 20)
  340. {
  341. return JsonService::successful(User::getSpreadList($two_uid,$first,$limit));
  342. }
  343. /**
  344. * 获取用户所有地址
  345. * @return \think\response\Json
  346. */
  347. public function user_address_list($page=1,$limit=8)
  348. {
  349. $list = UserAddress::getUserValidAddressList($this->userInfo['uid'],$page,$limit,'id,real_name,phone,province,city,district,detail,is_default');
  350. return JsonService::successful($list);
  351. }
  352. /**
  353. * 修改用户通知为已查看
  354. * @param $nid
  355. * @return \think\response\Json
  356. */
  357. public function see_notice($nid)
  358. {
  359. UserNotice::seeNotice($this->userInfo['uid'],$nid);
  360. return JsonService::successful();
  361. }
  362. /*
  363. * 用户提现申请
  364. * @param array
  365. * @return \think\response\Json
  366. * */
  367. public function user_extract()
  368. {
  369. $data=UtilService::postMore([
  370. ['alipay_code',''],
  371. ['extract_type',''],
  372. ['money',0],
  373. ['name',''],
  374. ['bankname',''],
  375. ['cardnum',''],
  376. ['weixin',''],
  377. ],$this->request);
  378. if(UserExtract::userExtract($this->userInfo,$data))
  379. return JsonService::successful('申请提现成功!');
  380. else
  381. return JsonService::fail(UserExtract::getErrorInfo('提现失败'));
  382. }
  383. /**
  384. * 用户下级的订单
  385. * @param int $first
  386. * @param int $limit
  387. * @return json
  388. */
  389. public function subordinateOrderlist($first = 0, $limit = 8)
  390. {
  391. list($xUid,$status)=UtilService::postMore([
  392. ['uid',''],
  393. ['status',''],
  394. ],$this->request,true);
  395. switch ($status){
  396. case 0:
  397. $type='';
  398. break;
  399. case 1:
  400. $type=4;
  401. break;
  402. case 2:
  403. $type=3;
  404. break;
  405. default:
  406. return JsonService::fail();
  407. }
  408. return JsonService::successful(StoreOrder::getSubordinateOrderlist($xUid,$this->uid,$type,$first,$limit));
  409. }
  410. /**
  411. * 个人中心 用户下级的订单
  412. * @param int $first
  413. * @param int $limit
  414. * @return json
  415. */
  416. public function subordinateOrderlistmoney()
  417. {
  418. $request = Request::instance();
  419. $lists=$request->param();
  420. $status = $lists['status'];
  421. $type = '';
  422. if($status == 1) $type = 4;
  423. elseif($status == 2) $type = 3;
  424. $arr = User::where('spread_uid',$this->userInfo['uid'])->column('uid');
  425. $list = StoreOrder::getUserOrderCount(implode(',',$arr),$type);
  426. $price = [];
  427. // if(!empty($list)) foreach ($list as $k=>$v) $price[]=$v['pay_price'];
  428. if(!empty($list)) foreach ($list as $k=>$v) $price[]=$v;
  429. $cont = count($list);
  430. $sum = array_sum($price);
  431. return JsonService::successful(['cont'=>$cont,'sum'=>$sum]);
  432. }
  433. /*
  434. * 用户提现记录列表
  435. * @param int $first 截取行数
  436. * @param int $limit 展示条数
  437. * @return json
  438. */
  439. public function extract($first = 0,$limit = 8)
  440. {
  441. return JsonService::successful(UserExtract::extractList($this->uid,$first,$limit));
  442. }
  443. /**
  444. * 个人中心 订单 评价订单
  445. * @param string $unique
  446. * @return \think\response\Json
  447. */
  448. public function user_comment_product($unique = '')
  449. {
  450. if(!$unique) return JsonService::fail('参数错误!');
  451. $cartInfo = StoreOrderCartInfo::where('unique',$unique)->find();
  452. $uid = $this->userInfo['uid'];
  453. if(!$cartInfo || $uid != $cartInfo['cart_info']['uid']) return JsonService::fail('评价产品不存在!');
  454. if(StoreProductReply::be(['oid'=>$cartInfo['oid'],'unique'=>$unique]))
  455. return JsonService::fail('该产品已评价!');
  456. $group = UtilService::postMore([
  457. ['comment',''],['pics',[]],['product_score',5],['service_score',5]
  458. ],Request::instance());
  459. $group['comment'] = htmlspecialchars(trim($group['comment']));
  460. if($group['product_score'] < 1) return JsonService::fail('请为产品评分');
  461. else if($group['service_score'] < 1) return JsonService::fail('请为商家服务评分');
  462. if($cartInfo['cart_info']['combination_id']) $productId = $cartInfo['cart_info']['product_id'];
  463. else if($cartInfo['cart_info']['seckill_id']) $productId = $cartInfo['cart_info']['product_id'];
  464. else if($cartInfo['cart_info']['bargain_id']) $productId = $cartInfo['cart_info']['product_id'];
  465. else $productId = $cartInfo['product_id'];
  466. $group = array_merge($group,[
  467. 'uid'=>$uid,
  468. 'oid'=>$cartInfo['oid'],
  469. 'unique'=>$unique,
  470. 'product_id'=>$productId,
  471. 'reply_type'=>'product'
  472. ]);
  473. StoreProductReply::beginTrans();
  474. $res = StoreProductReply::reply($group,'product');
  475. if(!$res) {
  476. StoreProductReply::rollbackTrans();
  477. return JsonService::fail('评价失败!');
  478. }
  479. try{
  480. // HookService::listen('store_product_order_reply',$group,$cartInfo,false,StoreProductBehavior::class);
  481. StoreOrder::checkOrderOver($cartInfo['oid']);
  482. }catch (\Exception $e){
  483. StoreProductReply::rollbackTrans();
  484. return JsonService::fail($e->getMessage());
  485. }
  486. StoreProductReply::commitTrans();
  487. return JsonService::successful();
  488. }
  489. /*
  490. * 个人中心 查物流
  491. * @param int $uid 用户id
  492. * @param string $uni 订单id或者订单唯一键
  493. * @return json
  494. */
  495. public function express($uni = '')
  496. {
  497. if(!$uni || !($order = StoreOrder::getUserOrderDetail($this->uid,$uni))) return JsonService::fail('查询订单不存在!');
  498. if($order['delivery_type'] != 'express' || !$order['delivery_id']) return JsonService::fail('该订单不存在快递单号!');
  499. $cacheName = $uni.$order['delivery_id'];
  500. CacheService::rm($cacheName);
  501. $result = CacheService::get($cacheName,null);
  502. if($result === NULL){
  503. $result = Express::query($order['delivery_id']);
  504. if(is_array($result) &&
  505. isset($result['result']) &&
  506. isset($result['result']['deliverystatus']) &&
  507. $result['result']['deliverystatus'] >= 3)
  508. $cacheTime = 0;
  509. else
  510. $cacheTime = 1800;
  511. CacheService::set($cacheName,$result,$cacheTime);
  512. }
  513. return JsonService::successful([ 'order'=>StoreOrder::tidyOrder($order,true), 'express'=>$result ? $result : []]);
  514. }
  515. /**
  516. * 修改收货地址
  517. * @return \think\response\Json
  518. */
  519. public function edit_user_address()
  520. {
  521. $request = Request::instance();
  522. if(!$request->isPost()) return JsonService::fail('参数错误!');
  523. $addressInfo = UtilService::postMore([
  524. ['address',[]],
  525. ['is_default',false],
  526. ['real_name',''],
  527. ['post_code',''],
  528. ['phone',''],
  529. ['detail',''],
  530. ['id',0]
  531. ],$request);
  532. $addressInfo['province'] = $addressInfo['address']['province'];
  533. $addressInfo['city'] = $addressInfo['address']['city'];
  534. $addressInfo['district'] = $addressInfo['address']['district'];
  535. $addressInfo['is_default'] = $addressInfo['is_default'] == true ? 1 : 0;
  536. $addressInfo['uid'] = $this->userInfo['uid'];
  537. unset($addressInfo['address']);
  538. if($addressInfo['id'] && UserAddress::be(['id'=>$addressInfo['id'],'uid'=>$this->userInfo['uid'],'is_del'=>0])){
  539. $id = $addressInfo['id'];
  540. unset($addressInfo['id']);
  541. if(UserAddress::edit($addressInfo,$id,'id')){
  542. if($addressInfo['is_default'])
  543. UserAddress::setDefaultAddress($id,$this->userInfo['uid']);
  544. return JsonService::successful();
  545. }else
  546. return JsonService::fail('编辑收货地址失败!');
  547. }else{
  548. if($address = UserAddress::set($addressInfo)){
  549. if($addressInfo['is_default'])
  550. UserAddress::setDefaultAddress($address->id,$this->userInfo['uid']);
  551. return JsonService::successful(['id'=>$address->id]);
  552. }else
  553. return JsonService::fail('添加收货地址失败!');
  554. }
  555. }
  556. /**
  557. * 用户通知
  558. * @param int $page
  559. * @param int $limit
  560. * @return \think\response\Json
  561. */
  562. public function get_notice_list($page = 0, $limit = 8)
  563. {
  564. $list = UserNotice::getNoticeList($this->userInfo['uid'],$page,$limit);
  565. return JsonService::successful($list);
  566. }
  567. /*
  568. * 昨日推广佣金
  569. * @return json
  570. */
  571. public function yesterday_commission()
  572. {
  573. return JsonService::successful(UserBill::yesterdayCommissionSum($this->uid));
  574. }
  575. /*
  576. * 累计已提金额
  577. * @return json
  578. */
  579. public function extractsum()
  580. {
  581. return JsonService::successful(UserExtract::extractSum($this->uid));
  582. }
  583. /**
  584. * 绑定推荐人
  585. * @param Request $request
  586. * @return \think\response\Json
  587. */
  588. public function spread_uid(Request $request){
  589. $data = UtilService::postMore(['spread_uid',0],$request);
  590. if($data['spread_uid']){
  591. if(!$this->userInfo['spread_uid']){
  592. $res = User::edit(['spread_uid'=>$data['spread_uid']],$this->userInfo['uid']);
  593. if($res) return JsonService::successful('绑定成功');
  594. else return JsonService::successful('绑定失败');
  595. }else return JsonService::fail('已存在被推荐人');
  596. }else return JsonService::fail('没有推荐人');
  597. }
  598. /**
  599. * 设置为默认地址
  600. * @param string $addressId
  601. * @return \think\response\Json
  602. */
  603. public function set_user_default_address($addressId = '')
  604. {
  605. if(!$addressId || !is_numeric($addressId)) return JsonService::fail('参数错误!');
  606. if(!UserAddress::be(['is_del'=>0,'id'=>$addressId,'uid'=>$this->userInfo['uid']]))
  607. return JsonService::fail('地址不存在!');
  608. $res = UserAddress::setDefaultAddress($addressId,$this->userInfo['uid']);
  609. if(!$res)
  610. return JsonService::fail('地址不存在!');
  611. else
  612. return JsonService::successful();
  613. }
  614. /**
  615. * 获取分销二维码
  616. * @return \think\response\Json
  617. */
  618. public function get_code(){
  619. header('content-type:image/jpg');
  620. if(!$this->userInfo['uid']) return JsonService::fail('授权失败,请重新授权');
  621. $path = makePathToUrl('routine/code');
  622. if($path == '')
  623. return JsonService::fail('生成上传目录失败,请检查权限!');
  624. $picname = $path.'/'.$this->userInfo['uid'].'.jpg';
  625. $domain = SystemConfigService::get('site_url').'/';
  626. $domainTop = substr($domain,0,5);
  627. if($domainTop != 'https') $domain = 'https:'.substr($domain,5,strlen($domain));
  628. if(file_exists($picname)) return JsonService::successful($domain.$picname);
  629. else{
  630. $res = RoutineCode::getCode($this->userInfo['uid'],$picname);
  631. if($res) file_put_contents($picname,$res);
  632. else return JsonService::fail('二维码生成失败');
  633. }
  634. return JsonService::successful($domain.$picname);
  635. }
  636. /*
  637. * 修改用户信息
  638. * */
  639. public function edit_user($formid=''){
  640. list($avatar,$nickname)=UtilService::postMore([
  641. ['avatar',''],
  642. ['nickname',''],
  643. ],$this->request,true);
  644. RoutineFormId::SetFormId($formid,$this->uid);
  645. if(User::editUser($avatar,$nickname,$this->uid))
  646. return JsonService::successful('修改成功');
  647. else
  648. return JsonService::fail('');
  649. }
  650. /*
  651. * 查找用户消费充值记录
  652. *
  653. * */
  654. public function get_user_bill_list($page=1,$limit=8,$type=0)
  655. {
  656. return JsonService::successful(UserBill::getUserBillList($this->uid,$page,$limit,$type));
  657. }
  658. /*
  659. * 获取活动是否存在
  660. * */
  661. public function get_activity()
  662. {
  663. $data['is_bargin']=StoreBargain::validBargain() ? true : false;
  664. $data['is_pink']=StoreCombination::getPinkIsOpen() ? true : false;
  665. $data['is_seckill']=StoreSeckill::getSeckillCount() ? true : false;
  666. return JsonService::successful($data);
  667. }
  668. /**
  669. * TODO 获取记录总和
  670. * @param int $type
  671. */
  672. public function get_record_list_count($type = 3)
  673. {
  674. $count = 0;
  675. if($type == 3) $count = UserBill::getRecordCount($this->uid, 'now_money', 'brokerage');
  676. else if($type == 4) $count = UserExtract::userExtractTotalPrice($this->uid);//累计提现
  677. $count = $count ? $count : 0;
  678. JsonService::successful('',$count);
  679. }
  680. /**
  681. * TODO 获取订单返佣记录
  682. * @param int $first
  683. * @param int $limit
  684. * @param string $category
  685. * @param string $type
  686. */
  687. public function get_record_order_list($page = 0,$limit = 8,$category = 'now_money', $type = 'brokerage'){
  688. $data['list'] = [];
  689. $data['count'] = 0;
  690. $data['list'] = UserBill::getRecordList($this->uid,$page,$limit,$category,$type);
  691. $count = UserBill::getRecordOrderCount($this->uid, $category, $type);
  692. $data['count'] = $count ? $count : 0;
  693. if(!count($data['list'])) return JsonService::successful([]);
  694. foreach ($data['list'] as $key=>&$value){
  695. $value['child'] = UserBill::getRecordOrderListDraw($this->uid, $value['time'],$category, $type);
  696. $value['count'] = count($value['child']);
  697. }
  698. return JsonService::successful($data);
  699. }
  700. /**
  701. * TODO 获取推广人列表
  702. * @param int $first
  703. * @param int $limit
  704. * @param int $type
  705. * @param int $keyword
  706. * @param string $order
  707. */
  708. public function user_spread_new_list($page = 0,$limit = 8,$grade = 0,$keyword = 0,$sort = ''){
  709. if(!$keyword) $keyword = '';
  710. $data['list'] = User::getUserSpreadGrade($this->userInfo['uid'],$grade,$sort,$keyword,$page,$limit);
  711. $data['total'] = User::getSpreadCount($this->uid);
  712. $data['totalLevel'] = User::getSpreadLevelCount($this->uid);
  713. return JsonService::successful($data);
  714. }
  715. /**
  716. * 分销二维码海报生成
  717. */
  718. public function user_spread_banner_list(){
  719. try{
  720. $routineSpreadBanner = GroupDataService::getData('routine_spread_banner');
  721. if(!count($routineSpreadBanner)) return JsonService::fail('暂无海报');
  722. $name = $this->userInfo['uid'].'_'.$this->userInfo['is_promoter'].'_user.jpg';
  723. $imageInfo = SystemAttachment::getInfo($name,'name');
  724. $siteUrl = SystemConfigService::get('site_url').DS;
  725. //检测远程文件是否存在
  726. if(isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'],'http')!==false && UtilService::CurlFileExist($imageInfo['att_dir']) === false){
  727. $imageInfo=null;
  728. SystemAttachment::where(['name'=>$name])->delete();
  729. }
  730. if(!$imageInfo){
  731. $res = RoutineCode::getShareCode($this->uid, 'spread', '', '');
  732. if(!$res) return JsonService::fail('二维码生成失败');
  733. $imageInfo = UploadService::imageStream($name,$res['res'],'routine/spread/code');
  734. if(!is_array($imageInfo)) return JsonService::fail($imageInfo);
  735. SystemAttachment::attachmentAdd($imageInfo['name'],$imageInfo['size'],$imageInfo['type'],$imageInfo['dir'],$imageInfo['thumb_path'],1,$imageInfo['image_type'],$imageInfo['time'],2);
  736. RoutineQrcode::setRoutineQrcodeFind($res['id'],['status'=>1,'time'=>time(),'qrcode_url'=>$imageInfo['dir']]);
  737. $urlCode = $imageInfo['dir'];
  738. }else $urlCode = $imageInfo['att_dir'];
  739. if($imageInfo['image_type'] == 1) $urlCode = ROOT_PATH.$urlCode;
  740. $res = true;
  741. $domainTop = substr($siteUrl,0,5);
  742. if($domainTop != 'https') $siteUrl = 'https:'.substr($siteUrl,5,strlen($siteUrl));
  743. $filelink=[
  744. 'Bold'=>'public/static/font/Alibaba-PuHuiTi-Regular.otf',
  745. 'Normal'=>'public/static/font/Alibaba-PuHuiTi-Regular.otf',
  746. ];
  747. if(!file_exists($filelink['Bold'])) return JsonService::fail('缺少字体文件Bold');
  748. if(!file_exists($filelink['Normal'])) return JsonService::fail('缺少字体文件Normal');
  749. foreach ($routineSpreadBanner as $key=>&$item){
  750. $posterInfo = '海报生成失败:(';
  751. $config = array(
  752. 'image'=>array(
  753. array(
  754. 'url'=>strstr($urlCode,ROOT_PATH) === false && strpos($urlCode,'http') === false ? str_replace('//','/',ROOT_PATH.$urlCode) : $urlCode, //二维码资源
  755. 'stream'=>0,
  756. 'left'=>114,
  757. 'top'=>790,
  758. 'right'=>0,
  759. 'bottom'=>0,
  760. 'width'=>120,
  761. 'height'=>120,
  762. 'opacity'=>100
  763. )
  764. ),
  765. 'text'=>array(
  766. array(
  767. 'text'=>$this->userInfo['nickname'],
  768. 'left'=>250,
  769. 'top'=>840,
  770. 'fontPath'=>ROOT_PATH.$filelink['Bold'], //字体文件
  771. 'fontSize'=>16, //字号
  772. 'fontColor'=>'40,40,40', //字体颜色
  773. 'angle'=>0,
  774. ),
  775. array(
  776. 'text'=>'邀请您加入'.SystemConfigService::get('site_name'),
  777. 'left'=>250,
  778. 'top'=>880,
  779. 'fontPath'=>ROOT_PATH.$filelink['Normal'], //字体文件
  780. 'fontSize'=>16, //字号
  781. 'fontColor'=>'40,40,40', //字体颜色
  782. 'angle'=>0,
  783. )
  784. ),
  785. 'background'=>$item['pic']
  786. );
  787. $res = $res && $posterInfo = UtilService::setSharePoster($config,'routine/spread/poster');
  788. if(!is_array($posterInfo)) return JsonService::fail($posterInfo);
  789. SystemAttachment::attachmentAdd($posterInfo['name'],$posterInfo['size'],$posterInfo['type'],$posterInfo['dir'],$posterInfo['thumb_path'],1,$posterInfo['image_type'],$posterInfo['time'],2);
  790. if($res){
  791. if($posterInfo['image_type'] == 1) $item['poster'] = $siteUrl.$posterInfo['dir'];
  792. else $item['poster'] = $posterInfo['dir'];
  793. $item['poster'] = str_replace('\\','/',$item['poster']);
  794. if(strstr($item['poster'],'http')===false) $item['poster']=SystemConfigService::get('site_url').$item['poster'];
  795. }
  796. }
  797. if($res) return JsonService::successful($routineSpreadBanner);
  798. else return JsonService::fail('生成图片失败');
  799. }catch (\Exception $e){
  800. return JsonService::fail('生成图片时,系统错误',['line'=>$e->getLine(),'message'=>$e->getMessage()]);
  801. }
  802. }
  803. }