MemberService.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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_vip_1', 'vip_1_expired', 'is_vip_2', 'vip_2_expired', 'is_vip_3', 'vip_3_expired', 'is_vip_4', 'vip_4_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. $isVip1 = isset($info['is_vip_1']) ? $info['is_vip_1'] : 0;
  315. $vip1Expired = isset($info['vip_1_expired']) && !empty($info['vip_1_expired']) ? $info['vip_1_expired'] : '';
  316. if ($isVip1 == 1 && $vip1Expired && $vip1Expired > date('Y-m-d H:i:s')) {
  317. $isVip = 1;
  318. $isVip1 = 1;
  319. $vipExpired = $vip1Expired;
  320. } else {
  321. $isVip1 = 0;
  322. }
  323. $isVip2 = isset($info['is_vip_2']) ? $info['is_vip_2'] : 0;
  324. $vip2Expired = isset($info['vip_2_expired']) && !empty($info['vip_2_expired']) ? $info['vip_2_expired'] : '';
  325. if ($isVip2 == 1 && $vip2Expired && $vip2Expired > date('Y-m-d H:i:s')) {
  326. $isVip = 1;
  327. $isVip2 = 1;
  328. $vipExpired = $vip2Expired;
  329. } else {
  330. $isVip2 = 0;
  331. }
  332. $isVip3 = isset($info['is_vip_3']) ? $info['is_vip_3'] : 0;
  333. $vip3Expired = isset($info['vip_3_expired']) && !empty($info['vip_3_expired']) ? $info['vip_3_expired'] : '';
  334. if ($isVip3 == 1 && $vip3Expired && $vip3Expired > date('Y-m-d H:i:s')) {
  335. $isVip = 1;
  336. $isVip3 = 1;
  337. $vipExpired = $vip3Expired;
  338. } else {
  339. $isVip3 = 0;
  340. }
  341. $isVip4 = isset($info['is_vip_4']) ? $info['is_vip_4'] : 0;
  342. $vip4Expired = isset($info['vip_4_expired']) && !empty($info['vip_4_expired']) ? $info['vip_4_expired'] : '';
  343. if ($isVip4 == 1 && $vip4Expired && $vip4Expired > date('Y-m-d H:i:s')) {
  344. $isVip = 1;
  345. $isVip4 = 1;
  346. $vipExpired = $vip4Expired;
  347. } else {
  348. $isVip4 = 0;
  349. }
  350. $isVideoVip = isset($info['is_video_vip']) ? $info['is_video_vip'] : 0;
  351. $videoVipExpired = isset($info['video_vip_expired']) && !empty($info['video_vip_expired']) ? $info['video_vip_expired'] : '';
  352. if ($isVideoVip == 1 && $videoVipExpired && $videoVipExpired > date('Y-m-d H:i:s')) {
  353. $isVideoVip = 1;
  354. } else {
  355. $isVideoVip = 0;
  356. }
  357. $info['is_vip'] = $isVip;
  358. $info['is_vip_1'] = $isVip1;
  359. $info['is_vip_2'] = $isVip2;
  360. $info['is_vip_3'] = $isVip3;
  361. $info['is_vip_4'] = $isVip4;
  362. $info['vip_expired'] = $vipExpired;
  363. $info['vip_1_expired'] = $vip1Expired;
  364. $info['vip_1_day'] = max(0, ceil((strtotime($vip1Expired) - time()) / 86400));
  365. $info['vip_2_expired'] = $vip2Expired;
  366. $info['vip_2_day'] = max(0, ceil((strtotime($vip2Expired) - time()) / 86400));
  367. $info['vip_3_expired'] = $vip3Expired;
  368. $info['vip_3_day'] = max(0, ceil((strtotime($vip3Expired) - time()) / 86400));
  369. $info['vip_4_expired'] = $vip4Expired;
  370. $info['vip_4_day'] = max(0, ceil((strtotime($vip4Expired) - time()) / 86400));
  371. $info['is_video_vip'] = $isVideoVip;
  372. $info['video_vip_expired'] = $videoVipExpired;
  373. $info['video_vip_day'] = max(0, ceil((strtotime($videoVipExpired) - time()) / 86400));
  374. RedisService::set($cacheKey, $info, rand(5, 10));
  375. }
  376. return $info;
  377. }
  378. /**
  379. * 生成普通参数二维码
  380. * @param $str 参数
  381. * @param bool $refresh 是否重新生成
  382. * @return bool
  383. */
  384. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  385. {
  386. $basePath = base_path() . '/public';
  387. $qrFile = '/images/qrcode/';
  388. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  389. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  390. }
  391. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  392. $qrFile = $qrFile . "C_{$key}.png";
  393. $cacheKey = "caches:qrcodes:member_" . $key;
  394. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  395. return $qrFile;
  396. }
  397. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  398. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  399. return false;
  400. }
  401. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  402. return $qrFile;
  403. }
  404. /**
  405. * 修改头像
  406. * @param $userId
  407. * @param $avatar
  408. * @return mixed
  409. */
  410. public function saveAvatar($userId, $avatar)
  411. {
  412. $oldAvatar = $this->model->where(['id' => $userId])->value('avatar');
  413. if ($this->model->where(['id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  414. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  415. @unlink(ATTACHMENT_PATH . $oldAvatar);
  416. }
  417. return true;
  418. }
  419. return false;
  420. }
  421. /**
  422. * 修改账号信息
  423. * @param $userId
  424. * @param $params
  425. * @return bool
  426. */
  427. public function modify($userId, $params)
  428. {
  429. $cacheLockKey = "caches:members:modify_{$userId}";
  430. if (RedisService::get($cacheLockKey)) {
  431. $this->error = 1034;
  432. return false;
  433. }
  434. // 用户验证
  435. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  436. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  437. ->select(['id', 'password', 'status'])
  438. ->first();
  439. $userPassword = isset($info['password']) ? $info['password'] : '';
  440. if (!$info || $info['status'] != 1) {
  441. $this->error = 1029;
  442. RedisService::clear($cacheLockKey);
  443. return false;
  444. }
  445. // 密码校验
  446. $data = ['update_time' => time()];
  447. // 修改数据
  448. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  449. if (isset($params['nickname']) && $nickname) {
  450. $data['nickname'] = $nickname;
  451. }
  452. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  453. if (isset($params['mobile']) && $mobile) {
  454. $data['mobile'] = $mobile;
  455. }
  456. $password = isset($params['password']) ? $params['password'] : '';
  457. $newPassword = isset($params['new_password']) ? $params['new_password'] : '';
  458. if (isset($params['password']) && $password) {
  459. if ($userPassword != get_password($password)) {
  460. $this->error = 1038;
  461. RedisService::clear($cacheLockKey);
  462. return false;
  463. }
  464. if (empty($newPassword)) {
  465. $this->error = 1039;
  466. RedisService::clear($cacheLockKey);
  467. return false;
  468. }
  469. $data['password'] = get_password($newPassword);
  470. }
  471. // 头像
  472. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  473. if (isset($params['avatar']) && $avatar) {
  474. $data['avatar'] = get_image_path($avatar);
  475. }
  476. if (!$this->model->where(['id' => $userId])->update($data)) {
  477. $this->error = 1014;
  478. RedisService::clear($cacheLockKey);
  479. return false;
  480. }
  481. $oldAvatar = isset($info['avatar']) ? $info['avatar'] : '';
  482. if ($avatar && $oldAvatar && ($avatar != $oldAvatar) && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  483. @unlink(ATTACHMENT_PATH . $oldAvatar);
  484. }
  485. $this->error = 1013;
  486. RedisService::clear($cacheLockKey);
  487. return true;
  488. }
  489. /**
  490. * 设置入口类型
  491. * @param $userId
  492. * @param $params
  493. * @return bool
  494. */
  495. public function setEntry($userId, $params)
  496. {
  497. $cacheLockKey = "caches:members:entry_{$userId}";
  498. if (RedisService::get($cacheLockKey)) {
  499. $this->error = 1034;
  500. return false;
  501. }
  502. $realname = isset($params['realname']) ? trim($params['realname']) : '';
  503. $university = isset($params['university']) ? trim($params['university']) : '';
  504. $entryType = isset($params['entry_type']) ? intval($params['entry_type']) : 0;
  505. // 用户验证
  506. RedisService::set($cacheLockKey.'_temp', ['user_id' => $userId, 'params' => $params], 3600);
  507. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  508. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  509. ->select(['id', 'password', 'status'])
  510. ->first();
  511. if (!$info || $info['status'] != 1) {
  512. $this->error = 1043;
  513. RedisService::clear($cacheLockKey);
  514. return false;
  515. }
  516. // 密码校验
  517. $data = [
  518. 'entry_type' => $entryType,
  519. 'need_paper' => isset($params['need_paper']) ? intval($params['need_paper']) : 0,
  520. 'realname' => $realname,
  521. 'university' => $university,
  522. 'update_time' => time()
  523. ];
  524. if ($params['province'] && $params['city']) {
  525. $data['province'] = isset($params['province']) ? trim($params['province']) : '';
  526. $data['city'] = isset($params['city']) ? trim($params['city']) : '';
  527. $data['district'] = isset($params['district']) ? trim($params['district']) : '';
  528. }
  529. if (!$this->model->where(['id' => $userId])->update($data)) {
  530. $this->error = 1020;
  531. RedisService::clear($cacheLockKey);
  532. return false;
  533. }
  534. $this->error = 1019;
  535. RedisService::clear($cacheLockKey);
  536. return true;
  537. }
  538. /**
  539. * 设置地址信息
  540. * @param $userId
  541. * @param $params
  542. * @return bool
  543. */
  544. public function setArea($userId, $params)
  545. {
  546. $cacheLockKey = "caches:members:area_{$userId}";
  547. if (RedisService::get($cacheLockKey)) {
  548. $this->error = 1034;
  549. return false;
  550. }
  551. // 用户验证
  552. RedisService::set($cacheLockKey.'_temp', ['user_id' => $userId, 'params' => $params], 3600);
  553. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  554. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  555. ->select(['id', 'password', 'status'])
  556. ->first();
  557. if (!$info || $info['status'] != 1) {
  558. $this->error = 1043;
  559. RedisService::clear($cacheLockKey);
  560. return false;
  561. }
  562. // 密码校验
  563. $data = [
  564. 'province' => isset($params['province']) ? trim($params['province']) : '',
  565. 'city' => isset($params['city']) ? trim($params['city']) : '',
  566. 'district' => isset($params['district']) ? trim($params['district']) : '',
  567. 'update_time' => time()
  568. ];
  569. $this->model->where(['id' => $userId])->update($data);
  570. $this->error = 1019;
  571. RedisService::clear($cacheLockKey);
  572. return true;
  573. }
  574. /**
  575. * 获取VIP类别
  576. * @param int $type
  577. * @return array|mixed
  578. */
  579. public function getVipList($type = 1)
  580. {
  581. $cacheKey = "caches:members:vipList:{$type}";
  582. $datas = RedisService::get($cacheKey);
  583. if ($datas) {
  584. return $datas;
  585. }
  586. $datas = VipModel::where(['type' => $type, 'status' => 1, 'mark' => 1])
  587. ->select(['id', 'name', 'type', 'price', 'original_price', 'day', 'remark', 'status'])
  588. ->orderBy('id', 'asc')
  589. ->get();
  590. $datas = $datas ? $datas->toArray() : [];
  591. if ($datas) {
  592. RedisService::set($cacheKey, $datas, rand(3600, 7200));
  593. }
  594. return $datas;
  595. }
  596. /**
  597. * 购买VIP
  598. * @param $userId
  599. * @param $vipId
  600. * @return array|false
  601. */
  602. public function vipBuy($userId, $params)
  603. {
  604. $vipId = isset($params['id']) ? $params['id'] : 0; // VIP
  605. $cId = isset($params['cid']) ? $params['cid'] : 0; // 课程ID
  606. $cacheKey = "caches:members:vipBuy:{$userId}_{$vipId}";
  607. if (RedisService::get($cacheKey . '_lock')) {
  608. $this->error = '请不要频繁提交~';
  609. return false;
  610. }
  611. RedisService::set($cacheKey, ['date' => date('Y-m-d H:i:s')], rand(2, 3));
  612. $vipInfo = VipModel::where(['id' => $vipId, 'status' => 1, 'mark' => 1])
  613. ->select(['id', 'name', 'type', 'price', 'day', 'status'])
  614. ->first();
  615. $price = isset($vipInfo['price']) ? moneyFormat($vipInfo['price'],1) : 0;
  616. $day = isset($vipInfo['day']) ? intval($vipInfo['day']) : 0;
  617. $vipType = isset($vipInfo['type']) && $vipInfo['type'] ? intval($vipInfo['type']) : 1;
  618. $vipName = isset($vipInfo['name']) ? $vipInfo['name'] : '';
  619. if (empty($vipInfo)) {
  620. $this->error = '该VIP已下架';
  621. RedisService::clear($cacheKey . '_lock');
  622. return false;
  623. }
  624. if ($price <= 0 || $day <= 0) {
  625. $this->error = '该VIP参数错误';
  626. RedisService::clear($cacheKey . '_lock');
  627. return false;
  628. }
  629. $showVip = ConfigService::make()->getConfigByCode("show_{$vipType}_entry", 1);
  630. if($showVip != 1){
  631. $this->error = '该VIP套餐暂未开放~';
  632. RedisService::clear($cacheKey . '_lock');
  633. return false;
  634. }
  635. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  636. ->select(['id', 'openid', 'mobile', 'is_vip_1', 'vip_1_expired', 'is_vip_2', 'vip_2_expired', 'is_vip_3', 'vip_3_expired', 'is_vip_4', 'vip_4_expired', 'is_video_vip', 'video_vip_expired', 'status'])
  637. ->first();
  638. $isVip1 = isset($info['is_vip_1']) ? $info['is_vip_1'] : 0;
  639. $isVip2 = isset($info['is_vip_2']) ? $info['is_vip_2'] : 0;
  640. $isVip3 = isset($info['is_vip_2']) ? $info['is_vip_2'] : 0;
  641. $isVip4 = isset($info['is_vip_2']) ? $info['is_vip_2'] : 0;
  642. $vip1Expired = isset($info['vip_1_expired']) ? $info['vip_1_expired'] : '';
  643. $vip2Expired = isset($info['vip_2_expired']) ? $info['vip_2_expired'] : '';
  644. $vip3Expired = isset($info['vip_3_expired']) ? $info['vip_3_expired'] : '';
  645. $vip4Expired = isset($info['vip_4_expired']) ? $info['vip_4_expired'] : '';
  646. $isVideoVip = isset($info['is_video_vip']) ? $info['is_video_vip'] : 0;
  647. $videoVipExpired = isset($info['video_vip_expired']) ? $info['video_vip_expired'] : '';
  648. $openid = isset($info['openid']) ? $info['openid'] : '';
  649. if (!$info || $info['status'] != 1) {
  650. $this->error = 1045;
  651. RedisService::clear($cacheKey . '_lock');
  652. return false;
  653. }
  654. if (empty($openid)) {
  655. $this->error = '用户参数错误,请重新授权登录后尝试~';
  656. RedisService::clear($cacheKey . '_lock');
  657. return false;
  658. }
  659. // 验证是否
  660. $goodsId = 0;
  661. $scene = 'vip';
  662. $expiredTime = time();
  663. if ($vipId == 11 && $vipType==5) {
  664. // 单节视频验证是否已付费过
  665. if (VideoOrderModel::where(['goods_id' => $vipId, 'status' => 2, 'mark' => 1])->where('expired_at', '>', date('Y-m-d H:i:s'))->value('id')) {
  666. $this->error = '抱歉,您已购买过该节视频课,请刷新后尝试观看~';
  667. return false;
  668. }
  669. // 视频数据
  670. $goodsId = $cId;
  671. $scene = 'course';
  672. $courseInfo = VideoCoursesModel::where(['id' => $goodsId, 'status' => 1, 'mark' => 1])->select(['id', 'fee', 'course_name', 'status'])->first();
  673. $fee = isset($courseInfo['fee']) ? $courseInfo['fee'] : 0;
  674. if (empty($courseInfo)) {
  675. $this->error = '抱歉,您购买的课程已下架,请刷新后重试~';
  676. return false;
  677. }
  678. if ($fee <= 0) {
  679. $this->error = '抱歉,您购买的课程为免费课程,请刷新后直接观看~';
  680. return false;
  681. }
  682. } else {
  683. // VIP 验证
  684. $goodsId = $vipId;
  685. $vipLimitOpen = ConfigService::make()->getConfigByCode('vip_limit_more', 0);
  686. if ($vipLimitOpen) {
  687. // 已开通VIP1无法再开通
  688. if ($vipType == 1 && ($isVip1 == 1 && $vip1Expired >= date('Y-m-d H:i:s'))) {
  689. $this->error = '抱歉,您的医师资格VIP未到期,到期后再尝试~';
  690. return false;
  691. }
  692. // 已开通VIP2无法再开通
  693. if ($vipType == 2 && ($isVip2 == 1 && $vip2Expired >= date('Y-m-d H:i:s'))) {
  694. $this->error = '抱歉,您的护士执业VIP未到期,到期后再尝试~';
  695. return false;
  696. }
  697. // 已开通VIP3无法再开通
  698. if ($vipType == 3 && ($isVip3 == 1 && $vip3Expired >= date('Y-m-d H:i:s'))) {
  699. $this->error = '抱歉,您的医学技师VIP未到期,到期后再尝试~';
  700. return false;
  701. }
  702. // 已开通VIP4无法再开通
  703. if ($vipType == 4 && ($isVip4 == 1 && $vip4Expired >= date('Y-m-d H:i:s'))) {
  704. $this->error = '抱歉,您的执业药师VIP未到期,到期后再尝试~';
  705. return false;
  706. }
  707. // 已开通全部视频VIP无法再开通
  708. if ($vipType == 5 && ($isVideoVip && $videoVipExpired >= date('Y-m-d H:i:s'))) {
  709. $this->error = '抱歉,您的已开通全部视频VIP,请到期后再尝试~';
  710. return false;
  711. }
  712. } else {
  713. if ($vipType == 1 && $isVip1 == 1 && $vip1Expired >= date('Y-m-d H:i:s')) {
  714. $expiredTime = strtotime($vip1Expired);
  715. }
  716. if ($vipType == 2 && $isVip2 == 1 && $vip2Expired >= date('Y-m-d H:i:s')) {
  717. $expiredTime = strtotime($vip2Expired);
  718. }
  719. if ($vipType == 3 && $isVip3 == 1 && $vip3Expired >= date('Y-m-d H:i:s')) {
  720. $expiredTime = strtotime($vip3Expired);
  721. }
  722. if ($vipType == 4 && $isVip4 == 1 && $vip4Expired >= date('Y-m-d H:i:s')) {
  723. $expiredTime = strtotime($vip4Expired);
  724. }
  725. if ($vipType == 5 && $isVideoVip == 1 && $videoVipExpired >= date('Y-m-d H:i:s')) {
  726. $expiredTime = strtotime($videoVipExpired);
  727. }
  728. }
  729. }
  730. // 创建订单
  731. $orderNo = get_order_num('VP');
  732. $remark = $vipType == 5 ? "购买{$vipName}" : "购买{$vipName}VIP";
  733. $order = [
  734. 'order_no' => $orderNo,
  735. 'user_id' => $userId,
  736. 'goods_id' => $goodsId,
  737. 'total' => $price,
  738. 'expired_at' => date('Y-m-d H:i:s', $expiredTime + $day * 86400),
  739. 'create_time' => time(),
  740. 'remark' => $remark,
  741. 'status' => 1,
  742. 'mark' => 1
  743. ];
  744. DB::beginTransaction();
  745. $model = $vipId ==11 ? new VideoOrderModel : new OrderModel;
  746. if (!$orderId = $model::insertGetId($order)) {
  747. $this->error = '创建VIP订单失败';
  748. return false;
  749. }
  750. /* TODO 支付处理 */
  751. $payOrder = [
  752. 'type' => 1,
  753. 'order_no' => $orderNo,
  754. 'pay_money' => $price,
  755. 'body' => $remark,
  756. 'openid' => $openid
  757. ];
  758. // 调起支付
  759. $payment = PaymentService::make()->minPay($info, $payOrder, $scene);
  760. if (empty($payment)) {
  761. DB::rollBack();
  762. RedisService::clear($cacheKey . '_lock');
  763. $this->error = PaymentService::make()->getError();
  764. return false;
  765. }
  766. // 用户操作记录
  767. DB::commit();
  768. $this->error = '创建VIP订单成功,请前往支付~';
  769. RedisService::clear($cacheKey . '_lock');
  770. RedisService::keyDel("caches:videos:list_by_group*");
  771. RedisService::clear("caches:videos:info_{$userId}_{$cId}");
  772. return [
  773. 'order_id' => $orderId,
  774. 'payment' => $payment,
  775. 'total' => $payOrder['pay_money'],
  776. 'pay_type' => 10,
  777. ];
  778. }
  779. }