Usercenter.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace app\api\controller;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use think\Db;
  7. use EasyWeChat\Factory;
  8. /**
  9. * @title 会员个人中心
  10. * @controller Usercenter
  11. * @group base
  12. */
  13. class Usercenter extends Base
  14. {
  15. public function initialize()
  16. {
  17. parent::initialize();
  18. parent::check_login();
  19. }
  20. /**
  21. * @title 获取个人信息
  22. * @desc 个人信息
  23. * @author qc
  24. * @url /api/Usercenter/getUserInfo
  25. * @method GET
  26. * @tag 个人信息
  27. * @header name:Authorization require:1 desc:Token
  28. * @return name:phone type:int default:-- desc:手机号
  29. * @return name:name type:string default:-- desc:姓名
  30. * @return name:headimg type:string default:-- desc:头像地址
  31. * @return name:synopsis type:string default:-- desc:个人简介
  32. * @return name:background type:string default:-- desc:个人中心背景图片地址
  33. * @return name:password type:string default:-- desc:不加密
  34. * @return name:decode_password type:string default:-- desc:加密后
  35. * @return name:true_name type:string default:-- desc:真实姓名
  36. * @return name:integral type:int default:-- desc:剩余积分
  37. * @return name:growth type:int default:-- desc:成长值
  38. * @return name:level_id type:int default:-- desc:等级id
  39. * @return name:lev_title type:string default:-- desc:等级名称
  40. */
  41. public function getUserInfo()
  42. {
  43. $uid = $this->uid;
  44. $field = 'id,phone,name,headimg,synopsis,status,background,decode_password,password,true_name,integral,level_id,growth';
  45. $user_info = Db::name('store_member')->field($field)->where('id',$uid)->find();
  46. if(empty($user_info)) $this->error('用户信息不正确');
  47. if($user_info['status'] == 0) $this->error('该用户已被禁用');
  48. $user_info['lev_title'] = $user_info['level_id'] ? Db::table('member_level')->where(['id'=>$user_info['level_id']])->value('name') : '普通会员';
  49. $this->success('获取成功',$user_info);
  50. }
  51. /**
  52. * @title 编辑个人信息
  53. * @desc 编辑个人信息
  54. * @author qc
  55. * @url /api/Usercenter/updateUserInfo
  56. * @method POST
  57. * @tag 编辑信息
  58. * @header name:Authorization require:1 desc:Token
  59. * @param name:type type:int require:1 default:1 desc:修改的类型(1:昵称,2:头像)
  60. * @param name:name type:string require:0 default:-- desc:昵称(type为1时必传)
  61. * @param name:headimg type:string require:0 default:-- desc:头像地址(type为2时必传)
  62. * @return name:phone type:int default:-- desc:手机号
  63. * @return name:name type:string default:-- desc:姓名
  64. * @return name:headimg type:string default:-- desc:头像地址
  65. * @return name:password type:string default:-- desc:不加密
  66. * @return name:decode_password type:string default:-- desc:加密后
  67. */
  68. public function updateUserInfo()
  69. {
  70. $type = input('post.type',1);
  71. $headimg = input('post.headimg');
  72. $name = input('post.name');
  73. if(!in_array($type,[1,2])) $this->error('参数错误');
  74. $update_data= [];
  75. if($type == 1 && $name) $update_data['name'] = $name;
  76. if($type == 2 && $headimg) $update_data['headimg'] = $headimg;
  77. if(empty($update_data)) $this->error('参数错误');
  78. Db::name('store_member')->where('id',$this->uid)->update($update_data);
  79. $field = 'phone,name,headimg,status,decode_password,password';
  80. $user_info = Db::name('store_member')->field($field)->where('id',$this->uid)->find();
  81. $this->success('编辑成功',$user_info);
  82. }
  83. /**
  84. * @title 绑定手机号
  85. * @desc 绑定手机号
  86. * @author qc
  87. * @url /api/Usercenter/modifyPhone
  88. * @method POST
  89. * @header name:Authorization require:1 desc:Token
  90. * @param name:code type:int require:1 default:-- desc:code值
  91. * @param name:iv type:int require:1 default:0 desc:iv
  92. * @param name:encrypted type:int require:1 default:0 desc:encrypted
  93. * @return name:phone type:int default:-- desc:手机号
  94. * @return name:name type:string default:-- desc:姓名
  95. * @return name:headimg type:string default:-- desc:头像地址
  96. */
  97. public function modifyPhone()
  98. {
  99. $uid = $this->uid;
  100. $iv = input('post.iv');
  101. $code = input('post.code');
  102. $encryptedData = input('post.encrypted');
  103. if(!$code || !$iv || !$encryptedData ) $this->error('参数错误');
  104. $app = Factory::miniProgram(config('app.mini_program'));
  105. $data = $app->auth->session($code);
  106. require_once env('root_path').'/vendor/program/wxBizDataCrypt.php';
  107. $sessionKey = $data['session_key'];
  108. $pc = new \WXBizDataCrypt(config('app.mini_program')['app_id'], $sessionKey);
  109. $errCode = $pc->decryptData($encryptedData, $iv, $info);
  110. if($errCode != 0) $this->error('参数错误2');
  111. $info = json_decode($info,true);
  112. $phone = $info['purePhoneNumber'];
  113. //判断手机号是否已注册
  114. $member_id = Db::name('store_member')->where('phone','=',$phone)->where('id','<>',$this->uid)->value('id');
  115. if($member_id) $this->error('手机号已注册过');
  116. $up = ['phone'=>$phone];
  117. Db::name('store_member')->where('id',$uid)->update($up);
  118. $field = 'phone,name,headimg';
  119. $user_info = Db::name('store_member')->field($field)->where('id',$this->uid)->find();
  120. $this->success('绑定成功',$user_info);
  121. }
  122. /**
  123. * @title 发送短信验证码
  124. * @desc 发送短信验证码
  125. * @author QGF
  126. * @url /api/Usercenter/sendSms
  127. * @method POST
  128. * @tag 短信验证码
  129. * @param name:phone type:int require:1 default:-- desc:要获取验证码的手机号
  130. * @return name:code type:string default:-- desc:验证码
  131. */
  132. public function sendSms(){
  133. $phone = input('post.phone');
  134. if(empty($phone)) $this ->error('参数错误');
  135. $code = rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
  136. AlibabaCloud::accessKeyClient('LTAI5tSFmMzKUQC4zJjc3Guu', 'gdEyLTubu0KT6WzR26upX3dgiu5FD6')
  137. ->regionId('cn-hangzhou')->asDefaultClient();
  138. try {
  139. $result = AlibabaCloud::rpc()
  140. ->product('Dysmsapi')
  141. ->version('2017-05-25')
  142. ->action('SendSms')
  143. ->method('POST')
  144. ->host('dysmsapi.aliyuncs.com')
  145. ->options([
  146. 'query' => [
  147. 'RegionId' => "cn-hangzhou",
  148. 'PhoneNumbers' => $phone,
  149. 'SignName' => "验证码",
  150. 'TemplateCode' => "SMS_204845304",
  151. 'TemplateParam' => json_encode(array("code"=>$code)),
  152. ],
  153. ])->request();
  154. $result = $result->toArray();
  155. var_dump($result);
  156. if($result['Code'] == "OK")
  157. {
  158. $sms_data = array(
  159. 'phone'=>$phone,
  160. 'code'=>$code,
  161. 'result'=>$result['Message']
  162. );
  163. Db::name('store_member_sms')->insert($sms_data);
  164. $this->error('发送成功',$code);
  165. }else{
  166. $this->error('发送失败');
  167. }
  168. } catch (ClientException $e) {
  169. echo $e->getErrorMessage() . PHP_EOL;
  170. } catch (ServerException $e) {
  171. echo $e->getErrorMessage() . PHP_EOL;
  172. }
  173. }
  174. }