| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace app\controller\api;
- use app\common\repositories\user\UserRepository;
- use crmeb\basic\BaseController;
- use crmeb\exceptions\AuthException;
- use crmeb\services\YunxinSmsService;
- use think\facade\Cache;
- use think\facade\Db;
- class YiJia extends BaseController
- {
- public const SUCCESS = 200; // Success
- public const ERROR = 400; // Error
- public const PHONE_ERROR = 1001; // 手机号码有误
- public const USER_EXIST = 1002; // 用户已存在
- public const USER_BOUND = 1003; // 用户已绑定
- public function sms(){
- $data = $this->request->params(['phone']);
- $sms_num_key = 'api.auth.num.' . $data['phone'];
- $sms_key = 'api.auth.sms.' . $data['phone'];
- $num = Cache::get($sms_num_key) ? Cache::get($sms_num_key) : 0;
- try {
- $sms_code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
- $sms_time = systemConfig('sms_time') ? systemConfig('sms_time') : 30 ;
- (YunxinSmsService::create())->send($data['phone'], 'VERIFICATION_CODE', ['code' => $sms_code,'time' => $sms_time]);
- } catch (\Exception $e) {
- return app('json')->fail($e->getMessage());
- }
- Cache::set($sms_key, $sms_code,$sms_time * 60);
- Cache::set($sms_num_key, $num + 1, 300);
- //'短信发送成功'
- return app('json')->success('短信发送成功');
- }
- public function lp(){
- echo 'lplplplp';
- }
- public function Auth(UserRepository $repository){
- $data = $this->request->params(['phone', 'sms_code', 'source']);
- $phone_number_pattern = '/^1[3-9]\d{9}$/';
- if (! preg_match($phone_number_pattern, $data['phone'])){
- return app('json')->make(self::PHONE_ERROR, '用户手机号码有误');
- }
- if ($data['source'] != "yijia"){
- throw new AuthException('非法请求');
- }
- $find = Db::name('user')->where("account",$data['phone'])->find();
- if ($find){
- if ($find['external'] == 3){
- return app('json')->make(self::USER_BOUND, '用户关系已绑定');
- }
- return app('json')->make(self::USER_EXIST, '无效绑定。用户已存在');
- }
- $user = $repository->registr($data['phone'],123456,'APP','','用户' . substr($data['phone'], 7, 4),3);
- if ($user){
- return app('json')->success('绑定成功');
- }
- return app('json')->error('绑定失败');
- }
- public function pension(){
- $data = $this->request->params(['phone']);
- [$page, $limit] = $this->getPage();
- $pension = Db::name('User')
- -> where("external",3)
- ->when(!empty($data['phone']),function($query) use($data){
- $query->where('account',$data['phone']);
- })
- -> field('account as phone,annuity as pension')->page($page, $limit)->select();
- return app('json')->success($pension);
- }
- }
|