Usercenter.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. /**
  8. * @title 会员个人中心管理
  9. * @controller Usercenter
  10. * @group base
  11. */
  12. class Usercenter extends Base
  13. {
  14. public function initialize()
  15. {
  16. parent::check_login();
  17. }
  18. /**
  19. * @title 获取个人信息
  20. * @desc 个人信息
  21. * @author qc
  22. * @url /api/Usercenter/getUserInfo
  23. * @method GET
  24. * @tag 个人信息
  25. * @header name:Authorization require:1 desc:Token
  26. * @return name:phone type:int default:-- desc:手机号
  27. * @return name:name type:string default:-- desc:姓名
  28. * @return name:headimg type:string default:-- desc:头像地址
  29. * @return name:synopsis type:string default:-- desc:个人简介
  30. * @return name:background type:string default:-- desc:个人中心背景图片地址
  31. * @return name:password type:string default:-- desc:不加密
  32. * @return name:decode_password type:string default:-- desc:加密后
  33. * @return name:true_name type:string default:-- desc:真实姓名
  34. */
  35. public function getUserInfo()
  36. {
  37. $uid = $this->uid;
  38. $field = 'phone,name,headimg,synopsis,status,background,decode_password,password,true_name';
  39. $user_info = Db::name('store_member')->field($field)->where('id',$uid)->find();
  40. if(empty($user_info)) $this->error('用户信息不正确');
  41. if($user_info['status'] == 0) $this->error('该用户已被禁用');
  42. $this->success('获取成功',$user_info);
  43. }
  44. /**
  45. * @title 编辑个人信息
  46. * @desc 编辑个人信息
  47. * @author qc
  48. * @url /api/Usercenter/updateUserInfo
  49. * @method POST
  50. * @tag 编辑信息
  51. * @header name:Authorization require:1 desc:Token
  52. * @param name:type type:int require:1 default:1 desc:修改的类型(1:昵称,2:头像)
  53. * @param name:name type:string require:0 default:-- desc:姓名(type为1时必传)
  54. * @param name:synopsis type:string require:0 default:-- desc:个人简介(type为2时必传)
  55. * @param name:headimg type:string require:0 default:-- desc:头像地址(type为3时必传)
  56. * @return name:phone type:int default:-- desc:手机号
  57. * @return name:name type:string default:-- desc:姓名
  58. * @return name:headimg type:string default:-- desc:头像地址
  59. * @return name:synopsis type:string default:-- desc:个人简介
  60. * @return name:background type:string default:-- desc:个人中心背景图片地址
  61. * @return name:password type:string default:-- desc:不加密
  62. * @return name:decode_password type:string default:-- desc:加密后
  63. * @return name:true_name type:string default:-- desc:真实姓名
  64. */
  65. public function updateUserInfo()
  66. {
  67. $type = input('post.type',1);
  68. $headimg = input('post.headimg');
  69. $name = input('post.name');
  70. if(!in_array($type,[1,2])) $this->error('参数错误');
  71. $update_data= [];
  72. if($type == 1 && $name) $update_data['name'] = $name;
  73. if($type == 2 && $headimg) $update_data['headimg'] = $headimg;
  74. if(empty($update_data)) $this->error('参数错误');
  75. Db::name('store_member')->where('id',$this->uid)->update($update_data);
  76. $field = 'phone,name,headimg,synopsis,status,background,decode_password,password';
  77. $user_info = Db::name('store_member')->field($field)->where('id',$this->uid)->find();
  78. $this->success('编辑成功',$user_info);
  79. }
  80. /**
  81. * @title 绑定手机号
  82. * @desc 绑定手机号
  83. * @author qc
  84. * @url /api/Usercenter/modifyPhone
  85. * @method POST
  86. * @header name:Authorization require:1 desc:Token
  87. * @param name:phone type:int require:1 default:-- desc:手机号
  88. * @param name:code type:int require:1 default:-- desc:手机号验证码
  89. * @param name:true_name type:string require:1 default:-- desc:真实姓名
  90. * @param name:password type:string require:1 default:-- desc:密码
  91. * @return name:phone type:int default:-- desc:手机号
  92. * @return name:name type:string default:-- desc:姓名
  93. * @return name:headimg type:string default:-- desc:头像地址
  94. * @return name:synopsis type:string default:-- desc:个人简介
  95. * @return name:background type:string default:-- desc:个人中心背景图片地址
  96. * @return name:password type:string default:-- desc:不加密
  97. * @return name:decode_password type:string default:-- desc:加密后
  98. * @return name:true_name type:string default:-- desc:真实姓名
  99. */
  100. public function modifyPhone()
  101. {
  102. $uid = $this->uid;
  103. $phone = input('post.phone');
  104. $code = input('post.code');
  105. $password = input('post.password');
  106. $true_name = input('post.true_name');
  107. if(empty($phone) || empty($code) || !$password || !$true_name) $this->error('参数错误');
  108. $store_member_sms = Db::name('store_member_sms')
  109. ->field('id,code')->where('phone',$phone)
  110. ->where('used',0)->order('id desc')
  111. ->find();
  112. // if($store_member_sms['code'] != $code) $this->error('验证码错误');
  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 = [
  117. 'phone'=>$phone,
  118. 'password'=>$password,
  119. 'decode_password'=>md5($password),
  120. 'true_name'=>$true_name
  121. ];
  122. $res = Db::name('store_member')->where('id',$uid)->update($up);
  123. $field = 'phone,name,headimg,synopsis,status,background,decode_password,password';
  124. $user_info = Db::name('store_member')->field($field)->where('id',$this->uid)->find();
  125. if($res !==false){
  126. Db::name('store_member_sms')->where('id',$store_member_sms['id'])->update(['used'=>1]);
  127. $this->success('绑定成功',$user_info);
  128. }else{
  129. $this->error('绑定失败');
  130. }
  131. }
  132. /**
  133. * @title 发送短信验证码
  134. * @desc 发送短信验证码
  135. * @author QGF
  136. * @url /api/Usercenter/sendSms
  137. * @method POST
  138. * @tag 短信验证码
  139. * @param name:phone type:int require:1 default:-- desc:要获取验证码的手机号
  140. * @return name:code type:string default:-- desc:验证码
  141. */
  142. public function sendSms(){
  143. $phone = input('post.phone');
  144. if(empty($phone)) $this ->error('参数错误');
  145. $code = rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
  146. AlibabaCloud::accessKeyClient('LTAI5tKATi9QpdtaBpEHeCP4', 'F8B77fPPitlfRScTuPmQxfphLRpt6a')
  147. ->regionId('cn-hangzhou')->asDefaultClient();
  148. try {
  149. $result = AlibabaCloud::rpc()
  150. ->product('Dysmsapi')
  151. ->version('2017-05-25')
  152. ->action('SendSms')
  153. ->method('POST')
  154. ->host('dysmsapi.aliyuncs.com')
  155. ->options([
  156. 'query' => [
  157. 'RegionId' => "cn-hangzhou",
  158. 'PhoneNumbers' => $phone,
  159. 'SignName' => "火星帝界",
  160. 'TemplateCode' => "SMS_223135158",
  161. 'TemplateParam' => json_encode(array("code"=>$code)),
  162. ],
  163. ])->request();
  164. $result = $result->toArray();
  165. if($result['Code'] == "OK")
  166. {
  167. $sms_data = array(
  168. 'phone'=>$phone,
  169. 'code'=>$code,
  170. 'result'=>$result['Message']
  171. );
  172. Db::name('store_member_sms')->insert($sms_data);
  173. $this->error('发送成功',$code);
  174. }else{
  175. $this->error('发送失败');
  176. }
  177. } catch (ClientException $e) {
  178. echo $e->getErrorMessage() . PHP_EOL;
  179. } catch (ServerException $e) {
  180. echo $e->getErrorMessage() . PHP_EOL;
  181. }
  182. }
  183. /**
  184. * @title 获取我的留言
  185. * @desc 获取我的留言
  186. * @author qc
  187. * @url /api/Usercenter/getMyMessage
  188. * @method GET
  189. * @header name:Authorization require:1 desc:Token
  190. * @param name:page type:int : default:1 desc:页数
  191. * @param name:page_num type:int : default:20 desc:每页数
  192. * @return name:message type:string default:-- desc:留言内容
  193. * @return name:images type:string default:-- desc:留言图片,多张有逗号(,)隔开
  194. * @return name:create_at type:string default:-- desc:留言时间
  195. * @return name:headimg type:string default:-- desc:留言人头像
  196. * @return name:name type:string default:-- desc:留言人名称
  197. */
  198. public function getMyMessage()
  199. {
  200. $page = input('page',1);
  201. $page_num = input('page_num',20);
  202. $off_set = $page*$page_num - $page_num;
  203. $list = Db::table('ticket_note')->alias('n')
  204. ->field('n.id,n.message,n.images,n.create_at,m.headimg,m.name')
  205. ->join('store_member m','n.from_uid = m.id','LEFT')
  206. ->where(['from_uid'=>$this->uid,'n.status'=>1])
  207. ->order('n.id desc')
  208. ->limit($off_set,$page_num)
  209. ->select();
  210. $this->success('获取成功',$list);
  211. }
  212. /**
  213. * @title 留言
  214. * @desc 留言
  215. * @author qc
  216. * @url /api/Usercenter/leaveMessage
  217. * @method POST
  218. * @header name:Authorization require:1 desc:Token
  219. * @param name:message type:string require:1 default:-- desc:留言内容
  220. * @param name:images type:string require:0 default:-- desc:影评图片(逗号隔开)
  221. * @param name:tel type:string require:0 default:-- desc:电话
  222. */
  223. public function leaveMessage()
  224. {
  225. $insert_data = [
  226. 'from_uid'=>$this->uid,
  227. 'to_uid'=>input('post.to_uid',0),
  228. 'message'=>input('post.message'),
  229. 'images'=>input('post.images'),
  230. 'tel'=>input('post.tel'),
  231. 'create_at'=>date('Y-m-d H:i:s')
  232. ];
  233. Db::table('ticket_note')->insert($insert_data);
  234. $this->success('留言成功',Db::getLastInsID());
  235. }
  236. }