MemberService.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Helpers\Jwt;
  13. use App\Models\ActionLogModel;
  14. use App\Models\MemberModel;
  15. use App\Models\OrderModel;
  16. use App\Models\VideoCoursesModel;
  17. use App\Models\VideoOrderModel;
  18. use App\Models\VipModel;
  19. use App\Services\BaseService;
  20. use App\Services\ConfigService;
  21. use App\Services\MpService;
  22. use App\Services\PaymentService;
  23. use App\Services\RedisService;
  24. use App\Services\SmsService;
  25. use phpQrcode\QRcode;
  26. use Illuminate\Support\Facades\DB;
  27. /**
  28. * 会员管理-服务类
  29. * @author laravel开发员
  30. * @since 2020/11/11
  31. * Class MemberService
  32. * @package App\Services\Api
  33. */
  34. class MemberService extends BaseService
  35. {
  36. // 静态对象
  37. protected static $instance = null;
  38. /**
  39. * 构造函数
  40. * @author laravel开发员
  41. * @since 2020/11/11
  42. * MemberService constructor.
  43. */
  44. public function __construct()
  45. {
  46. $this->model = new MemberModel();
  47. }
  48. /**
  49. * 静态入口
  50. * @return MemberService|static|null
  51. */
  52. public static function make()
  53. {
  54. if (!self::$instance) {
  55. self::$instance = new static();
  56. }
  57. return self::$instance;
  58. }
  59. /**
  60. * 账号登录
  61. * @param $params
  62. * @return array|false
  63. */
  64. public function login($params)
  65. {
  66. // 账号登录
  67. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  68. $password = isset($params['password']) ? $params['password'] : '';
  69. if (empty($params) || empty($mobile) || empty($password)) {
  70. $this->error = 1041;
  71. return false;
  72. }
  73. // 验证是否注册,没有则注册
  74. $data = $this->model->where(['mobile' => $mobile, 'mark' => 1])->select(['id', 'mobile', 'user_type', 'password', 'nickname', 'code', 'status'])->first();
  75. $data = $data ? $data->toArray() : [];
  76. $userId = isset($data['id']) ? $data['id'] : 0;
  77. $status = isset($data['status']) ? $data['status'] : 0;
  78. $userPassword = isset($data['password']) ? $data['password'] : '';
  79. if (empty($data) || $userId <= 0) {
  80. $this->error = 2014;
  81. return false;
  82. }
  83. if ($status != 1) {
  84. $this->error = 2015;
  85. return false;
  86. }
  87. // 验证登录密码
  88. if (empty($userPassword) || $userPassword != get_password($password)) {
  89. $this->error = 2017;
  90. return false;
  91. }
  92. // 更新
  93. if (!RedisService::get("caches:members:login_{$userId}")) {
  94. $system = isset($params['system']) ? $params['system'] : [];
  95. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  96. $appSources = isset($system['app_sources']) && $system['app_sources'] ? $system['app_sources'] : 'ios';
  97. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  98. $version = isset($system['app_version']) ? $system['app_version'] : '';
  99. $updateData = [
  100. 'update_time' => time(),
  101. 'login_ip' => get_client_ip(),
  102. 'login_time' => time(),
  103. 'device_code' => $uuid,
  104. 'login_count' => DB::raw("login_count+1"),
  105. 'app_version' => $version,
  106. 'device' => $appSources == 'ios' ? 1 : 2,
  107. ];
  108. $this->model->where(['id' => $userId])->update($updateData);
  109. RedisService::set("caches:members:login_{$userId}", $updateData, rand(300, 600));
  110. }
  111. // 获取登录授权token
  112. $jwt = new Jwt('jwt_jd_app');
  113. $token = $jwt->getToken($userId);
  114. // 结果返回
  115. $result = [
  116. 'access_token' => $token,
  117. 'info' => ['uid' => $userId, 'nickname' => $data['nickname']],
  118. ];
  119. // 用户缓存信息
  120. $this->error = 2019;
  121. $data['token'] = $token;
  122. unset($data['password']);
  123. unset($data['mobile']);
  124. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  125. return $result;
  126. }
  127. /**
  128. * 授权登录
  129. * @param $code
  130. * @param array $params
  131. * @return array|false
  132. */
  133. public function mpAuth($code, $params = [])
  134. {
  135. // 账号登录
  136. if (empty($code)) {
  137. $this->error = 1041;
  138. return false;
  139. }
  140. // 获取授权用户信息
  141. $phone = '';
  142. $pcode = isset($params['pcode']) ? $params['pcode'] : '';
  143. if ($pcode) {
  144. $phoneData = MpService::make()->getPhoneNumber($pcode);
  145. $phoneData = isset($phoneData['phone_info']) ? $phoneData['phone_info'] : [];
  146. $phone = isset($phoneData['phoneNumber']) ? $phoneData['phoneNumber'] : '';
  147. }
  148. if (empty($phone)) {
  149. $this->error = 2014;
  150. return false;
  151. }
  152. $userInfo = MpService::make()->getUserInfo($code);
  153. $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
  154. if (empty($userInfo)) {
  155. $this->error = MpService::make()->getError();
  156. return false;
  157. }
  158. if (empty($openid)) {
  159. $this->error = 1042;
  160. return false;
  161. }
  162. // 验证是否注册,没有则注册
  163. $where = ['mobile' => $phone];
  164. $data = $this->model->where($where)
  165. ->select(['id', 'openid', 'mobile', 'user_type', 'nickname', 'entry_type', 'province', 'city', 'district', 'need_paper', 'code', 'status','mark'])
  166. ->first();
  167. $data = $data ? $data->toArray() : [];
  168. $userId = isset($data['id']) ? $data['id'] : 0;
  169. $status = isset($data['status']) ? $data['status'] : 0;
  170. $mark = isset($data['mark']) ? $data['mark'] : 0;
  171. $entryType = isset($data['entry_type']) ? $data['entry_type'] : 0;
  172. $needPaper = isset($data['need_paper']) ? $data['need_paper'] : 0;
  173. $city = isset($data['city']) ? $data['city'] : '';
  174. $district = isset($data['district']) ? $data['district'] : '';
  175. if ($data && $userId && $status != 1 && $mark==1) {
  176. $this->error = 2011;
  177. return false;
  178. }
  179. $system = isset($params['system']) ? $params['system'] : [];
  180. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  181. $appSources = isset($system['app_sources']) && $system['app_sources'] ? $system['app_sources'] : 'ios';
  182. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  183. $version = isset($system['app_version']) ? $system['app_version'] : '';
  184. if (empty($data)) {
  185. $userId = $this->model->max('id') + 1;
  186. $data = [
  187. 'nickname' => $phone ? '用户' . substr($phone, -6, 6) : '用户' . $userId,
  188. 'openid' => $openid,
  189. 'mobile' => $phone,
  190. 'code' => get_random_code(9, 'S', $userId),
  191. 'password' => $phone ? get_password(substr($phone, -6, 6)) : get_password('123456'),
  192. 'login_ip' => get_client_ip(),
  193. 'create_time' => time(),
  194. 'login_time' => time(),
  195. 'login_count' => DB::raw("login_count+1"),
  196. 'app_version' => $version,
  197. 'app_uuid' => $uuid,
  198. 'device' => $appSources == 'ios' ? 1 : 2,
  199. ];
  200. if (!$userId = $this->model->insertGetId($data)) {
  201. $this->error = 2012;
  202. return false;
  203. }
  204. } // 更新登录信息
  205. else if ($mark==0 || !RedisService::get("caches:members:login_{$userId}")) {
  206. $updateData = [
  207. 'login_ip' => get_client_ip(),
  208. 'login_time' => time(),
  209. 'app_uuid' => $uuid,
  210. 'login_count' => DB::raw("login_count+1"),
  211. 'app_version' => $version,
  212. 'device' => $appSources == 'ios' ? 1 : 2,
  213. 'mark' => 1,
  214. ];
  215. if ($openid) {
  216. $updateData['openid'] = $openid;
  217. }
  218. if($mark==0){
  219. $data['mobile'] = $phone;
  220. $data['create_time'] = time();
  221. $data['openid'] = $openid;
  222. $data['prvoince'] = '';
  223. $data['city'] = '';
  224. $data['district'] = '';
  225. $city = '';
  226. $district = '';
  227. }
  228. $this->model->where(['id' => $userId])->update($updateData);
  229. RedisService::set("caches:members:login_{$userId}", $updateData, rand(180, 300));
  230. }
  231. // 获取登录授权token
  232. $jwt = new Jwt('jwt_jd_app');
  233. $token = $jwt->getToken($userId);
  234. // 结果返回
  235. $setEntry = $entryType && $needPaper ? 1 : 0;
  236. $result = [
  237. 'access_token' => $token,
  238. 'info' => ['uid' => $userId, 'openid' => $openid, 'set_entry' => $setEntry, 'has_area' => $city && $district ? 1 : 0, 'nickname' => $data['nickname']],
  239. ];
  240. // 用户缓存信息
  241. $this->error = 2013;
  242. $data['token'] = $token;
  243. unset($data['mobile']);
  244. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  245. return $result;
  246. }
  247. /**
  248. * 重置密码
  249. * @param $params
  250. * @return array|false
  251. */
  252. public function forget($params)
  253. {
  254. // 账号登录
  255. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  256. $password = isset($params['password']) ? trim($params['password']) : '';
  257. if (empty($params) || empty($mobile) || empty($password)) {
  258. $this->error = 1041;
  259. return false;
  260. }
  261. // 验证码验证
  262. $smsCode = isset($params['sms_code']) ? trim($params['sms_code']) : '';
  263. if (!SmsService::make()->check($mobile, $smsCode, 'reset_password')) {
  264. $this->error = SmsService::make()->getError();
  265. return false;
  266. }
  267. // 验证是否注册
  268. if (!$userId = $this->model->where(['mobile' => $mobile, 'mark' => 1])->value('id')) {
  269. $this->error = 1038;
  270. return false;
  271. }
  272. if (!$this->model->where(['id' => $userId])->update(['password' => get_password($password), 'update_time' => time()])) {
  273. $this->error = 2030;
  274. return false;
  275. }
  276. // 操作日志
  277. ActionLogModel::setRecord($userId, ['type' => 2, 'title' => '重置密码', 'content' => '重置登录密码', 'module' => 'member']);
  278. ActionLogModel::record();
  279. $this->error = 2031;
  280. return true;
  281. }
  282. /**
  283. * 获取资料详情
  284. * @param $where
  285. * @param array $field
  286. */
  287. public function getInfo($where, array $field = [], $refresh = true)
  288. {
  289. $cacheKey = "caches:members:info_" . (!is_array($where) ? $where . '_' : '') . md5(json_encode($where) . json_encode($field));
  290. $info = RedisService::get($cacheKey);
  291. if ($info && !$refresh) {
  292. return $info;
  293. }
  294. $defaultField = ['id', 'user_type', 'realname', 'mobile', 'nickname', 'is_zg_vip', 'zg_vip_expired', 'is_zsb_vip', 'is_zsb_vip', 'zsb_vip_expired', 'is_video_vip', 'video_vip_expired', 'city', 'district', 'balance', 'code', 'openid', 'status', 'avatar'];
  295. $field = $field ? $field : $defaultField;
  296. if (is_array($where)) {
  297. $info = $this->model->where(['mark' => 1])->where($where)->select($field)->first();
  298. } else {
  299. $info = $this->model->where(['mark' => 1])->where(['id' => (int)$where])->select($field)->first();
  300. }
  301. $info = $info ? $info->toArray() : [];
  302. if ($info) {
  303. if (isset($info['avatar'])) {
  304. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : '';
  305. }
  306. if (isset($info['mobile'])) {
  307. $info['mobile_text'] = $info['mobile'] ? format_mobile($info['mobile']) : '';
  308. }
  309. if (isset($info['create_time'])) {
  310. $info['create_at'] = datetime(strtotime($info['create_time']));
  311. }
  312. $isVip = 0;
  313. $vipExpired = '';
  314. $isZgVip = isset($info['is_zg_vip']) ? $info['is_zg_vip'] : 0;
  315. $zgVipExpired = isset($info['zg_vip_expired']) && !empty($info['zg_vip_expired']) ? $info['zg_vip_expired'] : '';
  316. if ($isZgVip == 1 && $zgVipExpired && $zgVipExpired > date('Y-m-d H:i:s')) {
  317. $isVip = 1;
  318. $isZgVip = 1;
  319. $vipExpired = $zgVipExpired;
  320. } else {
  321. $isZgVip = 0;
  322. }
  323. $isZsbVip = isset($info['is_zsb_vip']) ? $info['is_zsb_vip'] : 0;
  324. $zsbVipExpired = isset($info['zsb_vip_expired']) && !empty($info['zsb_vip_expired']) ? $info['zsb_vip_expired'] : '';
  325. if ($isZsbVip == 1 && $zsbVipExpired && $zsbVipExpired > date('Y-m-d H:i:s')) {
  326. $isVip = 1;
  327. $vipExpired = $zsbVipExpired > $vipExpired ? $zsbVipExpired : $vipExpired;
  328. } else {
  329. $isZsbVip = 0;
  330. }
  331. $isVideoVip = isset($info['is_video_vip']) ? $info['is_video_vip'] : 0;
  332. $videoVipExpired = isset($info['video_vip_expired']) && !empty($info['video_vip_expired']) ? $info['video_vip_expired'] : '';
  333. if ($isVideoVip == 1 && $videoVipExpired && $videoVipExpired > date('Y-m-d H:i:s')) {
  334. $isVideoVip = 1;
  335. } else {
  336. $isVideoVip = 0;
  337. }
  338. $info['is_vip'] = $isVip;
  339. $info['is_zg_vip'] = $isZgVip;
  340. $info['is_zsb_vip'] = $isZsbVip;
  341. $info['vip_expired'] = $vipExpired;
  342. $info['zg_vip_expired'] = $zgVipExpired;
  343. $info['zg_vip_day'] = max(0, intval((strtotime($zgVipExpired) - time()) / 86400));
  344. $info['zsb_vip_expired'] = $zsbVipExpired;
  345. $info['zsb_vip_day'] = max(0, intval((strtotime($zsbVipExpired) - time()) / 86400));
  346. $info['is_video_vip'] = $isVideoVip;
  347. $info['video_vip_expired'] = $videoVipExpired;
  348. $info['video_vip_day'] = max(0, intval((strtotime($videoVipExpired) - time()) / 86400));
  349. RedisService::set($cacheKey, $info, rand(5, 10));
  350. }
  351. return $info;
  352. }
  353. /**
  354. * 生成普通参数二维码
  355. * @param $str 参数
  356. * @param bool $refresh 是否重新生成
  357. * @return bool
  358. */
  359. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  360. {
  361. $basePath = base_path() . '/public';
  362. $qrFile = '/images/qrcode/';
  363. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  364. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  365. }
  366. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  367. $qrFile = $qrFile . "C_{$key}.png";
  368. $cacheKey = "caches:qrcodes:member_" . $key;
  369. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  370. return $qrFile;
  371. }
  372. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  373. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  374. return false;
  375. }
  376. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  377. return $qrFile;
  378. }
  379. /**
  380. * 修改头像
  381. * @param $userId
  382. * @param $avatar
  383. * @return mixed
  384. */
  385. public function saveAvatar($userId, $avatar)
  386. {
  387. $oldAvatar = $this->model->where(['id' => $userId])->value('avatar');
  388. if ($this->model->where(['id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  389. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  390. @unlink(ATTACHMENT_PATH . $oldAvatar);
  391. }
  392. return true;
  393. }
  394. return false;
  395. }
  396. /**
  397. * 修改账号信息
  398. * @param $userId
  399. * @param $params
  400. * @return bool
  401. */
  402. public function modify($userId, $params)
  403. {
  404. $cacheLockKey = "caches:members:modify_{$userId}";
  405. if (RedisService::get($cacheLockKey)) {
  406. $this->error = 1034;
  407. return false;
  408. }
  409. // 用户验证
  410. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  411. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  412. ->select(['id', 'password', 'status'])
  413. ->first();
  414. $userPassword = isset($info['password']) ? $info['password'] : '';
  415. if (!$info || $info['status'] != 1) {
  416. $this->error = 1029;
  417. RedisService::clear($cacheLockKey);
  418. return false;
  419. }
  420. // 密码校验
  421. $data = ['update_time' => time()];
  422. // 修改数据
  423. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  424. if (isset($params['nickname']) && $nickname) {
  425. $data['nickname'] = $nickname;
  426. }
  427. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  428. if (isset($params['mobile']) && $mobile) {
  429. $data['mobile'] = $mobile;
  430. }
  431. $password = isset($params['password']) ? $params['password'] : '';
  432. $newPassword = isset($params['new_password']) ? $params['new_password'] : '';
  433. if (isset($params['password']) && $password) {
  434. if ($userPassword != get_password($password)) {
  435. $this->error = 1038;
  436. RedisService::clear($cacheLockKey);
  437. return false;
  438. }
  439. if (empty($newPassword)) {
  440. $this->error = 1039;
  441. RedisService::clear($cacheLockKey);
  442. return false;
  443. }
  444. $data['password'] = get_password($newPassword);
  445. }
  446. // 头像
  447. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  448. if (isset($params['avatar']) && $avatar) {
  449. $data['avatar'] = get_image_path($avatar);
  450. }
  451. if (!$this->model->where(['id' => $userId])->update($data)) {
  452. $this->error = 1014;
  453. RedisService::clear($cacheLockKey);
  454. return false;
  455. }
  456. $oldAvatar = isset($info['avatar']) ? $info['avatar'] : '';
  457. if ($avatar && $oldAvatar && ($avatar != $oldAvatar) && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  458. @unlink(ATTACHMENT_PATH . $oldAvatar);
  459. }
  460. $this->error = 1013;
  461. RedisService::clear($cacheLockKey);
  462. return true;
  463. }
  464. /**
  465. * 设置入口类型
  466. * @param $userId
  467. * @param $params
  468. * @return bool
  469. */
  470. public function setEntry($userId, $params)
  471. {
  472. $cacheLockKey = "caches:members:entry_{$userId}";
  473. if (RedisService::get($cacheLockKey)) {
  474. $this->error = 1034;
  475. return false;
  476. }
  477. $realname = isset($params['realname']) ? trim($params['realname']) : '';
  478. $university = isset($params['university']) ? trim($params['university']) : '';
  479. $entryType = isset($params['entry_type']) ? intval($params['entry_type']) : 0;
  480. // if(empty($realname)){
  481. // $this->error = '请输入姓名';
  482. // return false;
  483. // }
  484. //
  485. // if(empty($realname)){
  486. // $this->error = '请输入毕业院校';
  487. // return false;
  488. // }
  489. //
  490. // if($entryType<=0){
  491. // $this->error = '请选择报考类型';
  492. // return false;
  493. // }
  494. // 用户验证
  495. RedisService::set($cacheLockKey.'_temp', ['user_id' => $userId, 'params' => $params], 3600);
  496. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  497. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  498. ->select(['id', 'password', 'status'])
  499. ->first();
  500. if (!$info || $info['status'] != 1) {
  501. $this->error = 1043;
  502. RedisService::clear($cacheLockKey);
  503. return false;
  504. }
  505. // 密码校验
  506. $data = [
  507. 'entry_type' => $entryType,
  508. 'need_paper' => isset($params['need_paper']) ? intval($params['need_paper']) : 0,
  509. 'realname' => $realname,
  510. 'university' => $university,
  511. 'update_time' => time()
  512. ];
  513. if ($params['province'] && $params['city']) {
  514. $data['province'] = isset($params['province']) ? trim($params['province']) : '';
  515. $data['city'] = isset($params['city']) ? trim($params['city']) : '';
  516. $data['district'] = isset($params['district']) ? trim($params['district']) : '';
  517. }
  518. if (!$this->model->where(['id' => $userId])->update($data)) {
  519. $this->error = 1020;
  520. RedisService::clear($cacheLockKey);
  521. return false;
  522. }
  523. $this->error = 1019;
  524. RedisService::clear($cacheLockKey);
  525. return true;
  526. }
  527. /**
  528. * 设置地址信息
  529. * @param $userId
  530. * @param $params
  531. * @return bool
  532. */
  533. public function setArea($userId, $params)
  534. {
  535. $cacheLockKey = "caches:members:area_{$userId}";
  536. if (RedisService::get($cacheLockKey)) {
  537. $this->error = 1034;
  538. return false;
  539. }
  540. // 用户验证
  541. RedisService::set($cacheLockKey.'_temp', ['user_id' => $userId, 'params' => $params], 3600);
  542. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  543. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  544. ->select(['id', 'password', 'status'])
  545. ->first();
  546. if (!$info || $info['status'] != 1) {
  547. $this->error = 1043;
  548. RedisService::clear($cacheLockKey);
  549. return false;
  550. }
  551. // 密码校验
  552. $data = [
  553. 'province' => isset($params['province']) ? trim($params['province']) : '',
  554. 'city' => isset($params['city']) ? trim($params['city']) : '',
  555. 'district' => isset($params['district']) ? trim($params['district']) : '',
  556. 'update_time' => time()
  557. ];
  558. $this->model->where(['id' => $userId])->update($data);
  559. $this->error = 1019;
  560. RedisService::clear($cacheLockKey);
  561. return true;
  562. }
  563. /**
  564. * 获取VIP类别
  565. * @param int $type
  566. * @return array|mixed
  567. */
  568. public function getVipList($type = 1)
  569. {
  570. $cacheKey = "caches:members:vipList:{$type}";
  571. $datas = RedisService::get($cacheKey);
  572. if ($datas) {
  573. return $datas;
  574. }
  575. $datas = VipModel::where(['type' => $type, 'status' => 1, 'mark' => 1])
  576. ->select(['id', 'name', 'type', 'price', 'original_price', 'day', 'remark', 'status'])
  577. ->orderBy('id', 'asc')
  578. ->get();
  579. $datas = $datas ? $datas->toArray() : [];
  580. if ($datas) {
  581. RedisService::set($cacheKey, $datas, rand(3600, 7200));
  582. }
  583. return $datas;
  584. }
  585. /**
  586. * 购买VIP
  587. * @param $userId
  588. * @param $vipId
  589. * @return array|false
  590. */
  591. public function vipBuy($userId, $params)
  592. {
  593. $vipId = isset($params['id']) ? $params['id'] : 0; // VIP
  594. $cId = isset($params['cid']) ? $params['cid'] : 0; // 课程ID
  595. $cacheKey = "caches:members:vipBuy:{$userId}_{$vipId}";
  596. if (RedisService::get($cacheKey . '_lock')) {
  597. $this->error = '请不要频繁提交~';
  598. return false;
  599. }
  600. RedisService::set($cacheKey, ['date' => date('Y-m-d H:i:s')], rand(2, 3));
  601. $vipInfo = VipModel::where(['id' => $vipId, 'status' => 1, 'mark' => 1])
  602. ->select(['id', 'name', 'type', 'price', 'day', 'status'])
  603. ->first();
  604. $price = isset($vipInfo['price']) ? moneyFormat($vipInfo['price'],1) : 0;
  605. $day = isset($vipInfo['day']) ? intval($vipInfo['day']) : 0;
  606. $vipType = isset($vipInfo['type']) && $vipInfo['type'] ? intval($vipInfo['type']) : 1;
  607. $vipName = isset($vipInfo['name']) ? $vipInfo['name'] : '';
  608. if (empty($vipInfo)) {
  609. $this->error = '该VIP已下架';
  610. RedisService::clear($cacheKey . '_lock');
  611. return false;
  612. }
  613. // if($vipId== 4){
  614. // $this->error = '套餐价格:'.$price;
  615. // RedisService::clear($cacheKey . '_lock');
  616. // return false;
  617. // }
  618. if ($price <= 0 || $day <= 0) {
  619. $this->error = '该VIP参数错误';
  620. RedisService::clear($cacheKey . '_lock');
  621. return false;
  622. }
  623. $showZsb = ConfigService::make()->getConfigByCode('show_zsb_entry', 0);
  624. if($vipType == 2 && !$showZsb){
  625. $this->error = '该VIP套餐暂未开放~';
  626. RedisService::clear($cacheKey . '_lock');
  627. return false;
  628. }
  629. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  630. ->select(['id', 'openid', 'mobile', 'is_zg_vip', 'zg_vip_expired', 'is_zsb_vip', 'zsb_vip_expired', 'is_video_vip', 'video_vip_expired', 'status'])
  631. ->first();
  632. $isZgVip = isset($info['is_zg_vip']) ? $info['is_zg_vip'] : 0;
  633. $isZsbVip = isset($info['is_zsb_vip']) ? $info['is_zsb_vip'] : 0;
  634. $isVideoVip = isset($info['is_video_vip']) ? $info['is_video_vip'] : 0;
  635. $zgVipExpired = isset($info['zg_vip_expired']) ? $info['zg_vip_expired'] : '';
  636. $zsbVipExpired = isset($info['zsb_vip_expired']) ? $info['zsb_vip_expired'] : '';
  637. $videoVipExpired = isset($info['video_vip_expired']) ? $info['video_vip_expired'] : '';
  638. $openid = isset($info['openid']) ? $info['openid'] : '';
  639. if (!$info || $info['status'] != 1) {
  640. $this->error = 1045;
  641. RedisService::clear($cacheKey . '_lock');
  642. return false;
  643. }
  644. if (empty($openid)) {
  645. $this->error = '用户参数错误,请重新授权登录后尝试~';
  646. RedisService::clear($cacheKey . '_lock');
  647. return false;
  648. }
  649. // 验证是否
  650. $goodsId = 0;
  651. $scene = 'vip';
  652. $expiredTime = time();
  653. if ($vipId == 5) {
  654. // 单节视频验证是否已付费过
  655. if (VideoOrderModel::where(['goods_id' => $vipId, 'status' => 2, 'mark' => 1])->where('expired_at', '>', date('Y-m-d H:i:s'))->value('id')) {
  656. $this->error = '抱歉,您已购买过该节视频课,请刷新后尝试观看~';
  657. return false;
  658. }
  659. // 视频数据
  660. $goodsId = $cId;
  661. $scene = 'course';
  662. $courseInfo = VideoCoursesModel::where(['id' => $goodsId, 'status' => 1, 'mark' => 1])->select(['id', 'fee', 'course_name', 'status'])->first();
  663. $fee = isset($courseInfo['fee']) ? $courseInfo['fee'] : 0;
  664. if (empty($courseInfo)) {
  665. $this->error = '抱歉,您购买的课程已下架,请刷新后重试~';
  666. return false;
  667. }
  668. if ($fee <= 0) {
  669. $this->error = '抱歉,您购买的课程为免费课程,请刷新后直接观看~';
  670. return false;
  671. }
  672. } else {
  673. // VIP 验证
  674. $goodsId = $vipId;
  675. $vipLimitOpen = ConfigService::make()->getConfigByCode('vip_limit_more', 0);
  676. if ($vipLimitOpen) {
  677. // 已开通职高VIP无法再开通
  678. if ($vipType == 1 && ($isZgVip == 1 && $zgVipExpired >= date('Y-m-d H:i:s'))) {
  679. $this->error = '抱歉,您的职高VIP未到期,到期后再尝试~';
  680. return false;
  681. }
  682. // 已开通专升本VIP无法再开通
  683. if ($vipType == 2 && ($isZsbVip == 1 && $zsbVipExpired >= date('Y-m-d H:i:s'))) {
  684. $this->error = '抱歉,您的专升本VIP未到期,到期后再尝试~';
  685. return false;
  686. }
  687. // 已开通全部视频VIP无法再开通
  688. if ($vipType == 3 && ($isVideoVip && $videoVipExpired >= date('Y-m-d H:i:s'))) {
  689. $this->error = '抱歉,您的已开通全部视频VIP,请到期后再尝试~';
  690. return false;
  691. }
  692. } else {
  693. if ($vipType == 1 && $isZgVip == 1 && $zgVipExpired >= date('Y-m-d H:i:s')) {
  694. $expiredTime = strtotime($zgVipExpired);
  695. }
  696. if ($vipType == 2 && $isZsbVip == 1 && $zsbVipExpired >= date('Y-m-d H:i:s')) {
  697. $expiredTime = strtotime($zsbVipExpired);
  698. }
  699. if ($vipType == 3 && $isVideoVip == 1 && $videoVipExpired >= date('Y-m-d H:i:s')) {
  700. $expiredTime = strtotime($videoVipExpired);
  701. }
  702. }
  703. }
  704. // 创建订单
  705. $orderNo = get_order_num('VP');
  706. $remark = $vipType == 3 ? "购买{$vipName}" : "购买{$vipName}VIP";
  707. $order = [
  708. 'order_no' => $orderNo,
  709. 'user_id' => $userId,
  710. 'goods_id' => $goodsId,
  711. 'total' => $price,
  712. 'expired_at' => date('Y-m-d H:i:s', $expiredTime + $day * 86400),
  713. 'create_time' => time(),
  714. 'remark' => $remark,
  715. 'status' => 1,
  716. 'mark' => 1
  717. ];
  718. DB::beginTransaction();
  719. $model = $vipId == 5 ? new VideoOrderModel : new OrderModel;
  720. if (!$orderId = $model::insertGetId($order)) {
  721. $this->error = '创建VIP订单失败';
  722. return false;
  723. }
  724. /* TODO 支付处理 */
  725. $payOrder = [
  726. 'type' => 1,
  727. 'order_no' => $orderNo,
  728. 'pay_money' => $price,
  729. 'body' => $remark,
  730. 'openid' => $openid
  731. ];
  732. // 调起支付
  733. $payment = PaymentService::make()->minPay($info, $payOrder, $scene);
  734. if (empty($payment)) {
  735. DB::rollBack();
  736. RedisService::clear($cacheKey . '_lock');
  737. $this->error = PaymentService::make()->getError();
  738. return false;
  739. }
  740. // 用户操作记录
  741. DB::commit();
  742. $this->error = '创建VIP订单成功,请前往支付~';
  743. RedisService::clear($cacheKey . '_lock');
  744. RedisService::keyDel("caches:videos:list_by_group*");
  745. RedisService::clear("caches:videos:info_{$userId}_{$cId}");
  746. return [
  747. 'order_id' => $orderId,
  748. 'payment' => $payment,
  749. 'total' => $payOrder['pay_money'],
  750. 'pay_type' => 10,
  751. ];
  752. }
  753. }