YiJia.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\controller\api;
  3. use app\common\repositories\user\UserRepository;
  4. use crmeb\basic\BaseController;
  5. use crmeb\exceptions\AuthException;
  6. use crmeb\services\YunxinSmsService;
  7. use think\facade\Cache;
  8. use think\facade\Db;
  9. class YiJia extends BaseController
  10. {
  11. public const SUCCESS = 200; // Success
  12. public const ERROR = 400; // Error
  13. public const PHONE_ERROR = 1001; // 手机号码有误
  14. public const USER_EXIST = 1002; // 用户已存在
  15. public const USER_BOUND = 1003; // 用户已绑定
  16. public function sms(){
  17. $data = $this->request->params(['phone']);
  18. $sms_num_key = 'api.auth.num.' . $data['phone'];
  19. $sms_key = 'api.auth.sms.' . $data['phone'];
  20. $num = Cache::get($sms_num_key) ? Cache::get($sms_num_key) : 0;
  21. try {
  22. $sms_code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
  23. $sms_time = systemConfig('sms_time') ? systemConfig('sms_time') : 30 ;
  24. (YunxinSmsService::create())->send($data['phone'], 'VERIFICATION_CODE', ['code' => $sms_code,'time' => $sms_time]);
  25. } catch (\Exception $e) {
  26. return app('json')->fail($e->getMessage());
  27. }
  28. Cache::set($sms_key, $sms_code,$sms_time * 60);
  29. Cache::set($sms_num_key, $num + 1, 300);
  30. //'短信发送成功'
  31. return app('json')->success('短信发送成功');
  32. }
  33. public function lp(){
  34. echo 'lplplplp';
  35. }
  36. public function Auth(UserRepository $repository){
  37. $data = $this->request->params(['phone', 'sms_code', 'source']);
  38. $phone_number_pattern = '/^1[3-9]\d{9}$/';
  39. if (! preg_match($phone_number_pattern, $data['phone'])){
  40. return app('json')->make(self::PHONE_ERROR, '用户手机号码有误');
  41. }
  42. if ($data['source'] != "yijia"){
  43. throw new AuthException('非法请求');
  44. }
  45. $find = Db::name('user')->where("account",$data['phone'])->find();
  46. if ($find){
  47. if ($find['external'] == 3){
  48. return app('json')->make(self::USER_BOUND, '用户关系已绑定');
  49. }
  50. return app('json')->make(self::USER_EXIST, '无效绑定。用户已存在');
  51. }
  52. $user = $repository->registr($data['phone'],123456,'APP','','用户' . substr($data['phone'], 7, 4),3);
  53. if ($user){
  54. return app('json')->success('绑定成功');
  55. }
  56. return app('json')->error('绑定失败');
  57. }
  58. public function pension(){
  59. $data = $this->request->params(['phone']);
  60. [$page, $limit] = $this->getPage();
  61. $pension = Db::name('User')
  62. -> where("external",3)
  63. ->when(!empty($data['phone']),function($query) use($data){
  64. $query->where('account',$data['phone']);
  65. })
  66. -> field('account as phone,annuity as pension')->page($page, $limit)->select();
  67. return app('json')->success($pension);
  68. }
  69. }