AuthApi.php 39 KB

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