MemberService.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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, 'mark' => 1];
  164. $data = $this->model->where($where)
  165. ->select(['id', 'openid', 'mobile', 'user_type', 'nickname', 'entry_type', 'need_paper', 'code', 'status'])
  166. ->first();
  167. $data = $data ? $data->toArray() : [];
  168. $userId = isset($data['id']) ? $data['id'] : 0;
  169. $status = isset($data['status']) ? $data['status'] : 0;
  170. $entryType = isset($data['entry_type']) ? $data['entry_type'] : 0;
  171. $needPaper = isset($data['need_paper']) ? $data['need_paper'] : 0;
  172. if ($data && $userId && $status != 1) {
  173. $this->error = 2011;
  174. return false;
  175. }
  176. $system = isset($params['system']) ? $params['system'] : [];
  177. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  178. $appSources = isset($system['app_sources']) && $system['app_sources'] ? $system['app_sources'] : 'ios';
  179. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  180. $version = isset($system['app_version']) ? $system['app_version'] : '';
  181. if (empty($data)) {
  182. $userId = $this->model->max('id') + 1;
  183. $data = [
  184. 'nickname' => $phone ? '用户' . substr($phone, -6, 6) : '用户' . $userId,
  185. 'openid' => $openid,
  186. 'mobile' => $phone,
  187. 'code' => get_random_code(9,'S', $userId),
  188. 'password' => $phone ? get_password(substr($phone, -6, 6)) : get_password('123456'),
  189. 'login_ip' => get_client_ip(),
  190. 'create_time' => time(),
  191. 'login_time' => time(),
  192. 'login_count' => DB::raw("login_count+1"),
  193. 'app_version' => $version,
  194. 'app_uuid' => $uuid,
  195. 'device' => $appSources == 'ios' ? 1 : 2,
  196. ];
  197. if ($this->model->insertGetId($data)) {
  198. $this->error = 2012;
  199. return false;
  200. }
  201. } // 更新登录信息
  202. else if (!RedisService::get("caches:members:login_{$userId}")) {
  203. $updateData = [
  204. 'login_ip' => get_client_ip(),
  205. 'create_time' => time(),
  206. 'login_time' => time(),
  207. 'app_uuid' => $uuid,
  208. 'login_count' => DB::raw("login_count+1"),
  209. 'app_version' => $version,
  210. 'device' => $appSources == 'ios' ? 1 : 2,
  211. ];
  212. if ($openid) {
  213. $updateData['openid'] = $openid;
  214. }
  215. $this->model->where(['id' => $userId])->update($updateData);
  216. RedisService::set("caches:members:login_{$userId}", $updateData, rand(180, 300));
  217. }
  218. // 获取登录授权token
  219. $jwt = new Jwt('jwt_jd_app');
  220. $token = $jwt->getToken($userId);
  221. // 结果返回
  222. $setEntry = $entryType && $needPaper ? 1 : 0;
  223. $result = [
  224. 'access_token' => $token,
  225. 'info' => ['uid' => $userId, 'openid' => $openid, 'set_entry' => $setEntry, 'nickname' => $data['nickname']],
  226. ];
  227. // 用户缓存信息
  228. $this->error = 2013;
  229. $data['token'] = $token;
  230. unset($data['mobile']);
  231. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  232. return $result;
  233. }
  234. /**
  235. * 重置密码
  236. * @param $params
  237. * @return array|false
  238. */
  239. public function forget($params)
  240. {
  241. // 账号登录
  242. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  243. $password = isset($params['password']) ? trim($params['password']) : '';
  244. if (empty($params) || empty($mobile) || empty($password)) {
  245. $this->error = 1041;
  246. return false;
  247. }
  248. // 验证码验证
  249. $smsCode = isset($params['sms_code']) ? trim($params['sms_code']) : '';
  250. if (!SmsService::make()->check($mobile, $smsCode, 'reset_password')) {
  251. $this->error = SmsService::make()->getError();
  252. return false;
  253. }
  254. // 验证是否注册
  255. if (!$userId = $this->model->where(['mobile' => $mobile, 'mark' => 1])->value('id')) {
  256. $this->error = 1038;
  257. return false;
  258. }
  259. if (!$this->model->where(['id' => $userId])->update(['password' => get_password($password), 'update_time' => time()])) {
  260. $this->error = 2030;
  261. return false;
  262. }
  263. // 操作日志
  264. ActionLogModel::setRecord($userId, ['type' => 2, 'title' => '重置密码', 'content' => '重置登录密码', 'module' => 'member']);
  265. ActionLogModel::record();
  266. $this->error = 2031;
  267. return true;
  268. }
  269. /**
  270. * 获取资料详情
  271. * @param $where
  272. * @param array $field
  273. */
  274. public function getInfo($where, array $field = [], $refresh = true)
  275. {
  276. $cacheKey = "caches:members:info_" . (!is_array($where) ? $where . '_' : '') . md5(json_encode($where) . json_encode($field));
  277. $info = RedisService::get($cacheKey);
  278. if ($info && !$refresh) {
  279. return $info;
  280. }
  281. $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', 'balance', 'code', 'openid', 'status', 'avatar'];
  282. $field = $field ? $field : $defaultField;
  283. if (is_array($where)) {
  284. $info = $this->model->where(['mark' => 1])->where($where)->select($field)->first();
  285. } else {
  286. $info = $this->model->where(['mark' => 1])->where(['id' => (int)$where])->select($field)->first();
  287. }
  288. $info = $info ? $info->toArray() : [];
  289. if ($info) {
  290. if (isset($info['avatar'])) {
  291. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : '';
  292. }
  293. if (isset($info['mobile'])) {
  294. $info['mobile_text'] = $info['mobile'] ? format_mobile($info['mobile']) : '';
  295. }
  296. if (isset($info['create_time'])) {
  297. $info['create_at'] = datetime(strtotime($info['create_time']));
  298. }
  299. $isVip = 0;
  300. $vipExpired = '';
  301. $isZgVip = isset($info['is_zg_vip']) ? $info['is_zg_vip'] : 0;
  302. $zgVipExpired = isset($info['zg_vip_expired']) && !empty($info['zg_vip_expired']) ? $info['zg_vip_expired'] : '';
  303. if ($isZgVip==1 && $zgVipExpired && $zgVipExpired > date('Y-m-d H:i:s')) {
  304. $isVip = 1;
  305. $isZgVip = 1;
  306. $vipExpired = $zgVipExpired;
  307. } else {
  308. $isZgVip = 0;
  309. }
  310. $isZsbVip = isset($info['is_zsb_vip']) ? $info['is_zsb_vip'] : 0;
  311. $zsbVipExpired = isset($info['zsb_vip_expired']) && !empty($info['zsb_vip_expired']) ? $info['zsb_vip_expired'] : '';
  312. if ($isZsbVip==1 && $zsbVipExpired && $zsbVipExpired > date('Y-m-d H:i:s')) {
  313. $isVip = 1;
  314. $vipExpired = $zsbVipExpired > $vipExpired ? $zsbVipExpired : $vipExpired;
  315. } else {
  316. $isZsbVip = 0;
  317. }
  318. $isVideoVip = isset($info['is_video_vip']) ? $info['is_video_vip'] : 0;
  319. $videoVipExpired = isset($info['video_vip_expired']) && !empty($info['video_vip_expired']) ? $info['video_vip_expired'] : '';
  320. if ($isVideoVip==1 && $videoVipExpired && $videoVipExpired > date('Y-m-d H:i:s')) {
  321. $isVideoVip = 1;
  322. } else {
  323. $isVideoVip = 0;
  324. }
  325. $info['is_vip'] = $isVip;
  326. $info['is_zg_vip'] = $isZgVip;
  327. $info['is_zsb_vip'] = $isZsbVip;
  328. $info['vip_expired'] = $vipExpired;
  329. $info['zg_vip_expired'] = $zgVipExpired;
  330. $info['zg_vip_day'] = max(0, intval((strtotime($zgVipExpired) - time())/86400));
  331. $info['zsb_vip_expired'] = $zsbVipExpired;
  332. $info['zsb_vip_day'] = max(0, intval((strtotime($zsbVipExpired) - time())/86400));
  333. $info['is_video_vip'] = $isVideoVip;
  334. $info['video_vip_expired'] = $videoVipExpired;
  335. $info['video_vip_day'] = max(0, intval((strtotime($videoVipExpired) - time())/86400));
  336. RedisService::set($cacheKey, $info, rand(5, 10));
  337. }
  338. return $info;
  339. }
  340. /**
  341. * 生成普通参数二维码
  342. * @param $str 参数
  343. * @param bool $refresh 是否重新生成
  344. * @return bool
  345. */
  346. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  347. {
  348. $basePath = base_path() . '/public';
  349. $qrFile = '/images/qrcode/';
  350. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  351. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  352. }
  353. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  354. $qrFile = $qrFile . "C_{$key}.png";
  355. $cacheKey = "caches:qrcodes:member_" . $key;
  356. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  357. return $qrFile;
  358. }
  359. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  360. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  361. return false;
  362. }
  363. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  364. return $qrFile;
  365. }
  366. /**
  367. * 修改头像
  368. * @param $userId
  369. * @param $avatar
  370. * @return mixed
  371. */
  372. public function saveAvatar($userId, $avatar)
  373. {
  374. $oldAvatar = $this->model->where(['id' => $userId])->value('avatar');
  375. if ($this->model->where(['id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  376. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  377. @unlink(ATTACHMENT_PATH . $oldAvatar);
  378. }
  379. return true;
  380. }
  381. return false;
  382. }
  383. /**
  384. * 修改账号信息
  385. * @param $userId
  386. * @param $params
  387. * @return bool
  388. */
  389. public function modify($userId, $params)
  390. {
  391. $cacheLockKey = "caches:members:modify_{$userId}";
  392. if (RedisService::get($cacheLockKey)) {
  393. $this->error = 1034;
  394. return false;
  395. }
  396. // 用户验证
  397. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  398. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  399. ->select(['id', 'password', 'status'])
  400. ->first();
  401. $userPassword = isset($info['password']) ? $info['password'] : '';
  402. if (!$info || $info['status'] != 1) {
  403. $this->error = 1029;
  404. RedisService::clear($cacheLockKey);
  405. return false;
  406. }
  407. // 密码校验
  408. $data = ['update_time' => time()];
  409. // 修改数据
  410. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  411. if (isset($params['nickname']) && $nickname) {
  412. $data['nickname'] = $nickname;
  413. }
  414. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  415. if (isset($params['mobile']) && $mobile) {
  416. $data['mobile'] = $mobile;
  417. }
  418. $password = isset($params['password']) ? $params['password'] : '';
  419. $newPassword = isset($params['new_password']) ? $params['new_password'] : '';
  420. if (isset($params['password']) && $password) {
  421. if ($userPassword != get_password($password)) {
  422. $this->error = 1038;
  423. RedisService::clear($cacheLockKey);
  424. return false;
  425. }
  426. if (empty($newPassword)) {
  427. $this->error = 1039;
  428. RedisService::clear($cacheLockKey);
  429. return false;
  430. }
  431. $data['password'] = get_password($newPassword);
  432. }
  433. // 头像
  434. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  435. if (isset($params['avatar']) && $avatar) {
  436. $data['avatar'] = get_image_path($avatar);
  437. }
  438. if (!$this->model->where(['id' => $userId])->update($data)) {
  439. $this->error = 1014;
  440. RedisService::clear($cacheLockKey);
  441. return false;
  442. }
  443. $oldAvatar = isset($info['avatar']) ? $info['avatar'] : '';
  444. if ($avatar && $oldAvatar && ($avatar != $oldAvatar) && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  445. @unlink(ATTACHMENT_PATH . $oldAvatar);
  446. }
  447. $this->error = 1013;
  448. RedisService::clear($cacheLockKey);
  449. return true;
  450. }
  451. /**
  452. * 设置入口类型
  453. * @param $userId
  454. * @param $params
  455. * @return bool
  456. */
  457. public function setEntry($userId, $params)
  458. {
  459. $cacheLockKey = "caches:members:entry_{$userId}";
  460. if (RedisService::get($cacheLockKey)) {
  461. $this->error = 1034;
  462. return false;
  463. }
  464. // 用户验证
  465. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  466. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  467. ->select(['id', 'password', 'status'])
  468. ->first();
  469. if (!$info || $info['status'] != 1) {
  470. $this->error = 1043;
  471. RedisService::clear($cacheLockKey);
  472. return false;
  473. }
  474. // 密码校验
  475. $data = [
  476. 'entry_type' => isset($params['entry_type']) ? intval($params['entry_type']) : 0,
  477. 'need_paper' => isset($params['need_paper']) ? intval($params['need_paper']) : 0,
  478. 'update_time' => time()
  479. ];
  480. if (!$this->model->where(['id' => $userId])->update($data)) {
  481. $this->error = 1020;
  482. RedisService::clear($cacheLockKey);
  483. return false;
  484. }
  485. $this->error = 1019;
  486. RedisService::clear($cacheLockKey);
  487. return true;
  488. }
  489. /**
  490. * 获取VIP类别
  491. * @param int $type
  492. * @return array|mixed
  493. */
  494. public function getVipList($type = 1)
  495. {
  496. $cacheKey = "caches:members:vipList:{$type}";
  497. $datas = RedisService::get($cacheKey);
  498. if ($datas) {
  499. return $datas;
  500. }
  501. $datas = VipModel::where(['type' => $type, 'status' => 1, 'mark' => 1])
  502. ->select(['id', 'name', 'type', 'price','original_price', 'day', 'remark', 'status'])
  503. ->orderBy('id', 'asc')
  504. ->get();
  505. $datas = $datas ? $datas->toArray() : [];
  506. if ($datas) {
  507. RedisService::set($cacheKey, $datas, rand(3600, 7200));
  508. }
  509. return $datas;
  510. }
  511. /**
  512. * 购买VIP
  513. * @param $userId
  514. * @param $vipId
  515. * @return array|false
  516. */
  517. public function vipBuy($userId, $params)
  518. {
  519. $vipId = isset($params['id'])? $params['id'] : 0; // VIP
  520. $cId = isset($params['cid'])? $params['cid'] : 0; // 课程ID
  521. $cacheKey = "caches:members:vipBuy:{$userId}_{$vipId}";
  522. if (RedisService::get($cacheKey . '_lock')) {
  523. $this->error = '请不要频繁提交~';
  524. return false;
  525. }
  526. RedisService::set($cacheKey, ['date' => date('Y-m-d H:i:s')], rand(2, 3));
  527. $vipInfo = VipModel::where(['id' => $vipId, 'status' => 1, 'mark' => 1])
  528. ->select(['id','name', 'type', 'price', 'day', 'status'])
  529. ->first();
  530. $price = isset($vipInfo['price']) ? floatval($vipInfo['price']) : 0;
  531. $day = isset($vipInfo['day']) ? intval($vipInfo['day']) : 0;
  532. $vipType = isset($vipInfo['type']) && $vipInfo['type']? intval($vipInfo['type']) : 1;
  533. $vipName = isset($vipInfo['name'])? $vipInfo['name'] : '';
  534. if (empty($vipInfo)) {
  535. $this->error = '该VIP已下架';
  536. RedisService::clear($cacheKey . '_lock');
  537. return false;
  538. }
  539. if ($price <= 0 || $day <= 0) {
  540. $this->error = '该VIP参数错误';
  541. RedisService::clear($cacheKey . '_lock');
  542. return false;
  543. }
  544. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  545. ->select(['id','openid','mobile','is_zg_vip', 'zg_vip_expired','is_zsb_vip','zsb_vip_expired','is_video_vip','video_vip_expired', 'status'])
  546. ->first();
  547. $isZgVip = isset($info['is_zg_vip'])? $info['is_zg_vip'] : 0;
  548. $isZsbVip = isset($info['is_zsb_vip'])? $info['is_zsb_vip'] : 0;
  549. $isVideoVip = isset($info['is_video_vip'])? $info['is_video_vip'] : 0;
  550. $zgVipExpired = isset($info['zg_vip_expired'])? $info['zg_vip_expired'] : '';
  551. $zsbVipExpired = isset($info['zsb_vip_expired'])? $info['zsb_vip_expired'] : '';
  552. $videoVipExpired = isset($info['video_vip_expired'])? $info['video_vip_expired'] : '';
  553. $openid = isset($info['openid'])? $info['openid'] : '';
  554. if (!$info || $info['status'] != 1) {
  555. $this->error = 1045;
  556. RedisService::clear($cacheKey . '_lock');
  557. return false;
  558. }
  559. if(empty($openid)){
  560. $this->error = '用户参数错误,请重新授权登录后尝试~';
  561. RedisService::clear($cacheKey . '_lock');
  562. return false;
  563. }
  564. // 验证是否
  565. $goodsId = 0;
  566. $scene = 'vip';
  567. $expiredTime = time();
  568. if($vipId == 5){
  569. // 单节视频验证是否已付费过
  570. if(VideoOrderModel::where(['goods_id'=> $vipId,'status'=>2,'mark'=>1])->where('expired_at','>', date('Y-m-d H:i:s'))->value('id')){
  571. $this->error = '抱歉,您已购买过该节视频课,请刷新后尝试观看~';
  572. return false;
  573. }
  574. // 视频数据
  575. $goodsId = $cId;
  576. $scene = 'course';
  577. $courseInfo = VideoCoursesModel::where(['id'=>$goodsId,'status'=>1,'mark'=>1])->select(['id','fee','course_name','status'])->first();
  578. $fee = isset($courseInfo['fee'])?$courseInfo['fee'] : 0;
  579. if(empty($courseInfo)){
  580. $this->error = '抱歉,您购买的课程已下架,请刷新后重试~';
  581. return false;
  582. }
  583. if($fee<=0){
  584. $this->error = '抱歉,您购买的课程为免费课程,请刷新后直接观看~';
  585. return false;
  586. }
  587. }else {
  588. // VIP 验证
  589. $goodsId = $vipId;
  590. $vipLimitOpen = ConfigService::make()->getConfigByCode('vip_limit_more', 0);
  591. if($vipLimitOpen){
  592. // 已开通职高VIP无法再开通
  593. if($vipType == 1 && ($isZgVip==1 && $zgVipExpired >= date('Y-m-d H:i:s'))){
  594. $this->error = '抱歉,您的职高VIP未到期,到期后再尝试~';
  595. return false;
  596. }
  597. // 已开通专升本VIP无法再开通
  598. if($vipType == 2 && ($isZsbVip==1 && $zsbVipExpired >= date('Y-m-d H:i:s'))){
  599. $this->error = '抱歉,您的专升本VIP未到期,到期后再尝试~';
  600. return false;
  601. }
  602. // 已开通全部视频VIP无法再开通
  603. if($vipType == 3 && ($isVideoVip && $videoVipExpired >= date('Y-m-d H:i:s'))){
  604. $this->error = '抱歉,您的已开通全部视频VIP,请到期后再尝试~';
  605. return false;
  606. }
  607. }else{
  608. if($vipType == 1 && $isZgVip==1 && $zgVipExpired >= date('Y-m-d H:i:s')){
  609. $expiredTime = strtotime($zgVipExpired);
  610. }
  611. if($vipType == 2 && $isZsbVip==1 && $zsbVipExpired >= date('Y-m-d H:i:s')){
  612. $expiredTime = strtotime($zsbVipExpired);
  613. }
  614. if($vipType == 3 && $isVideoVip==1 && $videoVipExpired >= date('Y-m-d H:i:s')){
  615. $expiredTime = strtotime($videoVipExpired);
  616. }
  617. }
  618. }
  619. // 创建订单
  620. $orderNo = get_order_num('VP');
  621. $remark = $vipType==3? "购买{$vipName}" : "购买{$vipName}VIP";
  622. $order = [
  623. 'order_no'=> $orderNo,
  624. 'user_id'=> $userId,
  625. 'goods_id'=> $goodsId,
  626. 'total'=> $price,
  627. 'expired_at'=> date('Y-m-d H:i:s', $expiredTime + $day * 86400),
  628. 'create_time'=> time(),
  629. 'remark'=> $remark,
  630. 'status'=>1,
  631. 'mark'=>1
  632. ];
  633. DB::beginTransaction();
  634. $model = $vipId==5? new VideoOrderModel : new OrderModel;
  635. if(!$orderId = $model::insertGetId($order)){
  636. $this->error = '创建VIP订单失败';
  637. return false;
  638. }
  639. /* TODO 支付处理 */
  640. $payOrder = [
  641. 'type'=> 1,
  642. 'order_no'=> $orderNo,
  643. 'pay_money'=> $price,
  644. 'body'=> $remark,
  645. 'openid'=> $openid
  646. ];
  647. // 调起支付
  648. $payment = PaymentService::make()->minPay($info, $payOrder,$scene);
  649. if(empty($payment)){
  650. DB::rollBack();
  651. RedisService::clear($cacheKey.'_lock');
  652. $this->error = PaymentService::make()->getError();
  653. return false;
  654. }
  655. // 用户操作记录
  656. DB::commit();
  657. $this->error = '创建VIP订单成功,请前往支付~';
  658. RedisService::clear($cacheKey.'_lock');
  659. RedisService::keyDel("caches:videos:list_by_group*");
  660. RedisService::clear("caches:videos:info_{$userId}_{$cId}");
  661. return [
  662. 'order_id'=> $orderId,
  663. 'payment'=> $payment,
  664. 'total'=> $payOrder['pay_money'],
  665. 'pay_type'=> 10,
  666. ];
  667. }
  668. }