MemberService.php 27 KB

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