UserController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\v1\user;
  12. use app\model\user\CertType;
  13. use app\model\member\MemberCheck;
  14. use app\Request;
  15. use app\services\product\product\StoreProductLogServices;
  16. use app\services\user\UserCancelServices;
  17. use app\services\user\UserServices;
  18. use app\services\wechat\WechatUserServices;
  19. //use crmeb\repositories\OrderRepository;
  20. /**
  21. * 用户类
  22. * Class UserController
  23. * @package app\api\controller\store
  24. */
  25. class UserController
  26. {
  27. protected $services = NUll;
  28. /**
  29. * UserController constructor.
  30. * @param UserServices $services
  31. */
  32. public function __construct(UserServices $services)
  33. {
  34. $this->services = $services;
  35. }
  36. /**
  37. * 获取用户信息
  38. * @param Request $request
  39. * @return mixed
  40. */
  41. public function userInfo(Request $request)
  42. {
  43. $info = $request->user()->toArray();
  44. return app('json')->success($this->services->userInfo($info));
  45. }
  46. /**
  47. * 用户资金统计
  48. * @param Request $request
  49. * @return mixed
  50. */
  51. public function balance(Request $request)
  52. {
  53. $uid = (int)$request->uid();
  54. return app('json')->success($this->services->balance($uid));
  55. }
  56. /**
  57. * 个人中心
  58. * @param Request $request
  59. * @return mixed
  60. */
  61. public function user(Request $request)
  62. {
  63. $user = $request->user()->toArray();
  64. return app('json')->success($this->services->personalHome($user, $request->tokenData()));
  65. }
  66. /**
  67. * 获取活动状态
  68. * @return mixed
  69. */
  70. public function activity()
  71. {
  72. return app('json')->success($this->services->activity());
  73. }
  74. /**
  75. * 用户修改信息
  76. * @param Request $request
  77. * @return mixed
  78. */
  79. public function edit(Request $request)
  80. {
  81. list($avatar, $nickname) = $request->postMore([
  82. ['avatar', ''],
  83. ['nickname', ''],
  84. ], true);
  85. if (!$avatar && $nickname == '') {
  86. return app('json')->fail(410134);
  87. }
  88. $uid = (int)$request->uid();
  89. if ($this->services->eidtNickname($uid, ['avatar' => $avatar, 'nickname' => $nickname])) {
  90. return app('json')->success(100014);
  91. }
  92. return app('json')->fail(100015);
  93. }
  94. /**
  95. * 推广人排行
  96. * @param Request $request
  97. * @return mixed
  98. * @throws \think\db\exception\DataNotFoundException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. */
  101. public function rank(Request $request)
  102. {
  103. $data = $request->getMore([
  104. ['page', ''],
  105. ['limit', ''],
  106. ['type', '']
  107. ]);
  108. return app('json')->success($this->services->getRankList($data));
  109. }
  110. /**
  111. * 添加访问记录
  112. * @param Request $request
  113. * @return mixed
  114. */
  115. public function set_visit(Request $request)
  116. {
  117. $data = $request->postMore([
  118. ['url', ''],
  119. ['stay_time', 0]
  120. ]);
  121. if ($data['url'] == '') return app('json')->fail(100100);
  122. $data['uid'] = (int)$request->uid();
  123. $data['ip'] = $request->ip();
  124. if ($this->services->setVisit($data)) {
  125. return app('json')->success(100021);
  126. } else {
  127. return app('json')->fail(100022);
  128. }
  129. }
  130. /**
  131. * 静默绑定推广人
  132. * @param Request $request
  133. * @return mixed
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public function spread(Request $request)
  139. {
  140. [$spreadUid, $code] = $request->postMore([
  141. ['puid', 0],
  142. ['code', 0]
  143. ], true);
  144. $uid = (int)$request->uid();
  145. $res = $this->services->spread($uid, (int)$spreadUid, $code);
  146. return app('json')->success($res);
  147. }
  148. /**
  149. * 推荐用户
  150. * @param Request $request
  151. * @return mixed
  152. */
  153. public function spread_people(Request $request)
  154. {
  155. $spreadInfo = $request->postMore([
  156. ['grade', 0],
  157. ['keyword', ''],
  158. ['sort', ''],
  159. ]);
  160. if (!in_array($spreadInfo['grade'], [0, 1])) {
  161. return app('json')->fail(100100);
  162. }
  163. $uid = $request->uid();
  164. return app('json')->success($this->services->getUserSpreadGrade($uid, $spreadInfo['grade'], $spreadInfo['sort'], $spreadInfo['keyword']));
  165. }
  166. /**
  167. * 是否关注
  168. * @param Request $request
  169. * @return mixed
  170. */
  171. public function subscribe(Request $request)
  172. {
  173. if ($request->uid()) {
  174. /** @var WechatUserServices $wechatUserService */
  175. $wechatUserService = app()->make(WechatUserServices::class);
  176. $subscribe = (bool)$wechatUserService->value(['uid' => $request->uid()], 'subscribe');
  177. return app('json')->success(['subscribe' => $subscribe]);
  178. } else {
  179. return app('json')->success(['subscribe' => true]);
  180. }
  181. }
  182. /**
  183. * 用户注销
  184. * @param Request $request
  185. * @return mixed
  186. */
  187. public function SetUserCancel(Request $request)
  188. {
  189. /** @var UserCancelServices $userCancelServices */
  190. $userCancelServices = app()->make(UserCancelServices::class);
  191. $userCancelServices->SetUserCancel($request->uid());
  192. return app('json')->success(410135);
  193. }
  194. /**
  195. * 商品浏览记录
  196. * @param Request $request
  197. * @param StoreProductLogServices $services
  198. * @return mixed
  199. * @throws \think\db\exception\DataNotFoundException
  200. * @throws \think\db\exception\DbException
  201. * @throws \think\db\exception\ModelNotFoundException
  202. */
  203. public function visitList(Request $request, StoreProductLogServices $services)
  204. {
  205. $where['uid'] = (int)$request->uid();
  206. $where['type'] = 'visit';
  207. $result = $services->getList($where, 'product_id', 'id,product_id,max(add_time) as add_time');
  208. $time_data = [];
  209. if ($result['list']) {
  210. foreach ($result['list'] as $key => &$item) {
  211. $add_time = strtotime($item['add_time']);
  212. if (date('Y') == date('Y', $add_time)) {//今年
  213. $item['time_key'] = date('m-d', $add_time);
  214. } else {
  215. $item['time_key'] = date('Y-m-d', $add_time);
  216. }
  217. }
  218. $time_data = array_merge(array_unique(array_column($result['list'], 'time_key')));
  219. }
  220. $result['time'] = $time_data;
  221. return app('json')->success($result);
  222. }
  223. /**
  224. * 商品浏览记录删除
  225. * @param Request $request
  226. * @param StoreProductLogServices $services
  227. * @return mixed
  228. * @throws \think\db\exception\DataNotFoundException
  229. * @throws \think\db\exception\DbException
  230. * @throws \think\db\exception\ModelNotFoundException
  231. */
  232. public function visitDelete(Request $request, StoreProductLogServices $services)
  233. {
  234. $uid = (int)$request->uid();
  235. [$ids] = $request->postMore([
  236. ['ids', []],
  237. ], true);
  238. if ($ids) {
  239. $where = ['uid' => $uid, 'product_id' => $ids];
  240. $services->delete($where);
  241. }
  242. return app('json')->success('删除成功');
  243. }
  244. public function jobs(Request $request)
  245. {
  246. if ($request->uid() == 2){
  247. return app('json')->fail('114514');
  248. }
  249. return app('json')->success('ok', ['data' => CertType::select()]);
  250. }
  251. public function applyMemberCheck($type, Request $request)
  252. {
  253. $uid = $request->uid();
  254. if ($uid == 2){
  255. return app('json')->fail('114514');
  256. }
  257. $param = $request->postMore([
  258. ['name', ''],
  259. ['id', ''],
  260. ['phone', ''],
  261. ['location', ''],
  262. ['address', ''],
  263. ['occupation', ''],
  264. ['careerYears', ''],
  265. ['sponsor', ''],
  266. ['img', ''],
  267. ['grade', ''],
  268. ['pay_type', 'weixin'],
  269. ['from', 'weixin'],
  270. ], true);
  271. $payType = $param['pay_type'];
  272. $from = $param['from'];
  273. $param['type'] = $type;
  274. $order = MemberCheck::createOrder($param, $uid, $payType, $from);
  275. if ($order === false) return app('json')->fail(MemberCheck::getErrorInfo('订单生成失败'));
  276. $orderId = $order['order_id'];
  277. $info = ['order_id' => $orderId];
  278. if ($orderId) {
  279. switch ($payType) {
  280. case "weixin":
  281. $orderInfo = MemberCheck::where('order_id', $orderId)->find();
  282. if (!$orderInfo || !isset($orderInfo['paid'])) return app('json')->fail('支付订单不存在!');
  283. $orderInfo = $orderInfo->toArray();
  284. if ($orderInfo['paid']) return app('json')->fail('订单已支付!');
  285. //支付金额为0
  286. if (bcsub((float)$orderInfo['pay_money'], 0, 2) <= 0) {
  287. //创建订单jspay支付
  288. $payPriceStatus = MemberCheck::jsPayPrice($orderId, $uid);
  289. if ($payPriceStatus)//0元支付成功
  290. return app('json')->status('success', '微信支付成功');
  291. else
  292. return app('json')->status('pay_error', MemberCheck::getErrorInfo());
  293. } else {
  294. try {
  295. if ($from == 'routine') {
  296. $jsConfig = OrderRepository::jsMemberPay($orderId); //创建订单jspay
  297. } else if ($from == 'weixinh5') {
  298. $jsConfig = OrderRepository::h5MemberPay($orderId);
  299. } else {
  300. $jsConfig = OrderRepository::wxMemberPay($orderId);
  301. }
  302. } catch (\Exception $e) {
  303. return app('json')->status('pay_error', $e->getMessage());
  304. }
  305. $info['jsConfig'] = $jsConfig;
  306. if ($from == 'weixinh5') {
  307. return app('json')->status('wechat_h5_pay', '订单创建成功', $info);
  308. } else {
  309. return app('json')->status('wechat_pay', '订单创建成功', $info);
  310. }
  311. }
  312. case 'yue':
  313. if (MemberCheck::yuePay($orderId, $request->uid()))
  314. return app('json')->status('success', '余额支付成功', $info);
  315. else {
  316. $errorinfo = MemberCheck::getErrorInfo();
  317. if (is_array($errorinfo))
  318. return app('json')->status($errorinfo['status'], $errorinfo['msg'], $info);
  319. else
  320. return app('json')->status('pay_error', $errorinfo);
  321. }
  322. break;
  323. }
  324. } else return app('json')->fail(MemberCheck::getErrorInfo('订单生成失败!'));
  325. }
  326. }