AuthApi.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\controller;
  12. use app\wap\model\user\SmsCode;
  13. use app\wap\model\store\StoreOrderCartInfo;
  14. use app\wap\model\store\StorePink;
  15. use app\wap\model\store\StoreProductReply;
  16. use app\wap\model\store\StoreService;
  17. use app\wap\model\store\StoreServiceLog;
  18. use app\wap\model\store\StoreCart;
  19. use app\wap\model\store\StoreCategory;
  20. use app\wap\model\store\StoreOrder;
  21. use app\wap\model\store\StoreProduct;
  22. use app\wap\model\store\StoreProductAttr;
  23. use app\wap\model\store\StoreProductRelation;
  24. use app\wap\model\user\User;
  25. use app\wap\model\user\UserAddress;
  26. use app\wap\model\user\UserBill;
  27. use app\wap\model\user\UserExtract;
  28. use app\wap\model\user\UserRecharge;
  29. use app\wap\model\user\UserNotice;
  30. use app\wap\model\user\UserSign;
  31. use app\wap\model\user\SignPoster;
  32. use app\wap\model\user\WechatUser;
  33. use behavior\wap\StoreProductBehavior;
  34. use service\AliMessageService;
  35. use service\WechatTemplateService;
  36. use service\CacheService;
  37. use service\HookService;
  38. use service\JsonService;
  39. use service\SystemConfigService;
  40. use service\GroupDataService;
  41. use service\UtilService;
  42. use service\WechatService;
  43. use think\Cache;
  44. use think\Request;
  45. use think\Session;
  46. use think\Url;
  47. use app\wap\model\user\MemberShip;
  48. use app\wap\model\user\MemberCard;//会员卡
  49. use app\wap\model\user\MemberCardBatch;//会员卡批次
  50. use service\sms\storage\Sms;
  51. class AuthApi extends AuthController
  52. {
  53. public static function WhiteList()
  54. {
  55. return [
  56. 'code'
  57. ];
  58. }
  59. public function upload()
  60. {
  61. $aliyunOss = \Api\AliyunOss::instance([
  62. 'AccessKey' => SystemConfigService::get('accessKeyId'),
  63. 'AccessKeySecret' => SystemConfigService::get('accessKeySecret'),
  64. 'OssEndpoint' => SystemConfigService::get('end_point'),
  65. 'OssBucket' => SystemConfigService::get('OssBucket'),
  66. 'uploadUrl' => SystemConfigService::get('uploadUrl'),
  67. ]);
  68. $res = $aliyunOss->upload('file');
  69. if ($res) {
  70. return JsonService::successful('上传成功', ['url' => $res['url']]);
  71. } else {
  72. return JsonService::fail('上传失败');
  73. }
  74. }
  75. /**
  76. * 发送短信验证码
  77. * @param string $phone
  78. */
  79. public function code($phone = '')
  80. {
  81. $name = "is_phone_code" . $phone;
  82. if ($phone == '') return JsonService::fail('请输入手机号码!');
  83. $time = Session::get($name, 'wap');
  84. if ($time < time() + 60) Session::delete($name, 'wap');
  85. if (Session::has($name, 'wap') && $time < time()) return JsonService::fail('您发送验证码的频率过高,请稍后再试!');
  86. $code = AliMessageService::getVerificationCode();
  87. SmsCode::set(['tel' => $phone, 'code' => $code, 'last_time' => time() + 300, 'uid' => $this->uid]);
  88. Session::set($name, time() + 60, 'wap');
  89. $smsHandle = new Sms();
  90. $sms_platform_selection=SystemConfigService::get('sms_platform_selection');
  91. $smsSignName=SystemConfigService::get('smsSignName');//短信模板ID
  92. $smsTemplateCode=SystemConfigService::get('smsTemplateCode');//短信模板ID
  93. if($sms_platform_selection==1){
  94. if(!$smsSignName || !$smsTemplateCode) return JsonService::fail('系统后台短信没有配置,请稍后在试!');
  95. $res = AliMessageService::sendmsg($phone, $code);
  96. }else{
  97. $res=$smsHandle->send($phone,$smsTemplateCode,['code'=>$code]);
  98. }
  99. if($res){
  100. return JsonService::successful('发送成功',$res);
  101. } else {
  102. return JsonService::fail('发送失败!');
  103. }
  104. }
  105. /**
  106. * 用户签到信息
  107. */
  108. public function getUserList(){
  109. $signList = UserSign::userSignInlist($this->userInfo['uid'],1,3);
  110. return JsonService::successful($signList);
  111. }
  112. /**
  113. * 签到明细
  114. */
  115. public function getUserSignList($page,$limit){
  116. $signList = UserSign::userSignInlist($this->userInfo['uid'],$page,$limit);
  117. return JsonService::successful($signList);
  118. }
  119. /**
  120. * 签到
  121. */
  122. public function user_sign()
  123. {
  124. $gold_name=SystemConfigService::get('gold_name');//虚拟币名称
  125. $signed = UserSign::checkUserSigned($this->userInfo['uid']);
  126. if ($signed) return JsonService::fail('已签到');
  127. if (false !== $gold_coin = UserSign::sign($this->userInfo,$gold_name)){
  128. $poster=SignPoster::todaySignPoster($this->userInfo['uid']);
  129. if($poster){
  130. return JsonService::successful('签到获得' . floatval($gold_coin).$gold_name,$poster);
  131. }else{
  132. return JsonService::fail('生成海报失败!');
  133. }
  134. }else
  135. return JsonService::fail('签到失败!');
  136. }
  137. /**
  138. * 用户信息
  139. */
  140. public function userInfo(){
  141. $user=$this->userInfo;
  142. $surplus=0; //会员剩余天数
  143. $time=bcsub($user['overdue_time'],time(),0);
  144. if($user['level']>0 && $time>0) $surplus=bcdiv($time,86400,2);
  145. $user['surplus']=ceil($surplus);
  146. return JsonService::successful($user);
  147. }
  148. /**
  149. * 商品退款原因
  150. */
  151. public function refund_reason()
  152. {
  153. $goods_order_refund_reason=GroupDataService::getData('goods_order_refund_reason') ?:[];
  154. return JsonService::successful($goods_order_refund_reason);
  155. }
  156. /**
  157. * 会员页数据
  158. */
  159. public function merberDatas(){
  160. $interests=GroupDataService::getData('membership_interests',3)?:[];
  161. $description=GroupDataService::getData('member_description')?:[];
  162. $interests_sort = array_column($interests,'sort');
  163. array_multisort($interests_sort,SORT_ASC,$interests);
  164. $description_sort = array_column($description,'sort');
  165. array_multisort($description_sort,SORT_ASC,$description);
  166. $data['interests']=$interests;
  167. $data['description']=$description;
  168. $data['member']=MemberShip::memberMinOne();
  169. return JsonService::successful($data);
  170. }
  171. /**
  172. * 免费会员/是否领取
  173. */
  174. public function isRecord()
  175. {
  176. $data['freeData']=MemberShip::memberFree($this->userInfo['uid']);
  177. return JsonService::successful($data);
  178. }
  179. /**
  180. * 会员设置列表
  181. */
  182. public function membershipLists(){
  183. $meList=MemberShip::membershipList();
  184. return JsonService::successful($meList);
  185. }
  186. /**
  187. * 会员卡激活
  188. */
  189. public function confirm_activation(){
  190. $request = Request::instance();
  191. if (!$request->isPost()) return JsonService::fail('参数错误!');
  192. $data = UtilService::postMore([
  193. ['member_code', ''],
  194. ['member_pwd', ''],
  195. ], $request);
  196. $res=MemberCard::confirmActivation($data,$this->userInfo);
  197. if($res)
  198. return JsonService::successful('激活成功');
  199. else
  200. return JsonService::fail(MemberCard::getErrorInfo('激活失败!'));
  201. }
  202. /**
  203. * 用户领取免费会员
  204. */
  205. public function memberPurchase($id=0){
  206. if(!$id) return JsonService::fail('参数错误!');
  207. $order = StoreOrder::cacheMemberCreateOrder($this->userInfo['uid'],$id,'weixin');
  208. $orderId = $order['order_id'];
  209. $info = compact('orderId');
  210. if ($orderId) {
  211. $orderInfo = StoreOrder::where('order_id', $orderId)->find();
  212. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  213. if ($orderInfo['paid']) exception('支付已支付!');
  214. if (bcsub((float)$orderInfo['pay_price'], 0, 2) <= 0) {
  215. if (StoreOrder::jsPayMePrice($orderId, $this->userInfo['uid']))
  216. return JsonService::status('success', '领取成功', $info);
  217. else
  218. return JsonService::status('pay_error', StoreOrder::getErrorInfo());
  219. } else {
  220. try {
  221. $jsConfig = StoreOrder::jsPayMember($orderId);
  222. } catch (\Exception $e) {
  223. return JsonService::status('pay_error', $e->getMessage(), $info);
  224. }
  225. $info['jsConfig'] = $jsConfig;
  226. return JsonService::status('wechat_pay', '领取成功', $info);
  227. }
  228. } else {
  229. return JsonService::fail(StoreOrder::getErrorInfo('领取失败!'));
  230. }
  231. }
  232. /**加入购物车
  233. * @param string $productId
  234. * @param int $cartNum
  235. * @param string $uniqueId
  236. */
  237. public function set_cart($productId = '', $cartNum = 1, $uniqueId = '')
  238. {
  239. if (!$productId || !is_numeric($productId)) return $this->failed('参数错误!');
  240. $res = StoreCart::setCart($this->userInfo['uid'], $productId, $cartNum, $uniqueId, 'product');
  241. if (!$res)
  242. return $this->failed(StoreCart::getErrorInfo('加入购物车失败!'));
  243. else {
  244. HookService::afterListen('store_product_set_cart_after', $res, $this->userInfo, false, StoreProductBehavior::class);
  245. return $this->successful('ok', ['cartId' => $res->id]);
  246. }
  247. }
  248. /**加入购物车立即购买
  249. * @param int $productId
  250. * @param int $cartNum
  251. * @param string $uniqueId
  252. */
  253. public function now_buy($productId = 0, $cartNum = 1, $uniqueId = '')
  254. {
  255. if ($productId == '' || $productId<0) return $this->failed('参数错误!');
  256. $res = StoreCart::setCart($this->userInfo['uid'], $productId, $cartNum, $uniqueId, 'product', 1);
  257. if (!$res)
  258. return $this->failed(StoreCart::getErrorInfo('加入购物车失败!'));
  259. else {
  260. return $this->successful('ok', ['cartId' => $res->id]);
  261. }
  262. }
  263. /**点赞
  264. * @param string $productId
  265. * @param string $category
  266. */
  267. public function like_product($productId = '', $category = 'product')
  268. {
  269. if (!$productId || !is_numeric($productId)) return $this->failed('参数错误!');
  270. $res = StoreProductRelation::productRelation($productId, $this->userInfo['uid'], 'like', $category);
  271. if (!$res)
  272. return $this->failed(StoreProductRelation::getErrorInfo('点赞失败!'));
  273. else
  274. return $this->successful();
  275. }
  276. /**取消点赞
  277. * @param string $productId
  278. * @param string $category
  279. */
  280. public function unlike_product($productId = '', $category = 'product')
  281. {
  282. if (!$productId || !is_numeric($productId)) return $this->failed('参数错误!');
  283. $res = StoreProductRelation::unProductRelation($productId, $this->userInfo['uid'], 'like', $category);
  284. if (!$res)
  285. return $this->failed(StoreProductRelation::getErrorInfo('取消点赞失败!'));
  286. else
  287. return $this->successful();
  288. }
  289. /**商品收藏
  290. * @param $productId
  291. * @param string $category
  292. */
  293. public function collect_product($productId, $category = 'product')
  294. {
  295. if (!$productId || !is_numeric($productId)) return $this->failed('参数错误!');
  296. $res = StoreProductRelation::productRelation($productId, $this->userInfo['uid'], 'collect', $category);
  297. if (!$res)
  298. return $this->failed(StoreProductRelation::getErrorInfo('收藏失败!'));
  299. else
  300. return $this->successful();
  301. }
  302. /**商品取消收藏
  303. * @param $productId
  304. * @param string $category
  305. */
  306. public function uncollect_product($productId, $category = 'product')
  307. {
  308. if (!$productId || !is_numeric($productId)) return $this->failed('参数错误!');
  309. $res = StoreProductRelation::unProductRelation($productId, $this->userInfo['uid'], 'collect', $category);
  310. if (!$res)
  311. return $this->failed(StoreProductRelation::getErrorInfo('取消收藏失败!'));
  312. else
  313. return $this->successful();
  314. }
  315. public function get_cart_num()
  316. {
  317. return JsonService::successful('ok', StoreCart::getUserCartNum($this->userInfo['uid'], 'product'));
  318. }
  319. public function get_cart_list()
  320. {
  321. return JsonService::successful('ok', StoreCart::getUserProductCartList($this->userInfo['uid']));
  322. }
  323. public function change_cart_num($cartId = '', $cartNum = '')
  324. {
  325. if (!$cartId || !$cartNum || !is_numeric($cartId) || !is_numeric($cartNum)) return JsonService::fail('参数错误!');
  326. StoreCart::changeUserCartNum($cartId, $cartNum, $this->userInfo['uid']);
  327. return JsonService::successful();
  328. }
  329. public function remove_cart($ids = '')
  330. {
  331. if (!$ids) return JsonService::fail('参数错误!');
  332. StoreCart::removeUserCart($this->userInfo['uid'], $ids);
  333. return JsonService::successful();
  334. }
  335. public function get_user_collect_product($first = 0, $limit = 8)
  336. {
  337. $list = StoreProductRelation::where('A.uid', $this->userInfo['uid'])
  338. ->field('B.id pid,B.store_name,B.price,B.ot_price,B.sales,B.image,B.is_del,B.is_show')->alias('A')
  339. ->where('A.type', 'collect')->where('A.category', 'product')
  340. ->order('A.add_time DESC')->join('__STORE_PRODUCT__ B', 'A.product_id = B.id')
  341. ->limit($first, $limit)->select()->toArray();
  342. foreach ($list as $k => $product) {
  343. if ($product['pid']) {
  344. $list[$k]['is_fail'] = $product['is_del'] && $product['is_show'];
  345. } else {
  346. unset($list[$k]);
  347. }
  348. }
  349. return JsonService::successful($list);
  350. }
  351. public function remove_user_collect_product($productId = '')
  352. {
  353. if (!$productId || !is_numeric($productId)) return JsonService::fail('参数错误!');
  354. StoreProductRelation::unProductRelation($productId, $this->userInfo['uid'], 'collect', 'product');
  355. return JsonService::successful();
  356. }
  357. /**设置默认地址
  358. * @param string $addressId
  359. */
  360. public function set_user_default_address($addressId = '')
  361. {
  362. if (!$addressId || !is_numeric($addressId)) return JsonService::fail('参数错误!');
  363. if (!UserAddress::be(['is_del' => 0, 'id' => $addressId, 'uid' => $this->userInfo['uid']]))
  364. return JsonService::fail('地址不存在!');
  365. $res = UserAddress::setDefaultAddress($addressId, $this->userInfo['uid']);
  366. if (!$res)
  367. return JsonService::fail('地址不存在!');
  368. else
  369. return JsonService::successful();
  370. }
  371. /**
  372. * 添加和修改地址
  373. */
  374. public function edit_user_address()
  375. {
  376. $request = Request::instance();
  377. if (!$request->isPost()) return JsonService::fail('参数错误!');
  378. $addressInfo = UtilService::postMore([
  379. ['address', []],
  380. ['is_default', false],
  381. ['real_name', ''],
  382. ['post_code', ''],
  383. ['phone', ''],
  384. ['detail', ''],
  385. ['id', 0]
  386. ], $request);
  387. $addressInfo['province'] = $addressInfo['address']['province'];
  388. $addressInfo['city'] = $addressInfo['address']['city'];
  389. $addressInfo['district'] = $addressInfo['address']['district'];
  390. $addressInfo['is_default'] = $addressInfo['is_default'] == true ? 1 : 0;
  391. $addressInfo['uid'] = $this->userInfo['uid'];
  392. unset($addressInfo['address']);
  393. if ($addressInfo['id'] && UserAddress::be(['id' => $addressInfo['id'], 'uid' => $this->userInfo['uid'], 'is_del' => 0])) {
  394. $id = $addressInfo['id'];
  395. unset($addressInfo['id']);
  396. if (UserAddress::edit($addressInfo, $id, 'id')) {
  397. if ($addressInfo['is_default'])
  398. UserAddress::setDefaultAddress($id, $this->userInfo['uid']);
  399. return JsonService::successful();
  400. } else
  401. return JsonService::fail('编辑收货地址失败!');
  402. } else {
  403. if ($address = UserAddress::set($addressInfo)) {
  404. if ($addressInfo['is_default'])
  405. UserAddress::setDefaultAddress($address->id, $this->userInfo['uid']);
  406. return JsonService::successful();
  407. } else
  408. return JsonService::fail('添加收货地址失败!');
  409. }
  410. }
  411. /**
  412. * 获取用户所有地址
  413. */
  414. public function user_address_list()
  415. {
  416. $list=UserAddress::getUserValidAddressList($this->userInfo['uid'], 'id,real_name,phone,province,city,district,detail,is_default');
  417. if ($list)
  418. return JsonService::successful('ok', $list);
  419. else
  420. return JsonService::successful('empty', []);
  421. }
  422. /**
  423. * 获取默认地址
  424. */
  425. public function user_default_address()
  426. {
  427. $defaultAddress = UserAddress::getUserDefaultAddress($this->userInfo['uid'], 'id,real_name,phone,province,city,district,detail,is_default');
  428. if ($defaultAddress)
  429. return JsonService::successful('ok', $defaultAddress);
  430. else
  431. return JsonService::successful('empty', []);
  432. }
  433. /**删除地址
  434. * @param string $addressId
  435. */
  436. public function remove_user_address($addressId = '')
  437. {
  438. if (!$addressId || !is_numeric($addressId)) return JsonService::fail('参数错误!');
  439. if (!UserAddress::be(['is_del' => 0, 'id' => $addressId, 'uid' => $this->userInfo['uid']]))
  440. return JsonService::fail('地址不存在!');
  441. if (UserAddress::edit(['is_del' => '1'], $addressId, 'id'))
  442. return JsonService::successful();
  443. else
  444. return JsonService::fail('删除地址失败!');
  445. }
  446. /**获取用户的商品订单列表
  447. * @param string $type
  448. * @param int $first
  449. * @param int $limit
  450. */
  451. public function get_user_order_list($type = 0, $first = 0, $limit = 8)
  452. {
  453. $list = StoreOrder::getUserOrderList($this->userInfo['uid'], $type, $first, $limit);
  454. return JsonService::successful($list);
  455. }
  456. /**
  457. * 用户订单数据
  458. */
  459. public function userOrderDate()
  460. {
  461. $data=StoreOrder::getOrderStatusNum($this->userInfo['uid']);
  462. return JsonService::successful($data);
  463. }
  464. /**删除订单
  465. * @param string $uni
  466. */
  467. public function user_remove_order($uni = '')
  468. {
  469. if (!$uni) return JsonService::fail('参数错误!');
  470. $res = StoreOrder::removeOrder($uni, $this->userInfo['uid']);
  471. if ($res)
  472. return JsonService::successful();
  473. else
  474. return JsonService::fail(StoreOrder::getErrorInfo());
  475. }
  476. /**提交商品退款
  477. * @param string $uni
  478. * @param string $text
  479. */
  480. public function apply_order_refund($uni = '')
  481. {
  482. if (!$uni) return JsonService::fail('参数错误!');
  483. $request = Request::instance();
  484. if (!$request->isPost()) return JsonService::fail('参数错误!');
  485. $data = UtilService::postMore([
  486. ['pics', []],
  487. ['refund_reason', ''],
  488. ['remarks', ''],
  489. ], $request);
  490. $res = StoreOrder::orderApplyRefund($uni, $this->userInfo['uid'],$data);
  491. if ($res)
  492. return JsonService::successful();
  493. else
  494. return JsonService::fail(StoreOrder::getErrorInfo());
  495. }
  496. /**用户确认收货
  497. * @param string $uni
  498. */
  499. public function user_take_order($uni = '')
  500. {
  501. if (!$uni) return JsonService::fail('参数错误!');
  502. $res = StoreOrder::takeOrder($uni, $this->userInfo['uid']);
  503. if ($res)
  504. return JsonService::successful();
  505. else
  506. return JsonService::fail(StoreOrder::getErrorInfo());
  507. }
  508. /**虚拟币充值
  509. * @param int $price
  510. * @param int $payType
  511. */
  512. public function user_wechat_recharge($price = 0,$payType = 0)
  513. {
  514. if (!$price || $price <= 0 || !is_numeric($price)) return JsonService::fail('参数错误');
  515. if (!isset($this->userInfo['uid']) || !$this->userInfo['uid']) return JsonService::fail('用户不存在');
  516. try {
  517. //充值记录
  518. $rechargeOrder = UserRecharge::addRecharge($this->userInfo['uid'], $price, $payType);
  519. if (!$rechargeOrder) return JsonService::fail('充值订单生成失败!');
  520. $orderId = $rechargeOrder['order_id'];
  521. $goldName = SystemConfigService::get("gold_name");
  522. switch ($payType) {
  523. case 'weixin':
  524. try {
  525. $jsConfig = UserRecharge::jsRechargePay($orderId);
  526. } catch (\Exception $e) {
  527. return JsonService::status('pay_error', $e->getMessage(), $rechargeOrder);
  528. }
  529. $rechargeOrder['jsConfig'] = $jsConfig;
  530. return JsonService::status('wechat_pay', '订单创建成功', $rechargeOrder);
  531. break;
  532. case 'yue':
  533. if (UserRecharge::yuePay($orderId, $this->userInfo))
  534. return JsonService::status('success', '余额支付成功', $rechargeOrder);
  535. else
  536. return JsonService::status('pay_error', StoreOrder::getErrorInfo());
  537. break;
  538. case 'zhifubao':
  539. $rechargeOrder['orderName'] = $goldName."充值";
  540. $rechargeOrder['orderId'] = $orderId;
  541. $rechargeOrder['pay_price'] = $price;
  542. return JsonService::status('zhifubao_pay','订单创建成功', base64_encode(json_encode($rechargeOrder)));
  543. break;
  544. }
  545. } catch (\Exception $e) {
  546. return JsonService::fail($e->getMessage());
  547. }
  548. }
  549. /**余额明细
  550. * @param int $index
  551. * @param int $first
  552. * @param int $limit
  553. */
  554. public function user_balance_list($index = 0,$first = 0, $limit = 8)
  555. {
  556. $model = UserBill::where('uid', $this->userInfo['uid'])->where('category', 'now_money')
  557. ->where('type','in', 'pay_goods,system_add,pay_product_refund,pay_product,recharge,system_sub,pay_sign_up')
  558. ->field('title,mark,pm,number,add_time')
  559. ->where('status', 1)->where('number','>',0);
  560. switch ($index){
  561. case 1:
  562. $model=$model->where('pm',0);
  563. break;
  564. case 2:
  565. $model=$model->where('pm',1);
  566. break;
  567. }
  568. $list=$model->order('add_time DESC')->page((int)$first, (int)$limit)->select();
  569. $list=count($list) >0 ? $list->toArray() : [];
  570. foreach ($list as &$v) {
  571. $v['add_time'] = date('Y/m/d H:i', $v['add_time']);
  572. }
  573. return JsonService::successful($list);
  574. }
  575. /**金币明细
  576. * @param int $index
  577. * @param int $first
  578. * @param int $limit
  579. */
  580. public function user_gold_num_list($index = 0,$first = 0, $limit = 8)
  581. {
  582. $model = UserBill::where('uid', $this->userInfo['uid'])->where('category', 'gold_num')
  583. ->field('title,mark,pm,number,add_time')
  584. ->where('status', 1)->where('number','>',0);
  585. switch ($index){
  586. case 1:
  587. $model=$model->where('pm',0);
  588. break;
  589. case 2:
  590. $model=$model->where('pm',1);
  591. break;
  592. }
  593. $list=$model->order('add_time DESC')->page((int)$first, (int)$limit)->select();
  594. $list=count($list) >0 ? $list->toArray() : [];
  595. foreach ($list as &$v) {
  596. $v['add_time'] = date('Y/m/d H:i', $v['add_time']);
  597. }
  598. return JsonService::successful($list);
  599. }
  600. /**
  601. * @param string $unique
  602. * @throws \think\db\exception\DataNotFoundException
  603. * @throws \think\db\exception\ModelNotFoundException
  604. * @throws \think\exception\DbException
  605. */
  606. public function user_comment_product($unique = '')
  607. {
  608. if (!$unique) return JsonService::fail('参数错误!');
  609. $cartInfo = StoreOrderCartInfo::where('unique', $unique)->find();
  610. $uid = $this->userInfo['uid'];
  611. if (!$cartInfo || $uid != $cartInfo['cart_info']['uid']) return JsonService::fail('评价产品不存在!');
  612. if (StoreProductReply::be(['oid' => $cartInfo['oid'], 'unique' => $unique]))
  613. return JsonService::fail('该产品已评价!');
  614. $group = UtilService::postMore([
  615. ['comment', ''], ['pics', []], ['product_score', 5], ['service_score', 5]
  616. ], Request::instance());
  617. $group['comment'] = htmlspecialchars(trim($group['comment']));
  618. if (sensitive_words_filter($group['comment'])) return JsonService::fail('请注意您的用词,谢谢!!');
  619. if ($group['product_score'] < 1) return JsonService::fail('请为产品评分');
  620. else if ($group['service_score'] < 1) return JsonService::fail('请为商家服务评分');
  621. $group = array_merge($group, [
  622. 'uid' => $uid,
  623. 'oid' => $cartInfo['oid'],
  624. 'unique' => $unique,
  625. 'product_id' => $cartInfo['product_id'],
  626. 'reply_type' => 'product'
  627. ]);
  628. StoreProductReply::beginTrans();
  629. $res = StoreProductReply::reply($group, 'product');
  630. if (!$res) {
  631. StoreProductReply::rollbackTrans();
  632. return JsonService::fail('评价失败!');
  633. }
  634. try {
  635. HookService::listen('store_product_order_reply', $group, $cartInfo, false, StoreProductBehavior::class);
  636. } catch (\Exception $e) {
  637. StoreProductReply::rollbackTrans();
  638. return JsonService::fail($e->getMessage());
  639. }
  640. StoreProductReply::commitTrans();
  641. return JsonService::successful();
  642. }
  643. public function get_spread_list($first = 0, $limit = 20)
  644. {
  645. $list = User::where('spread_uid', $this->userInfo['uid'])->field('uid,nickname,avatar,add_time')->limit($first, $limit)->order('add_time DESC')->select()->toArray();
  646. foreach ($list as $k => $user) {
  647. $list[$k]['add_time'] = date('Y/m/d', $user['add_time']);
  648. }
  649. return JsonService::successful($list);
  650. }
  651. public function product_reply_list($productId = '', $first = 0, $limit = 8, $filter = 'all')
  652. {
  653. if (!$productId || !is_numeric($productId)) return JsonService::fail('参数错误!');
  654. $list = StoreProductReply::getProductReplyList($productId, $filter, $first, $limit);
  655. return JsonService::successful($list);
  656. }
  657. public function product_attr_detail($productId = '')
  658. {
  659. if (!$productId || !is_numeric($productId)) return JsonService::fail('参数错误!');
  660. list($productAttr, $productValue) = StoreProductAttr::getProductAttrDetail($productId);
  661. return JsonService::successful(compact('productAttr', 'productValue'));
  662. }
  663. public function get_notice_list($page = 0, $limit = 8)
  664. {
  665. $list = UserNotice::getNoticeList($this->userInfo['uid'], $page, $limit);
  666. return JsonService::successful($list);
  667. }
  668. public function see_notice($nid)
  669. {
  670. UserNotice::seeNotice($this->userInfo['uid'], $nid);
  671. return JsonService::successful();
  672. }
  673. public function refresh_msn(Request $request)
  674. {
  675. $params = $request->post();
  676. $remind_where = "mer_id = " . $params["mer_id"] . " AND uid = " . $params["uid"] . " AND to_uid = " . $params["to_uid"] . " AND type = 0 AND remind = 0";
  677. $remind_list = StoreServiceLog::where($remind_where)->order("add_time asc")->select();
  678. foreach ($remind_list as $key => $value) {
  679. if (time() - $value["add_time"] > 3) {
  680. StoreServiceLog::edit(array("remind" => 1), $value["id"]);
  681. $now_user = StoreService::field("uid,nickname")->where(array("uid" => $params["uid"]))->find();
  682. if (!$now_user) $now_user = User::field("uid,nickname")->where(array("uid" => $params["uid"]))->find();
  683. if ($params["to_uid"]) {
  684. $userInfo = WechatUser::where('uid', $params["to_uid"])->field(['openid', 'subscribe'])->find();
  685. if($userInfo && $userInfo['openid'] && $userInfo['subscribe']) {
  686. $head = '客服提醒';
  687. $description = '您有新的消息,请注意查收!';
  688. $url = SystemConfigService::get('site_url').'/wap/service/service_ing/to_uid/'.$this->uid.'/mer_id/0';
  689. $message = WechatService::newsMessage($head, $description, $url, $this->userInfo['avatar']);
  690. try {
  691. WechatService::staffService()->message($message)->to($userInfo['openid'])->send();
  692. } catch (\Exception $e) {
  693. \think\Log::error($userInfo['nickname'] . '发送失败' . $e->getMessage());
  694. }
  695. }
  696. }
  697. }
  698. }
  699. $where = "mer_id = " . $params["mer_id"] . " AND uid = " . $params["to_uid"] . " AND to_uid = " . $params["uid"] . " AND type = 0";
  700. $list = StoreServiceLog::where($where)->order("add_time asc")->select()->toArray();
  701. $ids = [];
  702. foreach ($list as $key => $value) {
  703. //设置发送人与接收人区别
  704. if ($value["uid"] == $params["uid"])
  705. $list[$key]['my'] = "my";
  706. else
  707. $list[$key]['my'] = "to";
  708. array_push($ids, $value["id"]);
  709. }
  710. //设置这些消息为已读
  711. StoreServiceLog::where(array("id" => array("in", $ids)))->update(array("type" => 1, "remind" => 1));
  712. return JsonService::successful($list);
  713. }
  714. public function add_msn(Request $request)
  715. {
  716. $params = $request->post();
  717. if ($params["type"] == "html")
  718. $data["msn"] = htmlspecialchars_decode($params["msn"]);
  719. else
  720. $data["msn"] = $params["msn"];
  721. $data["uid"] = $params["uid"];
  722. $data["to_uid"] = $params["to_uid"];
  723. $data["mer_id"] = $params["mer_id"] > 0 ? $params["mer_id"] : 0;
  724. $data["add_time"] = time();
  725. StoreServiceLog::set($data);
  726. return JsonService::successful();
  727. }
  728. public function get_msn(Request $request)
  729. {
  730. $params = $request->post();
  731. $size = 10;
  732. $page = $params["page"] >= 0 ? $params["page"] : 1;
  733. $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"] . ")";
  734. $list = StoreServiceLog::where($where)->limit(($page - 1) * $size, $size)->order("add_time desc")->select()->toArray();
  735. foreach ($list as $key => $value) {
  736. //设置发送人与接收人区别
  737. if ($value["uid"] == $params["uid"])
  738. $list[$key]['my'] = "my";
  739. else
  740. $list[$key]['my'] = "to";
  741. //设置这些消息为已读
  742. if ($value["uid"] == $params["to_uid"] && $value["to_uid"] == $params["uid"]) StoreServiceLog::edit(array("type" => 1, "remind" => 1), $value["id"]);
  743. }
  744. $list = array_reverse($list);
  745. return JsonService::successful($list);
  746. }
  747. public function refresh_msn_new(Request $request)
  748. {
  749. $params = $request->post();
  750. $now_user = User::getUserInfo($this->userInfo['uid']);
  751. if ($params["last_time"] > 0)
  752. $where = "(uid = " . $now_user["uid"] . " OR to_uid = " . $now_user["uid"] . ") AND add_time>" . $params["last_time"];
  753. else
  754. $where = "uid = " . $now_user["uid"] . " OR to_uid = " . $now_user["uid"];
  755. $msn_list = StoreServiceLog::where($where)->order("add_time desc")->select()->toArray();
  756. $info_array = $list = [];
  757. foreach ($msn_list as $key => $value) {
  758. $to_uid = $value["uid"] == $now_user["uid"] ? $value["to_uid"] : $value["uid"];
  759. if (!in_array(["to_uid" => $to_uid, "mer_id" => $value["mer_id"]], $info_array)) {
  760. $info_array[count($info_array)] = ["to_uid" => $to_uid, "mer_id" => $value["mer_id"]];
  761. $to_user = StoreService::field("uid,nickname,avatar")->where(array("uid" => $to_uid))->find();
  762. if (!$to_user) $to_user = User::field("uid,nickname,avatar")->where(array("uid" => $to_uid))->find();
  763. $to_user["mer_id"] = $value["mer_id"];
  764. $to_user["mer_name"] = '';
  765. $value["to_info"] = $to_user;
  766. $value["count"] = StoreServiceLog::where(array("mer_id" => $value["mer_id"], "uid" => $to_uid, "to_uid" => $now_user["uid"], "type" => 0))->count();
  767. $list[count($list)] = $value;
  768. }
  769. }
  770. return JsonService::successful($list);
  771. }
  772. public function get_user_brokerage_list($uid, $first = 0, $limit = 8)
  773. {
  774. if (!$uid)
  775. return $this->failed('用户不存在');
  776. $list = UserBill::field('A.mark,A.add_time,A.number,A.pm')->alias('A')->limit($first, $limit)
  777. ->where('A.category', 'now_money')->where('A.type', 'brokerage')
  778. ->where('A.uid', $this->userInfo['uid'])
  779. ->join('__STORE_ORDER__ B', 'A.link_id = B.id AND B.uid = ' . $uid)->select()->toArray();
  780. return JsonService::successful($list);
  781. }
  782. public function user_extract()
  783. {
  784. if (UserExtract::userExtract($this->userInfo, UtilService::postMore([
  785. ['type', '', '', 'extract_type'], 'real_name', 'alipay_code', 'bank_code', 'bank_address', ['price', '', '', 'extract_price']
  786. ])))
  787. return JsonService::successful('申请提现成功!');
  788. else
  789. return JsonService::fail(Extract::getErrorInfo());
  790. }
  791. public function clear_cache($uni = '')
  792. {
  793. if ($uni) CacheService::clear();
  794. }
  795. /**
  796. * 获取今天正在拼团的人的头像和名称
  797. * @return \think\response\Json
  798. */
  799. public function get_pink_second_one()
  800. {
  801. $addTime = mt_rand(time() - 30000, time());
  802. $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();
  803. return JsonService::successful($storePink);
  804. }
  805. public function order_details($uni = '')
  806. {
  807. if (!$uni) return JsonService::fail('参数错误!');
  808. $order = StoreOrder::getUserOrderDetail($this->userInfo['uid'], $uni);
  809. if (!$order) return JsonService::fail('订单不存在!');
  810. $order = StoreOrder::tidyOrder($order, true);
  811. $res = array();
  812. foreach ($order['cartInfo'] as $v) {
  813. if ($v['combination_id']) return JsonService::fail('拼团产品不能再来一单,请在拼团产品内自行下单!');
  814. 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);
  815. }
  816. $cateId = [];
  817. foreach ($res as $v) {
  818. if (!$v) return JsonService::fail('再来一单失败,请重新下单!');
  819. $cateId[] = $v['id'];
  820. }
  821. return JsonService::successful('ok', implode(',', $cateId));
  822. }
  823. }