123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace app\api\controller;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
- use think\Db;
- use EasyWeChat\Factory;
- /**
- * @title 会员个人中心
- * @controller Usercenter
- * @group base
- */
- class Usercenter extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::check_login();
- }
- /**
- * @title 获取个人信息
- * @desc 个人信息
- * @author qc
- * @url /api/Usercenter/getUserInfo
- * @method GET
- * @tag 个人信息
- * @header name:Authorization require:1 desc:Token
- * @return name:phone type:int default:-- desc:手机号
- * @return name:name type:string default:-- desc:姓名
- * @return name:headimg type:string default:-- desc:头像地址
- * @return name:synopsis type:string default:-- desc:个人简介
- * @return name:background type:string default:-- desc:个人中心背景图片地址
- * @return name:password type:string default:-- desc:不加密
- * @return name:decode_password type:string default:-- desc:加密后
- * @return name:true_name type:string default:-- desc:真实姓名
- * @return name:integral type:int default:-- desc:剩余积分
- * @return name:growth type:int default:-- desc:成长值
- * @return name:level_id type:int default:-- desc:等级id
- * @return name:lev_title type:string default:-- desc:等级名称
- */
- public function getUserInfo()
- {
- $uid = $this->uid;
- $field = 'id,phone,name,headimg,synopsis,status,background,decode_password,password,true_name,integral,level_id,growth';
- $user_info = Db::name('store_member')->field($field)->where('id',$uid)->find();
- if(empty($user_info)) $this->error('用户信息不正确');
- if($user_info['status'] == 0) $this->error('该用户已被禁用');
- $user_info['lev_title'] = $user_info['level_id'] ? Db::table('member_level')->where(['id'=>$user_info['level_id']])->value('name') : '普通会员';
- $this->success('获取成功',$user_info);
- }
- /**
- * @title 编辑个人信息
- * @desc 编辑个人信息
- * @author qc
- * @url /api/Usercenter/updateUserInfo
- * @method POST
- * @tag 编辑信息
- * @header name:Authorization require:1 desc:Token
- * @param name:type type:int require:1 default:1 desc:修改的类型(1:昵称,2:头像)
- * @param name:name type:string require:0 default:-- desc:昵称(type为1时必传)
- * @param name:headimg type:string require:0 default:-- desc:头像地址(type为2时必传)
- * @return name:phone type:int default:-- desc:手机号
- * @return name:name type:string default:-- desc:姓名
- * @return name:headimg type:string default:-- desc:头像地址
- * @return name:password type:string default:-- desc:不加密
- * @return name:decode_password type:string default:-- desc:加密后
- */
- public function updateUserInfo()
- {
- $type = input('post.type',1);
- $headimg = input('post.headimg');
- $name = input('post.name');
- if(!in_array($type,[1,2])) $this->error('参数错误');
- $update_data= [];
- if($type == 1 && $name) $update_data['name'] = $name;
- if($type == 2 && $headimg) $update_data['headimg'] = $headimg;
- if(empty($update_data)) $this->error('参数错误');
- Db::name('store_member')->where('id',$this->uid)->update($update_data);
- $field = 'phone,name,headimg,status,decode_password,password';
- $user_info = Db::name('store_member')->field($field)->where('id',$this->uid)->find();
- $this->success('编辑成功',$user_info);
- }
- /**
- * @title 绑定手机号
- * @desc 绑定手机号
- * @author qc
- * @url /api/Usercenter/modifyPhone
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:code type:int require:1 default:-- desc:code值
- * @param name:iv type:int require:1 default:0 desc:iv
- * @param name:encrypted type:int require:1 default:0 desc:encrypted
- * @return name:phone type:int default:-- desc:手机号
- * @return name:name type:string default:-- desc:姓名
- * @return name:headimg type:string default:-- desc:头像地址
- */
- public function modifyPhone()
- {
- $uid = $this->uid;
- $iv = input('post.iv');
- $code = input('post.code');
- $encryptedData = input('post.encrypted');
- if(!$code || !$iv || !$encryptedData ) $this->error('参数错误');
- $app = Factory::miniProgram(config('app.mini_program'));
- $data = $app->auth->session($code);
- require_once env('root_path').'/vendor/program/wxBizDataCrypt.php';
- $sessionKey = $data['session_key'];
- $pc = new \WXBizDataCrypt(config('app.mini_program')['app_id'], $sessionKey);
- $errCode = $pc->decryptData($encryptedData, $iv, $info);
- if($errCode != 0) $this->error('参数错误2');
- $info = json_decode($info,true);
- $phone = $info['purePhoneNumber'];
- //判断手机号是否已注册
- $member_id = Db::name('store_member')->where('phone','=',$phone)->where('id','<>',$this->uid)->value('id');
- if($member_id) $this->error('手机号已注册过');
- $up = ['phone'=>$phone];
- Db::name('store_member')->where('id',$uid)->update($up);
- $field = 'phone,name,headimg';
- $user_info = Db::name('store_member')->field($field)->where('id',$this->uid)->find();
- $this->success('绑定成功',$user_info);
- }
- /**
- * @title 发送短信验证码
- * @desc 发送短信验证码
- * @author QGF
- * @url /api/Usercenter/sendSms
- * @method POST
- * @tag 短信验证码
- * @param name:phone type:int require:1 default:-- desc:要获取验证码的手机号
- * @return name:code type:string default:-- desc:验证码
- */
- public function sendSms(){
- $phone = input('post.phone');
- if(empty($phone)) $this ->error('参数错误');
- $code = rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
- AlibabaCloud::accessKeyClient('LTAI5tSFmMzKUQC4zJjc3Guu', 'gdEyLTubu0KT6WzR26upX3dgiu5FD6')
- ->regionId('cn-hangzhou')->asDefaultClient();
- try {
- $result = AlibabaCloud::rpc()
- ->product('Dysmsapi')
- ->version('2017-05-25')
- ->action('SendSms')
- ->method('POST')
- ->host('dysmsapi.aliyuncs.com')
- ->options([
- 'query' => [
- 'RegionId' => "cn-hangzhou",
- 'PhoneNumbers' => $phone,
- 'SignName' => "验证码",
- 'TemplateCode' => "SMS_204845304",
- 'TemplateParam' => json_encode(array("code"=>$code)),
- ],
- ])->request();
- $result = $result->toArray();
- var_dump($result);
- if($result['Code'] == "OK")
- {
- $sms_data = array(
- 'phone'=>$phone,
- 'code'=>$code,
- 'result'=>$result['Message']
- );
- Db::name('store_member_sms')->insert($sms_data);
- $this->error('发送成功',$code);
- }else{
- $this->error('发送失败');
- }
- } catch (ClientException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- } catch (ServerException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- }
- }
- }
|